forked from ziglang/zig-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·136 lines (125 loc) · 4.73 KB
/
build
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh
set -eu
JOBS="$1"
TARGET="$2" # Example: riscv64-linux-gnu
MCPU="$3" # Examples: `baseline`, `native`, `generic+v7a`, or `arm1176jzf_s`
ROOTDIR="$(pwd)"
ZIG_VERSION="0.10.0-dev.4193+f3a1b5c48"
TARGET_OS_AND_ABI=${TARGET#*-} # Example: linux-gnu
# Here we map the OS from the target triple to the value that CMake expects.
TARGET_OS_CMAKE=${TARGET_OS_AND_ABI%-*} # Example: linux
case $TARGET_OS_CMAKE in
macos) TARGET_OS_CMAKE="Darwin";;
freebsd) TARGET_OS_CMAKE="FreeBSD";;
windows) TARGET_OS_CMAKE="Windows";;
linux) TARGET_OS_CMAKE="Linux";;
native) TARGET_OS_CMAKE="";;
esac
# First build the libraries for Zig to link against, as well as native `llvm-tblgen`.
mkdir -p "$ROOTDIR/out/build-llvm-host"
cd "$ROOTDIR/out/build-llvm-host"
cmake "$ROOTDIR/llvm" \
-DLLVM_ENABLE_PROJECTS="lld;clang" \
-DLLVM_ENABLE_LIBXML2=OFF \
-DCMAKE_INSTALL_PREFIX="$ROOTDIR/out/host" \
-DCMAKE_PREFIX_PATH="$ROOTDIR/out/host" \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_GO_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_ENABLE_OCAMLDOC=OFF \
-DLLVM_ENABLE_Z3_SOLVER=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DCMAKE_BUILD_TYPE=Release
make "$JOBS" install
# Now we build Zig, still with system C/C++ compiler, linking against LLVM,
# Clang, LLD we just built from source.
mkdir -p "$ROOTDIR/out/build-zig-host"
cd "$ROOTDIR/out/build-zig-host"
cmake "$ROOTDIR/zig" \
-DCMAKE_INSTALL_PREFIX="$ROOTDIR/out/host" \
-DCMAKE_PREFIX_PATH="$ROOTDIR/out/host" \
-DCMAKE_BUILD_TYPE=Release \
-DZIG_VERSION="$ZIG_VERSION" \
-DZIG_STATIC_ZLIB=ON
make "$JOBS" install
# Now we have Zig as a cross compiler.
ZIG="$ROOTDIR/out/host/bin/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"
cd "$ROOTDIR/out/build-zlib-$TARGET-$MCPU"
cmake "$ROOTDIR/zlib" \
-DCMAKE_INSTALL_PREFIX="$ROOTDIR/out/$TARGET-$MCPU" \
-DCMAKE_PREFIX_PATH="$ROOTDIR/out/$TARGET-$MCPU" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CROSSCOMPILING=True \
-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" \
-DCMAKE_ASM_COMPILER="$ZIG;cc;-fno-sanitize=all;-s;-target;$TARGET;-mcpu=$MCPU" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib"
make "$JOBS" install
# Rebuild LLVM with Zig.
mkdir -p "$ROOTDIR/out/build-llvm-$TARGET-$MCPU"
cd "$ROOTDIR/out/build-llvm-$TARGET-$MCPU"
cmake "$ROOTDIR/llvm" \
-DLLVM_ENABLE_PROJECTS="lld;clang" \
-DLLVM_ENABLE_LIBXML2=OFF \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_BACKTRACES=OFF \
-DLLVM_ENABLE_PLUGINS=OFF \
-DLLVM_ENABLE_ZSTD=OFF \
-DCMAKE_INSTALL_PREFIX="$ROOTDIR/out/$TARGET-$MCPU" \
-DCMAKE_PREFIX_PATH="$ROOTDIR/out/$TARGET-$MCPU" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CROSSCOMPILING=True \
-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" \
-DCMAKE_ASM_COMPILER="$ZIG;cc;-fno-sanitize=all;-s;-target;$TARGET;-mcpu=$MCPU" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
-DLLVM_TABLEGEN="$ROOTDIR/out/host/bin/llvm-tblgen" \
-DCLANG_TABLEGEN="$ROOTDIR/out/build-llvm-host/bin/clang-tblgen" \
-DLLVM_BUILD_TOOLS=OFF \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_GO_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_ENABLE_OCAMLDOC=OFF \
-DLLVM_ENABLE_Z3_SOLVER=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DCLANG_ENABLE_ARCMT=ON \
-DLLVM_BUILD_STATIC=ON \
-DLIBCLANG_BUILD_STATIC=ON \
-DLLVM_DEFAULT_TARGET_TRIPLE="$TARGET"
cd "$ROOTDIR/out/build-llvm-$TARGET-$MCPU/tools/lld"
make "$JOBS" install
cd "$ROOTDIR/out/build-llvm-$TARGET-$MCPU/tools/clang/lib"
make "$JOBS" install
cd "$ROOTDIR/out/build-llvm-$TARGET-$MCPU/lib"
make "$JOBS" install
cd "$ROOTDIR/out/build-llvm-$TARGET-$MCPU"
make "$JOBS" install-llvm-headers install-clang-headers install-LLVMSupport install-LLVMDemangle
# Finally, we can cross compile Zig itself, with Zig.
cd "$ROOTDIR/zig"
$ZIG build \
--prefix "$ROOTDIR/out/zig-$TARGET-$MCPU" \
--search-prefix "$ROOTDIR/out/$TARGET-$MCPU" \
-Dstatic-llvm \
-Drelease \
-Dstrip \
-Dtarget="$TARGET" \
-Dcpu="$MCPU" \
-Dversion-string="$ZIG_VERSION" \
-Denable-stage1