Skip to content

feat: add support to x64 systems using musl #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ If you are using Mac and openjdk7 (or higher), use the following option:

$ make native LIBNAME=libsnappyjava.dylib

## Building with Alpine
This project supports Alpine-based container images, which use the musl libc implementation. When building on Alpine, be aware that:

- All required dependencies are compatible with musl libc
- The build process has been tested and verified on Alpine environments
- No additional configuration is needed for Alpine compatibility
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ native: jni-header snappy-header $(NATIVE_DLL)
native-nocmake: jni-header $(NATIVE_DLL)
snappy: native $(TARGET)/$(snappy-jar-version).jar

native-all: native native-arm clean-docker mac64 win32 win64 linux32 linux64 linux-ppc64le linux-riscv64 linux-s390x
native-all: native native-arm clean-docker mac64 win32 win64 linux32 linux64 linux-ppc64le linux-riscv64 linux-s390x musl-image musl

ifdef CI
# Clean docker images within CI to avoid no space left error
Expand Down Expand Up @@ -199,6 +199,12 @@ linux32: jni-header
linux64: jni-header
docker run $(DOCKER_RUN_OPTS) -i -v $$PWD:/work xerial/centos5-linux-x86_64-pic bash -c 'make clean-native native-nocmake OS_NAME=Linux OS_ARCH=x86_64'

musl-image:
docker build -f docker/Dockerfile.linux-x86_64-musl -t snappy-java-musl .

musl: jni-header
docker run $(DOCKER_RUN_OPTS) -i -v $$PWD:/work snappy-java-musl bash -c 'make clean-native native-nocmake OS_NAME=Linux OS_ARCH=x86_64-musl'

freebsd64:
$(MAKE) native OS_NAME=FreeBSD OS_ARCH=x86_64

Expand Down
14 changes: 12 additions & 2 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ifndef JAVA_HOME
$(warning Set JAVA_HOME environment variable)
endif


JAVA := "$$JAVA_HOME/bin/java"
JAVAC := "$$JAVA_HOME/bin/javac"
JAVAH := "$$JAVA_HOME/bin/javah"
Expand Down Expand Up @@ -51,7 +50,7 @@ endif
# os=Default is meant to be generic unix/linux

# To support new CPU architecture, add a new target name here
known_os_archs := Linux-x86 Linux-x86_64 Linux-arm Linux-armv6 Linux-armv7 Linux-android-arm Linux-android-aarch64 Linux-aarch64 Linux-ppc Linux-ppc64 Linux-ppc64le Linux-s390 Linux-s390x Mac-x86 Mac-x86_64 Mac-aarch64 FreeBSD-x86_64 Windows-x86 Windows-x86_64 Windows-aarch64 SunOS-x86 SunOS-sparc SunOS-x86_64 AIX-ppc AIX-ppc64 Linux-riscv Linux-riscv64 Linux-loongarch Linux-loongarch64
known_os_archs := Linux-x86_64-musl Linux-x86 Linux-x86_64 Linux-arm Linux-armv6 Linux-armv7 Linux-android-arm Linux-android-aarch64 Linux-aarch64 Linux-ppc Linux-ppc64 Linux-ppc64le Linux-s390 Linux-s390x Mac-x86 Mac-x86_64 Mac-aarch64 FreeBSD-x86_64 Windows-x86 Windows-x86_64 Windows-aarch64 SunOS-x86 SunOS-sparc SunOS-x86_64 AIX-ppc AIX-ppc64 Linux-riscv Linux-riscv64 Linux-loongarch Linux-loongarch64
os_arch := $(OS_NAME)-$(OS_ARCH)
IBM_JDK_7 := $(findstring IBM, $(shell $(JAVA) -version 2>&1 | grep IBM | grep "JRE 1.7"))

Expand Down Expand Up @@ -103,6 +102,17 @@ Linux-x86_64_LINKFLAGS := -shared -static-libgcc -static-libstdc++
Linux-x86_64_LIBNAME := libsnappyjava.so
Linux-x86_64_SNAPPY_FLAGS :=

Linux-x86_64-musl_CXX := $(CROSS_PREFIX)g++
Linux-x86_64-musl_STRIP := $(CROSS_PREFIX)strip
ifeq ($(IBM_JDK_7),)
Linux-x86_64-musl_CXXFLAGS := -Ilib/inc_linux -I$(JAVA_HOME)/include -O2 -fPIC -fvisibility=hidden -m64 -std=c++11
else
Linux-x86_64-musl_CXXFLAGS := -include $(IBM_JDK_LIB)/jni_md.h -include $(IBM_JDK_LIB)/jniport.h -I$(JAVA_HOME)/include -O2 -fPIC -fvisibility=hidden -m64 -std=c++11
endif
Linux-x86_64-musl_LINKFLAGS := -shared -static-libgcc -static-libstdc++
Linux-x86_64-musl_LIBNAME := libsnappyjava.so
Linux-x86_64-musl_SNAPPY_FLAGS :=

Linux-ppc_CXX := g++
Linux-ppc_STRIP := strip
ifeq ($(IBM_JDK_7),)
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ OsgiKeys.additionalHeaders := Map(
"org/xerial/snappy/native/Linux/ppc64/libsnappyjava.so;osname=linux;processor=ppc64le",
"org/xerial/snappy/native/Linux/s390x/libsnappyjava.so;osname=linux;processor=s390x",
"org/xerial/snappy/native/Linux/loongarch64/libsnappyjava.so;osname=linux;processor=loongarch64",
"org/xerial/snappy/native/Linux/x86_64-musl/libsnappyjava.so;osname=Linux;processor=x86-64-musl",
"org/xerial/snappy/native/AIX/ppc/libsnappyjava.a;osname=aix;processor=ppc",
"org/xerial/snappy/native/AIX/ppc64/libsnappyjava.a;osname=aix;processor=ppc64",
"org/xerial/snappy/native/SunOS/x86/libsnappyjava.so;osname=sunos;processor=x86",
Expand Down
36 changes: 36 additions & 0 deletions docker/Dockerfile.linux-x86_64-musl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM alpine:3.18

# Set workspace directory
WORKDIR /work

# Copy the project to the container
COPY . .

# Install build dependencies
RUN apk add --no-cache \
openjdk8 \
cmake \
make \
gcc \
g++ \
musl-dev \
linux-headers \
git \
util-linux \
bash \
curl

# Install SBT
RUN curl -L "https://github.com/sbt/sbt/releases/download/v1.9.7/sbt-1.9.7.tgz" | tar xz -C /usr/local
ENV PATH="/usr/local/sbt/bin:${PATH}"

# Install python/pip
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

# Set Env Vars
ENV CC=gcc CXX=g++
ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
ENV PATH="${JAVA_HOME}/bin:${PATH}"
14 changes: 14 additions & 0 deletions src/main/java/org/xerial/snappy/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.xerial.snappy;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
Expand Down Expand Up @@ -215,6 +216,10 @@ public static String getArchName() {
return "android-arm";
}

if (isX64Musl()) {
return "x86_64-musl";
}

if(osArch.startsWith("arm")) {
osArch = resolveArmArchType();
}
Expand All @@ -226,6 +231,15 @@ public static String getArchName() {
return translateArchNameToFolderName(osArch);
}

private static boolean isX64Musl() {
try {
return new File("/lib/ld-musl-x86_64.so.1").exists();
}
catch (Exception e) {
return false;
}
}

static String translateOSNameToFolderName(String osName) {
if(osName.contains("Windows")) {
return "Windows";
Expand Down
Binary file not shown.
Loading