Skip to content

Commit

Permalink
* Add javacppPlatformExtension property to BuildPlugin and map i…
Browse files Browse the repository at this point in the history
…t to `platform.extension` property
  • Loading branch information
saudet committed Dec 21, 2020
1 parent 86afaa4 commit 5889ebd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `javacppPlatformExtension` property to `BuildPlugin` and map it to `platform.extension` property
* Add instructions to integrate `BuildTask` with Android Studio ([issue #5](https://github.com/bytedeco/gradle-javacpp/issues/5))
* Fix `PlatformPlugin` evaluating `javacppPlatform` too early ([issue #8](https://github.com/bytedeco/gradle-javacpp/issues/8))
* Honor the `skip` property in `BuildTask` ([pull #7](https://github.com/bytedeco/gradle-javacpp/issues/7))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Fully functional sample projects are also provided in the [`samples`](samples) s
### The Build Plugin
To understand how [JavaCPP](https://github.com/bytedeco/javacpp) is meant to be used, one should first take a look at the [Mapping Recipes for C/C++ Libraries](https://github.com/bytedeco/javacpp/wiki/Mapping-Recipes), but a high-level overview of the [Basic Architecture](https://github.com/bytedeco/javacpp/wiki/Basic-Architecture) is also available to understand the bigger picture.

Once comfortable enough with the command line interface, the build plugin for Gradle can be used to integrate easily that workflow as part of `build.gradle` as shown below. By default, for Java libraries and applications, it creates a `javacppJar` task that archives the native libraries into a separate JAR file and sets its classifier to `$javacppPlatform`, while excluding those files from the default `jar` task. To customize the behavior, there are properties that can be modified and whose documentation is available as part of the source code in these files:
Once comfortable enough with the command line interface, the build plugin for Gradle can be used to integrate easily that workflow as part of `build.gradle` as shown below. By default, for Java libraries and applications, it creates a `javacppJar` task that archives the native libraries into a separate JAR file and sets its classifier to `$javacppPlatform$javacppPlatformExtension`, while excluding those files from the default `jar` task. To customize the behavior, there are properties that can be modified and whose documentation is available as part of the source code in these files:

* [`BuildTask.java`](src/main/java/org/bytedeco/gradle/javacpp/BuildTask.java)
* [`BuildPlugin.java`](src/main/java/org/bytedeco/gradle/javacpp/BuildPlugin.java)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/gradle/javacpp/BuildExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public List<MavenArtifact> existingArtifacts(Configuration configuration) throws
for (ResolvedDependency rd : configuration.getResolvedConfiguration().getLenientConfiguration().getFirstLevelModuleDependencies()) {
if (rd.getModuleGroup().equals(project.getGroup()) && rd.getModuleName().equals(name)) {
for (ResolvedArtifact ra : rd.getModuleArtifacts()) {
if (ra.getClassifier() != null && !ra.getClassifier().equals(plugin.getPlatform())) {
if (ra.getClassifier() != null && !ra.getClassifier().equals(plugin.getPlatform() + plugin.getPlatformExtension())) {
try {
File in = ra.getFile();
File out = new File(libsDir, in.getName());
Expand Down Expand Up @@ -123,7 +123,7 @@ public Action<? extends XmlProvider> xmlAction(Configuration configuration, Stri
String[] allPlatforms = {"android-arm", "android-arm64", "android-x86", "android-x86_64",
"ios-arm", "ios-arm64", "ios-x86", "ios-x86_64",
"linux-armhf", "linux-arm64", "linux-ppc64le", "linux-x86", "linux-x86_64",
"macosx-x86_64", "windows-x86", "windows-x86_64"};
"macosx-arm64", "macosx-x86_64", "windows-x86", "windows-x86_64"};

String[] osNameFrom = {"linux", "mac os x", "windows"};
String[] osNameKernel = {"linux", "darwin", "windows"};
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/org/bytedeco/gradle/javacpp/BuildPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import org.bytedeco.javacpp.Loader;
import org.gradle.api.Project;
Expand Down Expand Up @@ -74,16 +75,23 @@ String getPlatform() {
return (String)project.findProperty("javacppPlatform");
}

String getPlatformExtension() {
return (String)project.findProperty("javacppPlatformExtension");
}

boolean isLibraryPath(String path) {
String p = (String)project.findProperty("javacpp.platform.library.path");
return p != null && p.length() > 0 ? path.startsWith(p) : path.contains("/" + getPlatform() + "/");
return p != null && p.length() > 0 ? path.startsWith(p) : path.contains("/" + getPlatform() + getPlatformExtension() + "/");
}

@Override public void apply(Project project) {
this.project = project;
if (!project.hasProperty("javacppPlatform")) {
project.getExtensions().getExtraProperties().set("javacppPlatform", Loader.Detector.getPlatform());
}
if (!project.hasProperty("javacppPlatformExtension")) {
project.getExtensions().getExtraProperties().set("javacppPlatformExtension", "");
}
if (project.getExtensions().findByName("javacppBuild") == null) {
project.getExtensions().create("javacppBuild", BuildExtension.class, this);
}
Expand All @@ -105,7 +113,12 @@ boolean isLibraryPath(String path) {
project.getTasks().register("javacppBuildCommand", BuildTask.class, task -> {
task.classPath = paths;
task.properties = getPlatform();
if (getPlatformExtension() != null && getPlatformExtension().length() > 0) {
task.propertyKeysAndValues = new Properties();
task.propertyKeysAndValues.setProperty("platform.extension", getPlatformExtension());
}
task.classOrPackageNames = new String[0];
task.workingDirectory = project.getProjectDir();
});

project.getTasks().register("javacppCompileJava", JavaCompile.class, task -> {
Expand All @@ -118,6 +131,10 @@ boolean isLibraryPath(String path) {
project.getTasks().register("javacppBuildParser", BuildTask.class, task -> {
task.classPath = paths;
task.properties = getPlatform();
if (getPlatformExtension() != null && getPlatformExtension().length() > 0) {
task.propertyKeysAndValues = new Properties();
task.propertyKeysAndValues.setProperty("platform.extension", getPlatformExtension());
}
task.outputDirectory = main.getJava().getSrcDirs().iterator().next();
task.dependsOn("javacppCompileJava");
task.doFirst(t -> { main.getJava().srcDir(task.outputDirectory); });
Expand All @@ -128,6 +145,10 @@ boolean isLibraryPath(String path) {
project.getTasks().register("javacppBuildCompiler", BuildTask.class, task -> {
task.classPath = paths;
task.properties = getPlatform();
if (getPlatformExtension() != null && getPlatformExtension().length() > 0) {
task.propertyKeysAndValues = new Properties();
task.propertyKeysAndValues.setProperty("platform.extension", getPlatformExtension());
}
task.dependsOn("compileJava");
});

Expand All @@ -149,7 +170,7 @@ boolean isLibraryPath(String path) {

TaskProvider<Jar> javacppJarTask = project.getTasks().register("javacppJar", Jar.class, task -> {
task.from(main.getOutput());
task.setClassifier(getPlatform());
task.setClassifier(getPlatform() + getPlatformExtension());
task.include(file -> file.isDirectory() || isLibraryPath(file.getPath()));
task.dependsOn("jar");
});
Expand Down

0 comments on commit 5889ebd

Please sign in to comment.