forked from mstorsjo/llvm-mingw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-llvm.sh
executable file
·331 lines (301 loc) · 10.5 KB
/
build-llvm.sh
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/sh
#
# Copyright (c) 2018 Martin Storsjo
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
set -e
: ${LLVM_VERSION:=llvmorg-18.1.5}
ASSERTS=OFF
unset HOST
BUILDDIR="build"
LINK_DYLIB=ON
ASSERTSSUFFIX=""
LLDB=ON
CLANG_TOOLS_EXTRA=ON
while [ $# -gt 0 ]; do
case "$1" in
--disable-asserts)
ASSERTS=OFF
ASSERTSSUFFIX=""
;;
--enable-asserts)
ASSERTS=ON
ASSERTSSUFFIX="-asserts"
;;
--stage2)
STAGE2=1
BUILDDIR="$BUILDDIR-stage2"
;;
--thinlto)
LTO="thin"
BUILDDIR="$BUILDDIR-thinlto"
;;
--lto)
LTO="full"
BUILDDIR="$BUILDDIR-lto"
;;
--disable-dylib)
LINK_DYLIB=OFF
;;
--full-llvm)
FULL_LLVM=1
;;
--host=*)
HOST="${1#*=}"
;;
--with-python)
WITH_PYTHON=1
;;
--disable-lldb)
unset LLDB
;;
--disable-clang-tools-extra)
unset CLANG_TOOLS_EXTRA
;;
*)
PREFIX="$1"
;;
esac
shift
done
BUILDDIR="$BUILDDIR$ASSERTSSUFFIX"
if [ -z "$CHECKOUT_ONLY" ]; then
if [ -z "$PREFIX" ]; then
echo $0 [--enable-asserts] [--stage2] [--thinlto] [--lto] [--disable-dylib] [--full-llvm] [--with-python] [--disable-lldb] [--disable-clang-tools-extra] [--host=triple] dest
exit 1
fi
mkdir -p "$PREFIX"
PREFIX="$(cd "$PREFIX" && pwd)"
fi
if [ ! -d llvm-project ]; then
mkdir llvm-project
cd llvm-project
git init
git remote add origin https://github.com/llvm/llvm-project.git
cd ..
CHECKOUT=1
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT" ]; then
cd llvm-project
# Check if the intended commit or tag exists in the local repo. If it
# exists, just check it out instead of trying to fetch it.
# (Redoing a shallow fetch will refetch the data even if the commit
# already exists locally, unless fetching a tag with the "tag"
# argument.)
if git cat-file -e "$LLVM_VERSION" 2> /dev/null; then
# Exists; just check it out
git checkout "$LLVM_VERSION"
else
case "$LLVM_VERSION" in
llvmorg-*)
# If $LLVM_VERSION looks like a tag, fetch it with the
# "tag" keyword. This makes sure that the local repo
# gets the tag too, not only the commit itself. This allows
# later fetches to realize that the tag already exists locally.
git fetch --depth 1 origin tag "$LLVM_VERSION"
git checkout "$LLVM_VERSION"
;;
*)
git fetch --depth 1 origin "$LLVM_VERSION"
git checkout FETCH_HEAD
;;
esac
fi
cd ..
fi
[ -z "$CHECKOUT_ONLY" ] || exit 0
if [ -n "$HOST" ]; then
case $HOST in
*-mingw32)
TARGET_WINDOWS=1
;;
esac
else
case $(uname) in
MINGW*)
TARGET_WINDOWS=1
;;
esac
fi
if command -v ninja >/dev/null; then
CMAKE_GENERATOR="Ninja"
else
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
case $(uname) in
MINGW*)
CMAKE_GENERATOR="MSYS Makefiles"
;;
esac
fi
CMAKEFLAGS="$LLVM_CMAKEFLAGS"
if [ -n "$HOST" ]; then
ARCH="${HOST%%-*}"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_C_COMPILER=$HOST-gcc"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_CXX_COMPILER=$HOST-g++"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_SYSTEM_PROCESSOR=$ARCH"
case $HOST in
*-mingw32)
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_SYSTEM_NAME=Windows"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_RC_COMPILER=$HOST-windres"
;;
*-linux*)
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_SYSTEM_NAME=Linux"
;;
*)
echo "Unrecognized host $HOST"
exit 1
;;
esac
native=""
for dir in llvm-project/llvm/build/bin llvm-project/llvm/build-asserts/bin; do
if [ -x "$dir/llvm-tblgen.exe" ]; then
native="$(pwd)/$dir"
break
elif [ -x "$dir/llvm-tblgen" ]; then
native="$(pwd)/$dir"
break
fi
done
if [ -z "$native" ] && command -v llvm-tblgen >/dev/null; then
native="$(dirname $(command -v llvm-tblgen))"
fi
if [ -n "$native" ]; then
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_NATIVE_TOOL_DIR=$native"
fi
CROSS_ROOT=$(cd $(dirname $(command -v $HOST-gcc))/../$HOST && pwd)
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_FIND_ROOT_PATH=$CROSS_ROOT"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY"
BUILDDIR=$BUILDDIR-$HOST
if [ -n "$WITH_PYTHON" ] && [ -n "$TARGET_WINDOWS" ]; then
# The python3-config script requires executing with bash. It outputs
# an extra trailing space, which the extra 'echo' layer gets rid of.
EXT_SUFFIX="$(echo $(bash $PREFIX/python/bin/python3-config --extension-suffix))"
PYTHON_RELATIVE_PATH="$(cd "$PREFIX" && echo python/lib/python*/site-packages)"
PYTHON_INCLUDE_DIR="$(echo $PREFIX/python/include/python*)"
PYTHON_LIB="$(echo $PREFIX/python/lib/libpython3.*.dll.a)"
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_ENABLE_PYTHON=ON"
CMAKEFLAGS="$CMAKEFLAGS -DPYTHON_HOME=$PREFIX/python"
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_PYTHON_HOME=../python"
# Relative to the lldb install root
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_PYTHON_RELATIVE_PATH=$PYTHON_RELATIVE_PATH"
# Relative to LLDB_PYTHON_HOME
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_PYTHON_EXE_RELATIVE_PATH=bin/python3.exe"
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_PYTHON_EXT_SUFFIX=$EXT_SUFFIX"
CMAKEFLAGS="$CMAKEFLAGS -DPython3_INCLUDE_DIRS=$PYTHON_INCLUDE_DIR"
CMAKEFLAGS="$CMAKEFLAGS -DPython3_LIBRARIES=$PYTHON_LIB"
fi
elif [ -n "$STAGE2" ]; then
# Build using an earlier built and installed clang in the target directory
export PATH="$PREFIX/bin:$PATH"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_C_COMPILER=clang"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_CXX_COMPILER=clang++"
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_USE_LINKER=lld"
else
# Native compilation with the system default compiler.
# Use a faster linker, if available.
if command -v ld.lld >/dev/null; then
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_USE_LINKER=lld"
elif command -v ld.gold >/dev/null; then
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_USE_LINKER=gold"
fi
fi
if [ -n "$COMPILER_LAUNCHER" ]; then
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_C_COMPILER_LAUNCHER=$COMPILER_LAUNCHER"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_CXX_COMPILER_LAUNCHER=$COMPILER_LAUNCHER"
fi
if [ -n "$TARGET_WINDOWS" ]; then
# Custom, llvm-mingw specific defaults. We normally set these in
# the frontend wrappers, but this makes sure they are enabled by
# default if that wrapper is bypassed as well.
CMAKEFLAGS="$CMAKEFLAGS -DCLANG_DEFAULT_RTLIB=compiler-rt"
CMAKEFLAGS="$CMAKEFLAGS -DCLANG_DEFAULT_UNWINDLIB=libunwind"
CMAKEFLAGS="$CMAKEFLAGS -DCLANG_DEFAULT_CXX_STDLIB=libc++"
CMAKEFLAGS="$CMAKEFLAGS -DCLANG_DEFAULT_LINKER=lld"
CMAKEFLAGS="$CMAKEFLAGS -DLLD_DEFAULT_LD_LLD_IS_MINGW=ON"
fi
if [ -n "$LTO" ]; then
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_ENABLE_LTO=$LTO"
fi
if [ -n "$MACOS_REDIST" ]; then
: ${MACOS_REDIST_ARCHS:=arm64 x86_64}
: ${MACOS_REDIST_VERSION:=10.9}
ARCH_LIST=""
NATIVE=
for arch in $MACOS_REDIST_ARCHS; do
if [ -n "$ARCH_LIST" ]; then
ARCH_LIST="$ARCH_LIST;"
fi
ARCH_LIST="$ARCH_LIST$arch"
if [ "$(uname -m)" = "$arch" ]; then
NATIVE=1
fi
done
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_OSX_ARCHITECTURES=$ARCH_LIST"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_OSX_DEPLOYMENT_TARGET=$MACOS_REDIST_VERSION"
if [ -z "$NATIVE" ]; then
# If we're not building for the native arch, flag to CMake that we're
# cross compiling, to let it build native versions of tools used
# during the build.
ARCH="$(echo $MACOS_REDIST_ARCHS | awk '{print $1}')"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_SYSTEM_NAME=Darwin"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_SYSTEM_PROCESSOR=$ARCH"
fi
fi
if [ -z "$HOST" ] && [ "$(uname)" = "Darwin" ]; then
if [ -n "$LLDB" ]; then
# Building LLDB for macOS fails unless building libc++ is enabled at the
# same time, or unless the LLDB tests are disabled.
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_INCLUDE_TESTS=OFF"
# Don't build our own debugserver - use the system provided one.
# The newly built debugserver needs to be properly code signed to work.
# This silences a cmake warning.
CMAKEFLAGS="$CMAKEFLAGS -DLLDB_USE_SYSTEM_DEBUGSERVER=ON"
fi
fi
TOOLCHAIN_ONLY=ON
if [ -n "$FULL_LLVM" ]; then
TOOLCHAIN_ONLY=OFF
fi
cd llvm-project/llvm
PROJECTS="clang;lld"
if [ -n "$LLDB" ]; then
PROJECTS="$PROJECTS;lldb"
fi
if [ -n "$CLANG_TOOLS_EXTRA" ]; then
PROJECTS="$PROJECTS;clang-tools-extra"
fi
[ -z "$CLEAN" ] || rm -rf $BUILDDIR
mkdir -p $BUILDDIR
cd $BUILDDIR
[ -n "$NO_RECONF" ] || rm -rf CMake*
cmake \
${CMAKE_GENERATOR+-G} "$CMAKE_GENERATOR" \
-DCMAKE_INSTALL_PREFIX="$PREFIX" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=$ASSERTS \
-DLLVM_ENABLE_PROJECTS="$PROJECTS" \
-DLLVM_TARGETS_TO_BUILD="ARM;AArch64;X86;NVPTX" \
-DLLVM_INSTALL_TOOLCHAIN_ONLY=$TOOLCHAIN_ONLY \
-DLLVM_LINK_LLVM_DYLIB=$LINK_DYLIB \
-DLLVM_TOOLCHAIN_TOOLS="llvm-ar;llvm-ranlib;llvm-objdump;llvm-rc;llvm-cvtres;llvm-nm;llvm-strings;llvm-readobj;llvm-dlltool;llvm-pdbutil;llvm-objcopy;llvm-strip;llvm-cov;llvm-profdata;llvm-addr2line;llvm-symbolizer;llvm-windres;llvm-ml;llvm-readelf;llvm-size;llvm-cxxfilt" \
${HOST+-DLLVM_HOST_TRIPLE=$HOST} \
$CMAKEFLAGS \
..
cmake --build . ${CORES:+-j${CORES}}
cmake --install . --strip
cp ../LICENSE.TXT $PREFIX