Skip to content

Commit dd39c79

Browse files
committed
Update Musl Libc dependency setup with a symlink to musl-gcc
1 parent 409f801 commit dd39c79

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

docs/reference-manual/native-image/guides/build-static-and-mostly-static-executable.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,59 @@ However, you can create a statically linked or mostly-statically linked native e
1616
A static native executable is easy to distribute and deploy on a slim or distroless container (a scratch container).
1717
You can create a static native executable by statically linking it against [musl-libc](https://musl.libc.org/), a lightweight, fast and simple `libc` implementation.
1818

19-
**A mostly-static native executable** is a binary that links everything (`zlib`, JDK shared libraries) except the standard C library, `libc`. This is an alternative option to statically linking everything. Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
20-
This approach is ideal for deployment on a distroless container image.
19+
**A mostly-static native executable** is a binary that links everything (`zlib`, JDK-shared static libraries) except the standard C library, `libc`. This is an alternative option to statically linking everything. Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
20+
This approach is useful for deployment on a distroless container image.
2121

2222
> Note: This currently only works when linked against `libc`.
2323
2424
This guide shows how you can take advantage of Native Image linking options including fully dynamic, fully static, and mostly static (except `libc`) to generate an executable ideal for your deployment scenario.
2525

2626
## Prerequisites and Preparation
2727

28-
The following prerequisites should be met:
29-
30-
- Linux AMD64 operating system
31-
- GraalVM distribution for Java 17 of higher
28+
- Linux x64 operating system
29+
- GraalVM distribution for Java 17 or higher
3230
- A 64-bit `musl` toolchain, `make`, and `configure`
3331
- The latest `zlib` library
3432

35-
1. Install a GraalVM JDK.
36-
The easiest way to get started is with [SDKMAN!](https://sdkman.io/jdks#graal).
33+
The easiest way to install GraalVM is with [SDKMAN!](https://sdkman.io/jdks#graal).
3734
For other installation options, visit the [Downloads section](https://www.graalvm.org/downloads/).
3835

39-
Next, you should install the `musl` toolchain, compile, and install `zlib` into the toolchain.
40-
2. Download the `musl` toolchain from [musl.libc.org](https://musl.libc.org/)
41-
We recommend [musl-1.2.4](ttps://musl.libc.org/releases/musl-1.2.4.tar.gz).
42-
Extract the toolchain to a directory of your choice.
43-
Create a new environment variable, named `$TOOLCHAIN_DIR`, and set it to this directory.
44-
```bash
45-
export TOOLCHAIN_DIR=/path/to/musl-1.2.4
46-
```
47-
48-
3. Download the latest `zlib` library sources from [zlib.net](https://zlib.net/) and extract them. (This example application was tested with [zlib-1.2.13](https://zlib.net/fossils/zlib-1.2.13.tar.gz).)
49-
50-
4. Create a new environment variable, named `CC`:
51-
```bash
52-
CC=$TOOLCHAIN_DIR/bin/gcc
53-
```
36+
To be able to create static native applications with Native Image, a `musl` toolchain with the `zlib` library are required on the system.
37+
For the best compatibility, use [musl-1.2.4](https://musl.libc.org/releases/musl-1.2.4.tar.gz) or later.
38+
We recommend building `musl` from [source](https://musl.libc.org/) as shown below:
39+
40+
```bash
41+
# Specify an installation directory for musl:
42+
export MUSL_HOME=$PWD/musl-toolchain
43+
44+
# Download musl and zlib sources:
45+
curl -O https://musl.libc.org/releases/musl-1.2.4.tar.gz
46+
curl -O https://zlib.net/fossils/zlib-1.2.13.tar.gz
47+
48+
# Build musl from source
49+
tar -xzvf musl-1.2.4.tar.gz
50+
pushd musl-1.2.4
51+
./configure --prefix=$MUSL_HOME --static
52+
# The next operation may require privileged access to system resources, so use sudo
53+
sudo make && make install
54+
popd
55+
56+
# Install a symlink for use by native-image
57+
ln -s $MUSL_HOME/bin/musl-gcc $MUSL_HOME/bin/x86_64-linux-musl-gcc
58+
59+
# Extend the system path and confirm that musl is available by printing its version
60+
export PATH="$MUSL_HOME/bin:$PATH"
61+
x86_64-linux-musl-gcc --version
62+
63+
# Build zlib with musl from source and install into the MUSL_HOME directory
64+
tar -xzvf zlib-1.2.13.tar.gz
65+
pushd zlib-1.2.13
66+
CC=musl-gcc ./configure --prefix=$MUSL_HOME --static
67+
make && make install
68+
popd
69+
```
5470

55-
5. Change into the `zlib` directory, and then run the following commands to compile and install `zlib` into the toolchain:
56-
```bash
57-
cd zlib-1.2.13
58-
./configure --prefix=$TOOLCHAIN_DIR --static
59-
make
60-
make install
61-
```
71+
With the requirements set up, create the demo.
6272

6373
## Build a Static Native Executable
6474

@@ -82,27 +92,17 @@ Create a new environment variable, named `$TOOLCHAIN_DIR`, and set it to this di
8292
```
8393
This application iterates over your environment variables and prints out the ones that contain the `String` of characters passed as a command line argument.
8494

85-
2. Ensure the directory named `$TOOLCHAIN_DIR/bin` is present on your `PATH`.
86-
To verify this, run the following command:
87-
```bash
88-
x86_64-linux-musl-gcc
89-
```
90-
You should see output similar to the following:
91-
```
92-
x86_64-linux-musl-gcc: fatal error: no input files
93-
compilation terminated.
94-
```
95-
3. Compile the file:
95+
2. Compile the application:
9696
```shell
9797
javac EnvMap.java
9898
```
9999

100-
4. Build a static native executable by running this command:
100+
3. Build a static native executable by running this command:
101101
```shell
102102
native-image --static --libc=musl EnvMap
103103
```
104104
This produces a native executable with statically linked system libraries.
105-
You can pass other arguments before a class or JAR file.
105+
You can pass other arguments before a class or JAR file. Run it with `./envmap`.
106106

107107
## Build a Mostly-Static Native Executable
108108

@@ -119,7 +119,7 @@ To build a mostly-static native executable for the above `EnvMap` demo, run:
119119
native-image --static-nolibc EnvMap
120120
```
121121

122-
This produces a native executable that statically links all involved libraries (including JDK shared libraries) except for `libc`.
122+
This produces a native executable that statically links all involved libraries (including JDK-shared static libraries) except for `libc`.
123123
This includes `zlib`.
124124
Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
125125
One way to check what dynamic libraries your application depends on is to run `ldd` with the native executable, for example, `ldd envmap`.

0 commit comments

Comments
 (0)