Skip to content

Commit

Permalink
* Call Loader.createLibraryLink() when executing the user specifie…
Browse files Browse the repository at this point in the history
…d `Builder.buildCommand` as well

 * Introduce new `platform.extensions` property to manage more than one set of binaries per platform
  • Loading branch information
saudet committed Sep 7, 2017
1 parent 936dd64 commit 35463a6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

* Call `Loader.createLibraryLink()` when executing the user specified `Builder.buildCommand` as well
* Introduce new `platform.extensions` property to manage more than one set of binaries per platform
* Catch `SecurityException` in `Loader.getCacheDir()` ([pull #198](https://github.com/bytedeco/javacpp/pull/198))

### July 25, 2017 version 1.3.3
* Call `malloc_trim(0)` after `System.gc()` on Linux to make sure memory gets released ([issue bytedeco/javacpp-presets#423](https://github.com/bytedeco/javacpp-presets/issues/423))
* Make public the `Pointer.formatBytes()` and `Pointer.parseBytes()` static methods
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/bytedeco/javacpp/ClassProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ClassProperties(Properties properties) {
|| k.equals("platform.preloadpath") || k.equals("platform.preload")
|| k.equals("platform.resourcepath") || k.equals("platform.resource")
|| k.equals("platform.frameworkpath") || k.equals("platform.framework")
|| k.equals("platform.library.suffix")) {
|| k.equals("platform.library.suffix") || k.equals("platform.extensions")) {
addAll(k, v.split(pathSeparator));
} else {
setProperty(k, v);
Expand Down Expand Up @@ -178,7 +178,8 @@ public void load(Class cls, boolean inherit) {
}

String[] pragma = {}, define = {}, include = {}, cinclude = {}, includepath = {}, includeresource = {}, compiler = {},
linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preload = {}, resourcepath = {}, resource = {};
linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preload = {},
resourcepath = {}, resource = {}, extensions = {};
String library = "jni" + c.getSimpleName();
for (Platform p : platforms != null ? platforms : new Platform[0]) {
String[][] names = { p.value().length > 0 ? p.value() : defaultNames, p.not() };
Expand Down Expand Up @@ -208,6 +209,7 @@ public void load(Class cls, boolean inherit) {
if (p.preload() .length > 0) { preload = p.preload(); }
if (p.resourcepath().length > 0) { resourcepath = p.resourcepath(); }
if (p.resource() .length > 0) { resource = p.resource(); }
if (p.extensions() .length > 0) { extensions = p.extensions(); }
if (p.library().length() > 0) { library = p.library(); }
}
}
Expand Down Expand Up @@ -251,6 +253,7 @@ public void load(Class cls, boolean inherit) {
addAll("platform.preload", preload);
addAll("platform.resourcepath", resourcepath);
addAll("platform.resource", resource);
addAll("platform.extensions", extensions);
setProperty("platform.library", library);

if (platforms != null && platforms.length > 0) {
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/bytedeco/javacpp/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ public static URL[] findLibrary(Class cls, ClassProperties properties, String li
String version2 = s2.length > 1 ? s2[s2.length-1] : "";

// If we do not already have the native library file ...
String subdir = properties.getProperty("platform") + '/';
String platform = properties.getProperty("platform");
String[] extensions = properties.get("platform.extensions").toArray(new String[0]);
String prefix = properties.getProperty("platform.library.prefix", "");
String suffix = properties.getProperty("platform.library.suffix", "");
String[] styles = {
Expand Down Expand Up @@ -856,16 +857,19 @@ public static URL[] findLibrary(Class cls, ClassProperties properties, String li
ArrayList<URL> urls = new ArrayList<URL>(styles.length * (1 + paths.size()));
for (int i = 0; cls != null && i < styles.length; i++) {
// ... then find it from in our resources ...
URL u = cls.getResource(subdir + styles[i]);
if (u != null) {
if (!styles[i].equals(styles2[i])) {
try {
u = new URL(u + "#" + styles2[i]);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
for (String extension : Arrays.copyOf(extensions, extensions.length + 1)) {
String subdir = platform + (extension == null ? "" : extension) + "/";
URL u = cls.getResource(subdir + styles[i]);
if (u != null) {
if (!styles[i].equals(styles2[i])) {
try {
u = new URL(u + "#" + styles2[i]);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
urls.add(u);
}
urls.add(u);
}
}
// ... and in case of bad resources search the paths last, or first on user request.
Expand Down Expand Up @@ -1023,7 +1027,7 @@ public static String createLibraryLink(String filename, ClassProperties properti
String libname = s[0];
String version = s.length > 1 ? s[s.length-1] : "";

if (version.length() == 0) {
if (version.length() == 0 || !name.contains(libname)) {
return filename;
}
for (String suffix : properties.get("platform.library.suffix")) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/bytedeco/javacpp/annotation/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
String[] resourcepath() default {};
/** A list of resources, either files or directories, that can be copied and extracted. */
String[] resource() default {};
/** The platform extensions to attempt to load for this library. The names here are
* appended to the platform name and looked up in the class path. */
String[] extensions() 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. */
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public class BuildMojo extends AbstractMojo {
@Parameter(property = "javacpp.copyResources", defaultValue = "false")
boolean copyResources = false;

/** Build for the given extension by appending to the platform name. */
@Parameter(property = "javacpp.extension")
String extension = null;

/** Also create a JAR file named {@code <jarPrefix>-<platform>.jar}. */
@Parameter(property = "javacpp.jarPrefix")
String jarPrefix = null;
Expand Down Expand Up @@ -237,6 +241,7 @@ String[] merge(String[] ss, String s) {
log.debug("header: " + header);
log.debug("copyLibs: " + copyLibs);
log.debug("copyResources: " + copyResources);
log.debug("extension: " + extension);
log.debug("jarPrefix: " + jarPrefix);
log.debug("properties: " + properties);
log.debug("propertyFile: " + propertyFile);
Expand Down Expand Up @@ -274,6 +279,7 @@ String[] merge(String[] ss, String s) {
.header(header)
.copyLibs(copyLibs)
.copyResources(copyResources)
.extension(extension)
.jarPrefix(jarPrefix)
.properties(properties)
.propertyFile(propertyFile)
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ File generateAndCompile(Class[] classes, String outputName) throws IOException,
uri = new URI(resourceURL.substring(0, resourceURL.length() - resourceName.length() + 1));
File targetDir = libraryPath.length() > 0
? (isFile ? new File(uri) : new File(classPath))
: new File(packageDir, platform);
: new File(packageDir, platform + (extension != null ? extension : ""));
outputPath = new File(targetDir, libraryPath);
sourcePrefix = new File(packageDir, outputName).getPath();
} catch (URISyntaxException e) {
Expand Down Expand Up @@ -539,6 +539,8 @@ public Builder(Logger logger) {
boolean copyLibs = false;
/** If true, also copies to the output directory resources listed in properties. */
boolean copyResources = false;
/** The name of the platform extension to build for, appended to the platform name. */
String extension = null;
/** Accumulates the various properties loaded from resources, files, command line options, etc. */
Properties properties = null;
/** The instance of the {@link ClassScanner} that fills up a {@link Collection} of {@link Class} objects to process. */
Expand Down Expand Up @@ -602,6 +604,11 @@ public Builder copyResources(boolean copyResources) {
this.copyResources = copyResources;
return this;
}
/** Sets the {@link #extension} field to the argument. */
public Builder extension(String extension) {
this.extension = extension;
return this;
}
/** Sets the {@link #outputName} field to the argument. */
public Builder outputName(String outputName) {
this.outputName = outputName;
Expand Down Expand Up @@ -724,12 +731,40 @@ public File[] build() throws IOException, InterruptedException, ParserException
String resources = properties.getProperty("platform.buildresource", "");
String separator = properties.getProperty("platform.path.separator");
if (paths.length() > 0 || resources.length() > 0) {

// Get all native libraries for classes on the class path.
List<String> libs = new ArrayList<String>();
ClassProperties libProperties = null;
for (Class c : classScanner.getClasses()) {
if (Loader.getEnclosingClass(c) != c) {
continue;
}
libProperties = Loader.loadProperties(c, properties, true);
if (!libProperties.isLoaded()) {
logger.warn("Could not load platform properties for " + c);
continue;
}
libs.addAll(libProperties.get("platform.preload"));
libs.addAll(libProperties.get("platform.link"));
}

// Extract the required resources.
for (String s : resources.split(separator)) {
for (File f : Loader.cacheResources(s)) {
if (paths.length() > 0 && !paths.endsWith(separator)) {
paths += separator;
}
paths += f.getCanonicalPath();

// Also create symbolic links for native libraries found there.
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
for (String lib : libs) {
Loader.createLibraryLink(file.getAbsolutePath(), libProperties, lib);
}
}
}
}
}
if (paths.length() > 0) {
Expand Down Expand Up @@ -900,6 +935,7 @@ public static void printHelp() {
System.out.println(" -header Generate header file with declarations of callbacks functions");
System.out.println(" -copylibs Copy to output directory dependent libraries (link and preload)");
System.out.println(" -copyresources Copy to output directory resources listed in properties");
System.out.println(" -extension <name> Build for the given extension by appending to the platform name");
System.out.println(" -jarprefix <prefix> Also create a JAR file named \"<prefix>-<platform>.jar\"");
System.out.println(" -properties <resource> Load all properties from resource");
System.out.println(" -propertyfile <file> Load all properties from file");
Expand Down Expand Up @@ -939,6 +975,8 @@ public static void main(String[] args) throws Exception {
builder.copyLibs(true);
} else if ("-copyresources".equals(args[i])) {
builder.copyResources(true);
} else if ("-extension".equals(args[i])) {
builder.extension(args[++i]);
} else if ("-jarprefix".equals(args[i])) {
builder.jarPrefix(args[++i]);
} else if ("-properties".equals(args[i])) {
Expand Down

0 comments on commit 35463a6

Please sign in to comment.