-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from gradle/am/cpp
Add `cppLibrary` and `cppApplication` software types
- Loading branch information
Showing
19 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cppLibrary { | ||
cppVersion = "c++20" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "cpp-util.h" | ||
|
||
std::string Util::message() { | ||
return "Welcome to the C++ utils library!"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include <string> | ||
|
||
class Util { | ||
public: | ||
std::string message(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cppApplication { | ||
cppVersion = "c++20" | ||
|
||
dependencies { | ||
implementation(project(":cpp-util")) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
unified-prototype/testbed-cpp-application/src/main/cpp/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
#include <cpp-util.h> | ||
|
||
using namespace std; | ||
|
||
string cppVersion() { | ||
if (__cplusplus == 202002L) { | ||
return "C++20"; | ||
} else if (__cplusplus == 201703L) { | ||
return "C++17"; | ||
} else { | ||
return "unknown C++ version"; | ||
} | ||
} | ||
|
||
string os() { | ||
#ifdef __MACH__ | ||
return "macOS"; | ||
#else | ||
return "unknown OS"; | ||
#endif | ||
} | ||
|
||
int main() { | ||
cout << "Hello from " << cppVersion() << " on " << os() << endl; | ||
cout << Util().message() << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cppLibrary { | ||
cppVersion = "c++20" | ||
|
||
dependencies { | ||
implementation(project(":cpp-util")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include <string> | ||
#include <cpp-util.h> | ||
|
||
class Lib { | ||
std::string util = Util().message(); | ||
}; |
6 changes: 5 additions & 1 deletion
6
unified-prototype/testbed-swift-application/src/main/swift/main.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import Foundation | ||
import SwiftUtil | ||
|
||
print("Hello from Swift") | ||
#if swift(>=5.0) | ||
print("Hello from Swift >=5.0") | ||
#else | ||
print("Hello from unknown Swift version") | ||
#endif | ||
print(Utils().welcome) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
unified-prototype/unified-plugin/plugin-cpp/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
plugins { | ||
`kotlin-dsl` | ||
id("build-logic.publishing") | ||
} | ||
|
||
description = "Implements the declarative C++ DSL prototype" | ||
|
||
dependencies { | ||
implementation(project(":plugin-common")) | ||
} | ||
|
||
gradlePlugin { | ||
plugins { | ||
create("cpp-library") { | ||
id = "org.gradle.experimental.cpp-library" | ||
implementationClass = "org.gradle.api.experimental.cpp.StandaloneCppLibraryPlugin" | ||
tags = setOf("declarative-gradle") | ||
} | ||
create("cpp-application") { | ||
id = "org.gradle.experimental.cpp-application" | ||
implementationClass = "org.gradle.api.experimental.cpp.StandaloneCppApplicationPlugin" | ||
tags = setOf("declarative-gradle") | ||
} | ||
create("cpp-ecosystem") { | ||
id = "org.gradle.experimental.cpp-ecosystem" | ||
implementationClass = "org.gradle.api.experimental.cpp.CppEcosystemPlugin" | ||
tags = setOf("declarative-gradle") | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ified-plugin/plugin-cpp/src/main/java/org/gradle/api/experimental/cpp/CppApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.gradle.api.experimental.cpp; | ||
|
||
import org.gradle.api.experimental.common.HasApplicationDependencies; | ||
import org.gradle.api.experimental.common.HasCliExecutables; | ||
import org.gradle.declarative.dsl.model.annotations.Restricted; | ||
|
||
@Restricted | ||
public interface CppApplication extends HasCppTarget, HasApplicationDependencies, HasCliExecutables { | ||
} |
12 changes: 12 additions & 0 deletions
12
...d-plugin/plugin-cpp/src/main/java/org/gradle/api/experimental/cpp/CppEcosystemPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.gradle.api.experimental.cpp; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.initialization.Settings; | ||
import org.gradle.api.internal.plugins.software.RegistersSoftwareTypes; | ||
|
||
@RegistersSoftwareTypes({StandaloneCppLibraryPlugin.class, StandaloneCppApplicationPlugin.class}) | ||
public class CppEcosystemPlugin implements Plugin<Settings> { | ||
@Override | ||
public void apply(Settings target) { | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...e/unified-plugin/plugin-cpp/src/main/java/org/gradle/api/experimental/cpp/CppLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.gradle.api.experimental.cpp; | ||
|
||
import org.gradle.api.experimental.common.HasLibraryDependencies; | ||
import org.gradle.declarative.dsl.model.annotations.Restricted; | ||
|
||
@Restricted | ||
public interface CppLibrary extends HasCppTarget, HasLibraryDependencies { | ||
} |
10 changes: 10 additions & 0 deletions
10
...unified-plugin/plugin-cpp/src/main/java/org/gradle/api/experimental/cpp/HasCppTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.gradle.api.experimental.cpp; | ||
|
||
import org.gradle.api.provider.Property; | ||
import org.gradle.declarative.dsl.model.annotations.Restricted; | ||
|
||
@Restricted | ||
public interface HasCppTarget { | ||
@Restricted | ||
Property<String> getCppVersion(); | ||
} |
53 changes: 53 additions & 0 deletions
53
...gin-cpp/src/main/java/org/gradle/api/experimental/cpp/StandaloneCppApplicationPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.gradle.api.experimental.cpp; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.experimental.common.CliApplicationConventionsPlugin; | ||
import org.gradle.api.file.RegularFile; | ||
import org.gradle.api.internal.plugins.software.SoftwareType; | ||
import org.gradle.api.provider.Provider; | ||
import org.gradle.api.tasks.Exec; | ||
import org.gradle.api.tasks.TaskProvider; | ||
import org.gradle.language.cpp.CppBinary; | ||
import org.gradle.language.cpp.CppComponent; | ||
import org.gradle.language.cpp.CppExecutable; | ||
import org.gradle.language.cpp.plugins.CppApplicationPlugin; | ||
import org.gradle.util.internal.TextUtil; | ||
|
||
public abstract class StandaloneCppApplicationPlugin implements Plugin<Project> { | ||
public static final String CPP_APPLICATION = "cppApplication"; | ||
|
||
@SoftwareType(name = CPP_APPLICATION) | ||
abstract public CppApplication getApplication(); | ||
|
||
@Override | ||
public void apply(Project target) { | ||
CppApplication application = getApplication(); | ||
target.getExtensions().add(CPP_APPLICATION, application); | ||
|
||
target.getPlugins().apply(CppApplicationPlugin.class); | ||
target.getPlugins().apply(CliApplicationConventionsPlugin.class); | ||
|
||
linkDslModelToPlugin(target, application); | ||
} | ||
|
||
private void linkDslModelToPlugin(Project project, CppApplication application) { | ||
CppComponent model = project.getExtensions().getByType(CppComponent.class); | ||
|
||
model.getImplementationDependencies().getDependencies().addAllLater(application.getDependencies().getImplementation().getDependencies()); | ||
|
||
project.getComponents().withType(org.gradle.language.cpp.CppApplication.class).configureEach(applicationComponent -> | ||
applicationComponent.getBinaries().configureEach(binary -> { | ||
binary.getCompileTask().get().getCompilerArgs().add(application.getCppVersion().map(v -> "--std=" + v)); | ||
if (binary instanceof CppExecutable) { | ||
Provider<RegularFile> executable = ((CppExecutable) binary).getDebuggerExecutableFile(); | ||
TaskProvider<Exec> runTask = project.getTasks().register("run" + TextUtil.capitalize(binary.getName()), Exec.class, task -> { | ||
task.executable(executable.get().getAsFile().getAbsoluteFile()); | ||
task.dependsOn(executable); | ||
}); | ||
application.getRunTasks().add(runTask); | ||
} | ||
}) | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../plugin-cpp/src/main/java/org/gradle/api/experimental/cpp/StandaloneCppLibraryPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.gradle.api.experimental.cpp; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.internal.plugins.software.SoftwareType; | ||
import org.gradle.language.cpp.CppBinary; | ||
import org.gradle.language.cpp.plugins.CppLibraryPlugin; | ||
|
||
public abstract class StandaloneCppLibraryPlugin implements Plugin<Project> { | ||
public static final String CPP_LIBRARY = "cppLibrary"; | ||
|
||
@SoftwareType(name = CPP_LIBRARY) | ||
abstract public CppLibrary getLibrary(); | ||
|
||
@Override | ||
public void apply(Project target) { | ||
CppLibrary library = getLibrary(); | ||
target.getExtensions().add(CPP_LIBRARY, library); | ||
|
||
target.getPlugins().apply(CppLibraryPlugin.class); | ||
|
||
linkDslModelToPlugin(target, library); | ||
} | ||
|
||
private void linkDslModelToPlugin(Project project, CppLibrary library) { | ||
org.gradle.language.cpp.CppLibrary model = project.getExtensions().getByType(org.gradle.language.cpp.CppLibrary.class); | ||
|
||
model.getImplementationDependencies().getDependencies().addAllLater(library.getDependencies().getImplementation().getDependencies()); | ||
model.getApiDependencies().getDependencies().addAllLater(library.getDependencies().getApi().getDependencies()); | ||
|
||
project.getComponents().withType(org.gradle.language.cpp.CppLibrary.class).configureEach(libraryComponent -> | ||
libraryComponent.getBinaries().configureEach(binary -> { | ||
binary.getCompileTask().get().getCompilerArgs().add(library.getCppVersion().map(v -> "--std=" + v)); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters