Skip to content

Commit

Permalink
CMake: Add support building Qt with the 'mold' linker
Browse files Browse the repository at this point in the history
The mold linker is a new linker for Linux that provides faster link
times compared to BFD ld, ld.gold and lld.

It can be found at https://github.com/rui314/mold

To build Qt with mold, ensure that the binary in your PATH and then
configure Qt with with either

 cmake /path/to/qtbase -DINPUT_linker=mold

or

 /path/to/qtbase/configure --linker mold

The change was tested with gcc 9, clang 10, clang 12, mold
1.0.0. Only qtbase and qtdeclarative (and dependencies) were tested.

Pick-to: 6.2 6.3
Task-number: QTBUG-99270
Change-Id: I2e64a1f4257c37ff5b64a9326e548b9b46e07c80
Reviewed-by: Fabian Kosmale <[email protected]>
  • Loading branch information
alcroito committed Jan 20, 2022
1 parent f037148 commit 158287c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
59 changes: 59 additions & 0 deletions cmake/QtFeature.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,62 @@ function(qt_config_compiler_supports_flag_test name)
set(TEST_${name} "${TEST_${name}}" CACHE INTERNAL "${label}")
endfunction()

# gcc expects -fuse-ld=mold (no absolute path can be given) (gcc >= 12.1)
# or an 'ld' symlink to 'mold' in a dir that is passed via -B flag (gcc < 12.1)
#
# clang expects -fuse-ld=mold
# or -fuse-ld=<mold-abs-path>
# or --ldpath=<mold-abs-path> (clang >= 12)
# https://github.com/rui314/mold/#how-to-use
# TODO: In the gcc < 12.1 case, the qt_internal_check_if_linker_is_available(mold) check will
# always return TRUE because gcc will not error out if it is given a -B flag pointing to an
# invalid dir, as well as when the the symlink to the linker in the -B dir is not actually
# a valid linker.
# It would be nice to handle that case in a better way, but it's not that important
# given that gcc > 12.1 now supports -fuse-ld=mold
# NOTE: In comparison to clang, in the gcc < 12.1 case, we pass the full path to where mold is
# and that is recorded in PlatformCommonInternal's INTERFACE_LINK_OPTIONS target.
# Moving such a Qt to a different machine and trying to build another repo won't
# work because the recorded path will be invalid. This is not a problem with
# the gcc >= 12.1 case
function(qt_internal_get_mold_linker_flags out_var)
cmake_parse_arguments(PARSE_ARGV 1 arg "ERROR_IF_EMPTY" "" "")

find_program(QT_INTERNAL_LINKER_MOLD mold)

set(flag "")
if(QT_INTERNAL_LINKER_MOLD)
if(GCC)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "12.1")
set(flag "-fuse-ld=mold")
else()
set(mold_linker_dir "${CMAKE_CURRENT_BINARY_DIR}/.qt_linker")
set(mold_linker_path "${mold_linker_dir}/ld")
if(NOT EXISTS "${mold_linker_dir}")
file(MAKE_DIRECTORY "${mold_linker_dir}")
endif()
if(NOT EXISTS "${mold_linker_path}")
file(CREATE_LINK
"${QT_INTERNAL_LINKER_MOLD}"
"${mold_linker_path}"
SYMBOLIC)
endif()
set(flag "-B${mold_linker_dir}")
endif()
elseif(CLANG)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "12")
set(flag "--ld-path=mold")
else()
set(flag "-fuse-ld=mold")
endif()
endif()
endif()
if(arg_ERROR_IS_EMPTY AND NOT flag)
message(FATAL_ERROR "Could not determine the flags to use the mold linker.")
endif()
set(${out_var} "${flag}" PARENT_SCOPE)
endfunction()

function(qt_internal_get_active_linker_flags out_var)
set(flags "")
if(GCC OR CLANG)
Expand All @@ -1127,6 +1183,9 @@ function(qt_internal_get_active_linker_flags out_var)
list(APPEND flags "-fuse-ld=bfd")
elseif(QT_FEATURE_use_lld_linker)
list(APPEND flags "-fuse-ld=lld")
elseif(QT_FEATURE_use_mold_linker)
qt_internal_get_mold_linker_flags(mold_flags ERROR_IF_EMPTY)
list(APPEND flags "${mold_flags}")
endif()
endif()
set(${out_var} "${flags}" PARENT_SCOPE)
Expand Down
30 changes: 29 additions & 1 deletion configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,26 @@ qt_internal_check_if_linker_is_available(use_lld_linker
FLAG "-fuse-ld=lld"
)

