Skip to content

Instantly share code, notes, and snippets.

@sify21
Last active February 16, 2023 11:04
Show Gist options
  • Save sify21/2eec007e4b05d448c85192fc279232c6 to your computer and use it in GitHub Desktop.
Save sify21/2eec007e4b05d448c85192fc279232c6 to your computer and use it in GitHub Desktop.
build static native image with graal
# build gcc toolchain targeting x86_64-linux-musl with https://github.com/richfelker/musl-cross-make
# don't use master branch which uses musl v1.2.1(the new malloc implementation has deadlock problems, v1.2.2 fixes it, v1.2.0 doesn't have it)
cd ~
git clone https://github.com/richfelker/musl-cross-make -b v0.9.9
cd musl-cross-make
cp config.mak.dist config.mak
# edit config.mak: TARGET = x86_64-linux-musl
# edit cowpatch.sh to fix an error, add "OPTIND=1" after line 56. see https://github.com/richfelker/musl-cross-make/issues/86
# ensure you have g++, on fedora it's "dnf install gcc-c++"
make -j`nproc`
make install
ln -s output/bin/x86_64-linux-musl-gcc output/bin/musl-gcc
export PATH=$PATH:~/musl-cross-make/output/bin
# add zlib to gcc toolchain which is required by graal, see https://www.graalvm.org/latest/reference-manual/native-image/guides/build-static-executables/
cd ~
tar xvf zlib-{ver}.tar.gz
cd zlib-{ver}
CC=musl-gcc ./configure --static --prefix=~/musl-cross-make/output/x86_64-linux-musl/
make CC=musl-gcc
make install
# download graalvm and unzip
cd ~
tar xvf graalvm-ce-java11-linux-amd64-{ver}.tar.gz
export PATH=$PATH:~/graalvm-ce-java11-linux-amd64-{ver}/bin
# add native image to graal
gu install native-image
# generate graal configs for the runnable jar file
mkdir ~/graalconf
cd ~/graalvm-ce-java11-linux-amd64-{ver}/bin
./java -agentlib:native-image-agent=config-output-dir=~/graalconf -jar test.jar
# build static image to ~/test
./native-image --static --libc=musl --allow-incomplete-classpath --enable-http --enable-https --enable-all-security-services --report-unsupported-elements-at-runtime -H:ConfigurationFileDirectories=~/graalconf -jar ~/test.jar ~/test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment