Skip to content

Commit

Permalink
Merge pull request #77 from gradle/am/cpp
Browse files Browse the repository at this point in the history
Add `cppLibrary` and `cppApplication` software types
  • Loading branch information
ghale committed Jul 1, 2024
2 parents 0c80371 + 01f08fc commit 2fe3a3e
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 2 deletions.
10 changes: 10 additions & 0 deletions unified-prototype/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ To run the application, use:
```shell
../gradlew testbed-swift-application:runAll
```

## C++

The sample C++ projects live in the `testbed-cpp-library` and `testbed-cpp-application` directories.

To run the application, use:

```shell
../gradlew testbed-cpp-application:runAll
```
3 changes: 3 additions & 0 deletions unified-prototype/cpp-util/build.gradle.dcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cppLibrary {
cppVersion = "c++20"
}
5 changes: 5 additions & 0 deletions unified-prototype/cpp-util/src/main/cpp/cpp-util.cpp
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!";
}
6 changes: 6 additions & 0 deletions unified-prototype/cpp-util/src/main/public/cpp-util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <string>

class Util {
public:
std::string message();
};
4 changes: 4 additions & 0 deletions unified-prototype/settings.gradle.dcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
id("org.gradle.experimental.jvm-ecosystem")
id("org.gradle.experimental.kmp-ecosystem")
id("org.gradle.experimental.swift-ecosystem")
id("org.gradle.experimental.cpp-ecosystem")
}

dependencyResolutionManagement {
Expand Down Expand Up @@ -40,6 +41,7 @@ include("java-util")
include("kotlin-jvm-util")
include("kotlin-util")
include("swift-util")
include("cpp-util")
include("testbed-android-library")
include("testbed-android-application")
include("testbed-kotlin-library")
Expand All @@ -52,3 +54,5 @@ include("testbed-java-application")
include("testbed-java-library")
include("testbed-swift-library")
include("testbed-swift-application")
include("testbed-cpp-library")
include("testbed-cpp-application")
7 changes: 7 additions & 0 deletions unified-prototype/testbed-cpp-application/build.gradle.dcl
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 unified-prototype/testbed-cpp-application/src/main/cpp/main.cpp
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;
}
7 changes: 7 additions & 0 deletions unified-prototype/testbed-cpp-library/build.gradle.dcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cppLibrary {
cppVersion = "c++20"

dependencies {
implementation(project(":cpp-util"))
}
}
6 changes: 6 additions & 0 deletions unified-prototype/testbed-cpp-library/src/main/cpp/lib.cpp
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();
};
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)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id("build-logic.publishing")
}

description = "Common APIs and implementation classes shared by the Android, JVM, and KMP declarative prototypes"
description = "Common APIs and implementation classes shared by the ecosystem specific declarative prototypes"

dependencies {
implementation(libs.android.agp.application)
Expand Down
30 changes: 30 additions & 0 deletions unified-prototype/unified-plugin/plugin-cpp/build.gradle.kts
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")
}
}
}
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 {
}
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) {
}
}
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 {
}
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();
}
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);
}
})
);
}
}
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));
})
);
}
}
1 change: 1 addition & 0 deletions unified-prototype/unified-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include("plugin-android")
include("plugin-jvm")
include("plugin-kmp")
include("plugin-swift")
include("plugin-cpp")
include("plugin-common")
include("internal-testing-utils")

Expand Down

0 comments on commit 2fe3a3e

Please sign in to comment.