-
Notifications
You must be signed in to change notification settings - Fork 60
/
build-tblgen.sh
executable file
·150 lines (128 loc) · 4.57 KB
/
build-tblgen.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
#!/usr/bin/env bash
#
# Build the Linux/Mac llvm-tblgen tool. Note that this will be run during the
# Linux/Mac coredistools build. So, we only build the versions that we'll use
# during those builds. Thus, we need a linux-x64 version (used for
# linux-x64, linux-arm, linux-arm64 builds) and osx-x64 version (used for
# osx-x64 and osx-arm64 builds).
#
# The linux-x64 build is itself a cross-build when using CBL-Mariner container to build.
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"
case "$TargetOSArchitecture" in
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"
;;
osx-x64)
CMakeCrossCompiling=OFF
CMakeOSXArchitectures=x86_64
LLVMHostTriple=x86_64-apple-darwin
;;
*)
echo "Unknown target OS and architecture: $TargetOSArchitecture"
exit 1
esac
RootDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
SourcesDirectory=$RootDirectory/src
BinariesDirectory=$RootDirectory/obj
StagingDirectory=$RootDirectory/bin
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"
# Take first match from: clang clang-20 clang-19 .. clang-15
C_COMPILER=$(command -v clang{,-{20..15}} | head -n 1)
CXX_COMPILER=$(command -v clang++{,-{20..15}} | head -n 1)
if [ -z "$CrossRootfsDirectory" ]; then
BUILD_FLAGS=""
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=$RootDirectory \
-DCMAKE_OSX_ARCHITECTURES=$CMakeOSXArchitectures \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
$SourcesDirectory/llvm-project/llvm
elif [ $CrossBuildUsingMariner -eq 1 ]; then
BUILD_FLAGS="--sysroot=$CrossRootfsDirectory"
# 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_INSTALL_PREFIX=$RootDirectory \
-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_LIBRARY_PATH=$CrossRootfsDirectory/usr/lib/$LLVMHostTriple \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
$SourcesDirectory/llvm-project/llvm
else
BUILD_FLAGS="--sysroot=$CrossRootfsDirectory"
cmake \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$RootDirectory \
-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_LIBRARY_PATH=$CrossRootfsDirectory/usr/lib/$LLVMHostTriple \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
$SourcesDirectory/llvm-project/llvm
fi
popd
if [ "$?" -ne 0 ]; then
echo "ERROR: cmake exited with code $1"
exit 1
fi
cmake \
--build $BinariesDirectory \
--target llvm-tblgen \
--config Release
if [ "$?" -ne 0 ]; then
echo "ERROR: cmake exited with code $1"
exit 1
fi
if [ ! -d $StagingDirectory ]; then
mkdir -p $StagingDirectory
fi
# Copy llvm-tblgen from BinariesDirectory to StagingDirectory
find $BinariesDirectory -name llvm-tblgen -type f -exec cp -v {} $StagingDirectory \;
exit 0