Skip to content

Commit 583213d

Browse files
Automatically compile with CCache and link using LLD if possible (#842)
* Automatically compile with CCache and link using LLD if possible
1 parent 3c6d9a8 commit 583213d

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ option (ENABLE_RDTSCP
4141
"Fast performance counters on Intel using the RDTSCP instruction"
4242
OFF
4343
)
44-
option (ENABLE_APPLE_FRAMEWORK "Build libraries as frameworks on darwin platforms" OFF)
44+
option (ENABLE_APPLE_FRAMEWORK "Build libraries as frameworks on darwin platforms" OFF)
4545
set (ENABLE_ICU AUTO CACHE STRING "Enable ICU support, necessary to use non-ASCII usernames or passwords, default AUTO.")
4646
option (ENABLE_UNINSTALL "Enable creation of uninstall script and associated uninstall build target." ON)
4747
set (ENABLE_CLIENT_SIDE_ENCRYPTION AUTO CACHE STRING "Enable Client-Side Field Level Encryption support. Requires libmongocrypt. Set to ON/AUTO/OFF, default AUTO.")
4848
set (ENABLE_MONGODB_AWS_AUTH AUTO CACHE STRING "Enable support for MONGODB-AWS authentication mechanism. Set to ON/AUTO/OFF, default AUTO. (also requires ENABLE_SSL not set to OFF)")
49-
option (ENABLE_PIC
49+
option (ENABLE_PIC
5050
"Enables building of position independent code for static library components."
5151
ON
5252
)
@@ -70,6 +70,11 @@ set (CMAKE_MODULE_PATH
7070
include (InstallRequiredSystemLibraries)
7171
include (GNUInstallDirs)
7272

73+
# Enable CCache, if possible
74+
include (CCache)
75+
# Link with LLD, if possible
76+
include (LLDLinker)
77+
7378
set (BUILD_VERSION "0.0.0" CACHE STRING "Library version (for both libbson and libmongoc)")
7479

7580
include (ParseVersion)

build/cmake/CCache.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[[
2+
This module enables CCache support by inserting a ccache executable as
3+
the compiler launcher for C and C++ if there is a ccache executable availbale
4+
on the system.
5+
6+
CCache support will be automatically enabled if it is found on the system.
7+
CCache can be forced on or off by setting the MONGO_USE_CCACHE CMake option to
8+
ON or OFF.
9+
]]
10+
11+
# Find and enable ccache for compiling
12+
find_program (CCACHE_EXECUTABLE ccache)
13+
if (CCACHE_EXECUTABLE)
14+
message (STATUS "Found ccache: ${CCACHE_EXECUTABLE}")
15+
option (MONGO_USE_CCACHE "Use CCache when compiling" ON)
16+
endif ()
17+
18+
if (MONGO_USE_CCACHE)
19+
message (STATUS "Compiling with CCache enabled. Disable by setting MONGO_USE_CCACHE to OFF")
20+
set (CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}")
21+
set (CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}")
22+
endif ()

build/cmake/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set (build_cmake_MODULES
1111
MongoCPackage.cmake
1212
ParseVersion.cmake
1313
SphinxBuild.cmake
14+
CCache.cmake
15+
LLDLinker.cmake
1416
)
1517

1618
set_local_dist (build_cmake_DIST_local

build/cmake/LLDLinker.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)