Skip to content

Commit 7cc1ed7

Browse files
committed
Merge pull request XRPLF#309 from naveenatceg/staticbuild
RocksDB Staticbuild
2 parents 0a29ce5 + ba6d660 commit 7cc1ed7

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

Makefile

+33-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ endif # PLATFORM_SHARED_EXT
198198

199199
.PHONY: blackbox_crash_test check clean coverage crash_test ldb_tests \
200200
release tags valgrind_check whitebox_crash_test format static_lib shared_lib all \
201-
dbg install uninstall
201+
dbg rocksdbjavastatic rocksdbjava install uninstall
202202

203203
all: $(LIBRARY) $(PROGRAMS) $(TESTS)
204204

@@ -268,7 +268,7 @@ unity: unity.cc unity.o
268268
clean:
269269
-rm -f $(PROGRAMS) $(TESTS) $(LIBRARY) $(SHARED) $(MEMENVLIBRARY) build_config.mk unity.cc
270270
-rm -rf ios-x86/* ios-arm/*
271-
-find . -name "*.[od]" -exec rm {} \;
271+
-find . -name "*.[oda]" -exec rm {} \;
272272
-find . -type f -regex ".*\.\(\(gcda\)\|\(gcno\)\)" -exec rm {} \;
273273
tags:
274274
ctags * -R
@@ -518,6 +518,37 @@ ROCKSDBJNILIB = librocksdbjni.jnilib
518518
JAVA_INCLUDE = -I/System/Library/Frameworks/JavaVM.framework/Headers/
519519
endif
520520

521+
libz.a:
522+
-rm -rf zlib-1.2.8
523+
curl -O http://zlib.net/zlib-1.2.8.tar.gz
524+
tar xvzf zlib-1.2.8.tar.gz
525+
cd zlib-1.2.8 && CFLAGS='-fPIC' ./configure --static && make
526+
cp zlib-1.2.8/libz.a .
527+
528+
libbz2.a:
529+
-rm -rf bzip2-1.0.6
530+
curl -O http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz
531+
tar xvzf bzip2-1.0.6.tar.gz
532+
cd bzip2-1.0.6 && make CFLAGS='-fPIC -Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64'
533+
cp bzip2-1.0.6/libbz2.a .
534+
535+
libsnappy.a:
536+
-rm -rf snappy-1.1.1
537+
curl -O https://snappy.googlecode.com/files/snappy-1.1.1.tar.gz
538+
tar xvzf snappy-1.1.1.tar.gz
539+
cd snappy-1.1.1 && ./configure --with-pic --enable-static
540+
cd snappy-1.1.1 && make
541+
cp snappy-1.1.1/.libs/libsnappy.a .
542+
543+
544+
rocksdbjavastatic: libz.a libbz2.a libsnappy.a
545+
OPT="-fPIC -DNDEBUG -O2" $(MAKE) $(LIBRARY) -j
546+
cd java;$(MAKE) java;
547+
rm -f ./java/$(ROCKSDBJNILIB)
548+
$(CXX) $(CXXFLAGS) -I./java/. $(JAVA_INCLUDE) -shared -fPIC -o ./java/$(ROCKSDBJNILIB) $(JNI_NATIVE_SOURCES) $(LIBOBJECTS) $(COVERAGEFLAGS) libz.a libbz2.a libsnappy.a
549+
cd java;jar -cf $(ROCKSDB_JAR) org/rocksdb/*.class org/rocksdb/util/*.class HISTORY*.md $(ROCKSDBJNILIB)
550+
551+
521552
rocksdbjava:
522553
OPT="-fPIC -DNDEBUG -O2" $(MAKE) $(LIBRARY) -j32
523554
cd java;$(MAKE) java;
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.rocksdb;
2+
3+
import java.io.*;
4+
5+
6+
/**
7+
* This class is used to load the RocksDB shared library from within the jar.
8+
* The shared library is extracted to a temp folder and loaded from there.
9+
*/
10+
public class NativeLibraryLoader {
11+
private static String sharedLibraryName = "librocksdbjni.so";
12+
private static String tempFilePrefix = "librocksdbjni";
13+
private static String tempFileSuffix = ".so";
14+
15+
public static void loadLibraryFromJar(String tmpDir)
16+
throws IOException {
17+
File temp;
18+
if(tmpDir == null || tmpDir.equals(""))
19+
temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
20+
else
21+
temp = new File(tmpDir + "/" + sharedLibraryName);
22+
23+
temp.deleteOnExit();
24+
25+
if (!temp.exists()) {
26+
throw new RuntimeException("File " + temp.getAbsolutePath() + " does not exist.");
27+
}
28+
29+
byte[] buffer = new byte[102400];
30+
int readBytes;
31+
32+
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(sharedLibraryName);
33+
if (is == null) {
34+
throw new RuntimeException(sharedLibraryName + " was not found inside JAR.");
35+
}
36+
37+
OutputStream os = null;
38+
try {
39+
os = new FileOutputStream(temp);
40+
while ((readBytes = is.read(buffer)) != -1) {
41+
os.write(buffer, 0, readBytes);
42+
}
43+
} finally {
44+
if(os != null)
45+
os.close();
46+
47+
if(is != null)
48+
is.close();
49+
}
50+
51+
System.load(temp.getAbsolutePath());
52+
}
53+
/**
54+
* Private constructor to disallow instantiation
55+
*/
56+
private NativeLibraryLoader() {
57+
}
58+
}

java/org/rocksdb/Options.java

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
* native resources will be released as part of the process.
1414
*/
1515
public class Options extends RocksObject {
16+
static {
17+
RocksDB.loadLibrary();
18+
}
1619
static final long DEFAULT_CACHE_SIZE = 8 << 20;
1720
static final int DEFAULT_NUM_SHARD_BITS = -1;
1821
/**

java/org/rocksdb/RocksDB.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.Closeable;
1212
import java.io.IOException;
1313
import org.rocksdb.util.Environment;
14+
import org.rocksdb.NativeLibraryLoader;
1415

1516
/**
1617
* A RocksDB is a persistent ordered map from keys to values. It is safe for
@@ -23,11 +24,19 @@ public class RocksDB extends RocksObject {
2324
private static final String[] compressionLibs_ = {
2425
"snappy", "z", "bzip2", "lz4", "lz4hc"};
2526

27+
static {
28+
RocksDB.loadLibrary();
29+
}
30+
2631
/**
2732
* Loads the necessary library files.
2833
* Calling this method twice will have no effect.
34+
* By default the method extracts the shared library for loading at
35+
* java.io.tmpdir, however, you can override this temporary location by
36+
* setting the environment variable ROCKSDB_SHAREDLIB_DIR.
2937
*/
3038
public static synchronized void loadLibrary() {
39+
String tmpDir = System.getenv("ROCKSDB_SHAREDLIB_DIR");
3140
// loading possibly necessary libraries.
3241
for (String lib : compressionLibs_) {
3342
try {
@@ -36,8 +45,14 @@ public static synchronized void loadLibrary() {
3645
// since it may be optional, we ignore its loading failure here.
3746
}
3847
}
39-
// However, if any of them is required. We will see error here.
40-
System.loadLibrary("rocksdbjni");
48+
try
49+
{
50+
NativeLibraryLoader.loadLibraryFromJar(tmpDir);
51+
}
52+
catch (IOException e)
53+
{
54+
throw new RuntimeException("Unable to load the RocksDB shared library" + e);
55+
}
4156
}
4257

4358
/**

0 commit comments

Comments
 (0)