]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - runtimes/CMakeLists.txt
Vendor import of llvm release_40 branch r292009:
[FreeBSD/FreeBSD.git] / runtimes / CMakeLists.txt
1 # This file handles building LLVM runtime sub-projects.
2
3 # Runtimes are different from tools or other drop-in projects because runtimes
4 # should be built with the LLVM toolchain from the build directory. This file is
5 # a first step to formalizing runtime build interfaces.
6
7 # In the current state this file only works with compiler-rt, other runtimes
8 # will work as the runtime build interface standardizes.
9
10 # Find all subdirectories containing CMake projects
11 file(GLOB entries *)
12 foreach(entry ${entries})
13   if(IS_DIRECTORY ${entry} AND EXISTS ${entry}/CMakeLists.txt)
14     list(APPEND runtimes ${entry})
15   endif()
16 endforeach()
17
18 # If this file is acting as a top-level CMake invocation, this code path is
19 # triggered by the external project call for the runtimes target below.
20 if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
21
22   function(runtime_register_component name)
23     set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
24   endfunction()
25
26   cmake_minimum_required(VERSION 3.4.3)
27
28   # Add the root project's CMake modules, and the LLVM build's modules to the
29   # CMake module path.
30   list(INSERT CMAKE_MODULE_PATH 0
31     "${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
32     "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
33     "${LLVM_BINARY_DIR}/lib/cmake/llvm"
34   )
35
36   # Some of the runtimes will conditionally use the compiler-rt sanitizers
37   # to make this work smoothly we ensure that compiler-rt is added first in
38   # the list of sub-projects. This allows other sub-projects to have checks
39   # like `if(TARGET asan)` to enable building with asan.
40   foreach(entry ${runtimes})
41     if("${entry}" MATCHES "compiler-rt")
42       set(compiler_rt_path ${entry})
43       break()
44     endif()
45   endforeach()
46   if(compiler_rt_path)
47     list(REMOVE_ITEM runtimes ${compiler_rt_path})
48     list(INSERT runtimes 0 ${compiler_rt_path})
49   endif()
50
51   # LLVMConfig.cmake contains a bunch of CMake variables from the LLVM build.
52   # This file is installed as part of LLVM distributions, so this can be used
53   # either from a build directory or an installed LLVM.
54   include(LLVMConfig)
55
56   # Setting these variables will allow the sub-build to put their outputs into
57   # the library and bin directories of the top-level build.
58   set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
59   set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
60
61   # This variable makes sure that e.g. llvm-lit is found.
62   set(LLVM_MAIN_SRC_DIR ${LLVM_BUILD_MAIN_SRC_DIR})
63
64   # Handle common options used by all runtimes.
65   include(AddLLVM)
66   include(HandleLLVMOptions)
67
68   foreach(entry ${runtimes})
69     get_filename_component(projName ${entry} NAME)
70
71     # TODO: Clean this up as part of an interface standardization
72     string(REPLACE "-" "_" canon_name ${projName})
73     string(TOUPPER ${canon_name} canon_name)
74     # The subdirectories need to treat this as standalone builds
75     set(${canon_name}_STANDALONE_BUILD On)
76
77     # Setting a variable to let sub-projects detect which other projects
78     # will be included under here.
79     set(HAVE_${canon_name} On)
80   endforeach()
81
82   # We do this in two loops so that HAVE_* is set for each runtime before the
83   # other runtimes are added.
84   foreach(entry ${runtimes})
85     get_filename_component(projName ${entry} NAME)
86     
87     # Between each sub-project we want to cache and clear the LIT properties
88     set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
89     set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
90     set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
91     set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
92
93     add_subdirectory(${projName})
94
95     get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
96     get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
97     get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
98     get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
99
100     list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
101     list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
102     list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
103     list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
104   endforeach()
105
106   if(LLVM_INCLUDE_TESTS)
107     # Add a global check rule now that all subdirectories have been traversed
108     # and we know the total set of lit testsuites.
109     
110     add_lit_target(check-runtimes
111       "Running all regression tests"
112       ${RUNTIMES_LIT_TESTSUITES}
113       PARAMS ${RUNTIMES_LIT_PARAMS}
114       DEPENDS ${RUNTIMES_LIT_DEPENDS}
115       ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
116       )
117     add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
118   endif()
119
120   get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
121   if(SUB_COMPONENTS)
122     list(REMOVE_DUPLICATES SUB_COMPONENTS)
123     foreach(component ${SUB_COMPONENTS})
124       if(NOT TARGET ${component})
125         message(SEND_ERROR "Missing target for runtime component ${component}!")
126         continue()
127       endif()
128       if(LLVM_INCLUDE_TESTS AND NOT TARGET check-${component})
129         message(SEND_ERROR "Missing check target for runtime component ${component}!")
130         continue()
131       endif()
132
133       if(TARGET install-${component})
134         list(APPEND SUB_INSTALL_TARGETS install-${component})
135       endif()
136     endforeach()
137
138     configure_file(
139       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
140       ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
141   endif()
142
143 else() # if this is included from LLVM's CMake
144   include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
145   set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
146   include(LLVMExternalProjectUtils)
147
148   # If compiler-rt is present we need to build the builtin libraries first. This
149   # is required because the other runtimes need the builtin libraries present
150   # before the just-built compiler can pass the configuration tests.
151   if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt)
152     if(APPLE OR NOT LLVM_BUILTIN_TARGETS)
153       llvm_ExternalProject_Add(builtins
154                                ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
155                                CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
156                                           -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
157                                           -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
158                                           -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
159                                PASSTHROUGH_PREFIXES COMPILER_RT
160                                USE_TOOLCHAIN)
161     else()
162       get_cmake_property(variableNames VARIABLES)
163       add_custom_target(builtins)
164       foreach(target ${LLVM_BUILTIN_TARGETS})
165         foreach(variableName ${variableNames})
166           if(variableName MATCHES "^BUILTINS_${target}")
167             string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
168             list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
169           endif()
170         endforeach()
171         llvm_ExternalProject_Add(builtins-${target}
172                                ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
173                                CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
174                                           -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
175                                           -DCMAKE_C_COMPILER_TARGET=${target}
176                                           -DCMAKE_ASM_COMPILER_TARGET=${target}
177                                           -DCMAKE_C_COMPILER_WORKS=On
178                                           -DCMAKE_ASM_COMPILER_WORKS=On
179                                           -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On
180                                           ${${target}_extra_args}
181                                PASSTHROUGH_PREFIXES COMPILER_RT
182                                USE_TOOLCHAIN)
183         add_dependencies(builtins builtins-${target})
184       endforeach()
185     endif()
186     set(deps builtins)
187     # We don't need to depend on the builtins if we're building instrumented
188     # because the next stage will use the same compiler used to build this stage.
189     if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
190       add_dependencies(clang-bootstrap-deps builtins)
191     endif()
192   endif()
193
194   # We create a list the names of all the runtime projects in all uppercase and
195   # with dashes turned to underscores. This gives us the CMake variable prefixes
196   # for all variables that will apply to runtimes.
197   foreach(entry ${runtimes})
198     get_filename_component(projName ${entry} NAME)
199     string(REPLACE "-" "_" canon_name ${projName})
200     string(TOUPPER ${canon_name} canon_name)
201     list(APPEND prefixes ${canon_name})
202
203     string(FIND ${projName} "lib" LIB_IDX)
204     if(LIB_IDX EQUAL 0)
205       string(SUBSTRING ${projName} 3 -1 projName)
206     endif()
207     list(APPEND runtime_names ${projName})
208   endforeach()
209
210   if(runtimes)
211
212     foreach(runtime_name ${runtime_names})
213       list(APPEND extra_targets
214         ${runtime_name}
215         install-${runtime_name}
216         check-${runtime_name})
217     endforeach()
218
219     if(LLVM_INCLUDE_TESTS)
220       set(test_targets runtimes-test-depends check-runtimes)
221       foreach(component ${SUB_COMPONENTS})
222         list(APPEND SUB_COMPONENT_CHECK_TARGETS check-${component})
223       endforeach()
224     endif()
225
226     # Create a runtimes target that uses this file as its top-level CMake file.
227     # The runtimes target is a configuration of all the runtime libraries
228     # together in a single CMake invocaiton.
229     llvm_ExternalProject_Add(runtimes
230                              ${CMAKE_CURRENT_SOURCE_DIR}
231                              DEPENDS ${deps}
232                              # Builtins were built separately above
233                              CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
234                                         -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
235                              PASSTHROUGH_PREFIXES ${prefixes}
236                              EXTRA_TARGETS ${extra_targets}
237                                             ${test_targets}
238                                             ${SUB_COMPONENTS}
239                                             ${SUB_COMPONENT_CHECK_TARGETS}
240                                             ${SUB_INSTALL_TARGETS}
241                              USE_TOOLCHAIN)
242     
243     # TODO: This is a hack needed because the libcxx headers are copied into the
244     # build directory during configuration. Without that step the clang in the
245     # build directory cannot find the C++ headers in certain configurations.
246     # I need to build a mechanism for runtime projects to provide CMake code
247     # that executes at LLVM configuration time to handle this case.
248     if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
249       add_dependencies(clang-bootstrap-deps runtimes-configure)
250     endif()
251
252     if(LLVM_INCLUDE_TESTS)
253       set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
254       set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)
255     endif()
256   endif()
257 endif()