OSDN Git Service

[CMake][runtimes] Use -nodefaultlibs for the runtimes build
[android-x86/external-llvm.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   if(APPLE)
65    set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
66   endif()
67
68   set(SAFE_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
69   set(SAFE_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
70
71   include(CheckLibraryExists)
72   include(CheckCCompilerFlag)
73
74   check_library_exists(c fopen "" LLVM_HAS_C_LIB)
75   check_c_compiler_flag(-nodefaultlibs LLVM_HAS_NODEFAULTLIBS_FLAG)
76   if(LLVM_HAS_NODEFAULTLIBS_FLAG)
77     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
78     if(LLVM_HAS_C_LIB)
79       list(APPEND CMAKE_REQUIRED_LIBRARIES c)
80     endif()
81   endif()
82
83   # Handle common options used by all runtimes.
84   include(AddLLVM)
85   include(HandleLLVMOptions)
86
87   set(CMAKE_REQUIRED_FLAGS ${SAFE_CMAKE_REQUIRED_FLAGS})
88   set(CMAKE_REQUIRED_LIBRARIES ${SAFE_CMAKE_REQUIRED_LIBRARIES})
89
90   foreach(entry ${runtimes})
91     get_filename_component(projName ${entry} NAME)
92
93     # TODO: Clean this up as part of an interface standardization
94     string(REPLACE "-" "_" canon_name ${projName})
95     string(TOUPPER ${canon_name} canon_name)
96     # The subdirectories need to treat this as standalone builds
97     set(${canon_name}_STANDALONE_BUILD On)
98
99     # Setting a variable to let sub-projects detect which other projects
100     # will be included under here.
101     set(HAVE_${canon_name} On)
102   endforeach()
103
104   # We do this in two loops so that HAVE_* is set for each runtime before the
105   # other runtimes are added.
106   foreach(entry ${runtimes})
107     get_filename_component(projName ${entry} NAME)
108     
109     # Between each sub-project we want to cache and clear the LIT properties
110     set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
111     set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
112     set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
113     set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
114
115     add_subdirectory(${projName})
116
117     get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
118     get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
119     get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
120     get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
121
122     list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
123     list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
124     list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
125     list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
126   endforeach()
127
128   if(LLVM_INCLUDE_TESTS)
129     # Add a global check rule now that all subdirectories have been traversed
130     # and we know the total set of lit testsuites.
131     
132     add_lit_target(check-runtimes
133       "Running all regression tests"
134       ${RUNTIMES_LIT_TESTSUITES}
135       PARAMS ${RUNTIMES_LIT_PARAMS}
136       DEPENDS ${RUNTIMES_LIT_DEPENDS}
137       ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
138       )
139     add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
140   endif()
141
142   get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
143   if(SUB_COMPONENTS)
144     list(REMOVE_DUPLICATES SUB_COMPONENTS)
145     foreach(component ${SUB_COMPONENTS})
146       if(NOT TARGET ${component})
147         message(SEND_ERROR "Missing target for runtime component ${component}!")
148         continue()
149       endif()
150       if(LLVM_INCLUDE_TESTS AND NOT TARGET check-${component})
151         message(SEND_ERROR "Missing check target for runtime component ${component}!")
152         continue()
153       endif()
154
155       if(TARGET install-${component})
156         list(APPEND SUB_INSTALL_TARGETS install-${component})
157       endif()
158     endforeach()
159
160     configure_file(
161       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
162       ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
163   endif()
164
165 else() # if this is included from LLVM's CMake
166   include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
167   set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
168   include(LLVMExternalProjectUtils)
169
170   if(NOT LLVM_BUILD_RUNTIMES)
171     set(EXTRA_ARGS EXCLUDE_FROM_ALL)
172   endif()
173
174   # If compiler-rt is present we need to build the builtin libraries first. This
175   # is required because the other runtimes need the builtin libraries present
176   # before the just-built compiler can pass the configuration tests.
177   if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt)
178     if(NOT LLVM_BUILTIN_TARGETS)
179       llvm_ExternalProject_Add(builtins
180                                ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
181                                CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
182                                           -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
183                                           -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
184                                           -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
185                                PASSTHROUGH_PREFIXES COMPILER_RT
186                                USE_TOOLCHAIN
187                                ${EXTRA_ARGS})
188     else()
189       get_cmake_property(variableNames VARIABLES)
190       add_custom_target(builtins)
191       foreach(target ${LLVM_BUILTIN_TARGETS})
192         string(REPLACE "-" ";" builtin_target_list ${target})
193         foreach(item ${builtin_target_list})
194           string(TOLOWER "${item}" item_lower)
195           if(item_lower MATCHES "darwin")
196             message(FATAL_ERROR "LLVM_BUILTIN_TARGETS isn't implemented for Darwin platform!")
197           endif()
198         endforeach()
199
200         foreach(variableName ${variableNames})
201           if(variableName MATCHES "^BUILTINS_${target}")
202             string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
203             list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
204           endif()
205         endforeach()
206         llvm_ExternalProject_Add(builtins-${target}
207                                ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
208                                CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
209                                           -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
210                                           -DCMAKE_C_COMPILER_TARGET=${target}
211                                           -DCMAKE_ASM_COMPILER_TARGET=${target}
212                                           -DCMAKE_C_COMPILER_WORKS=On
213                                           -DCMAKE_ASM_COMPILER_WORKS=On
214                                           -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On
215                                           ${${target}_extra_args}
216                                TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib
217                                PASSTHROUGH_PREFIXES COMPILER_RT
218                                USE_TOOLCHAIN
219                                ${EXTRA_ARGS})
220         add_dependencies(builtins builtins-${target})
221       endforeach()
222     endif()
223     set(deps builtins)
224     # We don't need to depend on the builtins if we're building instrumented
225     # because the next stage will use the same compiler used to build this stage.
226     if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
227       add_dependencies(clang-bootstrap-deps builtins)
228     endif()
229   endif()
230
231   # We create a list the names of all the runtime projects in all uppercase and
232   # with dashes turned to underscores. This gives us the CMake variable prefixes
233   # for all variables that will apply to runtimes.
234   foreach(entry ${runtimes})
235     get_filename_component(projName ${entry} NAME)
236     string(REPLACE "-" "_" canon_name ${projName})
237     string(TOUPPER ${canon_name} canon_name)
238     list(APPEND prefixes ${canon_name})
239
240     string(FIND ${projName} "lib" LIB_IDX)
241     if(LIB_IDX EQUAL 0)
242       string(SUBSTRING ${projName} 3 -1 projName)
243     endif()
244     list(APPEND runtime_names ${projName})
245   endforeach()
246
247   if(runtimes)
248
249     foreach(runtime_name ${runtime_names})
250       list(APPEND extra_targets
251         ${runtime_name}
252         install-${runtime_name}
253         check-${runtime_name})
254     endforeach()
255
256     if(LLVM_INCLUDE_TESTS)
257       set(test_targets runtimes-test-depends check-runtimes)
258       foreach(component ${SUB_COMPONENTS})
259         list(APPEND SUB_COMPONENT_CHECK_TARGETS check-${component})
260       endforeach()
261     endif()
262
263     # Create a runtimes target that uses this file as its top-level CMake file.
264     # The runtimes target is a configuration of all the runtime libraries
265     # together in a single CMake invocaiton.
266     llvm_ExternalProject_Add(runtimes
267                              ${CMAKE_CURRENT_SOURCE_DIR}
268                              DEPENDS ${deps}
269                              # Builtins were built separately above
270                              CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
271                                         -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
272                              PASSTHROUGH_PREFIXES ${prefixes}
273                              EXTRA_TARGETS ${extra_targets}
274                                             ${test_targets}
275                                             ${SUB_COMPONENTS}
276                                             ${SUB_COMPONENT_CHECK_TARGETS}
277                                             ${SUB_INSTALL_TARGETS}
278                              USE_TOOLCHAIN
279                              ${EXTRA_ARGS})
280     
281     # TODO: This is a hack needed because the libcxx headers are copied into the
282     # build directory during configuration. Without that step the clang in the
283     # build directory cannot find the C++ headers in certain configurations.
284     # I need to build a mechanism for runtime projects to provide CMake code
285     # that executes at LLVM configuration time to handle this case.
286     if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
287       add_dependencies(clang-bootstrap-deps runtimes-configure)
288     endif()
289
290     if(LLVM_INCLUDE_TESTS)
291       set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
292       set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)
293     endif()
294   endif()
295 endif()