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