]> granicus.if.org Git - clang/blob - CMakeLists.txt
Remove the return type from the check string in test case.
[clang] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.4.3)
2
3 # If we are not building as a part of LLVM, build Clang as an
4 # standalone project, using LLVM as an external library:
5 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
6   project(Clang)
7
8   # Rely on llvm-config.
9   set(CONFIG_OUTPUT)
10   find_program(LLVM_CONFIG "llvm-config")
11   if(LLVM_CONFIG)
12     message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
13     set(CONFIG_COMMAND ${LLVM_CONFIG}
14       "--assertion-mode"
15       "--bindir"
16       "--libdir"
17       "--includedir"
18       "--prefix"
19       "--src-root"
20       "--cmakedir")
21     execute_process(
22       COMMAND ${CONFIG_COMMAND}
23       RESULT_VARIABLE HAD_ERROR
24       OUTPUT_VARIABLE CONFIG_OUTPUT
25     )
26     if(NOT HAD_ERROR)
27       string(REGEX REPLACE
28         "[ \t]*[\r\n]+[ \t]*" ";"
29         CONFIG_OUTPUT ${CONFIG_OUTPUT})
30     else()
31       string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
32       message(STATUS "${CONFIG_COMMAND_STR}")
33       message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
34     endif()
35   else()
36     message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
37   endif()
38
39   list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
40   list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
41   list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
42   list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
43   list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
44   list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
45   list(GET CONFIG_OUTPUT 6 LLVM_CMAKE_PATH)
46
47   if(NOT MSVC_IDE)
48     set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
49       CACHE BOOL "Enable assertions")
50     # Assertions should follow llvm-config's.
51     mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
52   endif()
53
54   set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
55   set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
56   set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
57   set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
58   set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
59
60   find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
61     NO_DEFAULT_PATH)
62
63   set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
64   if(EXISTS ${LLVMCONFIG_FILE})
65     list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
66     include(${LLVMCONFIG_FILE})
67   else()
68     message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
69   endif()
70
71   # They are used as destination of target generators.
72   set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
73   set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
74   if(WIN32 OR CYGWIN)
75     # DLL platform -- put DLLs into bin.
76     set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
77   else()
78     set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
79   endif()
80
81   option(LLVM_INSTALL_TOOLCHAIN_ONLY
82     "Only include toolchain files in the 'install' target." OFF)
83
84   option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
85     "Set to ON to force using an old, unsupported host toolchain." OFF)
86   option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
87
88   include(AddLLVM)
89   include(TableGen)
90   include(HandleLLVMOptions)
91   include(VersionFromVCS)
92
93   set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
94
95   if (NOT DEFINED LLVM_INCLUDE_TESTS)
96     set(LLVM_INCLUDE_TESTS ON)
97   endif()
98
99   include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
100   link_directories("${LLVM_LIBRARY_DIR}")
101
102   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
103   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
104   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
105
106   if(LLVM_INCLUDE_TESTS)
107     set(Python_ADDITIONAL_VERSIONS 2.7)
108     include(FindPythonInterp)
109     if(NOT PYTHONINTERP_FOUND)
110       message(FATAL_ERROR
111 "Unable to find Python interpreter, required for builds and testing.
112
113 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
114     endif()
115
116     if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
117       message(FATAL_ERROR "Python 2.7 or newer is required")
118     endif()
119
120     # Check prebuilt llvm/utils.
121     if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
122         AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
123         AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
124       set(LLVM_UTILS_PROVIDED ON)
125     endif()
126
127     if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
128       # Note: path not really used, except for checking if lit was found
129       set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
130       if(NOT LLVM_UTILS_PROVIDED)
131         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
132         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
133         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
134         set(LLVM_UTILS_PROVIDED ON)
135         set(CLANG_TEST_DEPS FileCheck count not)
136       endif()
137       set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
138       if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
139           AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
140           AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
141         add_subdirectory(${UNITTEST_DIR} utils/unittest)
142       endif()
143     else()
144       # Seek installed Lit.
145       find_program(LLVM_LIT
146                    NAMES llvm-lit lit.py lit
147                    PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
148                    DOC "Path to lit.py")
149     endif()
150
151     if(LLVM_LIT)
152       # Define the default arguments to use with 'lit', and an option for the user
153       # to override.
154       set(LIT_ARGS_DEFAULT "-sv")
155       if (MSVC OR XCODE)
156         set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
157       endif()
158       set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
159
160       # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
161       if( WIN32 AND NOT CYGWIN )
162         set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
163       endif()
164     else()
165       set(LLVM_INCLUDE_TESTS OFF)
166     endif()
167   endif()
168
169   set( CLANG_BUILT_STANDALONE 1 )
170   set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
171 else()
172   set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
173 endif()
174
175 # Make sure that our source directory is on the current cmake module path so that
176 # we can include cmake files from this directory.
177 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
178
179 find_package(LibXml2 2.5.3 QUIET)
180 if (LIBXML2_FOUND)
181   set(CLANG_HAVE_LIBXML 1)
182 endif()
183
184 include(CheckIncludeFile)
185 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
186
187 set(CLANG_RESOURCE_DIR "" CACHE STRING
188   "Relative directory from the Clang binary to its resource files.")
189
190 set(C_INCLUDE_DIRS "" CACHE STRING
191   "Colon separated list of directories clang will search for headers.")
192
193 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
194 set(DEFAULT_SYSROOT "" CACHE PATH
195   "Default <path> to all compiler invocations for --sysroot=<path>." )
196
197 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
198
199 set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
200     "enable x86 relax relocations by default")
201
202 set(CLANG_DEFAULT_LINKER "" CACHE STRING
203   "Default linker to use (linker name or absolute path, empty for platform default)")
204
205 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
206   "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
207 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
208         CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
209         CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
210   message(WARNING "Resetting default C++ stdlib to use platform default")
211   set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
212     "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
213 endif()
214
215 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
216   "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
217 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
218         CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
219         CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
220   message(WARNING "Resetting default rtlib to use platform default")
221   set(CLANG_DEFAULT_RTLIB "" CACHE STRING
222     "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
223 endif()
224
225 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
226   "Default OpenMP runtime used by -fopenmp.")
227
228 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
229   "Vendor-specific text for showing with version information.")
230
231 if( CLANG_VENDOR )
232   add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
233 endif()
234
235 set(CLANG_REPOSITORY_STRING "" CACHE STRING
236   "Vendor-specific text for showing the repository the source is taken from.")
237
238 if(CLANG_REPOSITORY_STRING)
239   add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
240 endif()
241
242 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
243   "Vendor-specific uti.")
244
245 # The libdir suffix must exactly match whatever LLVM's configuration used.
246 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
247
248 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
249 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
250
251 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
252   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
253 "the makefiles distributed with LLVM. Please create a directory and run cmake "
254 "from there, passing the path to this source directory as the last argument. "
255 "This process created the file `CMakeCache.txt' and the directory "
256 "`CMakeFiles'. Please delete them.")
257 endif()
258
259 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
260   file(GLOB_RECURSE
261     tablegenned_files_on_include_dir
262     "${CLANG_SOURCE_DIR}/include/clang/*.inc")
263   if( tablegenned_files_on_include_dir )
264     message(FATAL_ERROR "Apparently there is a previous in-source build, "
265 "probably as the result of running `configure' and `make' on "
266 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
267 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
268   endif()
269 endif()
270
271 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
272 if(NOT DEFINED CLANG_VERSION_MAJOR)
273   set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
274 endif()
275 if(NOT DEFINED CLANG_VERSION_MINOR)
276   set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
277 endif()
278 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
279   set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
280 endif()
281 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
282 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
283 message(STATUS "Clang version: ${CLANG_VERSION}")
284
285 # Configure the Version.inc file.
286 configure_file(
287   ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
288   ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
289
290 # Add appropriate flags for GCC
291 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
292   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
293   if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
294     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
295   endif ()
296
297   # Enable -pedantic for Clang even if it's not enabled for LLVM.
298   if (NOT LLVM_ENABLE_PEDANTIC)
299     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
300   endif ()
301
302   check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
303   if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
304     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
305   endif()
306 endif ()
307
308 # Determine HOST_LINK_VERSION on Darwin.
309 set(HOST_LINK_VERSION)
310 if (APPLE)
311   set(LD_V_OUTPUT)
312   execute_process(
313     COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
314     RESULT_VARIABLE HAD_ERROR
315     OUTPUT_VARIABLE LD_V_OUTPUT
316   )
317   if (NOT HAD_ERROR)
318     if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
319       string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
320     elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
321       string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
322     endif()
323   else()
324     message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
325   endif()
326 endif()
327
328 configure_file(
329   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
330   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
331
332 include(CMakeParseArguments)
333 include(AddClang)
334
335 set(CMAKE_INCLUDE_CURRENT_DIR ON)
336
337 include_directories(BEFORE
338   ${CMAKE_CURRENT_BINARY_DIR}/include
339   ${CMAKE_CURRENT_SOURCE_DIR}/include
340   )
341
342 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
343   install(DIRECTORY include/clang include/clang-c
344     DESTINATION include
345     FILES_MATCHING
346     PATTERN "*.def"
347     PATTERN "*.h"
348     PATTERN "config.h" EXCLUDE
349     PATTERN ".svn" EXCLUDE
350     )
351
352   install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
353     DESTINATION include
354     FILES_MATCHING
355     PATTERN "CMakeFiles" EXCLUDE
356     PATTERN "*.inc"
357     PATTERN "*.h"
358     )
359 endif()
360
361 add_definitions( -D_GNU_SOURCE )
362
363 option(CLANG_BUILD_TOOLS
364   "Build the Clang tools. If OFF, just generate build targets." ON)
365
366 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
367 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
368
369 if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
370   message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
371 endif()
372
373 if(CLANG_ENABLE_ARCMT)
374   add_definitions(-DCLANG_ENABLE_ARCMT)
375   add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
376 endif()
377 if(CLANG_ENABLE_STATIC_ANALYZER)
378   add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
379 endif()
380
381 # Clang version information
382 set(CLANG_EXECUTABLE_VERSION
383      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
384     "Version number that will be placed into the clang executable, in the form XX.YY")
385 set(LIBCLANG_LIBRARY_VERSION
386      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
387     "Version number that will be placed into the libclang library , in the form XX.YY")
388 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
389
390 option(CLANG_INCLUDE_TESTS
391        "Generate build targets for the Clang unit tests."
392        ${LLVM_INCLUDE_TESTS})
393
394 add_subdirectory(utils/TableGen)
395
396 add_subdirectory(include)
397
398 # All targets below may depend on all tablegen'd files.
399 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
400 list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
401
402 add_subdirectory(lib)
403 add_subdirectory(tools)
404 add_subdirectory(runtime)
405
406 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
407 add_subdirectory(examples)
408
409 if(APPLE)
410   # this line is needed as a cleanup to ensure that any CMakeCaches with the old
411   # default value get updated to the new default.
412   if(CLANG_ORDER_FILE STREQUAL "")
413     unset(CLANG_ORDER_FILE CACHE)
414     unset(CLANG_ORDER_FILE)
415   endif()
416
417
418   set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
419     "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
420
421   if(NOT EXISTS ${CLANG_ORDER_FILE})
422     string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
423     if(PATH_START EQUAL 0)
424       file(WRITE ${CLANG_ORDER_FILE} "\n")
425     else()
426       message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
427     endif()
428   endif()
429 endif()
430
431
432 if( CLANG_INCLUDE_TESTS )
433   if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
434     add_subdirectory(unittests)
435     list(APPEND CLANG_TEST_DEPS ClangUnitTests)
436     list(APPEND CLANG_TEST_PARAMS
437       clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
438       )
439   endif()
440   add_subdirectory(test)
441
442   if(CLANG_BUILT_STANDALONE)
443     # Add a global check rule now that all subdirectories have been traversed
444     # and we know the total set of lit testsuites.
445     get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
446     get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
447     get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
448     get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
449     add_lit_target(check-all
450       "Running all regression tests"
451       ${LLVM_LIT_TESTSUITES}
452       PARAMS ${LLVM_LIT_PARAMS}
453       DEPENDS ${LLVM_LIT_DEPENDS}
454       ARGS ${LLVM_LIT_EXTRA_ARGS}
455       )
456   endif()
457   add_subdirectory(utils/perf-training)
458 endif()
459
460 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
461   ${LLVM_INCLUDE_DOCS})
462 if( CLANG_INCLUDE_DOCS )
463   add_subdirectory(docs)
464 endif()
465
466 add_subdirectory(cmake/modules)
467
468 if(CLANG_STAGE)
469   message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
470 endif()
471
472 if (CLANG_ENABLE_BOOTSTRAP)
473   include(ExternalProject)
474
475   add_custom_target(clang-bootstrap-deps DEPENDS clang)
476
477   if(NOT CLANG_STAGE)
478     set(CLANG_STAGE stage1)
479   endif()
480
481   string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
482   if(MATCHED_STAGE)
483     if(NOT LLVM_BUILD_INSTRUMENTED)
484       math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
485       set(NEXT_CLANG_STAGE stage${STAGE_NUM})
486     else()
487       set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
488     endif()
489   else()
490     set(NEXT_CLANG_STAGE bootstrap)
491   endif()
492
493   if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
494     set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
495   endif()
496   message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
497   
498   
499   set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
500   set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
501
502   if(BOOTSTRAP_LLVM_ENABLE_LLD)
503     add_dependencies(clang-bootstrap-deps lld)
504   endif()
505
506   # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
507   if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
508     if(APPLE)
509       add_dependencies(clang-bootstrap-deps LTO)
510       # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
511       # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
512       # so that the host object file tools will use the just-built libLTO.
513       # However if System Integrity Protection is enabled the DYLD variables
514       # will be scrubbed from the environment of any base system commands. This
515       # includes /bin/sh, which ninja uses when executing build commands. To
516       # work around the envar being filtered away we pass it in as a CMake
517       # variable, and have LLVM's CMake append the envar to the archiver calls.
518       set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
519         -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
520     elseif(NOT WIN32)
521       add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
522       if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
523         add_dependencies(clang-bootstrap-deps LLVMgold)
524       endif()
525       set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
526       set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
527     endif()
528   endif()
529
530   add_custom_target(${NEXT_CLANG_STAGE}-clear
531     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
532     )
533   add_custom_command(
534     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
535     DEPENDS clang-bootstrap-deps
536     COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
537     COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
538     COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
539     COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
540     COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
541     )
542
543   if(CMAKE_VERBOSE_MAKEFILE)
544     set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
545   endif()
546
547   set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
548     PACKAGE_VERSION
549     PACKAGE_VENDOR
550     LLVM_VERSION_MAJOR
551     LLVM_VERSION_MINOR
552     LLVM_VERSION_PATCH
553     CLANG_VERSION_MAJOR
554     CLANG_VERSION_MINOR
555     CLANG_VERSION_PATCHLEVEL
556     LLVM_VERSION_SUFFIX
557     LLVM_BINUTILS_INCDIR
558     CLANG_REPOSITORY_STRING
559     CMAKE_MAKE_PROGRAM
560     CMAKE_OSX_ARCHITECTURES)
561
562   # We don't need to depend on compiler-rt if we're building instrumented
563   # because the next stage will use the same compiler used to build this stage.
564   if(TARGET compiler-rt AND NOT LLVM_BUILD_INSTRUMENTED)
565     add_dependencies(clang-bootstrap-deps compiler-rt)
566   endif()
567
568   set(COMPILER_OPTIONS
569     -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
570     -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
571     -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
572
573   if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
574     add_dependencies(clang-bootstrap-deps llvm-profdata)
575     set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
576   endif()
577
578   if(LLVM_BUILD_INSTRUMENTED)
579     add_dependencies(clang-bootstrap-deps generate-profdata)
580     set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
581     # Use the current tools for LTO instead of the instrumented ones
582     list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
583       CMAKE_CXX_COMPILER
584       CMAKE_C_COMPILER
585       CMAKE_ASM_COMPILER
586       CMAKE_AR
587       CMAKE_RANLIB
588       DARWIN_LTO_LIBRARY
589       DYLD_LIBRARY_PATH)
590
591     set(COMPILER_OPTIONS)
592     set(LTO_LIBRARY)
593     set(LTO_AR)
594     set(LTO_RANLIB)
595   endif()
596
597   # Find all variables that start with BOOTSTRAP_ and populate a variable with
598   # them.
599   get_cmake_property(variableNames VARIABLES)
600   foreach(variableName ${variableNames})
601     if(variableName MATCHES "^BOOTSTRAP_")
602       string(SUBSTRING ${variableName} 10 -1 varName)
603       string(REPLACE ";" "\;" value "${${variableName}}")
604       list(APPEND PASSTHROUGH_VARIABLES
605         -D${varName}=${value})
606     endif()
607     if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
608       list(APPEND PASSTHROUGH_VARIABLES
609         -D${variableName}=${${variableName}})
610     endif()
611   endforeach()
612
613   # Populate the passthrough variables
614   foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
615     if(DEFINED ${variableName})
616       if("${${variableName}}" STREQUAL "")
617         set(value "")
618       else()
619         string(REPLACE ";" "\;" value ${${variableName}})
620       endif()
621       list(APPEND PASSTHROUGH_VARIABLES
622         -D${variableName}=${value})
623     endif()
624   endforeach()
625
626   ExternalProject_Add(${NEXT_CLANG_STAGE}
627     DEPENDS clang-bootstrap-deps
628     PREFIX ${NEXT_CLANG_STAGE}
629     SOURCE_DIR ${CMAKE_SOURCE_DIR}
630     STAMP_DIR ${STAMP_DIR}
631     BINARY_DIR ${BINARY_DIR}
632     EXCLUDE_FROM_ALL 1
633     CMAKE_ARGS
634                 # We shouldn't need to set this here, but INSTALL_DIR doesn't
635                 # seem to work, so instead I'm passing this through
636                 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
637                 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
638                 ${PASSTHROUGH_VARIABLES}
639                  -DCLANG_STAGE=${NEXT_CLANG_STAGE}
640                 ${COMPILER_OPTIONS}
641                 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
642     INSTALL_COMMAND ""
643     STEP_TARGETS configure build
644     USES_TERMINAL_CONFIGURE 1
645     USES_TERMINAL_BUILD 1
646     USES_TERMINAL_INSTALL 1
647     )
648
649   # exclude really-install from main target
650   set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
651   ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
652     COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
653     COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
654     DEPENDEES build
655     USES_TERMINAL 1
656   )
657   ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
658   add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
659
660   if(NOT CLANG_BOOTSTRAP_TARGETS)
661     set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
662   endif()
663   foreach(target ${CLANG_BOOTSTRAP_TARGETS})
664     # exclude from main target
665     set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
666
667     ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
668       COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
669       COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
670       DEPENDEES configure
671       USES_TERMINAL 1
672     )
673
674     if(target MATCHES "^stage[0-9]*")
675       add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
676     endif()
677
678     ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
679   endforeach()
680 endif()
681
682 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
683   add_subdirectory(utils/ClangVisualizers)
684 endif()