62.5. zig-0.16.0
General-purpose programming language and toolchain
| Version | 0.16.0, release 3 |
| Shelf | lang — Language implementations, plus the modules of any language that has no module shelf of its own |
| Phase | 31_compilers (Compilers), 19 of 22 in build order |
| Licence | MIT |
| Homepage | https://ziglang.org/ |
| Build time | 81.3 SBU |
| Installed size | 206.2 MiB in 19544 files |
| Recipe | ports/core/lang/zig/kpkgbuild |
About
Zig is a systems programming language whose compiler is also a C and C++ compiler and a linker. In KDOS it builds ncdu and the Zig language server zls, and python3-maturin is built with its zig feature, the maturin build --zig that links through Zig. How a Zig port is written is in zig.
Zig is built against exactly one major series of LLVM, and its CMake search refuses an llvm-config from any other. It therefore depends on the slot ports llvm21, clang21 and lld21, which install LLVM, clang and lld of that series under /usr/lib/llvm21, and not on the system llvm; how the slot is kept apart from the system LLVM is in A second version beside the first.
The CMake build is upstream’s bootstrap. It translates stage1/zig1.wasm, a WebAssembly build of the compiler carried in the source archive, to C, compiles it, uses the result to build the compiler from its Zig source, and installs that compiler and the standard library under /usr/lib/zig. zig1.wasm is a bootstrap seed and is not installed.
Sources
zig-0.16.0.tar.xz(21.5 MiB)
sha25643186959edc87d5c7a1be7b7d2a25efffd22ce5807c7af99067f86f99641bfdf
upstream: https://ziglang.org/download/0.16.0/zig-0.16.0.tar.xz
archive copy: sources-3/lang--zig-0.16.0.tar.xz
Dependencies
Depends on: cmake, ninja, llvm21, clang21, lld21, zlib, zstd.
Required by (3): ncdu, python3-maturin, zls.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/31_compilers/phase.env && kpkg install zig'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/lang/zig.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/lang/zig
echo '43186959edc87d5c7a1be7b7d2a25efffd22ce5807c7af99067f86f99641bfdf zig-0.16.0.tar.xz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/zig
SRC=$SRC_ROOT/zig-0.16.0
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf zig-0.16.0.tar.xz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
cmake -B build -G Ninja \
-D CMAKE_INSTALL_PREFIX=/usr \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_PREFIX_PATH=/usr/lib/llvm21 \
-D CMAKE_C_FLAGS_RELEASE="$CFLAGS -DNDEBUG" \
-D CMAKE_CXX_FLAGS_RELEASE="$CXXFLAGS -DNDEBUG" \
-D ZIG_TARGET_TRIPLE=native \
-D ZIG_TARGET_MCPU=baseline \
-D ZIG_USE_LLVM_CONFIG=ON \
-D ZIG_STATIC_LLVM=OFF \
-D ZIG_STATIC_ZLIB=ON \
-D ZIG_STATIC_ZSTD=ON \
-D ZIG_PIE=ON \
-Wno-dev
CCACHE_DISABLE=1 cmake --build build
DESTDIR=$PKG cmake --install build
The switches, and why each is set:
CMAKE_PREFIX_PATH=/usr/lib/llvm21— Makes Zig’sllvm-configsearch look in the slot’s prefix beforePATH, so it finds/usr/lib/llvm21/bin/llvm-configand not the system LLVM’s.CMAKE_C_FLAGS_RELEASE— Replaces CMake’s Release flags,-O3 -DNDEBUG, with the tree’sCFLAGSand-DNDEBUG, so the C the build compiles, the bootstrap compiler translated fromzig1.wasmamong it, is built at the tree’s level with assertions off.CMAKE_CXX_FLAGS_RELEASE— The same for C++: Zig’s glue to LLVM’s C++ interfaces.ZIG_TARGET_TRIPLE=native— The target the installedzigbinary is built for: the machine it is built on. This is upstream’s default, named.ZIG_TARGET_MCPU=baseline— The CPU thezigbinary itself is compiled for. Upstream’s default isnative, the build machine’s own CPU, which would make the package stop with an illegal instruction on an older processor;baselineis the architecture’s base instruction set.ZIG_USE_LLVM_CONFIG=ON— Finds LLVM, clang and lld throughllvm-config, which is upstream’s default for a native build, named.ZIG_STATIC_LLVM=OFF— Links the slot’s shared LLVM libraries instead of static archives, which the slot does not install for LLVM and clang; this is upstream’s default, named. Thezigbinary records/usr/lib/llvm21/libas its run path, so it finds them without any search path being changed.ZIG_STATIC_ZLIB=ON— Links zlib’s static archive,libz.a, intozigin place of the-lzthatllvm-configreports, so the installed binary needs nolibz.so.ZIG_STATIC_ZSTD=ON— The same for zstd, withlibzstd.a.ZIG_PIE=ON— Buildszigas a position-independent executable, as every program in the tree is (see The release flags). Upstream’s default followsCMAKE_POSITION_INDEPENDENT_CODE, which this build does not set.CCACHE_DISABLE=1— Keeps the compiler cache out of this one build. ccache holds a file’s preprocessed form in memory to hash it, and the preprocessed form of the generatedzig2.cis far larger than the memory the build has, so ccache fails where gcc alone compiles the file; see The compiler cache.
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/zig, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf zig-0.16.0-3.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/zig, and runs the triggers its paths call for.
Contents
Installed programs: zig.
Installed directories: /usr/lib/zig, /usr/share/licenses/zig.
Notes
ZIG_STATIC_ZLIB and ZIG_STATIC_ZSTD take whatever find_library finds first among the static and shared names. They give a static link only while the zlib and zstd ports install libz.a and libzstd.a; without them the same build links the shared libraries and succeeds. readelf -d /usr/bin/zig shows which: its NEEDED entries list the slot’s LLVM and clang libraries, libstdc++ and libc, and no libz or libzstd.
A bump of this port to a release built against another LLVM series needs a slot of that series first: llvm21, clang21 and lld21 stay on their series (series = 21), and Zig’s CMake refuses any other.