Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·288 lines (245 loc) · 10.6 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·288 lines (245 loc) · 10.6 KB
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
#!/bin/bash
set -o errexit
set -o errtrace
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LLVM_VERSION=$1
LLVM_REPO_URL=${2:-https://github.com/llvm/llvm-project.git}
LLVM_CROSS="$3"
if [[ -z "$LLVM_REPO_URL" || -z "$LLVM_VERSION" ]]
then
echo "Usage: $0 <llvm-version> <llvm-repository-url> <target-id> [Release/Debug]"
exit 1
fi
# Detect OS
OS_TYPE="unknown"
case "${OSTYPE}" in
linux*) OS_TYPE="linux" ;;
darwin*) OS_TYPE="darwin" ;;
msys*|cygwin*) OS_TYPE="windows" ;;
esac
# Detect musl build
STATIC_BUILD=""
if [[ "$LLVM_CROSS" == *musl* ]]; then
STATIC_BUILD="ON"
fi
# Clone the LLVM project.
LLVM_REF="$LLVM_VERSION"
if [ ! -d llvm-project ]
then
if git clone -b "release/$LLVM_VERSION" --single-branch --depth=1 "$LLVM_REPO_URL" llvm-project; then
LLVM_REF="release/$LLVM_VERSION"
elif git clone -b "llvmorg-$LLVM_VERSION" --single-branch --depth=1 "$LLVM_REPO_URL" llvm-project; then
LLVM_REF="llvmorg-$LLVM_VERSION"
elif git clone -b "$LLVM_VERSION" --single-branch --depth=1 "$LLVM_REPO_URL" llvm-project; then
LLVM_REF="$LLVM_VERSION"
else
echo "Error: Could not find branch 'release/$LLVM_VERSION', tag 'llvmorg-$LLVM_VERSION', or branch/tag '$LLVM_VERSION'"
exit 1
fi
fi
cd llvm-project
git fetch origin
git checkout "$LLVM_REF"
git reset --hard "$LLVM_REF"
# Apply patches from patches directory
if [ -d "$SCRIPT_DIR/patches" ]; then
echo "Applying patches from $SCRIPT_DIR/patches..."
for patch in "$SCRIPT_DIR/patches"/*.patch; do
if [ -f "$patch" ]; then
echo "Applying $(basename "$patch")..."
git apply --ignore-space-change --whitespace=nowarn "$patch"
fi
done
fi
# Patch compiler-rt cpu_model ARM64 for MSVC (cl.exe) compatibility (2026-06-11 Manu)
if [[ "$LLVM_CROSS" == "windows-aarch64" ]]; then
echo "Applying MSVC ARM64 compatibility patches to compiler-rt..."
perl -pi -e 's|#include "aarch64.h"|#include "aarch64.h"\n#ifdef _MSC_VER\n#ifndef __clang__\n#define __attribute__(x)\n#endif\n#endif|' compiler-rt/lib/builtins/cpu_model/aarch64.c
perl -pi -e 's|#include <stdint.h>|#include <stdint.h>\n#ifdef _MSC_VER\n#ifndef __clang__\n#define __ATOMIC_RELAXED 0\n#define __atomic_load_n(ptr, mo) (*(volatile const unsigned long long *)(ptr))\n#define __atomic_store(ptr, val_ptr, mo) (*(volatile unsigned long long *)(ptr) = *(val_ptr))\n#endif\n#endif|' compiler-rt/lib/builtins/cpu_model/aarch64/fmv/windows.inc
perl -pi -e 's|#include <stdint.h>|#include <stdint.h>\n#ifdef _MSC_VER\n#ifndef __clang__\n#define __attribute__(x)\n#endif\n#endif|' compiler-rt/lib/builtins/aarch64/fp_mode.c
perl -pi -e 's|foreach\(pat cas swp ldadd ldclr ldeor ldset\)|if(CLANG_CL OR NOT MSVC)\nforeach(pat cas swp ldadd ldclr ldeor ldset)|' compiler-rt/lib/builtins/CMakeLists.txt
perl -pi -e 's|endforeach\(pat\)|endforeach(pat)\nendif()|' compiler-rt/lib/builtins/CMakeLists.txt
fi
# Apply pdb-patch (Windows-only, only for non-Release builds)
if [[ "$OS_TYPE" == "windows" && "$4" == "Debug" ]]; then
echo "Applying PDB patch for Windows Debug build..."
PDB_PATCH='
get_target_property(type ${name} TYPE)
if(${type} STREQUAL "STATIC_LIBRARY")
set(pdb_dir ${CMAKE_CURRENT_BINARY_DIR}/pdb)
set_target_properties(
${name}
PROPERTIES
COMPILE_PDB_NAME_DEBUG ${name}
COMPILE_PDB_OUTPUT_DIRECTORY_DEBUG ${pdb_dir}
)
install(
FILES ${pdb_dir}/${name}.pdb
CONFIGURATIONS Debug
DESTINATION lib${LLVM_LIBDIR_SUFFIX}
OPTIONAL
)
endif()
'
export PDB_PATCH
perl -i -0777 -pe 's/endmacro\s*\(add_llvm_library\)/$ENV{PDB_PATCH}\nendmacro(add_llvm_library)/g' llvm/cmake/modules/AddLLVM.cmake
fi
# Create directories
mkdir -p build
mkdir -p build_rt
# Adjust compilation based on build type
BUILD_TYPE_SPECIFIED=$4
if [[ -z "$BUILD_TYPE_SPECIFIED" ]]; then
BUILD_TYPE_SPECIFIED="Release"
fi
BUILD_TYPE=$BUILD_TYPE_SPECIFIED
# Adjust cross-compilation (Only RISC-V requires cross-compiling on current CI runners)
CROSS_COMPILE=""
TARGET_TRIPLE=""
if [[ "$LLVM_CROSS" == *riscv64* ]]; then
TARGET_TRIPLE="riscv64-linux-gnu"
CROSS_COMPILE="-DLLVM_HOST_TRIPLE=$TARGET_TRIPLE -DCMAKE_C_COMPILER=riscv64-linux-gnu-gcc-13 -DCMAKE_CXX_COMPILER=riscv64-linux-gnu-g++-13 -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=riscv64"
fi
# Set defaults
ENABLE_ASSERTIONS="OFF"
OPTIMIZED_TABLEGEN="ON"
PARALLEL_LINK_FLAGS=""
BUILD_PARALLEL_FLAGS=""
CMAKE_ARGUMENTS=""
if [[ "$BUILD_TYPE" == "Debug" ]]; then
BUILD_TYPE="Release"
ENABLE_ASSERTIONS="ON"
fi
if [[ "$OS_TYPE" == "windows" ]]; then
# Windows-specific flags matching build.ps1
CMAKE_ARGUMENTS="$CMAKE_ARGUMENTS -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded\$<\$<CONFIG:Debug>:Debug>"
CMAKE_ARGUMENTS="$CMAKE_ARGUMENTS -DLLVM_ENABLE_TERMINFO=OFF"
fi
df -h
ZLIB_DIR=""
if [[ "$OS_TYPE" == "windows" ]]; then
echo "Downloading and building static zlib for Windows LLVM build..."
# Fetch latest release source tarball URL
ZLIB_URL=$(curl -s https://api.github.com/repos/madler/zlib/releases/latest | jq -r '.tarball_url')
curl -L -o zlib-latest.tar.gz "$ZLIB_URL"
# Extract archive
tar -xzf zlib-latest.tar.gz
# Dynamically find the extracted source directory (matches madler-zlib-*)
ZLIB_SRC_DIR=$(find . -maxdepth 1 -type d -name "madler-zlib-*" | head -n 1)
if [[ -z "$ZLIB_SRC_DIR" ]]; then
# Fallback to any extracted directory starting with zlib or madler
ZLIB_SRC_DIR=$(find . -maxdepth 1 -type d ! -name "." ! -name "zlib-build" ! -name "llvm-project" ! -name "build" ! -name "build_rt" | head -n 1)
fi
mkdir -p zlib-build
cd zlib-build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DZLIB_BUILD_SHARED=OFF -DZLIB_BUILD_MINIZIP=OFF -DZLIB_BUILD_TESTING=OFF -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded\$<\$<CONFIG:Debug>:Debug>" "../$ZLIB_SRC_DIR"
cmake --build . --config Release
ZLIB_DIR=$(pwd)
cd ..
# Dynamically find the built static library (.lib)
ZLIB_LIB_FILE=$(find zlib-build -maxdepth 1 -name "*.lib" | head -n 1)
# Convert paths to mixed Windows format (C:/...) to prevent backslash escape character syntax errors in CMake config tests
WIN_ZLIB_LIB=$(cygpath -m "$(pwd)/$ZLIB_LIB_FILE")
WIN_ZLIB_INC=$(cygpath -m "$(pwd)/$ZLIB_SRC_DIR")
CMAKE_ARGUMENTS="$CMAKE_ARGUMENTS -DZLIB_LIBRARY=${WIN_ZLIB_LIB} -DZLIB_INCLUDE_DIR=${WIN_ZLIB_INC}"
fi
# -- PHASE 1: Build LLVM + LLD --
cd build
cmake \
-G Ninja \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DCMAKE_DISABLE_FIND_PACKAGE_LibXml2=TRUE \
-DCMAKE_INSTALL_PREFIX="/" \
-DLLVM_ENABLE_PROJECTS="lld" \
-DLLVM_ENABLE_RUNTIMES="" \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_ZSTD=$([[ "$OS_TYPE" == "windows" || "$OS_TYPE" == "darwin" ]] && echo "OFF" || echo "FORCE_ON") \
$(if [[ "$STATIC_BUILD" == "ON" ]]; then echo "-DZLIB_LIBRARY=/usr/lib/libz.a -DZLIB_INCLUDE_DIR=/usr/include -Dzstd_LIBRARY=/usr/lib/libzstd.a -Dzstd_INCLUDE_DIR=/usr/include"; fi) \
-DLLVM_TARGETS_TO_BUILD="X86;AArch64;RISCV;WebAssembly;LoongArch;ARM;AVR;" \
-DLLVM_INCLUDE_DOCS=OFF \
-DLLVM_BUILD_TESTS=OFF \
-DLLVM_ENABLE_ASSERTIONS="${ENABLE_ASSERTIONS}" \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_ENABLE_LIBXML2=0 \
-DLLVM_ENABLE_DOXYGEN=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_INCLUDE_TOOLS=ON \
-DLLVM_INCLUDE_RUNTIMES=OFF \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_ENABLE_CURL=OFF \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_OPTIMIZED_TABLEGEN="${OPTIMIZED_TABLEGEN}" \
$(if [[ "$STATIC_BUILD" == "ON" ]]; then echo "-DBUILD_SHARED_LIBS=OFF -DLLVM_BUILD_LLVM_DYLIB=OFF -DLLVM_LINK_LLVM_DYLIB=OFF -DCMAKE_EXE_LINKER_FLAGS=-static"; else echo "-DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_LINK_LLVM_DYLIB=ON"; fi) \
${CROSS_COMPILE} \
${PARALLEL_LINK_FLAGS} \
${CMAKE_ARGUMENTS} \
../llvm
cmake --build . --config "${BUILD_TYPE}" ${BUILD_PARALLEL_FLAGS}
find . -name "*.o" -type f -delete || true
find . -name "*.dwo" -type f -delete || true
# Install to the local destdir folder for packaging
cmake --install . --config "${BUILD_TYPE}" --prefix "destdir"
find . -maxdepth 1 ! -name 'destdir' ! -name 'bin' ! -name 'lib' ! -name '.' -exec rm -rf {} + || true
# -- PHASE 2: Build compiler-rt (Builtins & Sanitizers) --
# --- AUTO-DISCOVERY ---
# Locate llvm-config inside the destdir (looking specifically in bin directories to avoid headers)
LLVM_CONFIG_PATH=$(find "$(pwd)/destdir" -path "*/bin/llvm-config*" -type f | head -n 1)
if [[ -z "$LLVM_CONFIG_PATH" ]]; then
echo "Error: Could not find llvm-config"
exit 1
fi
LLVM_CMAKE_DIR_PATH=$(find "$(pwd)/destdir" -name LLVMConfig.cmake -type f -exec dirname {} + | head -n 1)
if [[ -z "$LLVM_CMAKE_DIR_PATH" ]]; then
echo "Error: Could not find LLVMConfig.cmake"
exit 1
fi
echo "Found LLVM CMake dir at: $LLVM_CMAKE_DIR_PATH"
# We need the host triple for standalone compiler-rt build.
# Skip running llvm-config if cross-compiling to avoid Exec format errors.
LLVM_LIB_DIR=$(find "$(pwd)/destdir" -name "lib" -type d | head -n 1)
# Tell the OS where to find libLLVM.so so llvm-config can run
if [[ "$OS_TYPE" == "linux" ]]; then
export LD_LIBRARY_PATH="$LLVM_LIB_DIR:$LD_LIBRARY_PATH"
elif [[ "$OS_TYPE" == "darwin" ]]; then
export DYLD_LIBRARY_PATH="$LLVM_LIB_DIR:$DYLD_LIBRARY_PATH"
elif [[ "$OS_TYPE" == "windows" ]]; then
export PATH="$LLVM_LIB_DIR:$PATH"
fi
HOST_TRIPLE=${TARGET_TRIPLE:-$("$LLVM_CONFIG_PATH" --host-target)}
echo "Host Triple: $HOST_TRIPLE"
cd ../build_rt
cmake \
-G Ninja \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DCMAKE_INSTALL_PREFIX="/" \
-DLLVM_CMAKE_DIR="$LLVM_CMAKE_DIR_PATH" \
-DCMAKE_C_COMPILER_TARGET="${HOST_TRIPLE}" \
-DCOMPILER_RT_BUILD_BUILTINS=ON \
$(if [[ "$STATIC_BUILD" == "ON" ]]; then echo "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"; else echo "-DCOMPILER_RT_BUILD_SANITIZERS=ON"; fi) \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_BUILD_PROFILE=OFF \
-DCOMPILER_RT_BUILD_MEMPROF=OFF \
-DCOMPILER_RT_BUILD_ORC=OFF \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
${CROSS_COMPILE} \
${CMAKE_ARGUMENTS} \
../compiler-rt
echo "Building compiler-rt..."
df -h
cmake --build . --config "${BUILD_TYPE}" ${BUILD_PARALLEL_FLAGS}
echo "Installing compiler-rt..."
df -h
# Install to the same destdir as LLVM
cmake --install . --config "${BUILD_TYPE}" --prefix "../build/destdir"
# -- PHASE 3: Post-processing (Stripping) --
if [[ "$BUILD_TYPE_SPECIFIED" == "Release" && "$OS_TYPE" != "windows" ]]; then
echo "Stripping binaries and libraries to save space..."
# Strip executables (using find to avoid issues with long argument lists)
find ../build/destdir -type f -executable -exec strip --strip-all {} + || true
# Strip static libraries
find ../build/destdir -name "*.a" -exec strip --strip-unneeded {} + || true
fi
echo "Final Disk Usage:"
df -h