1 cmake_minimum_required(VERSION 3.20.0)
3 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
4 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
9 # If we are not building as a part of LLVM, build Clang as an
10 # standalone project, using LLVM as an external library:
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
13 set(CLANG_BUILT_STANDALONE TRUE)
16 # Must go below project(..)
17 include(GNUInstallDirs)
19 if(CLANG_BUILT_STANDALONE)
20 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
21 set(CMAKE_CXX_STANDARD_REQUIRED YES)
22 set(CMAKE_CXX_EXTENSIONS NO)
25 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
26 CACHE BOOL "Enable assertions")
27 # Assertions should follow llvm-config's.
28 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
31 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
32 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
34 # Turn into CACHE PATHs for overwritting
35 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
36 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
37 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
38 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
39 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
41 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
44 # They are used as destination of target generators.
45 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
46 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
48 # DLL platform -- put DLLs into bin.
49 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
51 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
54 option(LLVM_INSTALL_TOOLCHAIN_ONLY
55 "Only include toolchain files in the 'install' target." OFF)
57 option(LLVM_FORCE_USE_OLD_TOOLCHAIN
58 "Set to ON to force using an old, unsupported host toolchain." OFF)
59 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
60 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
64 include(HandleLLVMOptions)
65 include(VersionFromVCS)
67 include(GetErrcMessages)
68 include(LLVMDistributionSupport)
70 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
71 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
72 "Default URL where bug reports are to be submitted.")
74 if (NOT DEFINED LLVM_INCLUDE_TESTS)
75 set(LLVM_INCLUDE_TESTS ON)
78 include_directories(${LLVM_INCLUDE_DIRS})
79 link_directories("${LLVM_LIBRARY_DIR}")
81 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
82 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
83 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
85 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
86 COMPONENTS Interpreter)
88 if(LLVM_INCLUDE_TESTS)
89 # Check prebuilt llvm/utils.
90 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
91 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
92 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
93 set(LLVM_UTILS_PROVIDED ON)
98 NAMES llvm-lit lit.py lit
99 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
100 DOC "Path to lit.py")
102 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
103 # Note: path not really used, except for checking if lit was found
104 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
105 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
107 if(NOT LLVM_UTILS_PROVIDED)
108 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
109 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
110 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
111 set(LLVM_UTILS_PROVIDED ON)
112 set(CLANG_TEST_DEPS FileCheck count not)
116 if (NOT TARGET llvm_gtest)
117 message(FATAL_ERROR "llvm-gtest not found. Please install llvm-gtest or disable tests with -DLLVM_INCLUDE_TESTS=OFF")
121 # Define the default arguments to use with 'lit', and an option for the user
123 set(LIT_ARGS_DEFAULT "-sv")
125 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
127 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
129 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
131 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
132 if( WIN32 AND NOT CYGWIN )
133 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
136 set(LLVM_INCLUDE_TESTS OFF)
139 umbrella_lit_testsuite_begin(check-all)
140 endif() # LLVM_INCLUDE_TESTS
143 # Make sure that our source directory is on the current cmake module path so that
144 # we can include cmake files from this directory.
145 list(INSERT CMAKE_MODULE_PATH 0
146 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
147 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
150 # This allows disabling clang's XML dependency even if LLVM finds libxml2.
151 # By default, clang depends on libxml2 if LLVM does.
152 option(CLANG_ENABLE_LIBXML2 "Whether libclang may depend on libxml2"
153 ${LLVM_ENABLE_LIBXML2})
155 if(CLANG_ENABLE_LIBXML2)
156 # Don't look for libxml if we're using MSan, since uninstrumented third party
157 # code may call MSan interceptors like strlen, leading to false positives.
158 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
159 set (LIBXML2_FOUND 0)
160 find_package(LibXml2 2.5.3 QUIET)
162 set(CLANG_HAVE_LIBXML 1)
167 include(CheckIncludeFile)
168 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
170 set(CLANG_RESOURCE_DIR "" CACHE STRING
171 "Relative directory from the Clang binary to its resource files.")
173 set(C_INCLUDE_DIRS "" CACHE STRING
174 "Colon separated list of directories clang will search for headers.")
176 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
177 set(DEFAULT_SYSROOT "" CACHE STRING
178 "Default <path> to all compiler invocations for --sysroot=<path>." )
180 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
182 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
183 "enable x86 relax relocations by default")
185 set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
186 "Enable IEEE binary128 as default long double format on PowerPC Linux.")
188 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
189 "Whether clang should use a new process for the CC1 invocation")
191 option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
193 set(CLANG_DEFAULT_LINKER "" CACHE STRING
194 "Default linker to use (linker name or absolute path, empty for platform default)")
196 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
197 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
198 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
199 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
200 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
201 message(WARNING "Resetting default C++ stdlib to use platform default")
202 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
203 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
206 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
207 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
208 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
209 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
210 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
211 message(WARNING "Resetting default rtlib to use platform default")
212 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
213 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
216 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
217 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
218 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
219 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
220 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
224 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
225 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
226 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
227 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
228 message(WARNING "Resetting default unwindlib to use platform default")
229 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
230 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
233 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
234 "Default objcopy executable to use.")
236 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
237 "Default OpenMP runtime used by -fopenmp.")
239 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
241 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
242 "Vendor-specific text for showing with version information.")
244 set(CLANG_REPOSITORY_STRING "" CACHE STRING
245 "Vendor-specific text for showing the repository the source is taken from.")
247 if(CLANG_REPOSITORY_STRING)
248 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
251 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
252 "Vendor-specific uti.")
254 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
255 "Python versions to install libclang python bindings for")
257 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
258 "Link tools against libclang-cpp.so")
260 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
261 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
262 "LLVM_LINK_LLVM_DYLIB=OFF")
265 # The libdir suffix must exactly match whatever LLVM's configuration used.
266 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
268 set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
269 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
270 mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
272 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
273 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
275 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
276 message(FATAL_ERROR "In-source builds are not allowed. "
277 "Please create a directory and run cmake "
278 "from there, passing the path to this source directory as the last argument. "
279 "This process created the file `CMakeCache.txt' and the directory "
280 "`CMakeFiles'. Please delete them.")
283 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
284 if(NOT DEFINED CLANG_VERSION_MAJOR)
285 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
287 if(NOT DEFINED CLANG_VERSION_MINOR)
288 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
290 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
291 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
293 if(NOT DEFINED CLANG_VERSION_SUFFIX)
294 set(CLANG_VERSION_SUFFIX ${LLVM_VERSION_SUFFIX})
296 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}${CLANG_VERSION_SUFFIX}")
297 message(STATUS "Clang version: ${CLANG_VERSION}")
299 # Configure the Version.inc file.
301 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
302 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
304 # Add appropriate flags for GCC
305 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
306 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
307 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
308 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
311 # Enable -pedantic for Clang even if it's not enabled for LLVM.
312 if (NOT LLVM_ENABLE_PEDANTIC)
313 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
316 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
317 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
318 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
322 # Determine HOST_LINK_VERSION on Darwin.
323 set(HOST_LINK_VERSION)
324 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
327 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
328 RESULT_VARIABLE HAD_ERROR
329 OUTPUT_VARIABLE LD_V_OUTPUT
332 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
334 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
335 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
336 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
337 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
339 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
342 include(CMakeParseArguments)
345 set(CMAKE_INCLUDE_CURRENT_DIR ON)
347 include_directories(BEFORE
348 ${CMAKE_CURRENT_BINARY_DIR}/include
349 ${CMAKE_CURRENT_SOURCE_DIR}/include
352 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
353 install(DIRECTORY include/clang include/clang-c
354 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
355 COMPONENT clang-headers
359 PATTERN "config.h" EXCLUDE
362 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
363 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
364 COMPONENT clang-headers
366 PATTERN "CMakeFiles" EXCLUDE
371 # Installing the headers needs to depend on generating any public
372 # tablegen'd headers.
373 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
374 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
375 if(NOT LLVM_ENABLE_IDE)
376 add_llvm_install_targets(install-clang-headers
377 DEPENDS clang-headers
378 COMPONENT clang-headers)
381 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
382 install(FILES utils/bash-autocomplete.sh
383 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
384 COMPONENT bash-autocomplete)
385 if(NOT LLVM_ENABLE_IDE)
386 add_llvm_install_targets(install-bash-autocomplete
387 DEPENDS bash-autocomplete
388 COMPONENT bash-autocomplete)
392 option(CLANG_BUILD_TOOLS
393 "Build the Clang tools. If OFF, just generate build targets." ON)
395 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
396 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
398 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
400 CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
401 "Build clang with plugin support" ON
402 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
404 # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
405 # itself, which is not possible in many platforms because of current limitations in
406 # JIT stack. (more platforms need to be supported by JITLink)
407 if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
408 set(HAVE_CLANG_REPL_SUPPORT ON)
411 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
412 option(CLANG_ENABLE_STATIC_ANALYZER
413 "Include static analyzer in clang binary." ON)
415 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
417 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
418 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
421 if(CLANG_ENABLE_ARCMT)
422 set(CLANG_ENABLE_OBJC_REWRITER ON)
425 # This option is a stop-gap, we should commit to removing this as
426 # soon as possible. See discussion:
427 # https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
428 option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
429 "Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
431 # Clang version information
432 set(CLANG_EXECUTABLE_VERSION
433 "${CLANG_VERSION_MAJOR}" CACHE STRING
434 "Major version number that will be appended to the clang executable name")
435 set(LIBCLANG_LIBRARY_VERSION
436 "${CLANG_VERSION_MAJOR}" CACHE STRING
437 "Major version number that will be appended to the libclang library")
438 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
440 option(CLANG_INCLUDE_TESTS
441 "Generate build targets for the Clang unit tests."
442 ${LLVM_INCLUDE_TESTS})
444 option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
445 # While HLSL support is experimental this should stay hidden.
446 mark_as_advanced(CLANG_ENABLE_HLSL)
448 add_subdirectory(utils/TableGen)
450 # Export CLANG_TABLEGEN_EXE for use by flang docs.
451 set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
453 add_subdirectory(include)
455 # All targets below may depend on all tablegen'd files.
456 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
457 add_custom_target(clang-tablegen-targets
461 ${CLANG_TABLEGEN_TARGETS})
462 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
463 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
465 # Force target to be built as soon as possible. Clang modules builds depend
466 # header-wise on it as they ship all headers from the umbrella folders. Building
467 # an entire module might include header, which depends on intrinsics_gen.
468 if(LLVM_ENABLE_MODULES)
469 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
472 add_subdirectory(lib)
473 add_subdirectory(tools)
474 add_subdirectory(runtime)
476 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
477 add_subdirectory(examples)
480 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
481 # default value get updated to the new default.
482 if(CLANG_ORDER_FILE STREQUAL "")
483 unset(CLANG_ORDER_FILE CACHE)
484 unset(CLANG_ORDER_FILE)
488 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
489 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
491 if(NOT EXISTS ${CLANG_ORDER_FILE})
492 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
493 if(PATH_START EQUAL 0)
494 file(WRITE ${CLANG_ORDER_FILE} "\n")
496 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
502 if( CLANG_INCLUDE_TESTS )
503 add_subdirectory(unittests)
504 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
505 list(APPEND CLANG_TEST_PARAMS
506 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
508 add_subdirectory(test)
509 add_subdirectory(bindings/python/tests)
511 if(CLANG_BUILT_STANDALONE)
512 umbrella_lit_testsuite_end(check-all)
514 add_subdirectory(utils/perf-training)
517 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
518 ${LLVM_INCLUDE_DOCS})
519 if( CLANG_INCLUDE_DOCS )
520 add_subdirectory(docs)
523 # Custom target to install all clang libraries.
524 add_custom_target(clang-libraries)
525 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
527 if(NOT LLVM_ENABLE_IDE)
528 add_llvm_install_targets(install-clang-libraries
529 DEPENDS clang-libraries
530 COMPONENT clang-libraries)
533 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
535 list(REMOVE_DUPLICATES CLANG_LIBS)
536 foreach(lib ${CLANG_LIBS})
537 add_dependencies(clang-libraries ${lib})
538 if(NOT LLVM_ENABLE_IDE)
539 add_dependencies(install-clang-libraries install-${lib})
540 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
545 add_subdirectory(cmake/modules)
548 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
551 if (CLANG_ENABLE_BOOTSTRAP)
552 include(ExternalProject)
554 add_custom_target(clang-bootstrap-deps DEPENDS clang)
557 set(CLANG_STAGE stage1)
560 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
562 if(NOT LLVM_BUILD_INSTRUMENTED)
563 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
564 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
566 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
569 set(NEXT_CLANG_STAGE bootstrap)
572 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
573 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
575 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
578 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
579 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
581 if(BOOTSTRAP_LLVM_ENABLE_LLD)
582 # adding lld to clang-bootstrap-deps without having it enabled in
583 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
584 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
585 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
587 add_dependencies(clang-bootstrap-deps lld)
591 # Build llvm-rc and llvm-mt which are needed by the Windows build.
592 add_dependencies(clang-bootstrap-deps llvm-rc)
593 if(LLVM_ENABLE_LIBXML2)
594 add_dependencies(clang-bootstrap-deps llvm-mt)
598 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
599 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
601 add_dependencies(clang-bootstrap-deps LTO)
602 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
603 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
604 # so that the host object file tools will use the just-built libLTO.
605 # However if System Integrity Protection is enabled the DYLD variables
606 # will be scrubbed from the environment of any base system commands. This
607 # includes /bin/sh, which ninja uses when executing build commands. To
608 # work around the envar being filtered away we pass it in as a CMake
609 # variable, and have LLVM's CMake append the envar to the archiver calls.
610 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
611 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
613 add_dependencies(clang-bootstrap-deps llvm-lib)
614 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
616 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
617 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
618 add_dependencies(clang-bootstrap-deps LLVMgold)
620 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
621 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
625 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
626 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
629 add_custom_target(${NEXT_CLANG_STAGE}-clear
630 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
633 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
634 DEPENDS clang-bootstrap-deps
635 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
636 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
637 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
638 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
639 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
642 if(CMAKE_VERBOSE_MAKEFILE)
643 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
646 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
654 CLANG_VERSION_PATCHLEVEL
659 CLANG_REPOSITORY_STRING
661 CMAKE_OSX_ARCHITECTURES
664 LLVM_ENABLE_RUNTIMES)
666 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
667 # because the next stage will use the same compiler used to build this stage.
668 if(NOT LLVM_BUILD_INSTRUMENTED)
669 if(TARGET compiler-rt)
670 add_dependencies(clang-bootstrap-deps compiler-rt)
672 if(TARGET cxx-headers)
673 add_dependencies(clang-bootstrap-deps cxx-headers)
677 set(C_COMPILER "clang")
678 set(CXX_COMPILER "clang++")
680 set(C_COMPILER "clang-cl.exe")
681 set(CXX_COMPILER "clang-cl.exe")
685 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
686 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
687 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
688 -DCMAKE_ASM_COMPILER_ID=Clang)
690 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
691 # otherwise it defaults the linker to be link.exe.
692 if(BOOTSTRAP_LLVM_ENABLE_LLD)
693 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
694 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
698 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
699 set(${CLANG_STAGE}_TABLEGEN
700 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
701 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
702 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
703 if(BOOTSTRAP_LLVM_ENABLE_LLD)
704 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
706 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
707 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
708 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
709 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
711 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
712 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
713 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
714 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
718 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
719 add_dependencies(clang-bootstrap-deps llvm-profdata)
720 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
723 if(LLVM_BUILD_INSTRUMENTED)
724 add_dependencies(clang-bootstrap-deps generate-profdata)
725 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
726 # Use the current tools for LTO instead of the instrumented ones
727 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
736 set(COMPILER_OPTIONS)
742 # Populate the passthrough variables
743 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
744 if(DEFINED ${variableName})
745 if("${${variableName}}" STREQUAL "")
748 string(REPLACE ";" "|" value "${${variableName}}")
750 list(APPEND PASSTHROUGH_VARIABLES
751 -D${variableName}=${value})
755 # Find all variables that start with BOOTSTRAP_ and populate a variable with
757 get_cmake_property(variableNames VARIABLES)
758 foreach(variableName ${variableNames})
759 if(variableName MATCHES "^BOOTSTRAP_")
760 string(SUBSTRING ${variableName} 10 -1 varName)
761 string(REPLACE ";" "|" value "${${variableName}}")
762 list(APPEND PASSTHROUGH_VARIABLES
763 -D${varName}=${value})
765 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
766 list(APPEND PASSTHROUGH_VARIABLES
767 -D${variableName}=${${variableName}})
771 # Build arguments for native tool used in CMake.
772 set(build_configuration "$<CONFIG>")
773 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
774 if(NOT build_tool_args STREQUAL "")
775 string(PREPEND build_tool_args "-- ")
776 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
779 ExternalProject_Add(${NEXT_CLANG_STAGE}
780 DEPENDS clang-bootstrap-deps
781 PREFIX ${NEXT_CLANG_STAGE}
782 SOURCE_DIR ${CMAKE_SOURCE_DIR}
783 STAMP_DIR ${STAMP_DIR}
784 BINARY_DIR ${BINARY_DIR}
787 # We shouldn't need to set this here, but INSTALL_DIR doesn't
788 # seem to work, so instead I'm passing this through
789 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
790 ${PASSTHROUGH_VARIABLES}
791 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
792 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
794 ${${CLANG_STAGE}_TABLEGEN}
795 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
796 ${${CLANG_STAGE}_LINKER}
798 ${${CLANG_STAGE}_RANLIB}
799 ${${CLANG_STAGE}_OBJCOPY}
800 ${${CLANG_STAGE}_STRIP}
801 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
802 --config ${build_configuration}
805 STEP_TARGETS configure build
806 USES_TERMINAL_CONFIGURE 1
807 USES_TERMINAL_BUILD 1
808 USES_TERMINAL_INSTALL 1
812 # exclude really-install from main target
813 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
814 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
815 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
816 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
820 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
821 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
823 if(NOT CLANG_BOOTSTRAP_TARGETS)
824 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
826 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
827 # Install targets have side effects, so we always want to execute them.
828 # "install" is reserved by CMake and can't be used as a step name for
829 # ExternalProject_Add_Step, so we can match against "^install-" instead of
830 # "^install" to get a tighter match. CMake's installation scripts already
831 # skip up-to-date files, so there's no behavior change if you install to the
832 # same destination multiple times.
833 if(target MATCHES "^install-")
839 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
840 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
841 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
843 ALWAYS ${step_always}
848 if(target MATCHES "^stage[0-9]*")
849 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
852 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
856 if (CLANG_BOLT_INSTRUMENT AND NOT LLVM_BUILD_INSTRUMENTED)
857 set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
858 set(CLANG_INSTRUMENTED ${CLANG_PATH}-bolt.inst)
859 set(BOLT_FDATA ${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/prof.fdata)
861 # Instrument clang with BOLT
862 add_custom_target(clang-instrumented
863 DEPENDS ${CLANG_INSTRUMENTED}
865 add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
866 DEPENDS clang llvm-bolt
867 COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
868 -instrument --instrumentation-file-append-pid
869 --instrumentation-file=${BOLT_FDATA}
870 COMMENT "Instrumenting clang binary with BOLT"
874 # Optimize original (pre-bolt) Clang using the collected profile
875 set(CLANG_OPTIMIZED ${CMAKE_CURRENT_BINARY_DIR}/clang.bolt)
876 add_custom_target(clang-bolt
877 DEPENDS ${CLANG_OPTIMIZED}
879 add_custom_command(OUTPUT ${CLANG_OPTIMIZED}
880 DEPENDS clang-bolt-profile
881 COMMAND llvm-bolt ${CLANG_PATH}
882 -o ${CLANG_OPTIMIZED}
884 -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions
885 -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack
886 COMMAND ${CMAKE_COMMAND} -E rename ${CLANG_OPTIMIZED} $<TARGET_FILE:clang>
887 COMMENT "Optimizing Clang with BOLT"
892 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
893 add_subdirectory(utils/ClangVisualizers)
895 add_subdirectory(utils/hmaptool)
897 if(CLANG_BUILT_STANDALONE)
898 llvm_distribution_add_targets()
899 process_llvm_pass_plugins()
902 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
905 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
906 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)