1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: sin-ack <sin-ack@users.noreply.github.com>
Date: Sun, 11 Dec 2022 17:22:27 +0000
Subject: [PATCH] build: Adjust build process for SerenityOS
---
build | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 52 insertions(+), 1 deletion(-)
diff --git a/build b/build
index 5db75471ba94da22550136754c5ae71a56b4527c..2e757e44e852e41bd998ff70c2a9992b69d2910f 100755
--- a/build
+++ b/build
@@ -17,6 +17,7 @@ case $TARGET_OS_CMAKE in
freebsd) TARGET_OS_CMAKE="FreeBSD";;
windows) TARGET_OS_CMAKE="Windows";;
linux) TARGET_OS_CMAKE="Linux";;
+ serenity) TARGET_OS_CMAKE="Serenity";;
native) TARGET_OS_CMAKE="";;
esac
@@ -62,10 +63,58 @@ cmake "$ROOTDIR/zig" \
-DCMAKE_BUILD_TYPE=Release \
-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
+crt_dir=$SERENITY_INSTALL_ROOT/usr/lib
+msvc_lib_dir=
+kernel32_lib_dir=
+gcc_dir=
+EOF
+
+export ZIG_LIBC="$ROOTDIR/out/libc_installation.txt"
+
+# Create a CMakeToolchain.txt file to tell CMake about the SerenityOS platform.
+# Adapted from Toolchain/CMake/GNUToolchain.txt.in, with the tool settings
+# removed as zig-bootstrap overrides them manually with the cmake command.
+cat <<EOF > "$ROOTDIR/out/CMakeToolchain.txt"
+if (\${CMAKE_VERSION} VERSION_LESS "3.25.0")
+ list(APPEND CMAKE_MODULE_PATH "$SERENITY_SOURCE_DIR/Toolchain/CMake")
+endif()
+
+set(CMAKE_SYSTEM_NAME SerenityOS)
+set(CMAKE_SYSTEM_PROCESSOR "$SERENITY_ARCH")
+
+set(CMAKE_C_COMPILER_WORKS TRUE)
+set(CMAKE_CXX_COMPILER_WORKS TRUE)
+
+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
+EOF
+
# Now we have Zig as a cross compiler.
ZIG="$ROOTDIR/out/host/bin/zig"
+# Create a serenity/constants.zig file. This package will overwrite the
+# constants.zig file in the upstream Zig in order to provide values for POSIX
+# constants.
+$ZIG run "$ROOTDIR/out/scripts/generate-serenity-constants.zig" \
+ -D__serenity__ \
+ -I"$SERENITY_INSTALL_ROOT/usr/include" \
+ > "$ROOTDIR/zig/lib/std/c/serenity/constants.zig"
+# HACK: Also copy this file over to the host Zig compiler in the prefix. It has
+# to be done this way because we can't generate the constants until we
+# have a Zig compiler, and by the time the host Zig compiler is built
+# the standard library sources have been copied into the prefix already.
+cp "$ROOTDIR/zig/lib/std/c/serenity/constants.zig" \
+ "$ROOTDIR/out/host/lib/zig/std/c/serenity/constants.zig"
+
# First cross compile zlib for the target, as we need the LLVM linked into
# the final zig binary to have zlib support enabled.
mkdir -p "$ROOTDIR/out/build-zlib-$TARGET-$MCPU"
@@ -81,7 +130,8 @@ cmake "$ROOTDIR/zlib" \
-DCMAKE_ASM_COMPILER="$ZIG;cc;-fno-sanitize=all;-s;-target;$TARGET;-mcpu=$MCPU" \
-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"
+ -DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
+ -DCMAKE_TOOLCHAIN_FILE="$ROOTDIR/out/CMakeToolchain.txt"
cmake --build . --target install
# Same deal for zstd.
@@ -144,6 +194,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" \
+ -DCMAKE_TOOLCHAIN_FILE="$ROOTDIR/out/CMakeToolchain.txt" \
-DLLVM_ENABLE_BACKTRACES=OFF \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_ENABLE_CRASH_OVERRIDES=OFF \
|