diff --git a/circe-crc32c-sse42/pom.xml b/circe-crc32c-sse42/pom.xml index ddde7c4..a6df8ad 100644 --- a/circe-crc32c-sse42/pom.xml +++ b/circe-crc32c-sse42/pom.xml @@ -53,36 +53,99 @@ com.github.maven-nar nar-maven-plugin true + + + org.apache.maven.plugins + maven-assembly-plugin + 2.4.1 - ${nar.runtime} - - - jni - com.scurrilous.circe.crc - - - - ${nar.cpp.optionSet} - false - false - full - + + src/main/assembly/assembly.xml + + false + + + make-assembly + package + + single + + + - test-direct-access - - - com.scurrilous - circe-direct-access - ${project.version} - test - - + mac + + + Mac OS X + + + + + + com.github.maven-nar + nar-maven-plugin + 3.1.0 + true + + ${nar.runtime} + circe-crc32c-sse42 + + + jni + + + + ${nar.cpp.optionSet} + false + false + full + + + + + + + + Linux + + + Linux + + + + + + com.github.maven-nar + nar-maven-plugin + 3.1.0 + true + + ${nar.runtime} + circe-crc32c-sse42 + + + jni + + + + ${nar.cpp.optionSet} + false + false + full + + + rt + + + + + diff --git a/circe-crc32c-sse42/src/main/assembly/assembly.xml b/circe-crc32c-sse42/src/main/assembly/assembly.xml new file mode 100644 index 0000000..83621b0 --- /dev/null +++ b/circe-crc32c-sse42/src/main/assembly/assembly.xml @@ -0,0 +1,51 @@ + + + + all + + jar + + + false + + + + com.scurrilous + *:nar:* + + + + + + ${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-MacOSX-gpp-jni/lib/${os.arch}-MacOSX-gpp/jni + + lib + + lib* + + + + ${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-Linux-gpp-jni/lib/${os.arch}-Linux-gpp/jni + + lib + + lib* + + + + ${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-${os.name}-gpp-jni/lib/${os.arch}-${os.name}-gpp/jni + + lib + + lib* + + + + ${project.build.directory}/classes + + + **/* + + + + \ No newline at end of file diff --git a/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/LibraryUtils.java b/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/LibraryUtils.java new file mode 100644 index 0000000..a067ba7 --- /dev/null +++ b/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/LibraryUtils.java @@ -0,0 +1,90 @@ +package com.scurrilous.circe.crc; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Locale; + +/** + * provides utility to load libraries at runtime. + * + */ +public class LibraryUtils { + + public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US); + + /** + * loads given library from the this jar. ie: this jar contains: + * /lib/libcirce-crc32c-sse42.jnilib + * + * @param path + * : absolute path of the library in the jar
+ * if this jar contains: /lib/libcirce-crc32c-sse42.jnilib then + * provide the same absolute path as input + * @throws Exception + */ + public static void loadLibraryFromJar(String path) throws Exception { + + if (!path.startsWith("/")) { + throw new IllegalArgumentException("absolute path must start with /"); + } + + String[] parts = path.split("/"); + String filename = (parts.length > 0) ? parts[parts.length - 1] : null; + + File dir = File.createTempFile("native", ""); + dir.delete(); + if (!(dir.mkdir())) { + throw new IOException("Failed to create temp directory " + dir.getAbsolutePath()); + } + dir.deleteOnExit(); + File temp = new File(dir, filename); + temp.deleteOnExit(); + + byte[] buffer = new byte[1024]; + int read; + + InputStream input = LibraryUtils.class.getResourceAsStream(path); + if (input == null) { + throw new FileNotFoundException("Couldn't find file into jar " + path); + } + + OutputStream out = new FileOutputStream(temp); + try { + while ((read = input.read(buffer)) != -1) { + out.write(buffer, 0, read); + } + } finally { + out.close(); + input.close(); + } + + if (!temp.exists()) { + throw new FileNotFoundException("Failed to copy file from jar at " + temp.getAbsolutePath()); + } + + System.load(temp.getAbsolutePath()); + } + + /** + * Returns jni library extension based on OS specification. Maven-nar + * generates jni library based on different OS : + * http://mark.donszelmann.org/maven-nar-plugin/aol.html (jni.extension) + * + * @return library type based on operating system + */ + public static String libType() { + + if (OS_NAME.indexOf("mac") >= 0) { + return "jnilib"; + } else if (OS_NAME.indexOf("nix") >= 0 || OS_NAME.indexOf("nux") >= 0 || OS_NAME.indexOf("aix") > 0) { + return "so"; + } else if (OS_NAME.indexOf("win") >= 0) { + return "dll"; + } + throw new TypeNotPresentException(OS_NAME + " not supported", null); + } +} diff --git a/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/Sse42Crc32C.java b/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/Sse42Crc32C.java index 1126f06..35e13c1 100644 --- a/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/Sse42Crc32C.java +++ b/circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/Sse42Crc32C.java @@ -20,6 +20,7 @@ import com.scurrilous.circe.IncrementalIntHash; import com.scurrilous.circe.impl.AbstractIncrementalIntHash; import com.scurrilous.circe.params.CrcParameters; +import static com.scurrilous.circe.crc.LibraryUtils.*; /** * Implementation of CRC-32C using the SSE 4.2 CRC instruction. @@ -30,7 +31,7 @@ public final class Sse42Crc32C extends AbstractIncrementalIntHash implements Inc private static boolean checkSupported() { try { - NarSystem.loadLibrary(); + loadLibraryFromJar("/lib/libcirce-crc32c-sse42." + libType()); return nativeSupported(); } catch (final Exception | UnsatisfiedLinkError e) { return false;