1 cmake_minimum_required(VERSION 3.13.4)
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 )
8 set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard to conform to")
9 set(CMAKE_CXX_STANDARD_REQUIRED YES)
10 set(CMAKE_CXX_EXTENSIONS NO)
12 # Rely on llvm-config.
15 set (LLVM_CONFIG_FOUND 1)
16 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
17 message(DEPRECATION "Using llvm-config to detect the LLVM installation is \
18 deprecated. The installed cmake files should be used \
19 instead. CMake should be able to detect your LLVM install \
20 automatically, but you can also use LLVM_DIR to specify \
21 the path containing LLVMConfig.cmake.")
22 set(CONFIG_COMMAND ${LLVM_CONFIG}
31 COMMAND ${CONFIG_COMMAND}
32 RESULT_VARIABLE HAD_ERROR
33 OUTPUT_VARIABLE CONFIG_OUTPUT
37 "[ \t]*[\r\n]+[ \t]*" ";"
38 CONFIG_OUTPUT ${CONFIG_OUTPUT})
40 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
41 message(STATUS "${CONFIG_COMMAND_STR}")
42 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
45 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
46 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
47 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
48 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
49 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
50 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
51 list(GET CONFIG_OUTPUT 6 LLVM_CONFIG_CMAKE_PATH)
53 # Normalize LLVM_CMAKE_PATH. --cmakedir might contain backslashes.
54 # CMake assumes slashes as PATH.
55 file(TO_CMAKE_PATH ${LLVM_CONFIG_CMAKE_PATH} LLVM_CMAKE_PATH)
60 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
61 CACHE BOOL "Enable assertions")
62 # Assertions should follow llvm-config's.
63 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
66 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_PATH}")
67 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
69 # We can't check LLVM_CONFIG here, because find_package(LLVM ...) also sets
71 if (NOT LLVM_CONFIG_FOUND)
72 # Pull values from LLVMConfig.cmake. We can drop this once the llvm-config
74 set(TOOLS_BINARY_DIR ${LLVM_TOOLS_BINARY_DIR})
75 set(LIBRARY_DIR ${LLVM_LIBRARY_DIR})
76 set(INCLUDE_DIR ${LLVM_INCLUDE_DIR})
77 set(LLVM_OBJ_DIR ${LLVM_BINARY_DIR})
78 # The LLVM_CMAKE_PATH variable is set when doing non-standalone builds and
79 # used in this project, so we need to make sure we set this value.
80 # FIXME: LLVM_CMAKE_DIR comes from LLVMConfig.cmake. We should rename
81 # LLVM_CMAKE_PATH to LLVM_CMAKE_DIR throughout the project.
82 set(LLVM_CMAKE_PATH ${LLVM_CMAKE_DIR})
85 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
86 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
87 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
88 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
89 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
91 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
94 # They are used as destination of target generators.
95 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
96 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
98 # DLL platform -- put DLLs into bin.
99 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
101 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
104 option(LLVM_INSTALL_TOOLCHAIN_ONLY
105 "Only include toolchain files in the 'install' target." OFF)
107 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
108 "Set to ON to force using an old, unsupported host toolchain." OFF)
109 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
110 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
114 include(HandleLLVMOptions)
115 include(VersionFromVCS)
116 include(GetErrcMessages)
117 include(LLVMDistributionSupport)
119 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
120 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
121 "Default URL where bug reports are to be submitted.")
123 if (NOT DEFINED LLVM_INCLUDE_TESTS)
124 set(LLVM_INCLUDE_TESTS ON)
127 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
128 link_directories("${LLVM_LIBRARY_DIR}")
130 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
131 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
132 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
134 if(LLVM_INCLUDE_TESTS)
135 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
136 COMPONENTS Interpreter)
138 # Check prebuilt llvm/utils.
139 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
140 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
141 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
142 set(LLVM_UTILS_PROVIDED ON)
145 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
146 # Note: path not really used, except for checking if lit was found
147 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
148 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
149 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
151 if(NOT LLVM_UTILS_PROVIDED)
152 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
153 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
154 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
155 set(LLVM_UTILS_PROVIDED ON)
156 set(CLANG_TEST_DEPS FileCheck count not)
158 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
159 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
160 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
161 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
162 add_subdirectory(${UNITTEST_DIR} utils/unittest)
165 # Seek installed Lit.
166 find_program(LLVM_LIT
167 NAMES llvm-lit lit.py lit
168 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
169 DOC "Path to lit.py")
173 # Define the default arguments to use with 'lit', and an option for the user
175 set(LIT_ARGS_DEFAULT "-sv")
177 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
179 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
181 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
183 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
184 if( WIN32 AND NOT CYGWIN )
185 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
188 set(LLVM_INCLUDE_TESTS OFF)
192 set( CLANG_BUILT_STANDALONE 1 )
193 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
195 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
198 # Make sure that our source directory is on the current cmake module path so that
199 # we can include cmake files from this directory.
200 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
202 if(LLVM_ENABLE_LIBXML2)
203 # Don't look for libxml if we're using MSan, since uninstrumented third party
204 # code may call MSan interceptors like strlen, leading to false positives.
205 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
206 set (LIBXML2_FOUND 0)
207 find_package(LibXml2 2.5.3 QUIET)
209 set(CLANG_HAVE_LIBXML 1)
214 include(CheckIncludeFile)
215 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
217 set(CLANG_RESOURCE_DIR "" CACHE STRING
218 "Relative directory from the Clang binary to its resource files.")
220 set(C_INCLUDE_DIRS "" CACHE STRING
221 "Colon separated list of directories clang will search for headers.")
223 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
224 set(DEFAULT_SYSROOT "" CACHE STRING
225 "Default <path> to all compiler invocations for --sysroot=<path>." )
227 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
229 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
230 "enable x86 relax relocations by default")
232 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
233 "Whether clang should use a new process for the CC1 invocation")
235 # TODO: verify the values against LangStandards.def?
236 set(CLANG_DEFAULT_STD_C "" CACHE STRING
237 "Default standard to use for C/ObjC code (IDENT from LangStandards.def, empty for platform default)")
238 set(CLANG_DEFAULT_STD_CXX "" CACHE STRING
239 "Default standard to use for C++/ObjC++ code (IDENT from LangStandards.def, empty for platform default)")
241 set(CLANG_DEFAULT_LINKER "" CACHE STRING
242 "Default linker to use (linker name or absolute path, empty for platform default)")
244 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
245 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
246 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
247 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
248 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
249 message(WARNING "Resetting default C++ stdlib to use platform default")
250 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
251 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
254 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
255 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
256 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
257 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
258 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
259 message(WARNING "Resetting default rtlib to use platform default")
260 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
261 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
264 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
265 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
266 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
267 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
268 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
272 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
273 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
274 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
275 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
276 message(WARNING "Resetting default unwindlib to use platform default")
277 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
278 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
281 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
282 "Default objcopy executable to use.")
284 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
285 "Default OpenMP runtime used by -fopenmp.")
287 # OpenMP offloading requires at least sm_35 because we use shuffle instructions
288 # to generate efficient code for reductions and the atomicMax instruction on
289 # 64-bit integers in the implementation of conditional lastprivate.
290 set(CUDA_ARCH_FLAGS "sm_35")
292 # Try to find the highest Nvidia GPU architecture the system supports
293 if (NOT DEFINED CLANG_OPENMP_NVPTX_DEFAULT_ARCH)
294 find_package(CUDA QUIET)
296 cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS)
299 set(CUDA_ARCH_FLAGS ${CLANG_OPENMP_NVPTX_DEFAULT_ARCH})
302 string(REGEX MATCH "sm_([0-9]+)" CUDA_ARCH_MATCH ${CUDA_ARCH_FLAGS})
303 if (NOT DEFINED CUDA_ARCH_MATCH OR "${CMAKE_MATCH_1}" LESS 35)
304 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH "sm_35" CACHE STRING
305 "Default architecture for OpenMP offloading to Nvidia GPUs." FORCE)
306 message(WARNING "Resetting default architecture for OpenMP offloading to Nvidia GPUs to sm_35")
308 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH ${CUDA_ARCH_MATCH} CACHE STRING
309 "Default architecture for OpenMP offloading to Nvidia GPUs.")
312 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
314 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
315 "Vendor-specific text for showing with version information.")
317 set(CLANG_REPOSITORY_STRING "" CACHE STRING
318 "Vendor-specific text for showing the repository the source is taken from.")
320 if(CLANG_REPOSITORY_STRING)
321 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
324 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
325 "Vendor-specific uti.")
327 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
328 "Python versions to install libclang python bindings for")
330 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
331 "Link tools against libclang-cpp.so")
333 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
334 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
335 "LLVM_LINK_LLVM_DYLIB=OFF")
338 # The libdir suffix must exactly match whatever LLVM's configuration used.
339 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
341 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
342 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
344 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
345 message(FATAL_ERROR "In-source builds are not allowed. "
346 "Please create a directory and run cmake "
347 "from there, passing the path to this source directory as the last argument. "
348 "This process created the file `CMakeCache.txt' and the directory "
349 "`CMakeFiles'. Please delete them.")
352 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
353 if(NOT DEFINED CLANG_VERSION_MAJOR)
354 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
356 if(NOT DEFINED CLANG_VERSION_MINOR)
357 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
359 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
360 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
362 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
363 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
364 message(STATUS "Clang version: ${CLANG_VERSION}")
366 # Configure the Version.inc file.
368 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
369 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
371 # Add appropriate flags for GCC
372 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
373 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
374 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
375 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
378 # Enable -pedantic for Clang even if it's not enabled for LLVM.
379 if (NOT LLVM_ENABLE_PEDANTIC)
380 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
383 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
384 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
385 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
389 # Determine HOST_LINK_VERSION on Darwin.
390 set(HOST_LINK_VERSION)
394 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
395 RESULT_VARIABLE HAD_ERROR
396 OUTPUT_VARIABLE LD_V_OUTPUT
399 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
401 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
402 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
403 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
404 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
406 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
409 include(CMakeParseArguments)
412 set(CMAKE_INCLUDE_CURRENT_DIR ON)
414 include_directories(BEFORE
415 ${CMAKE_CURRENT_BINARY_DIR}/include
416 ${CMAKE_CURRENT_SOURCE_DIR}/include
419 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
420 install(DIRECTORY include/clang include/clang-c
422 COMPONENT clang-headers
426 PATTERN "config.h" EXCLUDE
429 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
431 COMPONENT clang-headers
433 PATTERN "CMakeFiles" EXCLUDE
438 # Installing the headers needs to depend on generating any public
439 # tablegen'd headers.
440 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
441 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
442 if(NOT LLVM_ENABLE_IDE)
443 add_llvm_install_targets(install-clang-headers
444 DEPENDS clang-headers
445 COMPONENT clang-headers)
448 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
449 install(PROGRAMS utils/bash-autocomplete.sh
450 DESTINATION share/clang
451 COMPONENT bash-autocomplete)
452 if(NOT LLVM_ENABLE_IDE)
453 add_llvm_install_targets(install-bash-autocomplete
454 DEPENDS bash-autocomplete
455 COMPONENT bash-autocomplete)
459 add_definitions( -D_GNU_SOURCE )
461 option(CLANG_BUILD_TOOLS
462 "Build the Clang tools. If OFF, just generate build targets." ON)
464 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
465 option(CLANG_ENABLE_STATIC_ANALYZER
466 "Include static analyzer in clang binary." ON)
468 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
470 option(CLANG_ROUND_TRIP_CC1_ARGS
471 "Round-trip command line arguments in -cc1." ${LLVM_ENABLE_ASSERTIONS})
473 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
474 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
477 if(CLANG_ENABLE_ARCMT)
478 set(CLANG_ENABLE_OBJC_REWRITER ON)
481 if (CLANG_ROUND_TRIP_CC1_ARGS)
482 add_definitions(-DCLANG_ROUND_TRIP_CC1_ARGS=ON)
485 # Clang version information
486 set(CLANG_EXECUTABLE_VERSION
487 "${CLANG_VERSION_MAJOR}" CACHE STRING
488 "Major version number that will be appended to the clang executable name")
489 set(LIBCLANG_LIBRARY_VERSION
490 "${CLANG_VERSION_MAJOR}" CACHE STRING
491 "Major version number that will be appended to the libclang library")
492 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
494 option(CLANG_INCLUDE_TESTS
495 "Generate build targets for the Clang unit tests."
496 ${LLVM_INCLUDE_TESTS})
498 add_subdirectory(utils/TableGen)
500 add_subdirectory(include)
502 # All targets below may depend on all tablegen'd files.
503 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
504 add_custom_target(clang-tablegen-targets
507 ${CLANG_TABLEGEN_TARGETS})
508 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
509 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
511 # Force target to be built as soon as possible. Clang modules builds depend
512 # header-wise on it as they ship all headers from the umbrella folders. Building
513 # an entire module might include header, which depends on intrinsics_gen.
514 if(LLVM_ENABLE_MODULES)
515 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
518 add_subdirectory(lib)
519 add_subdirectory(tools)
520 add_subdirectory(runtime)
522 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
523 add_subdirectory(examples)
526 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
527 # default value get updated to the new default.
528 if(CLANG_ORDER_FILE STREQUAL "")
529 unset(CLANG_ORDER_FILE CACHE)
530 unset(CLANG_ORDER_FILE)
534 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
535 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
537 if(NOT EXISTS ${CLANG_ORDER_FILE})
538 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
539 if(PATH_START EQUAL 0)
540 file(WRITE ${CLANG_ORDER_FILE} "\n")
542 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
548 if( CLANG_INCLUDE_TESTS )
549 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
550 add_subdirectory(unittests)
551 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
552 list(APPEND CLANG_TEST_PARAMS
553 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
556 add_subdirectory(test)
557 add_subdirectory(bindings/python/tests)
559 if(CLANG_BUILT_STANDALONE)
560 # Add a global check rule now that all subdirectories have been traversed
561 # and we know the total set of lit testsuites.
562 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
563 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
564 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
565 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
566 get_property(LLVM_ADDITIONAL_TEST_TARGETS
567 GLOBAL PROPERTY LLVM_ADDITIONAL_TEST_TARGETS)
568 add_lit_target(check-all
569 "Running all regression tests"
570 ${LLVM_LIT_TESTSUITES}
571 PARAMS ${LLVM_LIT_PARAMS}
572 DEPENDS ${LLVM_LIT_DEPENDS} ${LLVM_ADDITIONAL_TEST_TARGETS}
573 ARGS ${LLVM_LIT_EXTRA_ARGS}
576 add_subdirectory(utils/perf-training)
579 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
580 ${LLVM_INCLUDE_DOCS})
581 if( CLANG_INCLUDE_DOCS )
582 add_subdirectory(docs)
585 # Custom target to install all clang libraries.
586 add_custom_target(clang-libraries)
587 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
589 if(NOT LLVM_ENABLE_IDE)
590 add_llvm_install_targets(install-clang-libraries
591 DEPENDS clang-libraries
592 COMPONENT clang-libraries)
595 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
597 list(REMOVE_DUPLICATES CLANG_LIBS)
598 foreach(lib ${CLANG_LIBS})
599 add_dependencies(clang-libraries ${lib})
600 if(NOT LLVM_ENABLE_IDE)
601 add_dependencies(install-clang-libraries install-${lib})
602 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
607 add_subdirectory(cmake/modules)
610 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
613 if (CLANG_ENABLE_BOOTSTRAP)
614 include(ExternalProject)
616 add_custom_target(clang-bootstrap-deps DEPENDS clang)
619 set(CLANG_STAGE stage1)
622 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
624 if(NOT LLVM_BUILD_INSTRUMENTED)
625 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
626 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
628 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
631 set(NEXT_CLANG_STAGE bootstrap)
634 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
635 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
637 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
640 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
641 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
643 if(BOOTSTRAP_LLVM_ENABLE_LLD)
644 # adding lld to clang-bootstrap-deps without having it enabled in
645 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
646 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
647 message(FATAL_ERROR "LLD is enabled in the boostrap build, but lld is not in LLVM_ENABLE_PROJECTS")
649 add_dependencies(clang-bootstrap-deps lld)
652 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
653 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
655 add_dependencies(clang-bootstrap-deps LTO)
656 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
657 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
658 # so that the host object file tools will use the just-built libLTO.
659 # However if System Integrity Protection is enabled the DYLD variables
660 # will be scrubbed from the environment of any base system commands. This
661 # includes /bin/sh, which ninja uses when executing build commands. To
662 # work around the envar being filtered away we pass it in as a CMake
663 # variable, and have LLVM's CMake append the envar to the archiver calls.
664 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
665 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
667 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
668 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
669 add_dependencies(clang-bootstrap-deps LLVMgold)
671 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
672 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
676 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
677 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
680 add_custom_target(${NEXT_CLANG_STAGE}-clear
681 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
684 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
685 DEPENDS clang-bootstrap-deps
686 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
687 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
688 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
689 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
690 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
693 if(CMAKE_VERBOSE_MAKEFILE)
694 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
697 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
705 CLANG_VERSION_PATCHLEVEL
709 CLANG_REPOSITORY_STRING
710 CMAKE_C_COMPILER_LAUNCHER
711 CMAKE_CXX_COMPILER_LAUNCHER
713 CMAKE_OSX_ARCHITECTURES
715 LLVM_ENABLE_RUNTIMES)
717 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
718 # because the next stage will use the same compiler used to build this stage.
719 if(NOT LLVM_BUILD_INSTRUMENTED)
720 if(TARGET compiler-rt)
721 add_dependencies(clang-bootstrap-deps compiler-rt)
723 if(TARGET cxx-headers)
724 add_dependencies(clang-bootstrap-deps cxx-headers)
728 set(C_COMPILER "clang")
729 set(CXX_COMPILER "clang++")
731 set(C_COMPILER "clang-cl.exe")
732 set(CXX_COMPILER "clang-cl.exe")
736 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
737 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
738 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
739 -DCMAKE_ASM_COMPILER_ID=Clang)
741 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
742 # otherwise it defaults the linker to be link.exe.
743 if(BOOTSTRAP_LLVM_ENABLE_LLD)
744 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
745 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
749 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
750 set(${CLANG_STAGE}_CONFIG -DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config)
751 set(${CLANG_STAGE}_TABLEGEN
752 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
753 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
754 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
755 if(BOOTSTRAP_LLVM_ENABLE_LLD)
756 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
758 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
759 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
760 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
761 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
763 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
764 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
765 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
769 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
770 add_dependencies(clang-bootstrap-deps llvm-profdata)
771 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
774 if(LLVM_BUILD_INSTRUMENTED)
775 add_dependencies(clang-bootstrap-deps generate-profdata)
776 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
777 # Use the current tools for LTO instead of the instrumented ones
778 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
787 set(COMPILER_OPTIONS)
793 # Find all variables that start with BOOTSTRAP_ and populate a variable with
795 get_cmake_property(variableNames VARIABLES)
796 foreach(variableName ${variableNames})
797 if(variableName MATCHES "^BOOTSTRAP_")
798 string(SUBSTRING ${variableName} 10 -1 varName)
799 string(REPLACE ";" "|" value "${${variableName}}")
800 list(APPEND PASSTHROUGH_VARIABLES
801 -D${varName}=${value})
803 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
804 list(APPEND PASSTHROUGH_VARIABLES
805 -D${variableName}=${${variableName}})
809 # Populate the passthrough variables
810 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
811 if(DEFINED ${variableName})
812 if("${${variableName}}" STREQUAL "")
815 string(REPLACE ";" "|" value "${${variableName}}")
817 list(APPEND PASSTHROUGH_VARIABLES
818 -D${variableName}=${value})
822 ExternalProject_Add(${NEXT_CLANG_STAGE}
823 DEPENDS clang-bootstrap-deps
824 PREFIX ${NEXT_CLANG_STAGE}
825 SOURCE_DIR ${CMAKE_SOURCE_DIR}
826 STAMP_DIR ${STAMP_DIR}
827 BINARY_DIR ${BINARY_DIR}
830 # We shouldn't need to set this here, but INSTALL_DIR doesn't
831 # seem to work, so instead I'm passing this through
832 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
833 ${PASSTHROUGH_VARIABLES}
834 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
835 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
837 ${${CLANG_STAGE}_CONFIG}
838 ${${CLANG_STAGE}_TABLEGEN}
839 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
840 ${${CLANG_STAGE}_LINKER}
842 ${${CLANG_STAGE}_RANLIB}
843 ${${CLANG_STAGE}_OBJCOPY}
844 ${${CLANG_STAGE}_STRIP}
846 STEP_TARGETS configure build
847 USES_TERMINAL_CONFIGURE 1
848 USES_TERMINAL_BUILD 1
849 USES_TERMINAL_INSTALL 1
853 # exclude really-install from main target
854 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
855 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
856 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
857 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
861 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
862 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
864 if(NOT CLANG_BOOTSTRAP_TARGETS)
865 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
867 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
868 # Install targets have side effects, so we always want to execute them.
869 # "install" is reserved by CMake and can't be used as a step name for
870 # ExternalProject_Add_Step, so we can match against "^install-" instead of
871 # "^install" to get a tighter match. CMake's installation scripts already
872 # skip up-to-date files, so there's no behavior change if you install to the
873 # same destination multiple times.
874 if(target MATCHES "^install-")
880 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
881 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
882 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
884 ALWAYS ${step_always}
889 if(target MATCHES "^stage[0-9]*")
890 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
893 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
897 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
898 add_subdirectory(utils/ClangVisualizers)
900 add_subdirectory(utils/hmaptool)
902 if(CLANG_BUILT_STANDALONE)
903 llvm_distribution_add_targets()
904 process_llvm_pass_plugins()
908 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
909 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)