|
| 1 | +#[[ |
| 2 | + This module conditionally enables -fuse-ld=lld if it is supported by the compiler. |
| 3 | + This is purely for build performance, and has no apparent effect on the generated |
| 4 | + code. |
| 5 | +
|
| 6 | + LLD is *significantly* faster to link and produces significantly better link-time |
| 7 | + error diagnostics. |
| 8 | +
|
| 9 | + LLD linking will be automatically enabled if it is supported, but can be forced |
| 10 | + on or off by setting the MONGO_USE_LLD CMake option to ON or OFF. |
| 11 | +]] |
| 12 | + |
| 13 | +if (NOT COMMAND add_link_options) |
| 14 | + # This only works on new-enough versions of CMake that support LINK_OPTIONS as |
| 15 | + # a separate configuration entity |
| 16 | + return () |
| 17 | +endif () |
| 18 | + |
| 19 | +include (CMakePushCheckState) |
| 20 | +include (CheckCSourceCompiles) |
| 21 | +cmake_push_check_state (RESET) |
| 22 | + # Newer GNU compilers support lld with the '-fuse-ld=lld' flag. |
| 23 | + set (CMAKE_REQUIRED_FLAGS "-fuse-ld=lld") |
| 24 | + set (CMAKE_REQUIRED_LINK_OPTIONS "-fuse-ld=lld") |
| 25 | + check_c_source_compiles ([[ |
| 26 | + #include <stdio.h> |
| 27 | +
|
| 28 | + int main (void) { |
| 29 | + puts ("Hello, world!"); |
| 30 | + return 0; |
| 31 | + } |
| 32 | + ]] HAVE_LLD_LINKER_SUPPORT) |
| 33 | +cmake_pop_check_state () |
| 34 | + |
| 35 | + |
| 36 | +if (HAVE_LLD_LINKER_SUPPORT) |
| 37 | + # Expose an option to toggle usage of lld |
| 38 | + option (MONGO_USE_LLD "Link runtime binaries using LLVM's lld linker" ON) |
| 39 | +elseif (NOT DEFINED _MONGO_LD_LLD_LINKER) |
| 40 | + # We don't have -fuse-lld support, but that might be because of a misconfig in |
| 41 | + # the environment. Issue a *one-time* diagnostic telling the user if they *almost* |
| 42 | + # have LLD support. This branch is only for diagnostic purposes. |
| 43 | + find_program (_MONGO_LD_LLD_LINKER |
| 44 | + NAMES ld.lld ld.lld-13 ld.lld-12 ld.lld-11 ld.lld-10 ld.lld-9 ld.lld-8 ld.lld-7 ld.lld-6 ld.lld-5 |
| 45 | + ) |
| 46 | + mark_as_advanced (_MONGO_LD_LLD_LINKER) |
| 47 | + # If we found one, we are compiling with GCC, *and* the found lld has a version suffix, issue a message |
| 48 | + # telling the user how they might be able to get lld work. |
| 49 | + if (_MONGO_LD_LLD_LINKER AND CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT _MONGO_LD_LLD_LINKER MATCHES "ld.lld$") |
| 50 | + message (STATUS "NOTE: A GNU-compatible lld linker was found (${_MONGO_LD_LLD_LINKER}), but support from GCC requires that") |
| 51 | + message (STATUS " the 'ld.lld' binary be named *exactly* 'ld.lld' (without a version suffix!)") |
| 52 | + message (STATUS " To enable 'lld' support, try creating a symlink of 'ld.lld' somewhere on your PATH that points") |
| 53 | + message (STATUS " to '${_MONGO_LD_LLD_LINKER}'") |
| 54 | + endif () |
| 55 | +endif () |
| 56 | + |
| 57 | +if (MONGO_USE_LLD) |
| 58 | + message (STATUS "Linking using LLVM lld. Disable by setting MONGO_USE_LLD to OFF") |
| 59 | + add_link_options (-fuse-ld=lld) |
| 60 | +endif () |
0 commit comments