-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbuild-llvm.sh
executable file
·98 lines (83 loc) · 2.99 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
#!/bin/bash
SRC=$(dirname $0)
BUILD="$1"
LLVM_SRC="$2"
if [ "$LLVM_SRC" == "" ]; then
LLVM_SRC=$(pwd)/upstream/llvm-project
fi
if [ "$BUILD" == "" ]; then
BUILD=$(pwd)/build
fi
SRC=$(realpath "$SRC")
BUILD=$(realpath "$BUILD")
LLVM_BUILD=$BUILD/llvm
LLVM_NATIVE=$BUILD/llvm-native
# If we don't have a copy of LLVM, make one
if [ ! -d $LLVM_SRC/ ]; then
git clone --depth 1 https://github.com/llvm/llvm-project.git "$LLVM_SRC/"
pushd $LLVM_SRC/
# This is the last tested commit of llvm-project.
# Feel free to try with a newer version
COMMIT=d5a963ab8b40fcf7a99acd834e5f10a1a30cc2e5
git fetch origin $COMMIT
git reset --hard $COMMIT
# The clang driver will sometimes spawn a new process to avoid memory leaks.
# Since this complicates matters quite a lot for us, just disable that.
git apply $SRC/patches/llvm-project.patch
popd
fi
# Cross compiling llvm needs a native build of "llvm-tblgen" and "clang-tblgen"
if [ ! -d $LLVM_NATIVE/ ]; then
cmake -G Ninja \
-S $LLVM_SRC/llvm/ \
-B $LLVM_NATIVE/ \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD=WebAssembly \
-DLLVM_ENABLE_PROJECTS="clang"
fi
cmake --build $LLVM_NATIVE/ -- llvm-tblgen clang-tblgen
if [ ! -d $LLVM_BUILD/ ]; then
CXXFLAGS="-Dwait4=__syscall_wait4" \
LDFLAGS="\
-s LLD_REPORT_UNDEFINED=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s EXPORTED_FUNCTIONS=_main,_free,_malloc \
-s EXPORTED_RUNTIME_METHODS=FS,PROXYFS,ERRNO_CODES,allocateUTF8 \
-lproxyfs.js \
--js-library=$SRC/emlib/fsroot.js \
" emcmake cmake -G Ninja \
-S $LLVM_SRC/llvm/ \
-B $LLVM_BUILD/ \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD=WebAssembly \
-DLLVM_ENABLE_PROJECTS="clang;lld;clang-tools-extra" \
-DLLVM_ENABLE_DUMP=OFF \
-DLLVM_ENABLE_ASSERTIONS=OFF \
-DLLVM_ENABLE_EXPENSIVE_CHECKS=OFF \
-DLLVM_ENABLE_BACKTRACES=OFF \
-DLLVM_BUILD_TOOLS=OFF \
-DLLVM_ENABLE_THREADS=OFF \
-DLLVM_BUILD_LLVM_DYLIB=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_TABLEGEN=$LLVM_NATIVE/bin/llvm-tblgen \
-DCLANG_TABLEGEN=$LLVM_NATIVE/bin/clang-tblgen
# Make sure we build js modules (.mjs).
# The patch-ninja.sh script assumes that.
sed -i -E 's/\.js/.mjs/g' $LLVM_BUILD/build.ninja
# The mjs patching is over zealous, and patches some source JS files rather than just output files.
# Undo that.
sed -i -E 's/(pre|post|proxyfs|fsroot)\.mjs/\1.js/g' $LLVM_BUILD/build.ninja
# Patch the build script to add the "llvm-box" target.
# This new target bundles many executables in one, reducing the total size.
pushd $SRC
TMP_FILE=$(mktemp)
./patch-ninja.sh \
$LLVM_BUILD/build.ninja \
llvm-box \
$BUILD/tooling \
clang lld llvm-nm llvm-ar llvm-objcopy llc \
> $TMP_FILE
cat $TMP_FILE >> $LLVM_BUILD/build.ninja
popd
fi
cmake --build $LLVM_BUILD/ -- llvm-box