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;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})
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})
17 message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
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}")
24 function(runtime_register_component name)
25 set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
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 # Add path for custom and the LLVM build's modules to the CMake module path.
32 list(INSERT CMAKE_MODULE_PATH 0
33 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
34 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
35 "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
36 "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
39 function(get_compiler_rt_path path)
40 foreach(entry ${runtimes})
41 get_filename_component(projName ${entry} NAME)
42 if("${projName}" MATCHES "compiler-rt")
43 set(${path} ${entry} PARENT_SCOPE)
49 # Some of the runtimes will conditionally use the compiler-rt sanitizers
50 # to make this work smoothly we ensure that compiler-rt is added first in
51 # the list of sub-projects. This allows other sub-projects to have checks
52 # like `if(TARGET asan)` to enable building with asan.
53 get_compiler_rt_path(compiler_rt_path)
55 list(REMOVE_ITEM runtimes ${compiler_rt_path})
56 if(NOT DEFINED LLVM_BUILD_COMPILER_RT OR LLVM_BUILD_COMPILER_RT)
57 list(INSERT runtimes 0 ${compiler_rt_path})
61 # Setting these variables will allow the sub-build to put their outputs into
62 # the library and bin directories of the top-level build.
63 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
64 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
66 # This variable makes sure that e.g. llvm-lit is found.
67 set(LLVM_MAIN_SRC_DIR ${LLVM_BUILD_MAIN_SRC_DIR})
68 set(LLVM_CMAKE_PATH ${LLVM_MAIN_SRC_DIR}/cmake/modules)
70 # This variable is used by individual runtimes to locate LLVM files.
71 set(LLVM_PATH ${LLVM_BUILD_MAIN_SRC_DIR})
74 set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
77 include(CheckLibraryExists)
78 include(CheckCCompilerFlag)
80 # We don't have libc++ (yet)...
81 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++ -nostdlib++")
83 # ...but we need access to libc++ headers for CMake checks to succeed.
84 if (LLVM_EXTERNAL_LIBCXX_SOURCE_DIR AND "libcxx" IN_LIST LLVM_ENABLE_RUNTIMES)
85 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -isystem ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR}/include")
88 # Avoid checking whether the compiler is working.
89 set(LLVM_COMPILER_CHECKED ON)
91 # Handle common options used by all runtimes.
93 include(HandleLLVMOptions)
94 include(FindPythonInterp)
96 # Remove the -nostdlib++ option we've added earlier.
97 string(REPLACE "-nostdlib++" "" CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
99 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
100 if(CMAKE_HOST_APPLE AND APPLE)
104 # This can be used to detect whether we're in the runtimes build.
105 set(RUNTIMES_BUILD ON)
107 foreach(entry ${runtimes})
108 get_filename_component(projName ${entry} NAME)
110 # TODO: Clean this up as part of an interface standardization
111 string(REPLACE "-" "_" canon_name ${projName})
112 string(TOUPPER ${canon_name} canon_name)
114 # The subdirectories need to treat this as standalone builds. D57992 tried
115 # to get rid of this, but the runtimes treat *_STANDALONE_BUILD=OFF as if
116 # llvm & clang are configured in the same CMake, and setup dependencies
117 # against their targets. OpenMP has fixed the issue so we don't set the
119 if (NOT ${canon_name} STREQUAL "OPENMP")
120 set(${canon_name}_STANDALONE_BUILD ON)
123 if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
124 set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
127 # Setting a variable to let sub-projects detect which other projects
128 # will be included under here.
129 set(HAVE_${canon_name} ON)
132 # We do this in two loops so that HAVE_* is set for each runtime before the
133 # other runtimes are added.
134 foreach(entry ${runtimes})
135 get_filename_component(projName ${entry} NAME)
137 # Between each sub-project we want to cache and clear the LIT properties
138 set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
139 set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
140 set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
141 set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
143 add_subdirectory(${entry} ${projName})
145 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
146 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
147 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
148 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
150 list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
151 list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
152 list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
153 list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
156 if(LLVM_INCLUDE_TESTS)
157 # Add a global check rule now that all subdirectories have been traversed
158 # and we know the total set of lit testsuites.
160 add_lit_target(check-runtimes
161 "Running all regression tests"
162 ${RUNTIMES_LIT_TESTSUITES}
163 PARAMS ${RUNTIMES_LIT_PARAMS}
164 DEPENDS ${RUNTIMES_LIT_DEPENDS}
165 ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
167 add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
170 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
172 list(REMOVE_DUPLICATES SUB_COMPONENTS)
173 foreach(component ${SUB_COMPONENTS})
174 if(NOT TARGET ${component})
175 message(SEND_ERROR "Missing target for runtime component ${component}!")
179 if(TARGET check-${component})
180 list(APPEND SUB_CHECK_TARGETS check-${component})
183 if(TARGET install-${component})
184 list(APPEND SUB_INSTALL_TARGETS install-${component})
186 if(TARGET install-${component}-stripped)
187 list(APPEND SUB_INSTALL_TARGETS install-${component}-stripped)
191 if(LLVM_RUNTIMES_TARGET)
193 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
194 ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
197 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
198 ${LLVM_BINARY_DIR}/runtimes/Components.cmake)