-
Notifications
You must be signed in to change notification settings - Fork 60
/
build-coredistools.sh
executable file
·219 lines (190 loc) · 6.87 KB
/
build-coredistools.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
#!/usr/bin/env bash
TargetOSArchitecture=$1
CrossRootfsDirectory=$2
# Set this to 1 to build using CBL-Mariner
CrossBuildUsingMariner=1
EnsureCrossRootfsDirectoryExists () {
if [ ! -d "$CrossRootfsDirectory" ]; then
echo "Invalid or unspecified CrossRootfsDirectory: $CrossRootfsDirectory"
exit 1
fi
}
CMakeOSXArchitectures=
LLVMTargetsToBuild="AArch64;ARM;X86"
# Figure out which `strip` to use. Prefer `llvm-strip` if it is available.
# `llvm-strip` is available in CBL-Mariner container,
# `llvm-strip-<version>` is available on standard cross build Ubuntu container,
# `strip` is available on macOS.
StripTool=$(command -v llvm-strip{,-{20..15}} strip | head -n 1)
if [ -z "$StripTool" ]; then
echo "Strip tool not found"
exit 1
fi
TblGenTool=$(command -v llvm-tblgen)
if [ -z "$TblGenTool" ]; then
echo "llvm-tblgen tool not found"
exit 1
fi
# Take first match from: clang clang-20 clang-19 .. clang-15
C_COMPILER=$(command -v clang{,-{20..15}} | head -n 1)
if [ -z "$C_COMPILER" ]; then
echo "C compiler not found"
# Keep going in case cmake can find one?
fi
CXX_COMPILER=$(command -v clang++{,-{20..15}} | head -n 1)
if [ -z "$CXX_COMPILER" ]; then
echo "C++ compiler not found"
# Keep going in case cmake can find one?
fi
echo "Using C compiler: $C_COMPILER"
echo "Using C++ compiler: $CXX_COMPILER"
case "$TargetOSArchitecture" in
linux-arm)
CMakeCrossCompiling=ON
LLVMDefaultTargetTriple=thumbv7-linux-gnueabihf
LLVMHostTriple=arm-linux-gnueabihf
LLVMTargetsToBuild="ARM"
EnsureCrossRootfsDirectoryExists
;;
linux-arm64)
CMakeCrossCompiling=ON
LLVMHostTriple=aarch64-linux-gnu
EnsureCrossRootfsDirectoryExists
;;
linux-x64)
LLVMHostTriple=x86_64-linux-gnu
if [ $CrossBuildUsingMariner -eq 1 ]; then
CMakeCrossCompiling=ON
EnsureCrossRootfsDirectoryExists
else
CMakeCrossCompiling=OFF
fi
;;
linux-loongarch64)
CMakeCrossCompiling=OFF
LLVMHostTriple=loongarch64-linux-gnu
LLVMTargetsToBuild="LoongArch"
;;
linux-riscv64)
CMakeCrossCompiling=ON
LLVMHostTriple=riscv64-linux-gnu
LLVMTargetsToBuild="RISCV"
EnsureCrossRootfsDirectoryExists
;;
osx-arm64)
CMakeCrossCompiling=ON
CMakeOSXArchitectures=arm64
LLVMHostTriple=arm64-apple-macos
;;
osx-x64)
CMakeCrossCompiling=OFF
CMakeOSXArchitectures=x86_64
LLVMHostTriple=x86_64-apple-darwin
;;
*)
echo "Unknown target OS and architecture: $TargetOSArchitecture"
exit 1
esac
LLVMDefaultTargetTriple=${LLVMDefaultTargetTriple:-$LLVMHostTriple}
RootDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
SourcesDirectory=$RootDirectory/src
BinariesDirectory=$RootDirectory/obj/$TargetOSArchitecture
StagingDirectory=$RootDirectory/artifacts/$TargetOSArchitecture
command -v cmake >/dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "ERROR: cmake is not found in the PATH"
exit 1
fi
if [ ! -d $BinariesDirectory ]; then
mkdir -p $BinariesDirectory
fi
pushd "$BinariesDirectory"
if [ -z "$CrossRootfsDirectory" ]; then
BUILD_FLAGS="-target $LLVMHostTriple"
cmake \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \
-DCMAKE_C_COMPILER=${C_COMPILER} \
-DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_INSTALL_PREFIX=$StagingDirectory \
-DCMAKE_OSX_ARCHITECTURES=$CMakeOSXArchitectures \
-DCMAKE_STRIP=$StripTool \
-DLLVM_DEFAULT_TARGET_TRIPLE=$LLVMDefaultTargetTriple \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_EXTERNAL_PROJECTS=coredistools \
-DLLVM_EXTERNAL_COREDISTOOLS_SOURCE_DIR=$SourcesDirectory/coredistools \
-DLLVM_HOST_TRIPLE=$LLVMHostTriple \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_TABLEGEN=$TblGenTool \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON \
$SourcesDirectory/llvm-project/llvm
elif [ $CrossBuildUsingMariner -eq 1 ]; then
BUILD_FLAGS="--sysroot=$CrossRootfsDirectory -target $LLVMHostTriple"
# CBL-Mariner doesn't have `ld` so need to tell clang to use `lld` with "-fuse-ld=lld"
cmake \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \
-DCMAKE_C_COMPILER=${C_COMPILER} \
-DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \
-DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=lld" \
-DCMAKE_INCLUDE_PATH=$CrossRootfsDirectory/usr/include \
-DCMAKE_INSTALL_PREFIX=$StagingDirectory \
-DCMAKE_LIBRARY_PATH=$CrossRootfsDirectory/usr/lib/$LLVMHostTriple \
-DCMAKE_STRIP=$StripTool \
-DLLVM_DEFAULT_TARGET_TRIPLE=$LLVMDefaultTargetTriple \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_EXTERNAL_PROJECTS=coredistools \
-DLLVM_EXTERNAL_COREDISTOOLS_SOURCE_DIR=$SourcesDirectory/coredistools \
-DLLVM_HOST_TRIPLE=$LLVMHostTriple \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_TABLEGEN=$TblGenTool \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON \
$SourcesDirectory/llvm-project/llvm
else
BUILD_FLAGS="--sysroot=$CrossRootfsDirectory -target $LLVMHostTriple"
cmake \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \
-DCMAKE_C_COMPILER=${C_COMPILER} \
-DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_INCLUDE_PATH=$CrossRootfsDirectory/usr/include \
-DCMAKE_INSTALL_PREFIX=$StagingDirectory \
-DCMAKE_LIBRARY_PATH=$CrossRootfsDirectory/usr/lib/$LLVMHostTriple \
-DCMAKE_STRIP=$StripTool \
-DLLVM_DEFAULT_TARGET_TRIPLE=$LLVMDefaultTargetTriple \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_EXTERNAL_PROJECTS=coredistools \
-DLLVM_EXTERNAL_COREDISTOOLS_SOURCE_DIR=$SourcesDirectory/coredistools \
-DLLVM_HOST_TRIPLE=$LLVMHostTriple \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_TABLEGEN=$TblGenTool \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON \
$SourcesDirectory/llvm-project/llvm
fi
popd
if [ "$?" -ne 0 ]; then
echo "ERROR: cmake exited with code $1"
exit 1
fi
cmake \
--build $BinariesDirectory \
--parallel \
--target install-coredistools-stripped
if [ "$?" -ne 0 ]; then
echo "ERROR: cmake exited with code $1"
exit 1
fi
exit 0