Skip to content

Commit

Permalink
Update coredistools (#370)
Browse files Browse the repository at this point in the history
1. Update from LLVM 13.0.1 to 17.0.6
2. Change Linux/Mac build to build llvm-tblgen from source instead of
downloading a pre-built version. LLVM doesn't always publish all
architecture versions of this tool.
3. Change Linux to build with CBL-Mariner container.

TO DO:
- [x] Mac: properly download built llvm-tblgen artifact and put it on
the path so the build-coredistools step can find it.
- [x] Linux: fix the CBL-Mariner build. Mariner currently has clang12
and is expected to update to clang16 soon. Will that fix problems? Or,
back off to ubuntu again, and stop doing cross-compiler builds for
linux-x64 (like we do for Mariner).

Fixes #372
  • Loading branch information
BruceForstall committed Jan 3, 2024
1 parent 597e750 commit 74a49ca
Show file tree
Hide file tree
Showing 11 changed files with 610 additions and 253 deletions.
41 changes: 37 additions & 4 deletions build-coredistools.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ if /i "%TargetOSArchitecture%" == "win-arm64" (
set LLVMTargetsToBuild=ARM;X86
) else (
echo ERROR: Unknown target OS and architecture: %TargetOSArchitecture%
echo Use one of win-arm64, win-x64, win-x86.
exit /b 1
)

set BuildFlavor=%2
if "%BuildFlavor%"=="" set BuildFlavor=Release

if /i "%BuildFlavor%" == "Release" (
@REM ok
) else if /i "%BuildFlavor%" == "Debug" (
@REM ok
) else (
echo ERROR: Unknown build flavor: %BuildFlavor%
exit /b 1
)

Expand All @@ -37,11 +50,19 @@ if %ERRORLEVEL% neq 0 (

where /q llvm-tblgen.exe

if %ERRORLEVEL% neq 0 (
if %ERRORLEVEL% equ 0 goto found_llvm_tblgen

@REM We expect it to be in the `bin` directory, so add that to the PATH if it's there.
if not exist %RootDirectory%bin\llvm-tblgen.exe (
echo ERROR: llvm-tblgen.exe is not found in the PATH
exit /b 1
)

echo Found llvm-tblgen.exe in %RootDirectory%bin; adding that directory to PATH.
set PATH=%RootDirectory%bin;%PATH%

:found_llvm_tblgen

for /f %%I in ('where llvm-tblgen.exe') do (
set LLVMTableGen=%%~I
)
Expand All @@ -51,11 +72,22 @@ if not exist "%BinariesDirectory%" (
)

pushd "%BinariesDirectory%"

@REM To use the Debug CRT, use:
@REM -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebug
@REM
@REM To build a Debug version (asserts, debug info):
@REM -DCMAKE_BUILD_TYPE=Debug
@REM To build a Release version (no asserts, no debug info):
@REM -DCMAKE_BUILD_TYPE=Release
@REM
@REM Misc. LLVM CMake documentation: https://llvm.org/docs/CMake.html

cmake.exe ^
-G "Visual Studio 17 2022" ^
-A %GeneratorPlatform% ^
-DCMAKE_INSTALL_PREFIX="%StagingDirectory%" ^
-DCMAKE_BUILD_TYPE=%BuildFlavor% ^
-DLLVM_DEFAULT_TARGET_TRIPLE=%LLVMDefaultTargetTriple% ^
-DLLVM_EXTERNAL_PROJECTS=coredistools ^
-DLLVM_EXTERNAL_COREDISTOOLS_SOURCE_DIR="%SourcesDirectory%\coredistools" ^
Expand All @@ -64,18 +96,19 @@ cmake.exe ^
-DLLVM_TABLEGEN="%LLVMTableGen%" ^
-DLLVM_TARGETS_TO_BUILD=%LLVMTargetsToBuild% ^
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON ^
-DLLVM_USE_CRT_DEBUG=MTd ^
-DLLVM_USE_CRT_RELEASE=MT ^
-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded ^
"%SourcesDirectory%\llvm-project\llvm"

popd

if %ERRORLEVEL% neq 0 goto :CMakeNonZeroExitStatus

@REM Use `--config Release` for release build, `--config Debug` for debug build

cmake.exe ^
--build "%BinariesDirectory%" ^
--target coredistools ^
--config Release
--config %BuildFlavor%

if %ERRORLEVEL% neq 0 goto :CMakeNonZeroExitStatus

Expand Down
99 changes: 84 additions & 15 deletions build-coredistools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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"
Expand All @@ -13,12 +16,44 @@ EnsureCrossRootfsDirectoryExists () {
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; `strip` is available on macOS.
StripTool=$(command -v llvm-strip)
if [ -z "$StripTool" ]; then
StripTool=$(command -v strip)
if [ -z "$StripTool" ]; then
echo "Strip tool not found"
exit 1
fi
fi

TblGenTool=$(command -v llvm-tblgen)
if [ -z "$TblGenTool" ]; then
echo "llvm-tblgen tool not found"
exit 1
fi

C_COMPILER=$(command -v clang)
if [ -z "$C_COMPILER" ]; then
echo "C compiler not found"
# Keep going in case cmake can find one?
fi

CXX_COMPILER=$(command -v clang++)
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
LLVMTargetsToBuild="ARM"
EnsureCrossRootfsDirectoryExists
;;

Expand All @@ -29,8 +64,13 @@ case "$TargetOSArchitecture" in
;;

linux-x64)
CMakeCrossCompiling=OFF
LLVMHostTriple=x86_64-linux-gnu
if [ $CrossBuildUsingMariner -eq 1 ]; then
CMakeCrossCompiling=ON
EnsureCrossRootfsDirectoryExists
else
CMakeCrossCompiling=OFF
fi
;;

linux-loongarch64)
Expand Down Expand Up @@ -63,7 +103,7 @@ SourcesDirectory=$RootDirectory/src
BinariesDirectory=$RootDirectory/obj/$TargetOSArchitecture
StagingDirectory=$RootDirectory/artifacts/$TargetOSArchitecture

which cmake >/dev/null 2>&1
command -v cmake >/dev/null 2>&1

if [ "$?" -ne 0 ]; then
echo "ERROR: cmake is not found in the PATH"
Expand All @@ -77,47 +117,76 @@ 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=$(which clang) \
-DCMAKE_C_FLAGS="-target $LLVMHostTriple" \
-DCMAKE_CXX_COMPILER=$(which clang++) \
-DCMAKE_CXX_FLAGS="-target $LLVMHostTriple" \
-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=$(which strip) \
-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=$(which llvm-tblgen) \
-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=$(which clang) \
-DCMAKE_C_FLAGS="-target $LLVMHostTriple --sysroot=$CrossRootfsDirectory" \
-DCMAKE_CXX_COMPILER=$(which clang++) \
-DCMAKE_CXX_FLAGS="-target $LLVMHostTriple --sysroot=$CrossRootfsDirectory" \
-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=/usr/$LLVMHostTriple/bin/strip \
-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=$(which llvm-tblgen) \
-DLLVM_TABLEGEN=$TblGenTool \
-DLLVM_TARGETS_TO_BUILD=$LLVMTargetsToBuild \
-DLLVM_TOOL_COREDISTOOLS_BUILD=ON \
$SourcesDirectory/llvm-project/llvm
Expand Down
Loading

0 comments on commit 74a49ca

Please sign in to comment.