Skip to content

Commit 3c61117

Browse files
committed
[GR-54633] Update Musl Libc dependency setup.
PullRequest: graal/18072
2 parents 2a17925 + dd39c79 commit 3c61117

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

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

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +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-
2. Next, you should install the `musl` toolchain, compile and install `zlib` into the toolchain.
40-
Download the `musl` toolchain from [musl.cc](https://musl.cc/).
41-
(We recommend [this one](https://more.musl.cc/10/x86_64-linux-musl/x86_64-linux-musl-native.tgz)).
42-
Extract the toolchain to a directory of your choice. This directory will be referred as `$TOOLCHAIN_DIR`.
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+
```
4370

44-
3. Download the latest `zlib` library sources from [zlib.net](https://zlib.net/) and extract them. (This documentation uses `zlib-1.2.11`.)
45-
46-
4. Create a new environment variable, named `CC`:
47-
```bash
48-
CC=$TOOLCHAIN_DIR/bin/gcc
49-
```
50-
51-
5. Change into the `zlib` directory, and then run the following commands to compile and install `zlib` into the toolchain:
52-
```bash
53-
./configure --prefix=$TOOLCHAIN_DIR --static
54-
make
55-
make install
56-
```
71+
With the requirements set up, create the demo.
5772

5873
## Build a Static Native Executable
5974

@@ -77,27 +92,17 @@ Extract the toolchain to a directory of your choice. This directory will be refe
7792
```
7893
This application iterates over your environment variables and prints out the ones that contain the `String` of characters passed as a command line argument.
7994

80-
2. Ensure the directory named `$TOOLCHAIN_DIR/bin` is present on your `PATH`.
81-
To verify this, run the following command:
82-
```bash
83-
x86_64-linux-musl-gcc
84-
```
85-
You should see output similar to the following:
86-
```
87-
x86_64-linux-musl-gcc: fatal error: no input files
88-
compilation terminated.
89-
```
90-
3. Compile the file:
95+
2. Compile the application:
9196
```shell
9297
javac EnvMap.java
9398
```
9499

95-
4. Build a static native executable by running this command:
100+
3. Build a static native executable by running this command:
96101
```shell
97102
native-image --static --libc=musl EnvMap
98103
```
99104
This produces a native executable with statically linked system libraries.
100-
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`.
101106

102107
## Build a Mostly-Static Native Executable
103108

@@ -114,9 +119,10 @@ To build a mostly-static native executable for the above `EnvMap` demo, run:
114119
native-image --static-nolibc EnvMap
115120
```
116121

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

121127
### Frequently Asked Questions
122128

0 commit comments

Comments
 (0)