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