From dac443fbff0b47fc34470c4074fb372a5668616c Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Tue, 5 Sep 2023 07:40:57 +0200 Subject: [PATCH] Meta: Link Lagom with LLD by default and allow configuring the linker This ports over the `LADYBIRD_USE_LLD` option from the standalone Ladybird build and generalizes it to work for mold as well: the `LAGOM_USE_LINKER` variable can be set to the desired name of the linker. If it's empty, we default to trying LLD and Mold on ELF platforms (in this order). --- Ladybird/CMakeLists.txt | 2 +- Ladybird/cmake/EnableLLD.cmake | 17 ----------------- Meta/CMake/lagom_compile_options.cmake | 12 ++++++++++++ Meta/CMake/lagom_options.cmake | 2 +- Meta/CMake/use_linker.cmake | 26 ++++++++++++++++++++++++++ Meta/Lagom/CMakeLists.txt | 10 ++++++---- 6 files changed, 46 insertions(+), 23 deletions(-) delete mode 100644 Ladybird/cmake/EnableLLD.cmake create mode 100644 Meta/CMake/use_linker.cmake diff --git a/Ladybird/CMakeLists.txt b/Ladybird/CMakeLists.txt index 3fbfc4e4c09..508b7ce4a63 100644 --- a/Ladybird/CMakeLists.txt +++ b/Ladybird/CMakeLists.txt @@ -27,7 +27,7 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -include(cmake/EnableLLD.cmake) +include(use_linker) if (ENABLE_ADDRESS_SANITIZER) add_compile_options(-fsanitize=address -fno-omit-frame-pointer) diff --git a/Ladybird/cmake/EnableLLD.cmake b/Ladybird/cmake/EnableLLD.cmake deleted file mode 100644 index ce0f107e004..00000000000 --- a/Ladybird/cmake/EnableLLD.cmake +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (c) 2022, Andrew Kaster -# -# SPDX-License-Identifier: BSD-2-Clause -# -option(LADYBIRD_USE_LLD "Use llvm lld to link application" ON) -if (LADYBIRD_USE_LLD AND NOT APPLE) - find_program(LLD_LINKER NAMES "ld.lld") - if (NOT LLD_LINKER) - message(INFO "LLD not found, cannot use to link. Disabling option...") - set(LADYBIRD_USE_LLD OFF CACHE BOOL "" FORCE) - endif() -endif() -if (LADYBIRD_USE_LLD AND NOT APPLE) - add_link_options(-fuse-ld=lld) - add_compile_options(-ggnu-pubnames) - add_link_options(LINKER:--gdb-index) -endif() diff --git a/Meta/CMake/lagom_compile_options.cmake b/Meta/CMake/lagom_compile_options.cmake index 7be594b6bd5..3c5d17935fd 100644 --- a/Meta/CMake/lagom_compile_options.cmake +++ b/Meta/CMake/lagom_compile_options.cmake @@ -4,7 +4,19 @@ add_compile_options(-Wno-maybe-uninitialized) add_compile_options(-Wno-shorten-64-to-32) add_compile_options(-fsigned-char) add_compile_options(-g1) +add_compile_options(-ggnu-pubnames) add_compile_options(-O2) if (NOT WIN32) add_compile_options(-fPIC) endif() + +function(add_linker_flag_if_supported flag) + include(CheckLinkerFlag) + + check_linker_flag(CXX ${flag} LAGOM_LINKER_SUPPORTS_${flag}) + if (${LAGOM_LINKER_SUPPORTS_${flag}}) + add_link_options(${flag}) + endif() +endfunction() + +add_linker_flag_if_supported(LINKER:--gdb-index) diff --git a/Meta/CMake/lagom_options.cmake b/Meta/CMake/lagom_options.cmake index 327b9cceaa4..02496f6758d 100644 --- a/Meta/CMake/lagom_options.cmake +++ b/Meta/CMake/lagom_options.cmake @@ -12,6 +12,6 @@ serenity_option(ENABLE_FUZZERS_LIBFUZZER OFF CACHE BOOL "Build fuzzers using Cla serenity_option(ENABLE_FUZZERS_OSSFUZZ OFF CACHE BOOL "Build OSS-Fuzz compatible fuzzers") serenity_option(BUILD_LAGOM OFF CACHE BOOL "Build parts of the system targeting the host OS for fuzzing/testing") serenity_option(ENABLE_LAGOM_CCACHE ON CACHE BOOL "Enable ccache for Lagom builds") -serenity_option(ENABLE_LAGOM_MOLD OFF CACHE BOOL "Enable mold for Lagom builds") serenity_option(ENABLE_LAGOM_LIBWEB ON CACHE BOOL "Enable compiling LibWeb for Lagom builds") serenity_option(ENABLE_LAGOM_LADYBIRD OFF CACHE BOOL "Enable compiling Ladybird from Lagom") +serenity_option(LAGOM_USE_LINKER "" CACHE STRING "The linker to use (e.g. lld, mold) instead of the system default") diff --git a/Meta/CMake/use_linker.cmake b/Meta/CMake/use_linker.cmake new file mode 100644 index 00000000000..6d3000b286a --- /dev/null +++ b/Meta/CMake/use_linker.cmake @@ -0,0 +1,26 @@ +# Copyright (c) 2022, Andrew Kaster +# Copyright (c) 2023, Daniel Bertalan +# +# SPDX-License-Identifier: BSD-2-Clause +# + +if (NOT APPLE AND "${LAGOM_USE_LINKER}" STREQUAL "") + find_program(LLD_LINKER NAMES "ld.lld") + if (LLD_LINKER) + message("Using LLD to link Lagom.") + set(LAGOM_USE_LINKER "lld" CACHE STRING "") + else() + find_program(MOLD_LINKER NAMES "ld.mold") + if (MOLD_LINKER) + message("Using mold to link Lagom.") + set(LAGOM_USE_LINKER "mold" CACHE STRING "") + endif() + endif() +endif() + +if (NOT "${LAGOM_USE_LINKER}" STREQUAL "") + set(LINKER_FLAG "-fuse-ld=${LAGOM_USE_LINKER}") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAG}") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAG}") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAG}") +endif() diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index 5f2690199ed..28da24c69ee 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -19,6 +19,11 @@ endif() # https://cmake.org/cmake/help/latest/policy/CMP0058.html cmake_policy(SET CMP0058 NEW) +# Make CMAKE_EXE_LINKER_FLAGS have an effect on `try_compile()` jobs. +# This is required if we want to have the `LAGOM_USE_LINKER` option +# take effect in `check_linker_flag` checks. +cmake_policy(SET CMP0056 NEW) + get_filename_component( SERENITY_PROJECT_ROOT "${PROJECT_SOURCE_DIR}/../.." ABSOLUTE CACHE @@ -34,6 +39,7 @@ if(NOT COMMAND serenity_option) endif() include(check_for_dependencies) +include(use_linker) include(lagom_options NO_POLICY_SCOPE) if(ENABLE_ALL_THE_DEBUG_MACROS) @@ -106,10 +112,6 @@ if (ENABLE_COMPILETIME_FORMAT_CHECK) add_compile_definitions(ENABLE_COMPILETIME_FORMAT_CHECK) endif() -if (ENABLE_LAGOM_MOLD) - add_link_options(-fuse-ld=mold) -endif() - if (ENABLE_FUZZERS) add_compile_options(-fno-omit-frame-pointer) endif()