File: UseFastestLinker.cmake

package info (click to toggle)
ccache 4.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,192 kB
  • sloc: cpp: 47,336; asm: 28,570; sh: 8,709; ansic: 5,357; python: 685; perl: 68; makefile: 23
file content (78 lines) | stat: -rw-r--r-- 2,261 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
if(NOT CCACHE_DEV_MODE)
  # For ccache, using a faster linker is in practice only relevant to reduce the
  # compile-link-test cycle for developers, so use the standard linker for
  # non-developer builds.
  return()
endif()

if(DISABLE_FASTEST_LINKER)
  message(STATUS "Not probing for fastest linker")
  return()
endif()

if(MSVC)
  message(STATUS "Using standard linker for MSVC")
  return()
endif()

if(ENABLE_IPO)
  message(STATUS "Using standard linker for IPO")
  return()
endif()

if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
  # Be conservative and only probe for a faster linker on platforms that likely
  # don't have toolchain bugs. See for example
  # <https://www.sourceware.org/bugzilla/show_bug.cgi?id=22838>.
  message(STATUS "Not probing for faster linker on ${CMAKE_SYSTEM_PROCESSOR}")
  return()
endif()

function(check_linker linker)
  string(TOUPPER ${linker} upper_linker)
  file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakefiles/CMakeTmp/main.c" "int main() { return 0; }")
  try_compile(
    HAVE_LD_${upper_linker}
    ${CMAKE_CURRENT_BINARY_DIR}
    "${CMAKE_CURRENT_BINARY_DIR}/CMakefiles/CMakeTmp/main.c"
    LINK_LIBRARIES "-fuse-ld=${linker}"
  )
endfunction()

function(use_fastest_linker)
  if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
    message(WARNING "use_fastest_linker() disabled, as it is not called at the project top level")
    return()
  endif()

  # prefer an lld that matches the clang version
  if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_VERSION MATCHES "^([0-9]*)\\.")
    check_linker(lld-${CMAKE_MATCH_1})
    if(HAVE_LD_LLD-${CMAKE_MATCH_1})
      link_libraries("-fuse-ld=lld-${CMAKE_MATCH_1}")
      message(STATUS "Using lld-${CMAKE_MATCH_1} linker")
      return()
    endif()
  endif()

  check_linker(lld)
  if(HAVE_LD_LLD)
    link_libraries("-fuse-ld=lld")
    message(STATUS "Using lld linker")
    return()
  endif()

  check_linker(gold)
  if(HAVE_LD_GOLD)
    link_libraries("-fuse-ld=gold")
    message(STATUS "Using gold linker")
    return()
  endif()

  message(STATUS "Using default linker")
endfunction()

option(USE_FASTER_LINKER "Use the lld or gold linker instead of the default for faster linking" TRUE)
if(USE_FASTER_LINKER)
  use_fastest_linker()
endif()