1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:17:44 +00:00

Meta: Add Clang support to the CMake build scripts

This commit is contained in:
Daniel Bertalan 2021-07-13 16:43:45 +02:00 committed by Andreas Kling
parent 15e217ea68
commit 13e3df41de
7 changed files with 128 additions and 22 deletions

View file

@ -22,6 +22,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(SERENITY_ARCH "i686" CACHE STRING "Target architecture for SerenityOS.")
if("${SERENITY_ARCH}" STREQUAL "i686")
set(SERENITY_CLANG_ARCH "i386")
else()
set(SERENITY_CLANG_ARCH ${SERENITY_ARCH})
endif()
# Central location for all custom options used in the Serenity build.
option(ENABLE_ADDRESS_SANITIZER "Enable address sanitizer testing in gcc/clang" OFF)
option(ENABLE_KERNEL_ADDRESS_SANITIZER "Enable kernel address sanitizer testing in gcc/clang" OFF)
@ -37,6 +43,7 @@ option(ENABLE_PCI_IDS_DOWNLOAD "Enable download of the pci.ids database at build
option(ENABLE_USB_IDS_DOWNLOAD "Enable download of the usb.ids database at build time" ON)
option(BUILD_LAGOM "Build parts of the system targeting the host OS for fuzzing/testing" OFF)
option(ENABLE_KERNEL_LTO "Build the kernel with link-time optimization" OFF)
option(USE_CLANG_TOOLCHAIN "Build the kernel with the experimental Clang toolchain" OFF)
include(Meta/CMake/wasm_spec_tests.cmake)
@ -60,18 +67,22 @@ add_custom_target(setup-and-run
add_custom_target(image
DEPENDS qemu-image
)
set(GCC_VERSION 11.2.0)
set(LLVM_VERSION 12.0.1)
add_custom_target(qemu-image
COMMAND ${CMAKE_COMMAND} -E env "SERENITY_SOURCE_DIR=${CMAKE_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" ${CMAKE_SOURCE_DIR}/Meta/build-image-qemu.sh
COMMAND ${CMAKE_COMMAND} -E env "SERENITY_SOURCE_DIR=${CMAKE_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" "USE_CLANG_TOOLCHAIN=$<BOOL:${USE_CLANG_TOOLCHAIN}>" "LLVM_VERSION=${LLVM_VERSION}" ${CMAKE_SOURCE_DIR}/Meta/build-image-qemu.sh
BYPRODUCTS ${CMAKE_BINARY_DIR}/_disk_image
USES_TERMINAL
)
add_custom_target(grub-image
COMMAND ${CMAKE_COMMAND} -E env "SERENITY_SOURCE_DIR=${CMAKE_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" ${CMAKE_SOURCE_DIR}/Meta/build-image-grub.sh
COMMAND ${CMAKE_COMMAND} -E env "SERENITY_SOURCE_DIR=${CMAKE_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" "USE_CLANG_TOOLCHAIN=$<BOOL:${USE_CLANG_TOOLCHAIN}>" "LLVM_VERSION=${LLVM_VERSION}" ${CMAKE_SOURCE_DIR}/Meta/build-image-grub.sh
BYPRODUCTS ${CMAKE_BINARY_DIR}/grub_disk_image
USES_TERMINAL
)
add_custom_target(extlinux-image
COMMAND ${CMAKE_COMMAND} -E env "SERENITY_SOURCE_DIR=${CMAKE_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" ${CMAKE_SOURCE_DIR}/Meta/build-image-extlinux.sh
COMMAND ${CMAKE_COMMAND} -E env "SERENITY_SOURCE_DIR=${CMAKE_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" "USE_CLANG_TOOLCHAIN=$<BOOL:${USE_CLANG_TOOLCHAIN}>" "LLVM_VERSION=${LLVM_VERSION}" ${CMAKE_SOURCE_DIR}/Meta/build-image-extlinux.sh
BYPRODUCTS ${CMAKE_BINARY_DIR}/extlinux_disk_image
USES_TERMINAL
)
@ -100,12 +111,13 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_compile_options(-Wno-literal-suffix)
add_compile_options(-fsized-deallocation)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-Wno-literal-suffix)
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang$")
add_compile_options(-Wno-overloaded-virtual -Wno-user-defined-literals)
add_compile_options(-Wno-overloaded-virtual)
add_compile_options(-Wno-user-defined-literals)
add_compile_options(-fconstexpr-steps=16777216)
endif()
if (ENABLE_ALL_DEBUG_FACILITIES)
@ -159,10 +171,23 @@ set(CMAKE_STAGING_PREFIX ${CMAKE_BINARY_DIR}/Root)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Root)
set(CMAKE_INSTALL_DATAROOTDIR ${CMAKE_BINARY_DIR}/Root/res)
set(GCC_VERSION 11.2.0)
if (${CMAKE_HOST_SYSTEM_NAME} MATCHES SerenityOS)
message("Good job on building cmake!")
elseif(USE_CLANG_TOOLCHAIN)
set(TOOLCHAIN_ROOT ${CMAKE_SOURCE_DIR}/Toolchain/Local/clang/${SERENITY_ARCH}/)
set(TOOLCHAIN_PATH ${TOOLCHAIN_ROOT}/bin)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/clang)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/clang++)
set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PATH}/clang)
set(CMAKE_LINKER ${TOOLCHAIN_PATH}/ld.lld)
set(CMAKE_RANLIB ${TOOLCHAIN_PATH}/llvm-ranlib)
set(CMAKE_STRIP ${TOOLCHAIN_PATH}/llvm-strip)
set(CMAKE_AR ${TOOLCHAIN_PATH}/llvm-ar)
set(CMAKE_CXXFILT ${TOOLCHAIN_PATH}/llvm-cxxfilt)
# FIXME: Persuade LLVM maintainers to add `--update-section` to llvm-objcopy, as it's required for the kernel symbol map.
set(CMAKE_OBJCOPY ${TOOLCHAIN_ROOT}/binutils/bin/${SERENITY_ARCH}-pc-serenity-objcopy)
else()
set(TOOLCHAIN_ROOT ${CMAKE_SOURCE_DIR}/Toolchain/Local/${SERENITY_ARCH}/)
set(TOOLCHAIN_PATH ${TOOLCHAIN_ROOT}/bin)
@ -219,11 +244,9 @@ add_compile_options(-Wextra)
# The following warnings are sorted by the "base" name (the part excluding the initial Wno or W).
add_compile_options(-Wno-address-of-packed-member)
add_compile_options(-Wcast-align)
add_compile_options(-Wcast-qual)
add_compile_options(-Wno-deprecated-copy)
add_compile_options(-Wduplicated-cond)
add_compile_options(-Wdouble-promotion)
add_compile_options(-Wno-expansion-to-defined)
add_compile_options(-Wformat=2)
add_compile_options(-Wimplicit-fallthrough)
@ -235,16 +258,52 @@ add_compile_options(-Wnon-virtual-dtor)
add_compile_options(-Wno-unknown-warning-option)
add_compile_options(-Wundef)
add_compile_options(-Wunused)
add_compile_options(-Wno-unused-private-field)
add_compile_options(-Wno-unused-const-variable)
add_compile_options(-Wno-unused-command-line-argument)
add_compile_options(-Wwrite-strings)
add_compile_options(-Wno-maybe-uninitialized)
add_compile_options(-ffile-prefix-map=${CMAKE_SOURCE_DIR}=.)
add_compile_options(-fno-exceptions)
add_compile_options(-ftls-model=initial-exec)
add_compile_options(-fno-semantic-interposition)
add_compile_options(-fstack-clash-protection)
add_compile_options(-fstack-protector-strong)
add_compile_options(-g1)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-fstack-clash-protection)
if (USE_CLANG_TOOLCHAIN)
add_compile_options(-Wno-atomic-alignment)
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-tautological-constant-out-of-range-compare)
add_compile_options(-Wno-unneeded-internal-declaration)
add_compile_options(-Wno-unused-function)
add_compile_options(-Wno-user-defined-literals)
# Without the 'SHELL' prefix, this would get removed through de-duplication with the flags set for the host compiler.
# Then, that would come before '-Wextra', so it would not negate the '-Woverloaded-virtual' set by '-Wextra'.
add_compile_options(SHELL:-Wno-overloaded-virtual)
add_compile_options(--sysroot=${CMAKE_BINARY_DIR}/Root)
add_compile_options(--target=${SERENITY_CLANG_ARCH}-pc-serenity)
add_compile_options(-fno-aligned-allocation)
add_compile_options(-fconstexpr-steps=16777216)
add_compile_options(-gdwarf-4)
# FIXME: Why is Clang not picking up this path?
link_directories(${TOOLCHAIN_ROOT}/lib/clang/${LLVM_VERSION}/lib/serenity)
add_link_options(LINKER:--allow-shlib-undefined)
else()
add_compile_options(-Wcast-align)
add_compile_options(-Wdouble-promotion)
endif()
if("${SERENITY_ARCH}" STREQUAL "i686")
add_compile_options(-march=i686)
else()
add_compile_options(-march=x86-64)
endif()
add_compile_definitions(SANITIZE_PTRS)
@ -256,6 +315,7 @@ if (ENABLE_COMPILETIME_FORMAT_CHECK)
endif()
add_link_options(--sysroot ${CMAKE_BINARY_DIR}/Root)
add_link_options(-Wno-unused-command-line-argument)
include_directories(Userland/Libraries/LibC)
include_directories(Userland/Libraries/LibCrypt)