# We set an invalid flag as a default flag so the compile test fails
# in case if no mold is found in PATH.
set(__qt_internal_mold_linker_flags "-Wl,-invalid-flag")
if(NOT QT_CONFIGURE_RUNNING)
qt_internal_get_mold_linker_flags(__qt_internal_mold_linker_flags)
endif()
qt_internal_check_if_linker_is_available(use_mold_linker
LABEL "mold linker"
FLAG "${__qt_internal_mold_linker_flags}"
)
unset(__qt_internal_mold_linker_flags)

qt_feature("use_bfd_linker"
PRIVATE
LABEL "bfd"
AUTODETECT false
CONDITION NOT WIN32 AND NOT INTEGRITY AND NOT WASM AND TEST_use_bfd_linker
ENABLE INPUT_linker STREQUAL 'bfd'
DISABLE INPUT_linker STREQUAL 'gold' OR INPUT_linker STREQUAL 'lld'
OR INPUT_linker STREQUAL 'mold'
)
qt_feature_config("use_bfd_linker" QMAKE_PRIVATE_CONFIG)

Expand All @@ -60,6 +73,7 @@ qt_feature("use_gold_linker"
CONDITION NOT WIN32 AND NOT INTEGRITY AND NOT WASM AND NOT rtems AND TEST_use_gold_linker
ENABLE INPUT_linker STREQUAL 'gold' OR QT_FEATURE_use_gold_linker_alias
DISABLE INPUT_linker STREQUAL 'bfd' OR INPUT_linker STREQUAL 'lld'
OR INPUT_linker STREQUAL 'mold'
)
qt_feature_config("use_gold_linker" QMAKE_PRIVATE_CONFIG)

Expand All @@ -70,14 +84,27 @@ qt_feature("use_lld_linker"
CONDITION NOT WIN32 AND NOT INTEGRITY AND NOT WASM AND TEST_use_lld_linker
ENABLE INPUT_linker STREQUAL 'lld'
DISABLE INPUT_linker STREQUAL 'bfd' OR INPUT_linker STREQUAL 'gold'
OR INPUT_linker STREQUAL 'mold'
)
qt_feature_config("use_lld_linker" QMAKE_PRIVATE_CONFIG)

qt_feature("use_mold_linker"
PRIVATE
LABEL "mold"
AUTODETECT FALSE
CONDITION NOT WIN32 AND NOT INTEGRITY AND NOT WASM AND TEST_use_mold_linker
ENABLE INPUT_linker STREQUAL 'mold'
DISABLE INPUT_linker STREQUAL 'bfd' OR INPUT_linker STREQUAL 'gold'
OR INPUT_linker STREQUAL 'lld'
)
qt_feature_config("use_mold_linker" QMAKE_PRIVATE_CONFIG)

if(NOT QT_CONFIGURE_RUNNING)
qt_evaluate_feature(use_bfd_linker)
qt_evaluate_feature(use_gold_linker_alias)
qt_evaluate_feature(use_gold_linker)
qt_evaluate_feature(use_lld_linker)
qt_evaluate_feature(use_mold_linker)
endif()


Expand Down Expand Up @@ -1004,9 +1031,10 @@ qt_configure_add_summary_entry(
)
qt_configure_add_summary_entry(
TYPE "firstAvailableFeature"
ARGS "use_bfd_linker use_gold_linker use_lld_linker"
ARGS "use_bfd_linker use_gold_linker use_lld_linker use_mold_linker"
MESSAGE "Linker"
CONDITION QT_FEATURE_use_bfd_linker OR QT_FEATURE_use_gold_linker OR QT_FEATURE_use_lld_linker
OR QT_FEATURE_use_mold_linker
)
qt_configure_add_summary_entry(
ARGS "enable_new_dtags"
Expand Down
2 changes: 1 addition & 1 deletion qt_cmdline.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ qt_commandline_option(gui TYPE boolean)
qt_commandline_option(headersclean TYPE boolean)
qt_commandline_option(incredibuild-xge TYPE boolean NAME incredibuild_xge)
qt_commandline_option(libudev TYPE boolean)
qt_commandline_option(linker TYPE optionalString VALUES bfd gold lld)
qt_commandline_option(linker TYPE optionalString VALUES bfd gold lld mold)
qt_commandline_option(ltcg TYPE boolean)
# special case begin
qt_commandline_option(make TYPE addString VALUES examples libs tests tools
Expand Down

0 comments on commit 158287c

Please sign in to comment.