Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate and package jni lib in jar + load lib from jar without assuming it in java.library.path #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 85 additions & 22 deletions circe-crc32c-sse42/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,99 @@
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<runtime>${nar.runtime}</runtime>
<libraries>
<library>
<type>jni</type>
<narSystemPackage>com.scurrilous.circe.crc</narSystemPackage>
</library>
</libraries>
<cpp>
<optionSet>${nar.cpp.optionSet}</optionSet>
<exceptions>false</exceptions>
<rtti>false</rtti>
<optimize>full</optimize>
</cpp>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>test-direct-access</id>
<dependencies>
<dependency>
<groupId>com.scurrilous</groupId>
<artifactId>circe-direct-access</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<id>mac</id>
<activation>
<os>
<name>Mac OS X</name>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.1.0</version>
<extensions>true</extensions>
<configuration>
<runtime>${nar.runtime}</runtime>
<output>circe-crc32c-sse42</output>
<libraries>
<library>
<type>jni</type>
</library>
</libraries>
<cpp>
<optionSet>${nar.cpp.optionSet}</optionSet>
<exceptions>false</exceptions>
<rtti>false</rtti>
<optimize>full</optimize>
</cpp>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>Linux</id>
<activation>
<os>
<name>Linux</name>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.1.0</version>
<extensions>true</extensions>
<configuration>
<runtime>${nar.runtime}</runtime>
<output>circe-crc32c-sse42</output>
<libraries>
<library>
<type>jni</type>
</library>
</libraries>
<cpp>
<optionSet>${nar.cpp.optionSet}</optionSet>
<exceptions>false</exceptions>
<rtti>false</rtti>
<optimize>full</optimize>
</cpp>
<linker>
<libSet>rt</libSet>
</linker>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
51 changes: 51 additions & 0 deletions circe-crc32c-sse42/src/main/assembly/assembly.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>

<assembly>
<id>all</id>
<formats>
<format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<excludes>
<exclude>com.scurrilous</exclude>
<exclude>*:nar:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-MacOSX-gpp-jni/lib/${os.arch}-MacOSX-gpp/jni
</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>lib*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-Linux-gpp-jni/lib/${os.arch}-Linux-gpp/jni
</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>lib*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-${os.name}-gpp-jni/lib/${os.arch}-${os.name}-gpp/jni
</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>lib*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Original file line number Diff line number Diff line change
@@ -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 <br/>
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down