2020-09-18 00:12:41 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
TargetOSArchitecture=$1
|
|
|
|
CrossRootfsDirectory=$2
|
|
|
|
|
2024-01-03 23:42:03 +03:00
|
|
|
# Set this to 1 to build using CBL-Mariner
|
|
|
|
CrossBuildUsingMariner=1
|
|
|
|
|
2021-12-31 01:26:08 +03:00
|
|
|
EnsureCrossRootfsDirectoryExists () {
|
|
|
|
if [ ! -d "$CrossRootfsDirectory" ]; then
|
|
|
|
echo "Invalid or unspecified CrossRootfsDirectory: $CrossRootfsDirectory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
CMakeOSXArchitectures=
|
|
|
|
LLVMTargetsToBuild="AArch64;ARM;X86"
|
|
|
|
|
2024-01-03 23:42:03 +03:00
|
|
|
# Figure out which `strip` to use. Prefer `llvm-strip` if it is available.
|
2024-01-23 20:12:08 +03:00
|
|
|
# `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)
|
2024-01-03 23:42:03 +03:00
|
|
|
if [ -z "$StripTool" ]; then
|
2024-01-23 20:12:08 +03:00
|
|
|
echo "Strip tool not found"
|
|
|
|
exit 1
|
2024-01-03 23:42:03 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
TblGenTool=$(command -v llvm-tblgen)
|
|
|
|
if [ -z "$TblGenTool" ]; then
|
|
|
|
echo "llvm-tblgen tool not found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-01-23 20:12:08 +03:00
|
|
|
# Take first match from: clang clang-20 clang-19 .. clang-15
|
|
|
|
C_COMPILER=$(command -v clang{,-{20..15}} | head -n 1)
|
2024-01-03 23:42:03 +03:00
|
|
|
if [ -z "$C_COMPILER" ]; then
|
|
|
|
echo "C compiler not found"
|
|
|
|
# Keep going in case cmake can find one?
|
|
|
|
fi
|
|
|
|
|
2024-01-23 20:12:08 +03:00
|
|
|
CXX_COMPILER=$(command -v clang++{,-{20..15}} | head -n 1)
|
2024-01-03 23:42:03 +03:00
|
|
|
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"
|
|
|
|
|
2020-09-18 00:12:41 +03:00
|
|
|
case "$TargetOSArchitecture" in
|
|
|
|
linux-arm)
|
2021-12-31 01:26:08 +03:00
|
|
|
CMakeCrossCompiling=ON
|
2020-09-24 01:18:57 +03:00
|
|
|
LLVMDefaultTargetTriple=thumbv7-linux-gnueabihf
|
|
|
|
LLVMHostTriple=arm-linux-gnueabihf
|
2024-01-03 23:42:03 +03:00
|
|
|
LLVMTargetsToBuild="ARM"
|
2021-12-31 01:26:08 +03:00
|
|
|
EnsureCrossRootfsDirectoryExists
|
2020-09-18 00:12:41 +03:00
|
|
|
;;
|
|
|
|
|
|
|
|
linux-arm64)
|
2021-12-31 01:26:08 +03:00
|
|
|
CMakeCrossCompiling=ON
|
2020-09-24 01:18:57 +03:00
|
|
|
LLVMHostTriple=aarch64-linux-gnu
|
2021-12-31 01:26:08 +03:00
|
|
|
EnsureCrossRootfsDirectoryExists
|
2020-09-18 00:12:41 +03:00
|
|
|
;;
|
|
|
|
|
2021-12-31 01:26:08 +03:00
|
|
|
linux-x64)
|
|
|
|
LLVMHostTriple=x86_64-linux-gnu
|
2024-01-03 23:42:03 +03:00
|
|
|
if [ $CrossBuildUsingMariner -eq 1 ]; then
|
|
|
|
CMakeCrossCompiling=ON
|
|
|
|
EnsureCrossRootfsDirectoryExists
|
|
|
|
else
|
|
|
|
CMakeCrossCompiling=OFF
|
|
|
|
fi
|
2021-12-31 01:26:08 +03:00
|
|
|
;;
|
|
|
|
|
2023-02-21 16:07:23 +03:00
|
|
|
linux-loongarch64)
|
|
|
|
CMakeCrossCompiling=OFF
|
|
|
|
LLVMHostTriple=loongarch64-linux-gnu
|
|
|
|
LLVMTargetsToBuild="LoongArch"
|
|
|
|
;;
|
|
|
|
|
2024-01-23 20:12:08 +03:00
|
|
|
linux-riscv64)
|
|
|
|
CMakeCrossCompiling=ON
|
|
|
|
LLVMHostTriple=riscv64-linux-gnu
|
|
|
|
LLVMTargetsToBuild="RISCV"
|
|
|
|
EnsureCrossRootfsDirectoryExists
|
|
|
|
;;
|
|
|
|
|
2021-12-31 01:26:08 +03:00
|
|
|
osx-arm64)
|
|
|
|
CMakeCrossCompiling=ON
|
|
|
|
CMakeOSXArchitectures=arm64
|
|
|
|
LLVMHostTriple=arm64-apple-macos
|
|
|
|
;;
|
|
|
|
|
|
|
|
osx-x64)
|
|
|
|
CMakeCrossCompiling=OFF
|
|
|
|
CMakeOSXArchitectures=x86_64
|
|
|
|
LLVMHostTriple=x86_64-apple-darwin
|
2020-09-18 00:12:41 +03:00
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo "Unknown target OS and architecture: $TargetOSArchitecture"
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
2021-12-31 01:26:08 +03:00
|
|
|
LLVMDefaultTargetTriple=${LLVMDefaultTargetTriple:-$LLVMHostTriple}
|
2020-09-18 00:12:41 +03:00
|
|
|
|
2020-09-29 01:33:54 +03:00
|
|
|
RootDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
|
|
|
SourcesDirectory=$RootDirectory/src
|
2020-11-04 04:42:51 +03:00
|
|
|
BinariesDirectory=$RootDirectory/obj/$TargetOSArchitecture
|
2020-09-29 01:33:54 +03:00
|
|
|
StagingDirectory=$RootDirectory/artifacts/$TargetOSArchitecture
|
|
|
|
|
2024-01-03 23:42:03 +03:00
|
|
|
command -v cmake >/dev/null 2>&1
|
2020-09-29 01:33:54 +03:00
|
|
|
|
|
|
|
if [ "$?" -ne 0 ]; then
|
|
|
|
echo "ERROR: cmake is not found in the PATH"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-09-18 00:12:41 +03:00
|
|
|
if [ ! -d $BinariesDirectory ]; then
|
|
|
|
mkdir -p $BinariesDirectory
|
|
|
|
fi
|
|
|
|
|
|
|
|
pushd "$BinariesDirectory"
|
|
|
|
|
2021-12-31 01:26:08 +03:00
|
|
|
if [ -z "$CrossRootfsDirectory" ]; then
|
2024-01-03 23:42:03 +03:00
|
|
|
BUILD_FLAGS="-target $LLVMHostTriple"
|
2020-09-18 00:12:41 +03:00
|
|
|
cmake \
|
|
|
|
-G "Unix Makefiles" \
|
|
|
|
-DCMAKE_BUILD_TYPE=Release \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \
|
2024-01-03 23:42:03 +03:00
|
|
|
-DCMAKE_C_COMPILER=${C_COMPILER} \
|
|
|
|
-DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
|
|
|
|
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
|
|
|
|
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
|
2020-09-29 01:33:54 +03:00
|
|
|
-DCMAKE_INSTALL_PREFIX=$StagingDirectory \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DCMAKE_OSX_ARCHITECTURES=$CMakeOSXArchitectures \
|
2024-01-03 23:42:03 +03:00
|
|
|
-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 \
|
2020-09-24 01:18:57 +03:00
|
|
|
-DLLVM_DEFAULT_TARGET_TRIPLE=$LLVMDefaultTargetTriple \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DLLVM_ENABLE_TERMINFO=OFF \
|
2020-09-18 00:12:41 +03:00
|
|
|
-DLLVM_EXTERNAL_PROJECTS=coredistools \
|
|
|
|
-DLLVM_EXTERNAL_COREDISTOOLS_SOURCE_DIR=$SourcesDirectory/coredistools \
|
2020-09-24 01:18:57 +03:00
|
|
|
-DLLVM_HOST_TRIPLE=$LLVMHostTriple \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DLLVM_INCLUDE_TESTS=OFF \
|
2024-01-03 23:42:03 +03:00
|
|
|
-DLLVM_TABLEGEN=$TblGenTool \
|
2020-09-24 01:18:57 +03:00
|
|
|
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
|
2020-09-18 00:12:41 +03:00
|
|
|
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON \
|
|
|
|
$SourcesDirectory/llvm-project/llvm
|
|
|
|
else
|
2024-01-03 23:42:03 +03:00
|
|
|
BUILD_FLAGS="--sysroot=$CrossRootfsDirectory -target $LLVMHostTriple"
|
2020-09-18 00:12:41 +03:00
|
|
|
cmake \
|
|
|
|
-G "Unix Makefiles" \
|
|
|
|
-DCMAKE_BUILD_TYPE=Release \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \
|
2024-01-03 23:42:03 +03:00
|
|
|
-DCMAKE_C_COMPILER=${C_COMPILER} \
|
|
|
|
-DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
|
|
|
|
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
|
|
|
|
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DCMAKE_INCLUDE_PATH=$CrossRootfsDirectory/usr/include \
|
2020-09-29 01:33:54 +03:00
|
|
|
-DCMAKE_INSTALL_PREFIX=$StagingDirectory \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DCMAKE_LIBRARY_PATH=$CrossRootfsDirectory/usr/lib/$LLVMHostTriple \
|
2024-01-03 23:42:03 +03:00
|
|
|
-DCMAKE_STRIP=$StripTool \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DLLVM_DEFAULT_TARGET_TRIPLE=$LLVMDefaultTargetTriple \
|
|
|
|
-DLLVM_ENABLE_TERMINFO=OFF \
|
2020-09-18 00:12:41 +03:00
|
|
|
-DLLVM_EXTERNAL_PROJECTS=coredistools \
|
|
|
|
-DLLVM_EXTERNAL_COREDISTOOLS_SOURCE_DIR=$SourcesDirectory/coredistools \
|
2021-12-31 01:26:08 +03:00
|
|
|
-DLLVM_HOST_TRIPLE=$LLVMHostTriple \
|
|
|
|
-DLLVM_INCLUDE_TESTS=OFF \
|
2024-01-03 23:42:03 +03:00
|
|
|
-DLLVM_TABLEGEN=$TblGenTool \
|
2020-09-29 01:33:54 +03:00
|
|
|
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
|
2020-09-18 00:12:41 +03:00
|
|
|
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON \
|
|
|
|
$SourcesDirectory/llvm-project/llvm
|
|
|
|
fi
|
|
|
|
|
|
|
|
popd
|
|
|
|
|
2020-09-29 01:33:54 +03:00
|
|
|
if [ "$?" -ne 0 ]; then
|
|
|
|
echo "ERROR: cmake exited with code $1"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-09-18 00:12:41 +03:00
|
|
|
cmake \
|
|
|
|
--build $BinariesDirectory \
|
2021-12-31 01:26:08 +03:00
|
|
|
--parallel \
|
2020-09-29 01:33:54 +03:00
|
|
|
--target install-coredistools-stripped
|
2020-09-18 00:12:41 +03:00
|
|
|
|
|
|
|
if [ "$?" -ne 0 ]; then
|
2020-09-29 01:33:54 +03:00
|
|
|
echo "ERROR: cmake exited with code $1"
|
2020-09-18 00:12:41 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-09-29 01:33:54 +03:00
|
|
|
exit 0
|