-
Notifications
You must be signed in to change notification settings - Fork 538
/
common.jl
384 lines (320 loc) · 13.6 KB
/
common.jl
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# LLVMBuilder -- reliable LLVM builds all the time.
using BinaryBuilder, Pkg, LibGit2
include("../../fancy_toys.jl")
# Everybody is just going to use the same set of platforms
platforms = expand_cxxstring_abis(supported_platforms())
const llvm_tags = Dict(
v"6.0.1" => "d359f2096850c68b708bc25a7baca4282945949f",
v"8.0.1" => "19a71f6bdf2dddb10764939e7f0ec2b98dba76c9",
v"9.0.1" => "c1a0a213378a458fbea1a5c77b315c7dce08fd05",
v"10.0.1" => "ef32c611aa214dea855364efd7ba451ec5ec3f74",
v"11.0.0" => "176249bd6732a8044d457092ed932768724a6f06",
)
const buildscript = raw"""
# We want to exit the program if errors occur.
set -o errexit
if [[ ${target} == *mingw32* ]]; then
export CCACHE_DISABLE=true
fi
cd ${WORKSPACE}/srcdir/llvm-project/llvm
LLVM_SRCDIR=$(pwd)
# Apply all our patches
if [ -d $WORKSPACE/srcdir/llvm_patches ]; then
for f in $WORKSPACE/srcdir/llvm_patches/*.patch; do
echo "Applying patch ${f}"
atomic_patch -p1 ${f}
done
fi
if [ -d $WORKSPACE/srcdir/clang_patches ]; then
cd ${WORKSPACE}/srcdir/llvm-project/clang
for f in $WORKSPACE/srcdir/clang_patches/*.patch; do
echo "Applying patch ${f}"
atomic_patch -p1 ${f}
done
fi
if [ -d $WORKSPACE/srcdir/crt_patches ]; then
cd ${WORKSPACE}/srcdir/llvm-project/compiler-rt
for f in $WORKSPACE/srcdir/crt_patches/*.patch; do
echo "Applying patch ${f}"
atomic_patch -p1 ${f}
done
fi
if [ -d $WORKSPACE/srcdir/libcxx_patches ]; then
cd ${WORKSPACE}/srcdir/llvm-project/libcxx
for f in $WORKSPACE/srcdir/libcxx_patches/*.patch; do
echo "Applying patch ${f}"
atomic_patch -p1 ${f}
done
fi
# Patches from the monorepo
if [ -d $WORKSPACE/srcdir/patches ]; then
cd ${WORKSPACE}/srcdir/llvm-project
for f in $WORKSPACE/srcdir/patches/*.patch; do
echo "Applying patch ${f}"
atomic_patch -p1 ${f}
done
fi
# The very first thing we need to do is to build llvm-tblgen for x86_64-linux-muslc
# This is because LLVM's cross-compile setup is kind of borked, so we just
# build the tools natively ourselves, directly. :/
# Build llvm-tblgen, clang-tblgen, and llvm-config
mkdir ${WORKSPACE}/bootstrap
pushd ${WORKSPACE}/bootstrap
CMAKE_FLAGS=()
CMAKE_FLAGS+=(-DLLVM_TARGETS_TO_BUILD:STRING=host)
CMAKE_FLAGS+=(-DLLVM_HOST_TRIPLE=${MACHTYPE})
CMAKE_FLAGS+=(-DCMAKE_BUILD_TYPE=Release)
CMAKE_FLAGS+=(-DLLVM_ENABLE_PROJECTS='clang;compiler-rt')
CMAKE_FLAGS+=(-DCMAKE_CROSSCOMPILING=False)
CMAKE_FLAGS+=(-DCMAKE_TOOLCHAIN_FILE=${CMAKE_HOST_TOOLCHAIN})
cmake -GNinja ${LLVM_SRCDIR} ${CMAKE_FLAGS[@]}
ninja -j${nproc} llvm-tblgen clang-tblgen llvm-config
popd
# Let's do the actual build within the `build` subdirectory
mkdir ${WORKSPACE}/build && cd ${WORKSPACE}/build
# Accumulate these flags outside CMAKE_FLAGS,
# they will be added at the end.
CMAKE_CPP_FLAGS=""
CMAKE_CXX_FLAGS=""
CMAKE_C_FLAGS=""
CMAKE_FLAGS=()
# Release build for best perofrmance
CMAKE_FLAGS+=(-DCMAKE_BUILD_TYPE=Release)
if [[ "${ASSERTS}" == "1" ]]; then
CMAKE_FLAGS+=(-DLLVM_ENABLE_ASSERTIONS:BOOL=ON)
fi
# build for our host arch and our GPU targets NVidia and AMD
TARGETS=(host NVPTX AMDGPU)
# Add WASM and BPF for LLVM >6
if [[ "${LLVM_MAJ_VER}" != "6" ]]; then
TARGETS+=(WebAssembly BPF)
fi
LLVM_TARGETS=$(IFS=';' ; echo "${TARGETS[*]}")
CMAKE_FLAGS+=(-DLLVM_TARGETS_TO_BUILD:STRING=$LLVM_TARGETS)
# We mostly care about clang and LLVM
CMAKE_FLAGS+=(-DLLVM_ENABLE_PROJECTS='clang;compiler-rt;lld')
CMAKE_FLAGS+=(-DLLVM_TOOL_CLANG_TOOLS_EXTRA_BUILD=OFF)
# We want a build with no bindings
CMAKE_FLAGS+=(-DLLVM_BINDINGS_LIST="" )
# Turn off ZLIB and XML2
CMAKE_FLAGS+=(-DLLVM_ENABLE_ZLIB=OFF)
CMAKE_FLAGS+=(-DLLVM_ENABLE_LIBXML2=OFF)
# Disable useless things like docs, terminfo, etc....
CMAKE_FLAGS+=(-DLLVM_INCLUDE_DOCS=Off)
CMAKE_FLAGS+=(-DLLVM_ENABLE_TERMINFO=Off)
CMAKE_FLAGS+=(-DHAVE_HISTEDIT_H=Off)
CMAKE_FLAGS+=(-DHAVE_LIBEDIT=Off)
# We want a shared library
CMAKE_FLAGS+=(-DLLVM_BUILD_LLVM_DYLIB:BOOL=ON)
CMAKE_FLAGS+=(-DLLVM_LINK_LLVM_DYLIB:BOOL=ON)
# set a SONAME suffix for FreeBSD https://github.com/JuliaLang/julia/issues/32462
CMAKE_FLAGS+=(-DLLVM_VERSION_SUFFIX:STRING="jl")
# Install things into $prefix, and make sure it knows we're cross-compiling
CMAKE_FLAGS+=(-DCMAKE_INSTALL_PREFIX=${prefix})
CMAKE_FLAGS+=(-DCMAKE_CROSSCOMPILING=True)
# Julia expects the produced LLVM tools to be installed into tools and not bin
# We can't simply move bin to tools since on MingW64 it will also contain the shlib.
CMAKE_FLAGS+=(-DLLVM_TOOLS_INSTALL_DIR="tools")
# Also build and install utils, since we want FileCheck, and lit
CMAKE_FLAGS+=(-DLLVM_UTILS_INSTALL_DIR="tools")
CMAKE_FLAGS+=(-DLLVM_INCLUDE_UTILS=True -DLLVM_INSTALL_UTILS=True)
# Include perf/oprofile/vtune markers
if [[ ${target} == *linux* ]]; then
CMAKE_FLAGS+=(-DLLVM_USE_PERF=1)
# CMAKE_FLAGS+=(-DLLVM_USE_OPROFILE=1)
fi
if [[ ${target} == *linux* ]] || [[ ${target} == *mingw32* ]]; then
CMAKE_FLAGS+=(-DLLVM_USE_INTEL_JITEVENTS=1)
fi
# Tell LLVM where our pre-built tblgen tools are
CMAKE_FLAGS+=(-DLLVM_TABLEGEN=${WORKSPACE}/bootstrap/bin/llvm-tblgen)
CMAKE_FLAGS+=(-DCLANG_TABLEGEN=${WORKSPACE}/bootstrap/bin/clang-tblgen)
CMAKE_FLAGS+=(-DLLVM_CONFIG_PATH=${WORKSPACE}/bootstrap/bin/llvm-config)
# Explicitly use our cmake toolchain file
CMAKE_FLAGS+=(-DCMAKE_TOOLCHAIN_FILE=/opt/${target}/${target}.cmake)
# Manually set the host triplet, as otherwise on some platforms it tries to guess using
# `ld -v`, which is hilariously wrong.
CMAKE_FLAGS+=(-DLLVM_HOST_TRIPLE=${target})
# Tell LLVM which compiler target to use, because it loses track for some reason
CMAKE_FLAGS+=(-DCMAKE_C_COMPILER_TARGET=${target})
CMAKE_FLAGS+=(-DCMAKE_CXX_COMPILER_TARGET=${target})
CMAKE_FLAGS+=(-DCMAKE_ASM_COMPILER_TARGET=${target})
if [[ "${target}" == *apple* ]]; then
# On OSX, we need to override LLVM's looking around for our SDK
CMAKE_FLAGS+=(-DDARWIN_macosx_CACHED_SYSROOT:STRING=/opt/${target}/${target}/sys-root)
CMAKE_FLAGS+=(-DDARWIN_macosx_OVERRIDE_SDK_VERSION:STRING=10.8)
# LLVM actually won't build against 10.8, so we bump ourselves up slightly to 10.9
export MACOSX_DEPLOYMENT_TARGET=10.9
export LDFLAGS=-mmacosx-version-min=10.9
# We need to link against libc++ on OSX
CMAKE_FLAGS+=(-DLLVM_ENABLE_LIBCXX=ON)
fi
if [[ "${target}" == *apple* ]] || [[ "${target}" == *freebsd* ]]; then
# On clang-based platforms we need to override the check for ffs because it doesn't work with `clang`.
export ac_cv_have_decl___builtin_ffs=yes
# We don't use X-ray on BSD systems
CMAKE_FLAGS+=(-DCOMPILER_RT_BUILD_XRAY=OFF)
fi
if [[ "${target}" == *mingw* ]]; then
CMAKE_CPP_FLAGS="${CMAKE_CPP_FLAGS} -remap -D__USING_SJLJ_EXCEPTIONS__ -D__CRT__NO_INLINE"
# Windows is case-insensitive and some dependencies take full advantage of that
echo "BaseTsd.h basetsd.h" >> /opt/${target}/${target}/include/header.gcc
fi
if [[ "${target}" == *musl* ]]; then
# Taken from https://git.alpinelinux.org/cgit/aports/tree/main/compiler-rt/APKBUILD
CMAKE_FLAGS+=(-DCOMPILER_RT_INCLUDE_TESTS=ON)
CMAKE_FLAGS+=(-DCOMPILER_RT_BUILD_SANITIZERS=OFF)
CMAKE_FLAGS+=(-DCOMPILER_RT_BUILD_XRAY=OFF)
fi
if [[ "${target}" == *freebsd* ]]; then
# On FreeBSD, we must force even statically-linked code to have -fPIC
CMAKE_FLAGS+=(-DCMAKE_POSITION_INDEPENDENT_CODE=TRUE)
fi
cmake -GNinja ${LLVM_SRCDIR} ${CMAKE_FLAGS[@]} -DCMAKE_CXX_FLAGS="${CMAKE_CPP_FLAGS} ${CMAKE_CXX_FLAGS}" -DCMAKE_C_FLAGS="${CMAKE_CPP_FLAGS} ${CMAKE_CXX_FLAGS}"
cmake -LA || true
ninja -j${nproc} -vv
# Install!
ninja install
# move clang products out of $prefix/bin to $prefix/tools
mv ${prefix}/bin/clang* ${prefix}/tools/
mv ${prefix}/bin/scan-* ${prefix}/tools/
mv ${prefix}/bin/c-index* ${prefix}/tools/
mv ${prefix}/bin/git-clang* ${prefix}/tools/
mv ${prefix}/bin/lld* ${prefix}/tools/
# Life is harsh on Windows and dynamic libraries are
# expected to live alongside the binaries. So we have
# to copy the *.dll from bin/ to tools/ as well...
if [[ "${target}" == *mingw* ]]; then
cp ${prefix}/bin/*.dll ${prefix}/tools/
fi
# Work around llvm-config bug by creating versioned symlink to libLLVM
# https://github.com/JuliaLang/julia/pull/30033
if [[ "${target}" == *darwin* ]]; then
LLVM_VER=$(basename $(echo ${prefix}/tools/clang-*.*))
ln -s libLLVM.dylib ${prefix}/lib/libLLVM-${LLVM_VER##*-}.dylib
fi
# Lit is a python dependency and there is no proper install target
cp -r ${LLVM_SRCDIR}/utils/lit ${prefix}/tools/
install_license ${WORKSPACE}/srcdir/llvm-project/llvm/LICENSE.TXT
"""
# Also define some scripts for extraction:
const libllvmscript = raw"""
# First, find (true) LLVM library directory in ~/.artifacts somewhere
LLVM_ARTIFACT_DIR=$(dirname $(dirname $(realpath ${prefix}/tools/opt${exeext})))
# Clear out our `${prefix}`
rm -rf ${prefix}
# Copy over `llvm-config`, `libLLVM` and `include`, specifically.
mkdir -p ${prefix}/include ${prefix}/tools ${libdir} ${prefix}/lib
mv -v ${LLVM_ARTIFACT_DIR}/include/llvm* ${prefix}/include/
mv -v ${LLVM_ARTIFACT_DIR}/tools/llvm-config* ${prefix}/tools/
mv -v ${LLVM_ARTIFACT_DIR}/$(basename ${libdir})/*LLVM*.${dlext}* ${libdir}/
mv -v ${LLVM_ARTIFACT_DIR}/lib/*LLVM*.a ${prefix}/lib
install_license ${LLVM_ARTIFACT_DIR}/share/licenses/LLVM_full/*
"""
const clangscript = raw"""
# First, find (true) LLVM library directory in ~/.artifacts somewhere
LLVM_ARTIFACT_DIR=$(dirname $(dirname $(realpath ${prefix}/tools/opt${exeext})))
# Clear out our `${prefix}`
rm -rf ${prefix}
# Copy over `clang`, `libclang` and `include`, specifically.
mkdir -p ${prefix}/include ${prefix}/tools ${libdir} ${prefix}/lib
mv -v ${LLVM_ARTIFACT_DIR}/include/clang* ${prefix}/include/
mv -v ${LLVM_ARTIFACT_DIR}/tools/clang* ${prefix}/tools/
mv -v ${LLVM_ARTIFACT_DIR}/$(basename ${libdir})/libclang*.${dlext}* ${libdir}/
mv -v ${LLVM_ARTIFACT_DIR}/lib/libclang*.a ${prefix}/lib
mv -v ${LLVM_ARTIFACT_DIR}/lib/clang ${prefix}/lib/clang
install_license ${LLVM_ARTIFACT_DIR}/share/licenses/LLVM_full/*
"""
const llvmscript = raw"""
# First, find (true) LLVM library directory in ~/.artifacts somewhere
LLVM_ARTIFACT_DIR=$(dirname $(dirname $(realpath ${prefix}/tools/opt${exeext})))
# Clear out our `${prefix}`
rm -rf ${prefix}/*
# Copy over everything, but eliminate things already put inside `Clang_jll` or `libLLVM_jll`:
mv -v ${LLVM_ARTIFACT_DIR}/* ${prefix}/
rm -vrf ${prefix}/include/{clang*,llvm*}
rm -vrf ${prefix}/tools/{clang*,llvm-config}
rm -vrf ${libdir}/libclang*.${dlext}*
rm -vrf ${libdir}/*LLVM*.${dlext}*
rm -vrf ${prefix}/lib/*LLVM*.a
rm -vrf ${prefix}/lib/libclang*.a
rm -vrf ${prefix}/lib/clang
"""
function configure_build(ARGS, version)
# Parse out some args
assert = false
if "--assert" in ARGS
assert = true
deleteat!(ARGS, findfirst(ARGS .== "--assert"))
end
sources = [
GitSource("https://github.com/llvm/llvm-project.git", llvm_tags[version]),
DirectorySource("./bundled"),
]
products = [
LibraryProduct("libclang", :libclang, dont_dlopen=true),
LibraryProduct(["LLVM", "libLLVM"], :libllvm, dont_dlopen=true),
LibraryProduct(["LTO", "libLTO"], :liblto, dont_dlopen=true),
ExecutableProduct("llvm-config", :llvm_config, "tools"),
ExecutableProduct("clang", :clang, "tools"),
ExecutableProduct("opt", :opt, "tools"),
ExecutableProduct("llc", :llc, "tools"),
]
if version >= v"8"
push!(products, ExecutableProduct("llvm-mca", :llvm_mca, "tools"))
end
name = "LLVM_full"
config = "LLVM_MAJ_VER=$(version.major)\n"
if assert
config *= "ASSERTS=1\n"
name = "$(name)_assert"
end
# Dependencies that must be installed before this package can be built
# TODO: Zlib, LibXML2
dependencies = Dependency[]
return name, version, sources, config * buildscript, platforms, products, dependencies
end
function configure_extraction(ARGS, LLVM_full_version, name, libLLVM_version=nothing)
if isempty(LLVM_full_version.build)
error("You must lock an extracted LLVM build to a particular LLVM_full build number!")
end
if name != "libLLVM" && (libLLVM_version === nothing || isempty(libLLVM_version.build))
error("You must lock an extracted LLVM build to a particular libLLVM build number!")
end
version = VersionNumber(LLVM_full_version.major, LLVM_full_version.minor, LLVM_full_version.patch)
if name == "libLLVM"
script = libllvmscript
products = [
LibraryProduct(["LLVM", "libLLVM"], :libllvm, dont_dlopen=true),
ExecutableProduct("llvm-config", :llvm_config, "tools"),
]
elseif name == "Clang"
script = clangscript
products = [
LibraryProduct("libclang", :libclang, dont_dlopen=true),
ExecutableProduct("clang", :clang, "tools"),
]
elseif name == "LLVM"
script = llvmscript
products = [
LibraryProduct(["LTO", "libLTO"], :liblto, dont_dlopen=true),
ExecutableProduct("opt", :opt, "tools"),
ExecutableProduct("llc", :llc, "tools"),
]
end
dependencies = BinaryBuilder.AbstractDependency[]
if "--assert" in ARGS
push!(dependencies, BuildDependency(get_addable_spec("LLVM_full_assert_jll", LLVM_full_version)))
if name in ("Clang", "LLVM")
push!(dependencies, Dependency(get_addable_spec("libLLVM_assert_jll", libLLVM_version)))
end
name = "$(name)_assert"
deleteat!(ARGS, findfirst(ARGS .== "--assert"))
else
push!(dependencies, BuildDependency(get_addable_spec("LLVM_full_jll", LLVM_full_version)))
if name in ("Clang", "LLVM")
push!(dependencies, Dependency(get_addable_spec("libLLVM_jll", libLLVM_version)))
end
end
return name, version, [], script, platforms, products, dependencies
end