1 include(ExternalProject)
3 set(BOLT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
4 set(BOLT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
5 set(CMAKE_CXX_STANDARD 17)
7 # Add path for custom modules.
8 list(INSERT CMAKE_MODULE_PATH 0 "${BOLT_SOURCE_DIR}/cmake/modules")
10 # Determine default set of targets to build -- the intersection of
11 # those BOLT supports and those LLVM is targeting.
12 set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
13 set(BOLT_TARGETS_TO_BUILD_default)
14 foreach (tgt ${BOLT_TARGETS_TO_BUILD_all})
15 if (tgt IN_LIST LLVM_TARGETS_TO_BUILD)
16 list(APPEND BOLT_TARGETS_TO_BUILD_default ${tgt})
20 # Allow the user to specify the BOLT targets, and then check that LLVM
21 # is indeed targeting those.
22 set(BOLT_TARGETS_TO_BUILD "${BOLT_TARGETS_TO_BUILD_default}"
23 CACHE STRING "Targets for BOLT to support.")
24 if (NOT BOLT_TARGETS_TO_BUILD)
25 message(FATAL_ERROR "BOLT enabled but BOLT_TARGETS_TO_BUILD is empty")
27 foreach (tgt ${BOLT_TARGETS_TO_BUILD})
28 if (NOT tgt IN_LIST LLVM_TARGETS_TO_BUILD)
29 message(FATAL_ERROR "BOLT target '${tgt}' is not in LLVM_TARGETS_TO_BUILD")
31 message(STATUS "Targeting ${tgt} in llvm-bolt")
34 set(BOLT_ENABLE_RUNTIME_default OFF)
35 if ((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
36 OR CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
37 AND (CMAKE_SYSTEM_NAME STREQUAL "Linux"
38 OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
39 AND (NOT CMAKE_CROSSCOMPILING))
40 set(BOLT_ENABLE_RUNTIME_default ON)
42 option(BOLT_ENABLE_RUNTIME "Enable BOLT runtime" ${BOLT_ENABLE_RUNTIME_default})
43 if (BOLT_ENABLE_RUNTIME)
44 # Some systems prevent reading /proc/self/map_files
45 execute_process(COMMAND ls /proc/self/map_files
46 RESULT_VARIABLE LS OUTPUT_QUIET ERROR_QUIET)
48 set(BOLT_ENABLE_RUNTIME OFF)
50 "BOLT runtime is disabled as /proc/self/map_files is unreadable.")
54 set(BOLT_CLANG_EXE "" CACHE FILEPATH "Path to clang executable for the target \
55 architecture for use in BOLT tests")
56 set(BOLT_LLD_EXE "" CACHE FILEPATH "Path to lld executable for the target \
57 architecture for use in BOLT tests")
59 set(BOLT_INCLUDE_TESTS OFF)
60 if (LLVM_INCLUDE_TESTS)
61 set(BOLT_CLANG_PRESENT OFF)
62 set(BOLT_LLD_PRESENT OFF)
64 if ("clang" IN_LIST LLVM_ENABLE_PROJECTS AND BOLT_CLANG_EXE)
65 message(WARNING "BOLT_CLANG_EXE is set and clang project is enabled. \
66 BOLT_CLANG_EXE will be used for BOLT tests.")
68 if ("clang" IN_LIST LLVM_ENABLE_PROJECTS OR BOLT_CLANG_EXE)
69 set(BOLT_CLANG_PRESENT ON)
72 if ("lld" IN_LIST LLVM_ENABLE_PROJECTS AND BOLT_LLD_EXE)
73 message(WARNING "BOLT_LLD_EXE is set and lld project is enabled. \
74 BOLT_LLD_EXE will be used for BOLT tests.")
76 if ("lld" IN_LIST LLVM_ENABLE_PROJECTS OR BOLT_LLD_EXE)
77 set(BOLT_LLD_PRESENT ON)
80 if (BOLT_CLANG_PRESENT AND BOLT_LLD_PRESENT)
81 set(BOLT_INCLUDE_TESTS ON)
83 message(WARNING "Not including BOLT tests since clang or lld is disabled. \
84 Add clang and lld to LLVM_ENABLE_PROJECTS or provide paths to clang \
85 and lld binaries in BOLT_CLANG_EXE and BOLT_LLD_EXE.")
89 if (BOLT_ENABLE_RUNTIME)
90 message(STATUS "Building BOLT runtime libraries for X86")
93 list(APPEND extra_args -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
95 ExternalProject_Add(bolt_rt
96 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/runtime"
97 STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-stamps
98 BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins
99 CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
100 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
101 -DCMAKE_BUILD_TYPE=Release
102 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
103 -DLLVM_LIBDIR_SUFFIX=${LLVM_LIBDIR_SUFFIX}
104 -DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR}
109 install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/cmake_install.cmake \)"
111 add_llvm_install_targets(install-bolt_rt
116 find_program(GNU_LD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.bfd ld.bfd DOC "GNU ld")
120 option(BOLT_BUILD_TOOLS
121 "Build the BOLT tools. If OFF, just generate build targets." ON)
123 add_custom_target(bolt)
124 set_target_properties(bolt PROPERTIES FOLDER "BOLT")
125 add_llvm_install_targets(install-bolt DEPENDS bolt COMPONENT bolt)
128 ${CMAKE_CURRENT_SOURCE_DIR}/include
129 ${CMAKE_CURRENT_BINARY_DIR}/include
132 add_subdirectory(lib)
133 add_subdirectory(tools)
135 if (BOLT_INCLUDE_TESTS)
136 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
137 add_subdirectory(unittests)
138 list(APPEND BOLT_TEST_DEPS BoltUnitTests)
140 add_subdirectory(test)
143 option(BOLT_INCLUDE_DOCS "Generate build targets for the BOLT docs."
144 ${LLVM_INCLUDE_DOCS})
145 if (BOLT_INCLUDE_DOCS)
146 add_subdirectory(docs)
149 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc.in
150 ${CMAKE_CURRENT_BINARY_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc @ONLY)