fix: add snappy support

This commit is contained in:
Samuel Fernandes 2025-01-16 17:53:36 +00:00
parent ec23d7c611
commit fab9375a35
No known key found for this signature in database
GPG Key ID: C74A5164FE13D9B6
5 changed files with 105 additions and 1 deletions

48
docker/Dockerfile.alpine Normal file
View File

@ -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"]

22
docker/README-musl.md Normal file
View File

@ -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
```

View File

@ -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")

View File

@ -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")) {

View File

@ -0,0 +1 @@
This directory contains native libraries for Linux-musl x86_64 platform