Skip to content

Commit c45f018

Browse files
LebedevRIdominichamon
authored andcommitted
CMake: implement LTO for clang. Fixes google#478 (google#487)
* CMake: implement LTO for clang. Fixes google#478 * LTO: add basic docs about required executables.
1 parent eae4221 commit c45f018

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
1919
option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
2020

2121
# Make sure we can import out CMake functions
22+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
2223
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
2324

2425
# Read the git tags to determine the project version
@@ -132,6 +133,8 @@ else()
132133
if (GCC_RANLIB)
133134
set(CMAKE_RANLIB ${GCC_RANLIB})
134135
endif()
136+
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
137+
include(llvm-toolchain)
135138
endif()
136139
endif()
137140

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,9 @@ To enable link-time optimisation, use
848848
cmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_LTO=true
849849
```
850850

851+
If you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake cache variables, if autodetection fails.
852+
If you are using clang, you may need to set `LLVMAR_EXECUTABLE`, `LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables.
853+
851854
## Linking against the library
852855
When using gcc, it is necessary to link against pthread to avoid runtime exceptions.
853856
This is due to how gcc implements std::thread.

cmake/Modules/FindLLVMAr.cmake

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include(FeatureSummary)
2+
3+
find_program(LLVMAR_EXECUTABLE
4+
NAMES llvm-ar
5+
DOC "The llvm-ar executable"
6+
)
7+
8+
include(FindPackageHandleStandardArgs)
9+
find_package_handle_standard_args(LLVMAr
10+
DEFAULT_MSG
11+
LLVMAR_EXECUTABLE)
12+
13+
SET_PACKAGE_PROPERTIES(LLVMAr PROPERTIES
14+
URL https://llvm.org/docs/CommandGuide/llvm-ar.html
15+
DESCRIPTION "create, modify, and extract from archives"
16+
)

cmake/Modules/FindLLVMNm.cmake

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include(FeatureSummary)
2+
3+
find_program(LLVMNM_EXECUTABLE
4+
NAMES llvm-nm
5+
DOC "The llvm-nm executable"
6+
)
7+
8+
include(FindPackageHandleStandardArgs)
9+
find_package_handle_standard_args(LLVMNm
10+
DEFAULT_MSG
11+
LLVMNM_EXECUTABLE)
12+
13+
SET_PACKAGE_PROPERTIES(LLVMNm PROPERTIES
14+
URL https://llvm.org/docs/CommandGuide/llvm-nm.html
15+
DESCRIPTION "list LLVM bitcode and object file’s symbol table"
16+
)

cmake/Modules/FindLLVMRanLib.cmake

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include(FeatureSummary)
2+
3+
find_program(LLVMRANLIB_EXECUTABLE
4+
NAMES llvm-ranlib
5+
DOC "The llvm-ranlib executable"
6+
)
7+
8+
include(FindPackageHandleStandardArgs)
9+
find_package_handle_standard_args(LLVMRanLib
10+
DEFAULT_MSG
11+
LLVMRANLIB_EXECUTABLE)
12+
13+
SET_PACKAGE_PROPERTIES(LLVMRanLib PROPERTIES
14+
DESCRIPTION "generate index for LLVM archive"
15+
)

cmake/llvm-toolchain.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
find_package(LLVMAr REQUIRED)
2+
set(CMAKE_AR "${LLVMAR_EXECUTABLE}" CACHE FILEPATH "" FORCE)
3+
4+
find_package(LLVMNm REQUIRED)
5+
set(CMAKE_NM "${LLVMNM_EXECUTABLE}" CACHE FILEPATH "" FORCE)
6+
7+
find_package(LLVMRanLib REQUIRED)
8+
set(CMAKE_RANLIB "${LLVMRANLIB_EXECUTABLE}" CACHE FILEPATH "" FORCE)

0 commit comments

Comments
 (0)