Skip to content

Commit

Permalink
Toolchain+Meta: Update LLVM version to 13.0.0
Browse files Browse the repository at this point in the history
This commit updates the Clang toolchain's version to 13.0.0, which comes
with better C++20 support and improved handling of new features by
clang-format. Due to the newly enabled `-Bsymbolic-functions` flag, our
Clang binaries will only be 2-4% slower than if we dynamically linked
them, but we save hundreds of megabytes of disk space.

The `BuildClang.sh` script has been reworked to build the entire
toolchain in just three steps: one for the compiler, one for GNU
binutils, and one for the runtime libraries. This reduces the complexity
of the build script, and will allow us to modify the CI configuration to
only rebuild the libraries when our libc headers change.

Most of the compile flags have been moved out to a separate CMake cache
file, similarly to how the Android and Fuchsia toolchains are
implemented within the LLVM repo. This provides a nicer interface than
the heaps of command-line arguments.

We no longer build separate toolchains for each architecture, as the
same Clang binary can compile code for multiple targets.

The horrible mess that `SERENITY_CLANG_ARCH` was, has been removed in
this commit. Clang happily accepts an `i686-pc-serenity` target triple,
which matches what our GCC toolchain accepts.
  • Loading branch information
BertalanD authored and linusg committed Oct 17, 2021
1 parent 28c088c commit 06fc64b
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 667 deletions.
20 changes: 4 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL "SerenityOS")
"Please re-read the BuildInstructions documentation, and use the superbuild configuration\n")
endif()

if(SERENITY_ARCH STREQUAL "i686")
set(SERENITY_CLANG_ARCH "i386")
else()
set(SERENITY_CLANG_ARCH "${SERENITY_ARCH}")
endif()

set(CMAKE_INSTALL_MESSAGE NEVER)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand Down Expand Up @@ -176,7 +170,6 @@ add_compile_options(-fdiagnostics-color=always)
add_compile_options(-fno-delete-null-pointer-checks)
add_compile_options(-ffile-prefix-map=${SerenityOS_SOURCE_DIR}=.)
add_compile_options(-fno-exceptions)
add_compile_options(-ftls-model=initial-exec)
add_compile_options(-fno-semantic-interposition)
add_compile_options(-fsized-deallocation)
add_compile_options(-fstack-clash-protection)
Expand All @@ -194,26 +187,21 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
add_compile_options(-Wno-c99-designator)
add_compile_options(-Wno-implicit-const-int-float-conversion)
add_compile_options(-Wno-inconsistent-missing-override)
add_compile_options(-Wno-null-pointer-subtraction)
add_compile_options(-Wno-tautological-constant-out-of-range-compare)
add_compile_options(-Wno-unneeded-internal-declaration)
add_compile_options(-Wno-unused-but-set-variable)
add_compile_options(-Wno-unused-function)
add_compile_options(-fno-aligned-allocation)
add_compile_options(-fconstexpr-steps=16777216)
add_compile_options(-gdwarf-4)

# FIXME: Why can't clang find this path for compiler_rt builtins?
link_directories(${TOOLCHAIN_ROOT}/lib/clang/${CMAKE_CXX_COMPILER_VERSION}/lib/serenity)
# Clang doesn't add compiler_rt to the search path when compiling with -nostdlib.
link_directories(${TOOLCHAIN_ROOT}/lib/clang/${CMAKE_CXX_COMPILER_VERSION}/lib/${SERENITY_ARCH}-pc-serenity/)
add_link_options(LINKER:--allow-shlib-undefined)
endif()

add_link_options(LINKER:-z,text)

if("${SERENITY_ARCH}" STREQUAL "i686")
add_compile_options(-march=i686)
elseif("${SERENITY_ARCH}" STREQUAL "x86_64")
add_compile_options(-march=x86-64)
endif()

add_compile_definitions(SANITIZE_PTRS)
set(CMAKE_CXX_FLAGS_STATIC "${CMAKE_CXX_FLAGS} -static")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pie -fpic")
Expand Down
9 changes: 7 additions & 2 deletions Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,14 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
endif()
link_directories(${TOOLCHAIN_ROOT}/${SERENITY_ARCH}-pc-serenity/lib)
link_directories(${TOOLCHAIN_ROOT}/lib/gcc/${SERENITY_ARCH}-pc-serenity/${GCC_VERSION}/)

set(TARGET_STRING "")
else() # Assume Clang
add_compile_options(-Waddress-of-packed-member)
add_compile_options(-faligned-allocation)

# We need this in order to pick up the #define __serenity__, otherwise we end up including unistd.h into the linker script
set(TARGET_STRING "--target=${CMAKE_CXX_COMPILER_TARGET}")

add_link_options(LINKER:--build-id=none)
endif()
Expand Down Expand Up @@ -476,7 +481,7 @@ add_dependencies(Kernel generate_EscapeSequenceStateMachine.h)

