Skip to content

Commit

Permalink
* Change the @Platform(executable=... property to an array and all…
Browse files Browse the repository at this point in the history
…ow bundling multiple files per class
  • Loading branch information
saudet committed Aug 2, 2020
1 parent a59010d commit 52951ca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Change the `@Platform(executable=...` property to an array and allow bundling multiple files per class
* Prevent `Builder` unnecessarily linking with `-framework JavaVM` to fix GraalVM Native Image on Mac ([issue #417](https://github.com/bytedeco/javacpp/issues/417))
* Add `Pointer.getPointer()` methods as shortcuts for `new P(p).position(p.position + i)` ([issue #155](https://github.com/bytedeco/javacpp/issues/155))
* Fix `Generator` for cases when a `FunctionPointer` returns another `FunctionPointer`
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/bytedeco/javacpp/ClassProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2018 Samuel Audet
* Copyright (C) 2011-2020 Samuel Audet
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -205,8 +205,7 @@ public void load(Class cls, boolean inherit) {

String[] pragma = {}, define = {}, exclude = {}, include = {}, cinclude = {}, includepath = {}, includeresource = {}, compiler = {},
linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preloadresource = {}, preload = {},
resourcepath = {}, resource = {}, extension = {}, executablepath = {};
String executable = "";
resourcepath = {}, resource = {}, extension = {}, executablepath = {}, executable = {};
String library = "jni" + c.getSimpleName();
if (hasPlatformProperties) {
if (ourTarget != null && ourTarget.length() > 0) {
Expand Down Expand Up @@ -262,7 +261,7 @@ public void load(Class cls, boolean inherit) {
if (p.resource() .length > 0) { resource = p.resource(); }
if (p.extension() .length > 0) { extension = p.extension(); }
if (p.executablepath().length > 0) { executablepath = p.executablepath(); }
if (p.executable().length() > 0) { executable = p.executable(); }
if (p.executable() .length > 0) { executable = p.executable(); }
if (p.library().length() > 0) { library = p.library(); }
}
}
Expand Down Expand Up @@ -313,7 +312,7 @@ public void load(Class cls, boolean inherit) {
addAll("platform.extension", extension);
}
addAll("platform.executablepath", executablepath);
setProperty("platform.executable", executable);
addAll("platform.executable", executable);
setProperty("platform.library", library);

if (LoadEnabled.class.isAssignableFrom(c)) {
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/org/bytedeco/javacpp/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1239,14 +1239,15 @@ public static String load(Class cls, Properties properties, boolean pathsFirst)
}
}

String executable = p.getProperty("platform.executable");
if (executable != null && executable.length() > 0) {
List<String> executables = p.get("platform.executable");
List<String> executablePaths = new ArrayList<String>();
if (executables.size() > 0) {
String platform = p.getProperty("platform");
String[] extensions = p.get("platform.extension").toArray(new String[0]);
String prefix = p.getProperty("platform.executable.prefix", "");
String suffix = p.getProperty("platform.executable.suffix", "");
String filename = prefix + executable + suffix;
String libraryPath = p.getProperty("platform.library.path", "");
String filename = prefix + executables.get(0) + suffix;
String libraryPath = p.getProperty("platform.library.path", "");
try {
if (libraryPath.length() > 0) {
// look for the libraries in system paths, and extract them in cache anyway,
Expand All @@ -1262,20 +1263,25 @@ public static String load(Class cls, Properties properties, boolean pathsFirst)
}
}
}
for (int i = extensions.length - 1; i >= -1; i--) {
// iterate extensions in reverse to be consistent with the overriding of properties
String extension = i >= 0 ? extensions[i] : "";
String subdir = libraryPath.length() > 0 ? "/" + libraryPath : platform + (extension == null ? "" : extension);
File f = cacheResource(cls, subdir + "/" + filename);
if (f != null) {
f.setExecutable(true);
return f.getAbsolutePath();
for (String executable : executables) {
filename = prefix + executable + suffix;
for (int i = extensions.length - 1; i >= -1; i--) {
// iterate extensions in reverse to be consistent with the overriding of properties
String extension = i >= 0 ? extensions[i] : "";
String subdir = libraryPath.length() > 0 ? "/" + libraryPath : platform + (extension == null ? "" : extension);
File f = cacheResource(cls, subdir + "/" + filename);
if (f != null) {
f.setExecutable(true);
executablePaths.add(f.getAbsolutePath());
}
}
}
} catch (IOException e) {
logger.error("Could not extract executable " + filename + ": " + e);
}
return null;
}
if (executables.size() > 0) {
return executablePaths.size() > 0 ? executablePaths.get(0) : null;
}

int librarySuffix = -1;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/annotation/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
String[] extension() default {};
/** A list of paths from which to copy executables from the {@link #executable()} value. */
String[] executablepath() default {};
/** An executable to bundle at build time and extract at runtime on load, instead of a library. */
String executable() default "";
/** Executables to bundle at build time and extract at runtime on load, instead of a library. */
String[] executable() default {};
/** The native JNI library associated with this class that {@link Builder} should
* try to build and {@link Loader} should try to load. If left empty, this value
* defaults to "jni" + the name that {@link Class#getSimpleName()} returns for
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/bytedeco/javacpp/tools/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1117,14 +1117,17 @@ public File[] build() throws IOException, InterruptedException, ParserException
continue;
}

String executableName = p.getProperty("platform.executable");
if (executableName != null && executableName.length() > 0) {
List<String> executableNames = p.get("platform.executable");
for (String executableName : executableNames) {
LinkedHashSet<Class> classList = executableMap.get(executableName);
if (classList == null) {
allNames.add(executableName);
executableMap.put(executableName, classList = new LinkedHashSet<Class>());
}
classList.addAll(p.getEffectiveClasses());
}
if (executableNames.size() > 0) {
// has executables -> skip over default libraryName
continue;
}

Expand Down

0 comments on commit 52951ca

Please sign in to comment.