Ports: Add helper script for Zig libc.txt handling

This commit is contained in:
Linus Groh authored and Nico Weber committed 2026-10-08 07:55:52 -04:00
1 parent 3af354da00
commit f03bb61135
5 files changed
+58 -39

No files matched your search

+1
View File
@@ -0,0 +1 @@
!libc.sh
+34
View File
@@ -0,0 +1,34 @@
_gcc_lib_dir() {
local gcc_version
gcc_version="$(cd "${PORT_META_DIR}/../gcc" && ./package.sh showproperty version)"
echo "${SERENITY_INSTALL_ROOT}/usr/local/lib/gcc/${SERENITY_ARCH}-serenity/${gcc_version}"
}
check_gcc_crt_files() {
local gcc_lib_dir
gcc_lib_dir="$(_gcc_lib_dir)"
if [ ! -f "${gcc_lib_dir}/crtbeginS.o" ] || [ ! -f "${gcc_lib_dir}/crtendS.o" ]; then
echo "crtbeginS.o or crtendS.o could not be found, ensure the GCC port is installed." >&2
exit 1
fi
}
create_libc_file() {
local gcc_lib_dir
gcc_lib_dir="$(_gcc_lib_dir)"
mkdir -p "$(dirname "$1")"
cat > "$1" <<EOF
include_dir=${SERENITY_INSTALL_ROOT}/usr/include
sys_include_dir=${SERENITY_INSTALL_ROOT}/usr/include
cc_dir=${gcc_lib_dir}
crt_dir=${SERENITY_INSTALL_ROOT}/usr/lib
msvc_lib_dir=
kernel32_lib_dir=
# FIXME: Building on macOS validates this field when setting ZIG_LIBC,
# regardless of the actual target (upstream bug?)
darwin_sdk_dir=$(xcrun --show-sdk-path 2>/dev/null || true)
EOF
}
+9 -13
View File
@@ -1,23 +1,20 @@
#!/usr/bin/env -S bash ../.port_include.sh
source libc.sh
port='zig'
version='0.17.0'
files=(
"https://ziglang.org/download/${version}/zig-bootstrap-${version}.tar.xz#1e9e9b8e3c753b35dfb1d9ea48f097579fadbd3e5c1e724ab3fc5bce05477b71"
"https://ziglang.org/download/${version}/zig-${version}.tar.xz#b6c7f1728f043700d6529bac980800792f824256a9d2f1839b3d62beed0b8abd"
)
useconfigure='true'
# The actual directory to build in.
workdir="zig-bootstrap-${version}"
# The newer Zig directory we move into the workdir.
zigdir="zig-${version}"
# TODO: This should probably be exported by .hosted_defs.sh for convenience
ports_dir=$(realpath "$(dirname "${BASH_SOURCE[0]}")/..")
# The patched Zig build script uses this to set gcc_dir in the generated libc_installation.txt
export SERENITY_GCC_VERSION="$("${ports_dir}/gcc/package.sh" showproperty version)"
post_fetch() {
# NOTE: Running this multiple times is a massive footgun as patches only get applied once,
# the next time we'd end up with a clean copy of the original Zig sources.
@@ -35,14 +32,13 @@ post_fetch() {
run cp zig/lib/libc/darwin/libSystem.tbd "${DESTDIR}/usr/lib/"
}
configure() {
check_gcc_crt_files
# The patched Zig build script exports ZIG_LIBC
create_libc_file "${PORT_BUILD_DIR}/${workdir}/out/libc.txt"
}
build() {
local gcc_lib_dir="${DESTDIR}/usr/local/lib/gcc/${SERENITY_ARCH}-serenity/${SERENITY_GCC_VERSION}"
if [ ! -f "${gcc_lib_dir}/crtbeginS.o" ] || [ ! -f "${gcc_lib_dir}/crtendS.o" ]; then
echo "crtbeginS.o or crtendS.o could not be found, ensure the GCC port is installed."
exit 1
fi
host_env
run ./build "${SERENITY_ARCH}-serenity-none" "baseline"
}
@@ -4,11 +4,11 @@ Date: Sun, 11 Dec 2022 17:22:27 +0000
Subject: [PATCH] build: Adjust build process for SerenityOS
---
build | 38 +++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)
build | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/build b/build
index 4759ef7a5f464439b64732d8fd22f6ddf1bea46c..50889c5b00f80a1f74f5e26b3dcbdfda2da30810 100755
index 4759ef7a5f464439b64732d8fd22f6ddf1bea46c..0ee72e2dd6027757aaa81088f7829540e6c75b1f 100755
--- a/build
+++ b/build
@@ -20,6 +20,7 @@ case $TARGET_OS_CMAKE in
@@ -19,26 +19,14 @@ index 4759ef7a5f464439b64732d8fd22f6ddf1bea46c..50889c5b00f80a1f74f5e26b3dcbdfda
native) TARGET_OS_CMAKE="";;
esac
@@ -68,6 +69,39 @@ cmake "$ROOTDIR/zig" \
-DCMAKE_BUILD_TYPE=Release \
@@ -69,6 +70,27 @@ cmake "$ROOTDIR/zig" \
-DZIG_VERSION="$ZIG_VERSION"
cmake --build . --target install
+
+# Create a libc installation file so Zig knows where to look for headers and
+# libraries while compiling for the Serenity target.
+cat <<EOF > "$ROOTDIR/out/libc_installation.txt"
+include_dir=$SERENITY_INSTALL_ROOT/usr/include
+sys_include_dir=$SERENITY_INSTALL_ROOT/usr/include
+cc_dir=$SERENITY_INSTALL_ROOT/usr/local/lib/gcc/$SERENITY_ARCH-serenity/$SERENITY_GCC_VERSION
+crt_dir=$SERENITY_INSTALL_ROOT/usr/lib
+msvc_lib_dir=
+kernel32_lib_dir=
+# FIXME: Building on macOS validates this field when setting ZIG_LIBC,
+# regardless of the actual target (upstream bug?)
+darwin_sdk_dir=$(xcrun --show-sdk-path 2>/dev/null || true)
+EOF
+
+export ZIG_LIBC="$ROOTDIR/out/libc_installation.txt"
+# Point Zig at the libc file generated via the port's configure step
+# so it knows where to look for headers and libraries while compiling
+# for the Serenity target.
+export ZIG_LIBC="$ROOTDIR/out/libc.txt"
+
+# Create a CMakeToolchain.txt file to tell CMake about the SerenityOS platform.
+# Adapted from Toolchain/CMake/GNUToolchain.txt.in, with the tool settings
@@ -56,10 +44,10 @@ index 4759ef7a5f464439b64732d8fd22f6ddf1bea46c..50889c5b00f80a1f74f5e26b3dcbdfda
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
+EOF
+
# Now we have Zig as a cross compiler.
ZIG="$ROOTDIR/out/host/bin/zig"
@@ -88,7 +122,8 @@ cmake "$ROOTDIR/zlib" \
@@ -88,7 +110,8 @@ cmake "$ROOTDIR/zlib" \
-DCMAKE_LINK_DEPENDS_USE_LINKER=OFF \
-DCMAKE_RC_COMPILER="$ROOTDIR/out/host/bin/llvm-rc" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
@@ -69,7 +57,7 @@ index 4759ef7a5f464439b64732d8fd22f6ddf1bea46c..50889c5b00f80a1f74f5e26b3dcbdfda
cmake --build . --target install
# Same deal for zstd.
@@ -152,6 +187,7 @@ cmake "$ROOTDIR/llvm" \
@@ -152,6 +175,7 @@ cmake "$ROOTDIR/llvm" \
-DCMAKE_RC_COMPILER="$ROOTDIR/out/host/bin/llvm-rc" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
@@ -8,10 +8,10 @@ Subject: [PATCH] build: Remove unsupported zig linker flag -z separate-code
1 file changed, 1 insertion(+)
diff --git a/build b/build
index 50889c5b00f80a1f74f5e26b3dcbdfda2da30810..99e2fa9c2a85eeaa00abd93d18ca703543ca30d8 100755
index 0ee72e2dd6027757aaa81088f7829540e6c75b1f..90526247f57e2fabcdc25eb8c7058adc76754122 100755
--- a/build
+++ b/build
@@ -182,6 +182,7 @@ cmake "$ROOTDIR/llvm" \
@@ -170,6 +170,7 @@ cmake "$ROOTDIR/llvm" \
-DCMAKE_SYSTEM_NAME="$TARGET_OS_CMAKE" \
-DCMAKE_C_COMPILER="$ZIG;cc;-fno-sanitize=all;-s;-target;$TARGET;-mcpu=$MCPU" \
-DCMAKE_CXX_COMPILER="$ZIG;c++;-fno-sanitize=all;-s;-target;$TARGET;-mcpu=$MCPU" \