-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
build
executable file
·207 lines (195 loc) · 7.58 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh
# This file is licensed under the public domain.
set -eu
TARGET="$1" # Example: riscv64-linux-gnu
MCPU="$2" # Examples: `baseline`, `native`, `generic+v7a`, or `arm1176jzf_s`
ROOTDIR="$(pwd)"
ZIG_VERSION="0.14.0-dev.2257+e6d2e1641"
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" \
-DCMAKE_INSTALL_PREFIX="$ROOTDIR/out/host" \
-DCMAKE_PREFIX_PATH="$ROOTDIR/out/host" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_ENABLE_LIBEDIT=OFF \
-DLLVM_ENABLE_LIBPFM=OFF \
-DLLVM_ENABLE_LIBXML2=OFF \
-DLLVM_ENABLE_OCAMLDOC=OFF \
-DLLVM_ENABLE_PLUGINS=OFF \
-DLLVM_ENABLE_PROJECTS="lld;clang" \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_ENABLE_Z3_SOLVER=OFF \
-DLLVM_ENABLE_ZSTD=OFF \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DLLVM_TOOL_LLVM_LTO2_BUILD=OFF \
-DLLVM_TOOL_LLVM_LTO_BUILD=OFF \
-DLLVM_TOOL_LTO_BUILD=OFF \
-DLLVM_TOOL_REMARKS_SHLIB_BUILD=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DCLANG_INCLUDE_TESTS=OFF \
-DCLANG_TOOL_CLANG_IMPORT_TEST_BUILD=OFF \
-DCLANG_TOOL_CLANG_LINKER_WRAPPER_BUILD=OFF \
-DCLANG_TOOL_C_INDEX_TEST_BUILD=OFF \
-DCLANG_TOOL_ARCMT_TEST_BUILD=OFF \
-DCLANG_TOOL_C_ARCMT_TEST_BUILD=OFF \
-DCLANG_TOOL_LIBCLANG_BUILD=OFF
cmake --build . --target 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"
cmake --build . --target 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_RC_COMPILER="$ROOTDIR/out/host/bin/llvm-rc" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib"
cmake --build . --target install
# Same deal for zstd.
# The build system for zstd is whack so I just put all the files here.
mkdir -p "$ROOTDIR/out/$TARGET-$MCPU/lib"
cp "$ROOTDIR/zstd/lib/zstd.h" "$ROOTDIR/out/$TARGET-$MCPU/include/zstd.h"
cd "$ROOTDIR/out/$TARGET-$MCPU/lib"
$ZIG build-lib \
--name zstd \
-target $TARGET \
-mcpu=$MCPU \
-fstrip -OReleaseFast \
-lc \
"$ROOTDIR/zstd/lib/decompress/zstd_ddict.c" \
"$ROOTDIR/zstd/lib/decompress/zstd_decompress.c" \
"$ROOTDIR/zstd/lib/decompress/huf_decompress.c" \
"$ROOTDIR/zstd/lib/decompress/huf_decompress_amd64.S" \
"$ROOTDIR/zstd/lib/decompress/zstd_decompress_block.c" \
"$ROOTDIR/zstd/lib/compress/zstdmt_compress.c" \
"$ROOTDIR/zstd/lib/compress/zstd_opt.c" \
"$ROOTDIR/zstd/lib/compress/hist.c" \
"$ROOTDIR/zstd/lib/compress/zstd_ldm.c" \
"$ROOTDIR/zstd/lib/compress/zstd_fast.c" \
"$ROOTDIR/zstd/lib/compress/zstd_compress_literals.c" \
"$ROOTDIR/zstd/lib/compress/zstd_double_fast.c" \
"$ROOTDIR/zstd/lib/compress/huf_compress.c" \
"$ROOTDIR/zstd/lib/compress/fse_compress.c" \
"$ROOTDIR/zstd/lib/compress/zstd_lazy.c" \
"$ROOTDIR/zstd/lib/compress/zstd_compress.c" \
"$ROOTDIR/zstd/lib/compress/zstd_compress_sequences.c" \
"$ROOTDIR/zstd/lib/compress/zstd_compress_superblock.c" \
"$ROOTDIR/zstd/lib/deprecated/zbuff_compress.c" \
"$ROOTDIR/zstd/lib/deprecated/zbuff_decompress.c" \
"$ROOTDIR/zstd/lib/deprecated/zbuff_common.c" \
"$ROOTDIR/zstd/lib/common/entropy_common.c" \
"$ROOTDIR/zstd/lib/common/pool.c" \
"$ROOTDIR/zstd/lib/common/threading.c" \
"$ROOTDIR/zstd/lib/common/zstd_common.c" \
"$ROOTDIR/zstd/lib/common/xxhash.c" \
"$ROOTDIR/zstd/lib/common/debug.c" \
"$ROOTDIR/zstd/lib/common/fse_decompress.c" \
"$ROOTDIR/zstd/lib/common/error_private.c" \
"$ROOTDIR/zstd/lib/dictBuilder/zdict.c" \
"$ROOTDIR/zstd/lib/dictBuilder/divsufsort.c" \
"$ROOTDIR/zstd/lib/dictBuilder/fastcover.c" \
"$ROOTDIR/zstd/lib/dictBuilder/cover.c"
# Rebuild LLVM with Zig.
mkdir -p "$ROOTDIR/out/build-llvm-$TARGET-$MCPU"
cd "$ROOTDIR/out/build-llvm-$TARGET-$MCPU"
cmake "$ROOTDIR/llvm" \
-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_RC_COMPILER="$ROOTDIR/out/host/bin/llvm-rc" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
-DLLVM_ENABLE_BACKTRACES=OFF \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_ENABLE_CRASH_OVERRIDES=OFF \
-DLLVM_ENABLE_LIBEDIT=OFF \
-DLLVM_ENABLE_LIBPFM=OFF \
-DLLVM_ENABLE_LIBXML2=OFF \
-DLLVM_ENABLE_OCAMLDOC=OFF \
-DLLVM_ENABLE_PLUGINS=OFF \
-DLLVM_ENABLE_PROJECTS="lld;clang" \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_ENABLE_Z3_SOLVER=OFF \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_ZSTD=FORCE_ON \
-DLLVM_USE_STATIC_ZSTD=ON \
-DLLVM_TABLEGEN="$ROOTDIR/out/host/bin/llvm-tblgen" \
-DLLVM_BUILD_UTILS=OFF \
-DLLVM_BUILD_TOOLS=OFF \
-DLLVM_BUILD_STATIC=ON \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DLLVM_DEFAULT_TARGET_TRIPLE="$TARGET" \
-DLLVM_TOOL_LLVM_LTO2_BUILD=OFF \
-DLLVM_TOOL_LLVM_LTO_BUILD=OFF \
-DLLVM_TOOL_LTO_BUILD=OFF \
-DLLVM_TOOL_REMARKS_SHLIB_BUILD=OFF \
-DCLANG_TABLEGEN="$ROOTDIR/out/build-llvm-host/bin/clang-tblgen" \
-DCLANG_BUILD_TOOLS=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DCLANG_INCLUDE_TESTS=OFF \
-DCLANG_ENABLE_ARCMT=ON \
-DCLANG_TOOL_CLANG_IMPORT_TEST_BUILD=OFF \
-DCLANG_TOOL_CLANG_LINKER_WRAPPER_BUILD=OFF \
-DCLANG_TOOL_C_INDEX_TEST_BUILD=OFF \
-DCLANG_TOOL_ARCMT_TEST_BUILD=OFF \
-DCLANG_TOOL_C_ARCMT_TEST_BUILD=OFF \
-DCLANG_TOOL_LIBCLANG_BUILD=OFF \
-DLIBCLANG_BUILD_STATIC=ON \
-DLLD_BUILD_TOOLS=OFF
cmake --build . --target install
# 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" \
-Dflat \
-Dstatic-llvm \
-Doptimize=ReleaseFast \
-Dstrip \
-Dtarget="$TARGET" \
-Dcpu="$MCPU" \
-Dversion-string="$ZIG_VERSION"