Skip to content

Commit

Permalink
final changes?
Browse files Browse the repository at this point in the history
  • Loading branch information
tiainen committed Nov 10, 2023
1 parent 9592b07 commit 5f65160
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 91 deletions.
32 changes: 14 additions & 18 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ jobs:
name: Build natives libraries
strategy:
matrix:
# os: [ ubuntu-latest, windows-latest ]
os: [ windows-latest ]
os: [ ubuntu-latest, windows-latest ]
target: [ native ]
# include:
# - os: ubuntu-latest
# target: mac64-signed
# - os: ubuntu-latest
# target: mac-arm64-signed
include:
- os: ubuntu-latest
target: mac64-signed
- os: ubuntu-latest
target: mac-arm64-signed
runs-on: ${{ matrix.os }}
steps:
- name: Make space
Expand Down Expand Up @@ -51,22 +50,19 @@ jobs:
perl Configure VC-WIN64A --prefix=C:\openssl1.1.1
nmake
nmake install
cp C:\openssl1.1.1\bin\libcrypto-1_1-x64.dll C:\openssl1.1.1\bin\libcrypto.dll
cp C:\openssl1.1.1\bin\libssl-1_1-x64.dll C:\openssl1.1.1\bin\libssl.dll
- name: Zip OpenSSL build
run: Compress-Archive -Destination openssl1.1.1w.zip -Path C:\openssl1.1.1
- uses: actions/upload-artifact@v3
with:
name: OpenSSL build
path: |
./openssl1.1.1w.zip
- name: Build binaries and package
- name: Build binaries
env:
OCI_EXE: docker
run: make ${{ matrix.target }} package

- name: Copy OpenSSL library
if: runner.os == 'Windows'
run: cp C:\openssl1.1.1\bin\libcrypto-1_1-x64.dll src/main/resources/org/sqlite/native/Windows/x86_64/libcrypto-1_1-x64.dll

- name: Package
run: make package

- name: Set platform classifier
shell: bash
run: |
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ win32: $(SQLITE_UNPACKED) jni-header
./docker/dockcross-windows-x86 -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native native CROSS_PREFIX=i686-w64-mingw32.static- OS_NAME=Windows OS_ARCH=x86'

win64: $(SQLITE_UNPACKED) jni-header
cp C:/openssl1.1.1/bin/libcrypto.dll $(NATIVE_DIR)
./docker/dockcross-windows-x64 -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native native CROSS_PREFIX=x86_64-w64-mingw32.static- OS_NAME=Windows OS_ARCH=x86_64'

win-armv7: $(SQLITE_UNPACKED) jni-header
Expand Down
4 changes: 2 additions & 2 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ CROSS_PREFIX :=
Default_CC := $(CROSS_PREFIX)gcc
Default_STRIP := $(CROSS_PREFIX)strip
Default_CCFLAGS := -I$(JAVA_HOME)/include -Ilib/inc_linux -Os -fPIC -fvisibility=hidden -std=gnu11 -march=native
Default_LINKFLAGS := -shared -static-libgcc -pthread -lm -lcrypto
Default_LINKFLAGS := -shared -static-libgcc -pthread -lm
Default_LIBNAME := libsqlitejdbc.so
Default_SQLITE_FLAGS :=

Expand All @@ -82,7 +82,7 @@ Linux-x86_SQLITE_FLAGS :=
Linux-x86_64_CC := $(CROSS_PREFIX)gcc
Linux-x86_64_STRIP := $(CROSS_PREFIX)strip
Linux-x86_64_CCFLAGS := -Ilib/inc_linux -I$(JAVA_HOME)/include -Os -fPIC -m64 -fvisibility=hidden -msse4.2 -maes
Linux-x86_64_LINKFLAGS := $(Default_LINKFLAGS)
Linux-x86_64_LINKFLAGS := $(Default_LINKFLAGS) -lcrypto
Linux-x86_64_LIBNAME := libsqlitejdbc.so
Linux-x86_64_SQLITE_FLAGS :=

Expand Down
96 changes: 26 additions & 70 deletions src/main/java/org/sqlite/SQLiteJDBCLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public static synchronized boolean initialize() throws Exception {
if (!extracted) {
cleanup();
}
if ("Windows".equalsIgnoreCase(OSInfo.getOSName())) {
loadSQLiteOpenSslLibrary();
}
extracted = false;
loadSQLiteNativeLibrary();
return extracted;
}
Expand Down Expand Up @@ -196,10 +192,11 @@ private static boolean contentsEquals(InputStream in1, InputStream in2) throws I
* @param libFolderForCurrentOS Library path.
* @param libraryFileName Library name.
* @param targetFolder Target folder.
* @param otherLibraryFileNames Other libraries to extract.
* @return
*/
private static boolean extractAndLoadLibraryFile(
String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
String libFolderForCurrentOS, String libraryFileName, String targetFolder, String...otherLibraryFileNames) {
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
// Include architecture name in temporary filename in order to avoid conflicts
// when multiple JVMs with different architectures running at the same time
Expand All @@ -212,6 +209,20 @@ private static boolean extractAndLoadLibraryFile(
Path extractedLckFile = Paths.get(targetFolder, extractedLckFileName);

try {
// Extract the other library files into the target directory
for (String otherLibraryFileName : otherLibraryFileNames) {
String otherLibraryFilePath = libFolderForCurrentOS + "/" + otherLibraryFileName;
System.out.println("OTHERLIB.Path: " + otherLibraryFilePath);

Path extractedOtherLibFile = Paths.get(targetFolder, otherLibraryFileNames);
try (InputStream reader = getResourceAsStream(otherLibraryFilePath)) {
Files.copy(reader, extractedOtherLibFile, StandardCopyOption.REPLACE_EXISTING);
} finally {
extractedOtherLibFile.toFile().deleteOnExit();
}
loadNativeLibrary(targetFolder, otherLibraryFileName);
}

// Extract a native library file into the target directory
try (InputStream reader = getResourceAsStream(nativeLibraryFilePath)) {
if (Files.notExists(extractedLckFile)) {
Expand Down Expand Up @@ -317,8 +328,10 @@ private static void loadSQLiteNativeLibrary() throws Exception {
// 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");
String sqliteOpenSslLibraryName = System.getProperty("org.sqlite.openssl.lib.name");
if (sqliteNativeLibraryName == null) {
sqliteNativeLibraryName = LibraryLoaderUtil.getNativeLibName();
sqliteOpenSslLibraryName = LibraryLoaderUtil.getNativeLibName("libcrypto-1_1-x64");
}

if (sqliteNativeLibraryPath != null) {
Expand All @@ -336,75 +349,18 @@ private static void loadSQLiteNativeLibrary() throws Exception {
LibraryLoaderUtil.hasNativeLib(sqliteNativeLibraryPath, sqliteNativeLibraryName);

if (hasNativeLib) {
// temporary library folder
String tempFolder = getTempDir().getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(
sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) {
extracted = true;
return;
} else {
triedPaths.add(sqliteNativeLibraryPath);
}
}

// As a last resort try from java.library.path
String javaLibraryPath = System.getProperty("java.library.path", "");
for (String ldPath : javaLibraryPath.split(File.pathSeparator)) {
if (ldPath.isEmpty()) {
continue;
}
if (loadNativeLibrary(ldPath, sqliteNativeLibraryName)) {
extracted = true;
return;
} else {
triedPaths.add(ldPath);
}
}

extracted = false;
throw new Exception(
String.format(
"No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(),
OSInfo.getArchName(),
StringUtils.join(triedPaths, File.pathSeparator)));
}

private static void loadSQLiteOpenSslLibrary() throws Exception {
if (extracted) {
return;
}

List<String> triedPaths = new LinkedList<>();

// Try loading library from org.sqlite.lib.path library path */
String sqliteNativeLibraryPath = System.getProperty("org.sqlite.lib.path");
String sqliteOpenSslLibraryName = System.getProperty("org.sqlite.openssl.lib.name");
if (sqliteOpenSslLibraryName == null) {
sqliteOpenSslLibraryName = LibraryLoaderUtil.getNativeLibName("crypto");
}

if (sqliteNativeLibraryPath != null) {
if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteOpenSslLibraryName)) {
extracted = true;
return;
} else {
triedPaths.add(sqliteNativeLibraryPath);
String[] extraLibNames = new String[] {};
if ("Windows".equalsIgnoreCase(OSInfo.getOSName())) {
extraLibNames = new String[] {
sqliteOpenSslLibraryName
};
}
}

// Load the os-dependent library from the jar file
sqliteNativeLibraryPath = LibraryLoaderUtil.getNativeLibResourcePath();
boolean hasNativeLib =
LibraryLoaderUtil.hasNativeLib(sqliteNativeLibraryPath, sqliteOpenSslLibraryName);

if (hasNativeLib) {
// temporary library folder
String tempFolder = getTempDir().getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(
sqliteNativeLibraryPath, sqliteOpenSslLibraryName, tempFolder)) {
sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder, extraLibNames)) {
extracted = true;
return;
} else {
Expand All @@ -418,7 +374,7 @@ private static void loadSQLiteOpenSslLibrary() throws Exception {
if (ldPath.isEmpty()) {
continue;
}
if (loadNativeLibrary(ldPath, sqliteOpenSslLibraryName)) {
if (loadNativeLibrary(ldPath, sqliteNativeLibraryName)) {
extracted = true;
return;
} else {
Expand All @@ -429,7 +385,7 @@ private static void loadSQLiteOpenSslLibrary() throws Exception {
extracted = false;
throw new Exception(
String.format(
"No openssl library found for os.name=%s, os.arch=%s, paths=[%s]",
"No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(),
OSInfo.getArchName(),
StringUtils.join(triedPaths, File.pathSeparator)));
Expand Down

0 comments on commit 5f65160

Please sign in to comment.