[LLD][COFF] Emit tail merge pdata for delay load thunks on ARM64EC (#116810)
[llvm-project.git] / bolt / CMakeLists.txt
blob9ac196ad0e8210077331bb715bb3f4921c8e6acd
1 cmake_minimum_required(VERSION 3.20.0)
3 set(LLVM_SUBPROJECT_TITLE "BOLT")
5 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
6   set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
7 endif()
8 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
9   NO_POLICY_SCOPE)
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12   project(bolt)
13   set(BOLT_BUILT_STANDALONE TRUE)
14 endif()
16 set(BOLT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
17 set(BOLT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
18 set(CMAKE_CXX_STANDARD 17)
20 # Add path for custom modules.
21 list(INSERT CMAKE_MODULE_PATH 0 "${BOLT_SOURCE_DIR}/cmake/modules")
23 include(GNUInstallDirs)
25 # standalone build, copied from clang
26 if(BOLT_BUILT_STANDALONE)
27   set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
28   set(CMAKE_CXX_STANDARD_REQUIRED YES)
29   set(CMAKE_CXX_EXTENSIONS NO)
31   if(NOT MSVC_IDE)
32     set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
33       CACHE BOOL "Enable assertions")
34     # Assertions should follow llvm-config's.
35     mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
36   endif()
38   find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
39   list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
41   set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
42   find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
43     NO_DEFAULT_PATH)
45   # They are used as destination of target generators.
46   set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
47   set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
49   include(AddLLVM)
50   include(TableGen)
51   include_directories(${LLVM_INCLUDE_DIRS})
52   link_directories("${LLVM_LIBRARY_DIR}")
54   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_INSTALL_BINDIR}" )
55   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_INSTALL_LIBDIR}/${LLVM_LIBDIR_SUFFIX}" )
56   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_INSTALL_LIBDIR}/${LLVM_LIBDIR_SUFFIX}")
57 endif() # standalone
59 # Determine default set of targets to build -- the intersection of
60 # those BOLT supports and those LLVM is targeting.
61 set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
62 set(BOLT_TARGETS_TO_BUILD_default)
63 foreach (tgt ${BOLT_TARGETS_TO_BUILD_all})
64   if (tgt IN_LIST LLVM_TARGETS_TO_BUILD)
65     list(APPEND BOLT_TARGETS_TO_BUILD_default ${tgt})
66   endif()
67 endforeach()
69 # Allow the user to specify the BOLT targets, and then check that LLVM
70 # is indeed targeting those.
71 set(BOLT_TARGETS_TO_BUILD "${BOLT_TARGETS_TO_BUILD_default}"
72   CACHE STRING "Targets for BOLT to support.")
73 if (NOT BOLT_TARGETS_TO_BUILD)
74   message(FATAL_ERROR "BOLT enabled but BOLT_TARGETS_TO_BUILD is empty")
75 endif()
76 foreach (tgt ${BOLT_TARGETS_TO_BUILD})
77   if (NOT tgt IN_LIST LLVM_TARGETS_TO_BUILD)
78     message(FATAL_ERROR "BOLT target '${tgt}' is not in LLVM_TARGETS_TO_BUILD")
79   endif()
80   message(STATUS "Targeting ${tgt} in llvm-bolt")
81 endforeach()
83 set(BOLT_ENABLE_RUNTIME_default OFF)
84 if ((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
85     OR CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)$")
86     AND (CMAKE_SYSTEM_NAME STREQUAL "Linux"
87       OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
88     AND (NOT CMAKE_CROSSCOMPILING))
89   set(BOLT_ENABLE_RUNTIME_default ON)
90 endif()
91 option(BOLT_ENABLE_RUNTIME "Enable BOLT runtime" ${BOLT_ENABLE_RUNTIME_default})
92 if (BOLT_ENABLE_RUNTIME)
93   # Some systems prevent reading /proc/self/map_files
94   execute_process(COMMAND ls /proc/self/map_files
95     RESULT_VARIABLE LS OUTPUT_QUIET ERROR_QUIET)
96   if (LS)
97     message(WARNING
98       "BOLT runtime may not be able to read /proc/self/map_files. Please use
99       `--instrumentation-binpath <path-to-instrumented-binary>` option.")
100   endif()
101 endif()
103 set(BOLT_CLANG_EXE "" CACHE FILEPATH "Path to clang executable for the target \
104 architecture for use in BOLT tests")
105 set(BOLT_LLD_EXE "" CACHE FILEPATH "Path to lld executable for the target \
106 architecture for use in BOLT tests")
108 set(BOLT_INCLUDE_TESTS OFF)
109 if (LLVM_INCLUDE_TESTS)
110   set(BOLT_CLANG_PRESENT OFF)
111   set(BOLT_LLD_PRESENT OFF)
113   if ("clang" IN_LIST LLVM_ENABLE_PROJECTS AND BOLT_CLANG_EXE)
114     message(WARNING "BOLT_CLANG_EXE is set and clang project is enabled. \
115           BOLT_CLANG_EXE will be used for BOLT tests.")
116   endif()
117   if ("clang" IN_LIST LLVM_ENABLE_PROJECTS OR BOLT_CLANG_EXE)
118     set(BOLT_CLANG_PRESENT ON)
119   endif()
121   if ("lld" IN_LIST LLVM_ENABLE_PROJECTS AND BOLT_LLD_EXE)
122     message(WARNING "BOLT_LLD_EXE is set and lld project is enabled. \
123           BOLT_LLD_EXE will be used for BOLT tests.")
124   endif()
125   if ("lld" IN_LIST LLVM_ENABLE_PROJECTS OR BOLT_LLD_EXE)
126     set(BOLT_LLD_PRESENT ON)
127   endif()
129   if (BOLT_CLANG_PRESENT AND BOLT_LLD_PRESENT)
130     set(BOLT_INCLUDE_TESTS ON)
131   else()
132     message(WARNING "Not including BOLT tests since clang or lld is disabled. \
133           Add clang and lld to LLVM_ENABLE_PROJECTS or provide paths to clang \
134           and lld binaries in BOLT_CLANG_EXE and BOLT_LLD_EXE.")
135   endif()
136 endif()
138 if (BOLT_ENABLE_RUNTIME)
139   message(STATUS "Building BOLT runtime libraries for X86")
140   set(extra_args "")
141   if(CMAKE_SYSROOT)
142     list(APPEND extra_args -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
143   endif()
145   include(ExternalProject)
146   ExternalProject_Add(bolt_rt
147     SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/runtime"
148     STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-stamps
149     BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins
150     CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
151                -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
152                -DCMAKE_BUILD_TYPE=Release
153                -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
154                -DLLVM_LIBDIR_SUFFIX=${LLVM_LIBDIR_SUFFIX}
155                -DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR}
156                -DBOLT_BUILT_STANDALONE=${BOLT_BUILT_STANDALONE}
157                ${extra_args}
158     INSTALL_COMMAND ""
159     BUILD_ALWAYS True
160     )
161   install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/cmake_install.cmake \)"
162     COMPONENT bolt)
163   add_llvm_install_targets(install-bolt_rt
164     DEPENDS bolt_rt bolt
165     COMPONENT bolt)
166   set(LIBBOLT_RT_INSTR "${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/lib/libbolt_rt_instr.a")
167   set(LIBBOLT_RT_HUGIFY "${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/lib/libbolt_rt_hugify.a")
168 endif()
170 find_program(GNU_LD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.bfd ld.bfd DOC "GNU ld")
172 include(AddBOLT)
174 option(BOLT_BUILD_TOOLS
175   "Build the BOLT tools. If OFF, just generate build targets." ON)
177 add_custom_target(bolt)
178 set_target_properties(bolt PROPERTIES FOLDER "BOLT/Metatargets")
179 add_llvm_install_targets(install-bolt DEPENDS bolt COMPONENT bolt)
181 include_directories(
182   ${CMAKE_CURRENT_SOURCE_DIR}/include
183   ${CMAKE_CURRENT_BINARY_DIR}/include
184   )
186 add_subdirectory(lib)
187 add_subdirectory(tools)
189 if (BOLT_INCLUDE_TESTS)
190   if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
191     add_subdirectory(unittests)
192     list(APPEND BOLT_TEST_DEPS BoltUnitTests)
193   endif()
194   add_subdirectory(test)
195 endif()
197 option(BOLT_INCLUDE_DOCS "Generate build targets for the BOLT docs."
198        ${LLVM_INCLUDE_DOCS})
199 if (BOLT_INCLUDE_DOCS)
200   add_subdirectory(docs)
201 endif()
203 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc.in
204                ${CMAKE_CURRENT_BINARY_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc @ONLY)