[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / runtimes / CMakeLists.txt
blobcedce7b3541e501a26c56166764f53bfa0e15fe7
1 # This file handles building LLVM runtime sub-projects.
2 cmake_minimum_required(VERSION 3.13.4)
3 project(Runtimes C CXX ASM)
5 set(LLVM_ALL_RUNTIMES "compiler-rt;libc;libcxx;libcxxabi;libunwind;openmp")
6 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
7   "Semicolon-separated list of runtimes to build (${LLVM_ALL_RUNTIMES}), or \"all\".")
8 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
9   set(LLVM_ENABLE_RUNTIMES ${LLVM_ALL_RUNTIMES})
10 endif()
12 foreach(proj ${LLVM_ENABLE_RUNTIMES})
13   set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
14   if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
15     list(APPEND runtimes ${proj_dir})
16   else()
17     message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
18   endif()
19   string(TOUPPER "${proj}" canon_name)
20   STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
21   set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
22 endforeach()
24 function(runtime_register_component name)
25   set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
26 endfunction()
28 find_package(LLVM PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
29 find_package(Clang PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
31 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
33 # Add path for custom and the LLVM build's modules to the CMake module path.
34 list(INSERT CMAKE_MODULE_PATH 0
35   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
36   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
37   "${LLVM_COMMON_CMAKE_UTILS}"
38   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
39   "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
40   "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
43 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
45 function(get_compiler_rt_path path)
46   foreach(entry ${runtimes})
47     get_filename_component(projName ${entry} NAME)
48     if("${projName}" MATCHES "compiler-rt")
49       set(${path} ${entry} PARENT_SCOPE)
50       return()
51     endif()
52   endforeach()
53 endfunction()
55 # Some of the runtimes will conditionally use the compiler-rt sanitizers
56 # to make this work smoothly we ensure that compiler-rt is added first in
57 # the list of sub-projects. This allows other sub-projects to have checks
58 # like `if(TARGET asan)` to enable building with asan.
59 get_compiler_rt_path(compiler_rt_path)
60 if(compiler_rt_path)
61   list(REMOVE_ITEM runtimes ${compiler_rt_path})
62   if(NOT DEFINED LLVM_BUILD_COMPILER_RT OR LLVM_BUILD_COMPILER_RT)
63     list(INSERT runtimes 0 ${compiler_rt_path})
64   endif()
65 endif()
67 # If building standalone by pointing CMake at this runtimes directory,
68 # LLVM_BINARY_DIR isn't set, find_package(LLVM) will fail and these
69 # intermediate paths are unset.
70 if (NOT LLVM_BINARY_DIR)
71   set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
72 endif()
73 if (NOT LLVM_FOUND)
74   set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
75   set(LLVM_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
76 endif()
78 # Setting these variables will allow the sub-build to put their outputs into
79 # the library and bin directories of the top-level build.
80 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
81 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
83 # This variable makes sure that e.g. llvm-lit is found.
84 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
85 set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules)
87 # This variable is used by individual runtimes to locate LLVM files.
88 set(LLVM_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
90 include(CheckLibraryExists)
91 include(CheckLinkerFlag)
92 include(CheckCCompilerFlag)
93 include(CheckCXXCompilerFlag)
96 check_c_compiler_flag("" LLVM_RUNTIMES_LINKING_WORKS)
97 if (NOT LLVM_RUNTIMES_LINKING_WORKS)
98   # The compiler driver may be implicitly trying to link against libunwind, which
99   # might not work if libunwind doesn't exist yet. Try to check if
100   # --unwindlib=none is supported, and use that if possible.
101   # Don't add this if not necessary to fix linking, as it can break using
102   # e.g. ASAN/TSAN.
103   llvm_check_linker_flag("--unwindlib=none" LLVM_RUNTIMES_SUPPORT_UNWINDLIB_NONE_FLAG)
104   if (LLVM_RUNTIMES_SUPPORT_UNWINDLIB_NONE_FLAG)
105     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
106   endif()
107 endif()
109 # Disable use of the installed C++ standard library when building runtimes.
110 # Check for -nostdlib++ first; if there's no C++ standard library yet,
111 # all check_cxx_compiler_flag commands will fail until we add -nostdlib++
112 # (or -nodefaultlibs).
113 llvm_check_linker_flag(-nostdlib++ LLVM_RUNTIMES_SUPPORT_NOSTDLIBXX_FLAG)
114 if (LLVM_RUNTIMES_SUPPORT_NOSTDLIBXX_FLAG)
115   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
116 endif()
117 check_cxx_compiler_flag(-nostdinc++ LLVM_RUNTIMES_SUPPORT_NOSTDINCXX_FLAG)
118 if (LLVM_RUNTIMES_SUPPORT_NOSTDINCXX_FLAG)
119   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
120 endif()
122 # Avoid checking whether the compiler is working.
123 set(LLVM_COMPILER_CHECKED ON)
125 # Handle common options used by all runtimes.
126 include(AddLLVM)
127 include(HandleLLVMOptions)
129 find_package(Python3 REQUIRED COMPONENTS Interpreter)
131 # Host triple is used by tests to check if they are running natively.
132 include(GetHostTriple)
133 get_host_triple(LLVM_HOST_TRIPLE)
134 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
135   "Default target for which the runtimes will be built.")
137 option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
138 option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
139 option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
141 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
142 if(CMAKE_HOST_APPLE AND APPLE)
143   include(UseLibtool)
144 endif()
146 # This can be used to detect whether we're in the runtimes build.
147 set(LLVM_RUNTIMES_BUILD ON)
149 foreach(entry ${runtimes})
150   get_filename_component(projName ${entry} NAME)
152   # TODO: Clean this up as part of an interface standardization
153   string(REPLACE "-" "_" canon_name ${projName})
154   string(TOUPPER ${canon_name} canon_name)
156   # TODO: compiler-rt has to use standalone build for now. We tried to remove
157   # this in D57992 but this broke the build because compiler-rt assumes that
158   # LLVM and Clang are configured in the same build to set up dependencies. We
159   # should clean up the compiler-rt build and remove this eventually.
160   if ("${canon_name}" STREQUAL "COMPILER_RT")
161     set(${canon_name}_STANDALONE_BUILD ON)
162   endif()
164   if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
165     set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
166   endif()
168   # Setting a variable to let sub-projects detect which other projects
169   # will be included under here.
170   set(HAVE_${canon_name} ON)
171 endforeach()
173 if(LLVM_INCLUDE_TESTS)
174   set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
175   if (MSVC OR XCODE)
176     set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
177   endif()
178   set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
179 endif()
181 # We do this in two loops so that HAVE_* is set for each runtime before the
182 # other runtimes are added.
183 foreach(entry ${runtimes})
184   get_filename_component(projName ${entry} NAME)
186   # Between each sub-project we want to cache and clear the LIT properties
187   set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
188   set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
189   set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
190   set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
192   add_subdirectory(${entry} ${projName})
194   get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
195   get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
196   get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
197   get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
199   list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
200   list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
201   list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
202   list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
203 endforeach()
205 if(LLVM_INCLUDE_TESTS)
206   # Add a global check rule now that all subdirectories have been traversed
207   # and we know the total set of lit testsuites.
208   add_lit_target(check-runtimes
209     "Running all regression tests"
210     ${RUNTIMES_LIT_TESTSUITES}
211     PARAMS ${RUNTIMES_LIT_PARAMS}
212     DEPENDS ${RUNTIMES_LIT_DEPENDS}
213     ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
214     )
215   add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
217   if (NOT HAVE_LLVM_LIT)
218     # If built by manually invoking cmake on this directory, we don't have
219     # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
220     # invocation already generated the llvm-lit script.
221     add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
222                      ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
223   endif()
224 endif()
226 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
227 if(SUB_COMPONENTS)
228   list(REMOVE_DUPLICATES SUB_COMPONENTS)
229   foreach(component ${SUB_COMPONENTS})
230     if(NOT TARGET ${component})
231       message(SEND_ERROR "Missing target for runtime component ${component}!")
232       continue()
233     endif()
235     if(TARGET check-${component})
236       list(APPEND SUB_CHECK_TARGETS check-${component})
237     endif()
239     if(TARGET install-${component})
240       list(APPEND SUB_INSTALL_TARGETS install-${component})
241     endif()
242     if(TARGET install-${component}-stripped)
243       list(APPEND SUB_INSTALL_TARGETS install-${component}-stripped)
244     endif()
245   endforeach()
247   if(LLVM_RUNTIMES_TARGET)
248     configure_file(
249       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
250       ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
251   else()
252     configure_file(
253       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
254       ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
255   endif()
256 endif()