add_custom_command(
OUTPUT linker.ld
COMMAND "${CMAKE_CXX_COMPILER}" -E -P -x c -I${CMAKE_CURRENT_SOURCE_DIR}/.. "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld" -o "${CMAKE_CURRENT_BINARY_DIR}/linker.ld"
COMMAND "${CMAKE_CXX_COMPILER}" ${TARGET_STRING} -E -P -x c -I${CMAKE_CURRENT_SOURCE_DIR}/.. "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld" -o "${CMAKE_CURRENT_BINARY_DIR}/linker.ld"
MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld"
COMMENT "Preprocessing linker.ld"
VERBATIM
Expand All @@ -497,7 +502,7 @@ if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(Kernel PRIVATE kernel_heap gcc)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
target_link_libraries(Kernel PRIVATE kernel_heap "clang_rt.builtins-${SERENITY_CLANG_ARCH}")
target_link_libraries(Kernel PRIVATE kernel_heap clang_rt.builtins)
endif()
endif()

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Prekernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ set_target_properties(${PREKERNEL_TARGET} PROPERTIES LINK_DEPENDS ${PREKERNEL_LI
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(${PREKERNEL_TARGET} PRIVATE gcc)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
target_link_libraries(${PREKERNEL_TARGET} PRIVATE "clang_rt.builtins-${SERENITY_CLANG_ARCH}" c++abi)
target_link_libraries(${PREKERNEL_TARGET} PRIVATE clang_rt.builtins)
endif()

if ("${SERENITY_ARCH}" STREQUAL "i686" OR "${SERENITY_ARCH}" STREQUAL "x86_64")
Expand Down
2 changes: 1 addition & 1 deletion Meta/CMake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function(serenity_libc target_name fs_name)
install(TARGETS ${target_name} DESTINATION usr/lib)
set_target_properties(${target_name} PROPERTIES OUTPUT_NAME ${fs_name})
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
target_link_libraries(${target_name} "clang_rt.builtins-${SERENITY_CLANG_ARCH}")
target_link_libraries(${target_name} clang_rt.builtins)
endif()
target_link_directories(LibC PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
serenity_generated_sources(${target_name})
Expand Down
9 changes: 4 additions & 5 deletions Meta/build-root-filesystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ else
fi

SERENITY_ARCH="${SERENITY_ARCH:-i686}"
LLVM_VERSION="${LLVM_VERSION:-12.0.1}"
LLVM_VERSION="${LLVM_VERSION:-13.0.0}"

if [ "$SERENITY_TOOLCHAIN" = "Clang" ]; then
TOOLCHAIN_DIR="$SERENITY_SOURCE_DIR"/Toolchain/Local/clang/"$SERENITY_ARCH"
TOOLCHAIN_DIR="$SERENITY_SOURCE_DIR"/Toolchain/Local/clang/
mkdir -p mnt/usr/lib/clang/"$LLVM_VERSION"/lib/serenity
$CP "$TOOLCHAIN_DIR"/lib/clang/"$LLVM_VERSION"/lib/serenity/* mnt/usr/lib/clang/"$LLVM_VERSION"/lib/serenity
$CP "$TOOLCHAIN_DIR"/lib/libunwind* mnt/usr/lib
$CP "$TOOLCHAIN_DIR"/lib/libc++* mnt/usr/lib
$CP "$TOOLCHAIN_DIR"/lib/clang/"$LLVM_VERSION"/lib/"$SERENITY_ARCH"-pc-serenity/* mnt/usr/lib/clang/"$LLVM_VERSION"/lib/serenity
$CP "$TOOLCHAIN_DIR"/lib/"$SERENITY_ARCH"-pc-serenity/* mnt/usr/lib
elif [ "$SERENITY_ARCH" != "aarch64" ]; then
$CP "$SERENITY_SOURCE_DIR"/Toolchain/Local/"$SERENITY_ARCH"/"$SERENITY_ARCH"-pc-serenity/lib/libgcc_s.so mnt/usr/lib
fi
Expand Down
8 changes: 6 additions & 2 deletions Meta/serenity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ cmd_with_target() {
BUILD_DIR="$SERENITY_SOURCE_DIR/Build/$TARGET$TARGET_TOOLCHAIN"
if [ "$TARGET" != "lagom" ]; then
export SERENITY_ARCH="$TARGET"
TOOLCHAIN_DIR="$SERENITY_SOURCE_DIR/Toolchain/Local/$TARGET_TOOLCHAIN/$TARGET"
if [ "$TOOLCHAIN_TYPE" = "Clang" ]; then
TOOLCHAIN_DIR="$SERENITY_SOURCE_DIR/Toolchain/Local/clang"
else
TOOLCHAIN_DIR="$SERENITY_SOURCE_DIR/Toolchain/Local/$TARGET_TOOLCHAIN/$TARGET"
fi
SUPER_BUILD_DIR="$SERENITY_SOURCE_DIR/Build/superbuild-$TARGET$TARGET_TOOLCHAIN"
else
SUPER_BUILD_DIR="$BUILD_DIR"
Expand Down Expand Up @@ -225,7 +229,7 @@ delete_target() {
build_toolchain() {
echo "build_toolchain: $TOOLCHAIN_DIR"
if [ "$TOOLCHAIN_TYPE" = "Clang" ]; then
( cd "$SERENITY_SOURCE_DIR/Toolchain" && ARCH="$TARGET" ./BuildClang.sh )
( cd "$SERENITY_SOURCE_DIR/Toolchain" && ./BuildClang.sh )
else
( cd "$SERENITY_SOURCE_DIR/Toolchain" && ARCH="$TARGET" ./BuildIt.sh )
fi
Expand Down
Loading

0 comments on commit 06fc64b

Please sign in to comment.