[libc][NFC] Move memfunction's byte per byte implementations to a separate header
[llvm-project.git] / runtimes / CMakeLists.txt
blob599529852688f25961804c17ea71b801cb6102a6
1 # This file handles building LLVM runtime sub-projects.
2 cmake_minimum_required(VERSION 3.20.0)
4 # Add path for custom and the LLVM build's modules to the CMake module path.
5 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
6 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
7   NO_POLICY_SCOPE)
9 project(Runtimes C CXX ASM)
11 list(INSERT CMAKE_MODULE_PATH 0
12   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
13   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
14   "${LLVM_COMMON_CMAKE_UTILS}"
15   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
16   "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
17   "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
20 # We order libraries to mirror roughly how they are layered, except that compiler-rt can depend
21 # on libc++, so we put it after.
22 set(LLVM_DEFAULT_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp")
23 set(LLVM_SUPPORTED_RUNTIMES "${LLVM_DEFAULT_RUNTIMES};llvm-libgcc")
24 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
25   "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
26 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
27   set(LLVM_ENABLE_RUNTIMES ${LLVM_DEFAULT_RUNTIMES})
28 endif()
29 include(SortSubset)
30 sort_subset("${LLVM_SUPPORTED_RUNTIMES}" "${LLVM_ENABLE_RUNTIMES}" LLVM_ENABLE_RUNTIMES)
32 foreach(proj ${LLVM_ENABLE_RUNTIMES})
33   set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
34   if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
35     list(APPEND runtimes ${proj_dir})
36   else()
37     message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
38   endif()
39   string(TOUPPER "${proj}" canon_name)
40   STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
41   set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
42 endforeach()
44 function(runtime_register_component name)
45   set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
46 endfunction()
48 find_package(LLVM PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
49 find_package(Clang PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
51 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
53 # If building standalone by pointing CMake at this runtimes directory,
54 # LLVM_BINARY_DIR isn't set, find_package(LLVM) will fail and these
55 # intermediate paths are unset.
56 if (NOT LLVM_BINARY_DIR)
57   set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
58 endif()
59 if (NOT LLVM_FOUND)
60   set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
61   set(LLVM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
62   set(LLVM_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
63 endif()
65 # Setting these variables will allow the sub-build to put their outputs into
66 # the library and bin directories of the top-level build.
67 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
68 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
70 # This variable makes sure that e.g. llvm-lit is found.
71 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
72 set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules)
74 # This variable is used by individual runtimes to locate LLVM files.
75 set(LLVM_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
77 include(CheckLibraryExists)
78 include(LLVMCheckCompilerLinkerFlag)
79 include(CheckCCompilerFlag)
80 include(CheckCXXCompilerFlag)
82 # CMake omits default compiler include paths, but in runtimes build, we use
83 # -nostdinc and -nostdinc++ and control include paths manually so this behavior
84 # is undesirable. Filtering CMAKE_{LANG}_IMPLICIT_INCLUDE_DIRECTORIES to remove
85 # paths that are inside the build directory disables this behavior.
87 # See https://gitlab.kitware.com/cmake/cmake/-/issues/19227 for further details.
89 function(filter_prefixed list prefix outvar)
90   foreach(str ${list})
91     string(FIND "${str}" "${prefix}" out)
92     if(NOT "${out}" EQUAL 0)
93       list(APPEND result ${str})
94     endif()
95   endforeach()
96   set(${outvar} ${result} PARENT_SCOPE)
97 endfunction()
99 filter_prefixed("${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
100 filter_prefixed("${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
101 filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES)
103 # The compiler driver may be implicitly trying to link against libunwind,
104 # which might not work if libunwind doesn't exist yet. Try to check if
105 # --unwindlib=none is supported, and use that if possible.
107 # TODO: Note that this is problematic when LLVM_USE_SANITIZER is used
108 # because some sanitizers require the unwinder and so the combination of
109 # -fsanitize=... --unwindlib=none will always result in a linking error.
110 # Currently, we counteract this issue by adding -fno-sanitize=all flag in
111 # the project specific code within */cmake/config-ix.cmake files but that's
112 # brittle. We should ideally move this to runtimes/CMakeLists.txt.
113 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
114 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
115   set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
116   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
117   # TODO: When we can require CMake 3.14, we should use
118   # CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround:
119   # When using CMAKE_REQUIRED_FLAGS, this option gets added both to
120   # compilation and linking commands. That causes warnings in the
121   # compilation commands during cmake tests. This is normally benign, but
122   # when testing whether -Werror works, that test fails (due to the
123   # preexisting warning).
124   #
125   # Therefore, before we can use CMAKE_REQUIRED_LINK_OPTIONS, check if we
126   # can use --start-no-unused-arguments to silence the warnings about
127   # --unwindlib=none during compilation.
128   #
129   # We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to
130   # allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS
131   # below.
132   check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
133   if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
134     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
135   endif()
136 endif()
138 # Disable use of the installed C++ standard library when building runtimes.
139 # Check for -nostdlib++ first; if there's no C++ standard library yet,
140 # all check_cxx_compiler_flag commands will fail until we add -nostdlib++
141 # (or -nodefaultlibs).
142 llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
143 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
144   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
145 endif()
146 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
147 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
148   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
149 endif()
151 # Avoid checking whether the compiler is working.
152 set(LLVM_COMPILER_CHECKED ON)
154 # Handle common options used by all runtimes.
155 include(AddLLVM)
156 include(HandleLLVMOptions)
158 find_package(Python3 REQUIRED COMPONENTS Interpreter)
160 # Host triple is used by tests to check if they are running natively.
161 include(GetHostTriple)
162 get_host_triple(LLVM_HOST_TRIPLE)
163 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
164   "Default target for which the runtimes will be built.")
166 option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
167 option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
168 option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
170 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
171 if(CMAKE_HOST_APPLE AND APPLE)
172   include(UseLibtool)
173 endif()
175 # This can be used to detect whether we're in the runtimes build.
176 set(LLVM_RUNTIMES_BUILD ON)
178 include(SetTargetTriple)
179 set_llvm_target_triple()
181 foreach(entry ${runtimes})
182   get_filename_component(projName ${entry} NAME)
184   # TODO: Clean this up as part of an interface standardization
185   string(REPLACE "-" "_" canon_name ${projName})
186   string(TOUPPER ${canon_name} canon_name)
188   # TODO: compiler-rt has to use standalone build for now. We tried to remove
189   # this in D57992 but this broke the build because compiler-rt assumes that
190   # LLVM and Clang are configured in the same build to set up dependencies. We
191   # should clean up the compiler-rt build and remove this eventually.
192   if ("${canon_name}" STREQUAL "COMPILER_RT")
193     set(${canon_name}_STANDALONE_BUILD ON)
194   endif()
196   if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
197     set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
198   endif()
200   # Setting a variable to let sub-projects detect which other projects
201   # will be included under here.
202   set(HAVE_${canon_name} ON)
203 endforeach()
205 if(LLVM_INCLUDE_TESTS)
206   set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
207   if (MSVC OR XCODE)
208     set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
209   endif()
210   set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
212   umbrella_lit_testsuite_begin(check-runtimes)
213 endif()
215 # llvm-libgcc incorporates both compiler-rt and libunwind as subprojects with very
216 # specific flags, which causes clashes when they're independently built too.
217 if("llvm-libgcc" IN_LIST runtimes)
218   if("compiler-rt" IN_LIST runtimes OR "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
219     message(FATAL_ERROR
220       "Attempting to build both compiler-rt and llvm-libgcc will cause irreconcilable "
221       "target clashes. Please choose one or the other, but not both.")
222   endif()
224   if("libunwind" IN_LIST runtimes)
225     message(
226       FATAL_ERROR
227       "Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
228       "target clashes. Please choose one or the other, but not both.")
229   endif()
230 endif()
232 # We do this in two loops so that HAVE_* is set for each runtime before the
233 # other runtimes are added.
234 foreach(entry ${runtimes})
235   get_filename_component(projName ${entry} NAME)
237   add_subdirectory(${entry} ${projName})
238 endforeach()
240 if(LLVM_INCLUDE_TESTS)
241   # LLVM_RUNTIMES_LIT_DEPENDS is populated when lit tests are added between
242   # umbrella_list_testsuite begin and end. The bootstrap runtimes builds
243   # currently assumes this target exists.
244   get_property(LLVM_RUNTIMES_LIT_DEPENDS GLOBAL PROPERTY LLVM_RUNTIMES_LIT_DEPENDS)
245   add_custom_target(runtimes-test-depends)
246   if(LLVM_RUNTIMES_LIT_DEPENDS)
247     # add_dependencies complains if called with no dependencies
248     add_dependencies(runtimes-test-depends ${LLVM_RUNTIMES_LIT_DEPENDS})
249   endif()
250   # Add a global check rule now that all subdirectories have been traversed
251   # and we know the total set of lit testsuites.
252   umbrella_lit_testsuite_end(check-runtimes)
254   if (NOT HAVE_LLVM_LIT)
255     # If built by manually invoking cmake on this directory, we don't have
256     # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
257     # invocation already generated the llvm-lit script.
258     add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
259                      ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
260   endif()
262   get_property(LLVM_RUNTIMES_LIT_TESTSUITES GLOBAL PROPERTY LLVM_RUNTIMES_LIT_TESTSUITES)
263   string(REPLACE ";" "\n" LLVM_RUNTIMES_LIT_TESTSUITES "${LLVM_RUNTIMES_LIT_TESTSUITES}")
264   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lit.tests ${LLVM_RUNTIMES_LIT_TESTSUITES})
265 else()
266   # Create empty files so the parent build can use these unconditionally.
267   file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/lit.tests)
268 endif()
270 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
271 if(SUB_COMPONENTS)
272   list(REMOVE_DUPLICATES SUB_COMPONENTS)
273   foreach(component ${SUB_COMPONENTS})
274     if(NOT TARGET ${component})
275       message(SEND_ERROR "Missing target for runtime component ${component}!")
276       continue()
277     endif()
279     if(TARGET check-${component})
280       list(APPEND SUB_CHECK_TARGETS check-${component})
281     endif()
283     if(TARGET install-${component})
284       list(APPEND SUB_INSTALL_TARGETS install-${component})
285     endif()
286   endforeach()
288   if(LLVM_RUNTIMES_TARGET)
289     configure_file(
290       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
291       ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
292   else()
293     configure_file(
294       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
295       ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
296   endif()
297 endif()