1 set(LLVM_SUBPROJECT_TITLE "BOLT")
3 include(ExternalProject)
5 set(BOLT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
6 set(BOLT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
7 set(CMAKE_CXX_STANDARD 17)
9 # Add path for custom modules.
10 list(INSERT CMAKE_MODULE_PATH 0 "${BOLT_SOURCE_DIR}/cmake/modules")
12 # Determine default set of targets to build -- the intersection of
13 # those BOLT supports and those LLVM is targeting.
14 set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
15 set(BOLT_TARGETS_TO_BUILD_default)
16 foreach (tgt ${BOLT_TARGETS_TO_BUILD_all})
17 if (tgt IN_LIST LLVM_TARGETS_TO_BUILD)
18 list(APPEND BOLT_TARGETS_TO_BUILD_default ${tgt})
22 # Allow the user to specify the BOLT targets, and then check that LLVM
23 # is indeed targeting those.
24 set(BOLT_TARGETS_TO_BUILD "${BOLT_TARGETS_TO_BUILD_default}"
25 CACHE STRING "Targets for BOLT to support.")
26 if (NOT BOLT_TARGETS_TO_BUILD)
27 message(FATAL_ERROR "BOLT enabled but BOLT_TARGETS_TO_BUILD is empty")
29 foreach (tgt ${BOLT_TARGETS_TO_BUILD})
30 if (NOT tgt IN_LIST LLVM_TARGETS_TO_BUILD)
31 message(FATAL_ERROR "BOLT target '${tgt}' is not in LLVM_TARGETS_TO_BUILD")
33 message(STATUS "Targeting ${tgt} in llvm-bolt")
36 set(BOLT_ENABLE_RUNTIME_default OFF)
37 if ((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
38 OR CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
39 AND (CMAKE_SYSTEM_NAME STREQUAL "Linux"
40 OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
41 AND (NOT CMAKE_CROSSCOMPILING))
42 set(BOLT_ENABLE_RUNTIME_default ON)
44 option(BOLT_ENABLE_RUNTIME "Enable BOLT runtime" ${BOLT_ENABLE_RUNTIME_default})
45 if (BOLT_ENABLE_RUNTIME)
46 # Some systems prevent reading /proc/self/map_files
47 execute_process(COMMAND ls /proc/self/map_files
48 RESULT_VARIABLE LS OUTPUT_QUIET ERROR_QUIET)
51 "BOLT runtime may not be able to read /proc/self/map_files. Please use
52 `--instrumentation-binpath <path-to-instrumented-binary>` option.")
56 set(BOLT_CLANG_EXE "" CACHE FILEPATH "Path to clang executable for the target \
57 architecture for use in BOLT tests")
58 set(BOLT_LLD_EXE "" CACHE FILEPATH "Path to lld executable for the target \
59 architecture for use in BOLT tests")
61 set(BOLT_INCLUDE_TESTS OFF)
62 if (LLVM_INCLUDE_TESTS)
63 set(BOLT_CLANG_PRESENT OFF)
64 set(BOLT_LLD_PRESENT OFF)
66 if ("clang" IN_LIST LLVM_ENABLE_PROJECTS AND BOLT_CLANG_EXE)
67 message(WARNING "BOLT_CLANG_EXE is set and clang project is enabled. \
68 BOLT_CLANG_EXE will be used for BOLT tests.")
70 if ("clang" IN_LIST LLVM_ENABLE_PROJECTS OR BOLT_CLANG_EXE)
71 set(BOLT_CLANG_PRESENT ON)
74 if ("lld" IN_LIST LLVM_ENABLE_PROJECTS AND BOLT_LLD_EXE)
75 message(WARNING "BOLT_LLD_EXE is set and lld project is enabled. \
76 BOLT_LLD_EXE will be used for BOLT tests.")
78 if ("lld" IN_LIST LLVM_ENABLE_PROJECTS OR BOLT_LLD_EXE)
79 set(BOLT_LLD_PRESENT ON)
82 if (BOLT_CLANG_PRESENT AND BOLT_LLD_PRESENT)
83 set(BOLT_INCLUDE_TESTS ON)
85 message(WARNING "Not including BOLT tests since clang or lld is disabled. \
86 Add clang and lld to LLVM_ENABLE_PROJECTS or provide paths to clang \
87 and lld binaries in BOLT_CLANG_EXE and BOLT_LLD_EXE.")
91 if (BOLT_ENABLE_RUNTIME)
92 message(STATUS "Building BOLT runtime libraries for X86")
95 list(APPEND extra_args -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
97 ExternalProject_Add(bolt_rt
98 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/runtime"
99 STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-stamps
100 BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins
101 CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
102 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
103 -DCMAKE_BUILD_TYPE=Release
104 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
105 -DLLVM_LIBDIR_SUFFIX=${LLVM_LIBDIR_SUFFIX}
106 -DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR}
111 install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/cmake_install.cmake \)"
113 add_llvm_install_targets(install-bolt_rt
118 find_program(GNU_LD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.bfd ld.bfd DOC "GNU ld")
122 option(BOLT_BUILD_TOOLS
123 "Build the BOLT tools. If OFF, just generate build targets." ON)
125 add_custom_target(bolt)
126 set_target_properties(bolt PROPERTIES FOLDER "BOLT/Metatargets")
127 add_llvm_install_targets(install-bolt DEPENDS bolt COMPONENT bolt)
130 ${CMAKE_CURRENT_SOURCE_DIR}/include
131 ${CMAKE_CURRENT_BINARY_DIR}/include
134 add_subdirectory(lib)
135 add_subdirectory(tools)
137 if (BOLT_INCLUDE_TESTS)
138 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
139 add_subdirectory(unittests)
140 list(APPEND BOLT_TEST_DEPS BoltUnitTests)
142 add_subdirectory(test)
145 option(BOLT_INCLUDE_DOCS "Generate build targets for the BOLT docs."
146 ${LLVM_INCLUDE_DOCS})
147 if (BOLT_INCLUDE_DOCS)
148 add_subdirectory(docs)
151 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc.in
152 ${CMAKE_CURRENT_BINARY_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc @ONLY)