Load a native library according to the current OS and CPU architecture

This commit is contained in:
Taro L. Saito 2011-03-30 09:37:04 +09:00
parent 19d1233d18
commit 61de0555db
3 changed files with 53 additions and 17 deletions

View File

@ -10,7 +10,6 @@ $(SNAPPY_ARCHIVE):
curl -o$@ http://snappy.googlecode.com/files/snappy-$(VERSION).tar.gz
$(SNAPPY_SRC): $(SNAPPY_ARCHIVE)
tar xvfz $< -C $(TARGET)
@ -20,10 +19,8 @@ $(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/Snappy.java
SNAPPY_CC:=snappy-sinksource.cc snappy-stubs-internal.cc snappy.cc
SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) SnappyNative.o)
snappy: $(SNAPPY_OUT)/$(LIBNAME)
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC)/%.cc $(SNAPPY_SRC)
@ -42,3 +39,13 @@ clean-native:
rm -rf $(SNAPPY_OBJ) $(SNAPPY_OUT)/$(LIBNAME)
NATIVE_DIR:=src/main/resources/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)
NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME)
snappy: $(NATIVE_DLL)
$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
@mkdir -p $(@D)
cp $< $@

View File

@ -34,7 +34,7 @@ public class LoadSnappy
public static boolean initialize() {
if (!extracted)
loadSQLiteNativeLibrary();
loadSnappyNativeLibrary();
return extracted;
}
@ -78,7 +78,7 @@ public class LoadSnappy
private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName,
String targetFolder) {
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
final String prefix = "sqlite-" + getVersion() + "-";
final String prefix = "snappy-" + getVersion() + "-";
String extractedLibFileName = prefix + libraryFileName;
File extractedLibFile = new File(targetFolder, extractedLibFileName);
@ -149,27 +149,29 @@ public class LoadSnappy
return false;
}
private static void loadSQLiteNativeLibrary() {
private static void loadSnappyNativeLibrary() {
if (extracted)
return;
// Try loading library from org.sqlite.lib.path library path */
String sqliteNativeLibraryPath = System.getProperty("org.sqlite.lib.path");
String sqliteNativeLibraryName = System.getProperty("org.sqlite.lib.name");
if (sqliteNativeLibraryName == null)
sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc");
String snappyNativeLibraryPath = System.getProperty("org.xerial.snappy.lib.path");
String snappyNativeLibraryName = System.getProperty("org.xerial.snappy.lib.name");
if (sqliteNativeLibraryPath != null) {
if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) {
// Resolve the library file name with a suffix (e.g., dll, .so, etc.)
if (snappyNativeLibraryName == null)
snappyNativeLibraryName = System.mapLibraryName("snappy");
if (snappyNativeLibraryPath != null) {
if (loadNativeLibrary(snappyNativeLibraryPath, snappyNativeLibraryName)) {
extracted = true;
return;
}
}
// Load the os-dependent library from a jar file
sqliteNativeLibraryPath = "/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
if (LoadSnappy.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) {
if (LoadSnappy.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) == null) {
// use nested VM version
return;
}
@ -177,7 +179,7 @@ public class LoadSnappy
// temporary library folder
String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) {
if (extractAndLoadLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder)) {
extracted = true;
return;
}
@ -204,9 +206,9 @@ public class LoadSnappy
public static String getVersion() {
URL versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial/snappy-java/pom.properties");
URL versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial.snappy/snappy-java/pom.properties");
if (versionFile == null)
versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial/snappy-java/VERSION");
versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial.snappy/snappy-java/VERSION");
String version = "unknown";
try {

View File

@ -0,0 +1,27 @@
//--------------------------------------
// snappy-java Project
//
// SnappyTest.java
// Since: 2011/03/30
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import java.nio.ByteBuffer;
import org.junit.Test;
public class SnappyTest
{
@Test
public void load() throws Exception {
ByteBuffer src = ByteBuffer.allocate(1024);
src.put("hello world".getBytes());
ByteBuffer dest = ByteBuffer.allocate(1024);
Snappy.compress(src, dest);
}
}