Skip to content

Instantly share code, notes, and snippets.

@alexarchambault
Forked from sify21/build.sh
Created July 15, 2021 13:54
Show Gist options
  • Save alexarchambault/e35bedf2549ae2ca076d6e1a80d9f7c5 to your computer and use it in GitHub Desktop.
Save alexarchambault/e35bedf2549ae2ca076d6e1a80d9f7c5 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
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/reference-manual/native-image/StaticImages/
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