diff --git a/docker/Dockerfile.alpine b/docker/Dockerfile.alpine new file mode 100644 index 0000000..2a47f12 --- /dev/null +++ b/docker/Dockerfile.alpine @@ -0,0 +1,48 @@ +FROM alpine:3.18 + +# Install build dependencies +RUN apk add --no-cache \ + openjdk11 \ + cmake \ + make \ + gcc \ + g++ \ + musl-dev \ + linux-headers + +# Set workspace directory +WORKDIR /workspace + +# Copy the source code +COPY . . + +# Build the project +ENV CC=gcc CXX=g++ TARGET_OS=Linux-musl + +# Install Maven +RUN apk add --no-cache maven + +# Build with Maven +RUN mvn package -Dlibrary.name=libsnappyjava.so -DskipTests -DskipJavadoc=true + +# Install snappy dependencies +RUN apk add --no-cache snappy-dev + +# Compile and link statically +RUN cd target/native/Linux-musl/x86_64 && \ + ${CXX} -std=c++11 -O2 -fPIC -DPIC -D_GNU_SOURCE \ + -I../../../../src/main/java/org/xerial/snappy/ \ + -I/usr/include \ + -shared -static-libstdc++ -static-libgcc \ + ../../../../src/main/java/org/xerial/snappy/SnappyNative.cpp \ + -L/usr/lib -lsnappy \ + -o libsnappyjava.so + +# Create necessary directories +RUN mkdir -p src/main/resources/org/xerial/snappy/native/Linux-musl/x86_64/ + +# Copy the built library +RUN cp target/native/Linux-musl/x86_64/libsnappyjava.so src/main/resources/org/xerial/snappy/native/Linux-musl/x86_64/ + +# Set the entry point +ENTRYPOINT ["java"] \ No newline at end of file diff --git a/docker/README-musl.md b/docker/README-musl.md new file mode 100644 index 0000000..1d56e36 --- /dev/null +++ b/docker/README-musl.md @@ -0,0 +1,22 @@ +# Building with musl libc + +This project now supports building with musl libc, which is commonly used in Alpine Linux. To build using musl: + +1. Use the provided Alpine-based Dockerfile: +```bash +docker build -f docker/Dockerfile.alpine -t snappy-java-alpine . +``` + +2. The build process will: +- Use musl libc instead of glibc +- Statically link required libraries +- Create a native library compatible with Alpine/musl systems + +The resulting library will be placed in: +`src/main/resources/org/xerial/snappy/native/Linux-musl/x86_64/libsnappyjava.so` + +## Testing +To test the musl build: +```bash +docker run --rm snappy-java-alpine -cp target/snappy-java-*.jar org.xerial.snappy.SnappyLoader +``` \ No newline at end of file diff --git a/docker/Toolchain-musl.cmake b/docker/Toolchain-musl.cmake new file mode 100644 index 0000000..ea21053 --- /dev/null +++ b/docker/Toolchain-musl.cmake @@ -0,0 +1,20 @@ +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_VERSION 1) + +# Specify the cross compilers +set(CMAKE_C_COMPILER gcc) +set(CMAKE_CXX_COMPILER g++) + +# Set target environment path +set(CMAKE_FIND_ROOT_PATH /usr) + +# Search for programs in the host environment +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + +# Search for libraries and headers in the target environment +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +# Set compiler flags for musl +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") \ No newline at end of file diff --git a/src/main/java/org/xerial/snappy/SnappyLoader.java b/src/main/java/org/xerial/snappy/SnappyLoader.java index f1d555c..25979db 100644 --- a/src/main/java/org/xerial/snappy/SnappyLoader.java +++ b/src/main/java/org/xerial/snappy/SnappyLoader.java @@ -327,7 +327,20 @@ public class SnappyLoader } // Load an OS-dependent native library inside a jar file - snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); + String osName = OSInfo.getOSName(); + // Check for musl by looking at the output of ldd --version + try { + Process p = Runtime.getRuntime().exec(new String[]{"ldd", "--version"}); + try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()))) { + String line = br.readLine(); + if (line != null && line.toLowerCase().contains("musl")) { + osName = "Linux-musl"; + } + } + } catch (Exception e) { + // Ignore errors - default to normal Linux detection + } + snappyNativeLibraryPath = "/org/xerial/snappy/native/" + osName + "/" + OSInfo.getArchName(); boolean hasNativeLib = hasResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName); if (!hasNativeLib) { if (OSInfo.getOSName().equals("Mac")) { diff --git a/src/main/resources/org/xerial/snappy/native/Linux-musl/x86_64/README.md b/src/main/resources/org/xerial/snappy/native/Linux-musl/x86_64/README.md new file mode 100644 index 0000000..61b40d6 --- /dev/null +++ b/src/main/resources/org/xerial/snappy/native/Linux-musl/x86_64/README.md @@ -0,0 +1 @@ +This directory contains native libraries for Linux-musl x86_64 platform \ No newline at end of file