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 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
294 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
295 message(STATUS "Clang version: ${CLANG_VERSION}")
297 # Configure the Version.inc file.
299 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
300 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
302 # Add appropriate flags for GCC
303 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
304 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
305 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
306 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
309 # Enable -pedantic for Clang even if it's not enabled for LLVM.
310 if (NOT LLVM_ENABLE_PEDANTIC)
311 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
314 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
315 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
316 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
320 # Determine HOST_LINK_VERSION on Darwin.
321 set(HOST_LINK_VERSION)
322 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
325 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
326 RESULT_VARIABLE HAD_ERROR
327 OUTPUT_VARIABLE LD_V_OUTPUT
330 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
332 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
333 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
334 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
335 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
337 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
340 include(CMakeParseArguments)
343 set(CMAKE_INCLUDE_CURRENT_DIR ON)
345 include_directories(BEFORE
346 ${CMAKE_CURRENT_BINARY_DIR}/include
347 ${CMAKE_CURRENT_SOURCE_DIR}/include
350 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
351 install(DIRECTORY include/clang include/clang-c
352 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
353 COMPONENT clang-headers
357 PATTERN "config.h" EXCLUDE
360 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
361 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
362 COMPONENT clang-headers
364 PATTERN "CMakeFiles" EXCLUDE
369 # Installing the headers needs to depend on generating any public
370 # tablegen'd headers.
371 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
372 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
373 if(NOT LLVM_ENABLE_IDE)
374 add_llvm_install_targets(install-clang-headers
375 DEPENDS clang-headers
376 COMPONENT clang-headers)
379 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
380 install(FILES utils/bash-autocomplete.sh
381 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
382 COMPONENT bash-autocomplete)
383 if(NOT LLVM_ENABLE_IDE)
384 add_llvm_install_targets(install-bash-autocomplete
385 DEPENDS bash-autocomplete
386 COMPONENT bash-autocomplete)
390 option(CLANG_BUILD_TOOLS
391 "Build the Clang tools. If OFF, just generate build targets." ON)
393 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
394 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
396 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
398 CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
399 "Build clang with plugin support" ON
400 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
402 # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
403 # itself, which is not possible in many platforms because of current limitations in
404 # JIT stack. (more platforms need to be supported by JITLink)
405 if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
406 set(HAVE_CLANG_REPL_SUPPORT ON)
409 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
410 option(CLANG_ENABLE_STATIC_ANALYZER
411 "Include static analyzer in clang binary." ON)
413 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
415 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
416 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
419 if(CLANG_ENABLE_ARCMT)
420 set(CLANG_ENABLE_OBJC_REWRITER ON)
423 # This option is a stop-gap, we should commit to removing this as
424 # soon as possible. See discussion:
425 # https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
426 option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
427 "Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
429 # Clang version information
430 set(CLANG_EXECUTABLE_VERSION
431 "${CLANG_VERSION_MAJOR}" CACHE STRING
432 "Major version number that will be appended to the clang executable name")
433 set(LIBCLANG_LIBRARY_VERSION
434 "${CLANG_VERSION_MAJOR}" CACHE STRING
435 "Major version number that will be appended to the libclang library")
436 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
438 option(CLANG_INCLUDE_TESTS
439 "Generate build targets for the Clang unit tests."
440 ${LLVM_INCLUDE_TESTS})
442 option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
443 # While HLSL support is experimental this should stay hidden.
444 mark_as_advanced(CLANG_ENABLE_HLSL)
446 add_subdirectory(utils/TableGen)
448 # Export CLANG_TABLEGEN_EXE for use by flang docs.
449 set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
451 add_subdirectory(include)
453 # All targets below may depend on all tablegen'd files.
454 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
455 add_custom_target(clang-tablegen-targets
459 ${CLANG_TABLEGEN_TARGETS})
460 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
461 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
463 # Force target to be built as soon as possible. Clang modules builds depend
464 # header-wise on it as they ship all headers from the umbrella folders. Building
465 # an entire module might include header, which depends on intrinsics_gen.
466 if(LLVM_ENABLE_MODULES)
467 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
470 add_subdirectory(lib)
471 add_subdirectory(tools)
472 add_subdirectory(runtime)
474 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
475 add_subdirectory(examples)
478 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
479 # default value get updated to the new default.
480 if(CLANG_ORDER_FILE STREQUAL "")
481 unset(CLANG_ORDER_FILE CACHE)
482 unset(CLANG_ORDER_FILE)
486 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
487 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
489 if(NOT EXISTS ${CLANG_ORDER_FILE})
490 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
491 if(PATH_START EQUAL 0)
492 file(WRITE ${CLANG_ORDER_FILE} "\n")
494 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
500 if( CLANG_INCLUDE_TESTS )
501 add_subdirectory(unittests)
502 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
503 list(APPEND CLANG_TEST_PARAMS
504 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
506 add_subdirectory(test)
507 add_subdirectory(bindings/python/tests)
509 if(CLANG_BUILT_STANDALONE)
510 umbrella_lit_testsuite_end(check-all)
512 add_subdirectory(utils/perf-training)
515 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
516 ${LLVM_INCLUDE_DOCS})
517 if( CLANG_INCLUDE_DOCS )
518 add_subdirectory(docs)
521 # Custom target to install all clang libraries.
522 add_custom_target(clang-libraries)
523 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
525 if(NOT LLVM_ENABLE_IDE)
526 add_llvm_install_targets(install-clang-libraries
527 DEPENDS clang-libraries
528 COMPONENT clang-libraries)
531 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
533 list(REMOVE_DUPLICATES CLANG_LIBS)
534 foreach(lib ${CLANG_LIBS})
535 add_dependencies(clang-libraries ${lib})
536 if(NOT LLVM_ENABLE_IDE)
537 add_dependencies(install-clang-libraries install-${lib})
538 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
543 add_subdirectory(cmake/modules)
546 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
549 if (CLANG_ENABLE_BOOTSTRAP)
550 include(ExternalProject)
552 add_custom_target(clang-bootstrap-deps DEPENDS clang)
555 set(CLANG_STAGE stage1)
558 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
560 if(NOT LLVM_BUILD_INSTRUMENTED)
561 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
562 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
564 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
567 set(NEXT_CLANG_STAGE bootstrap)
570 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
571 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
573 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
576 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
577 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
579 if(BOOTSTRAP_LLVM_ENABLE_LLD)
580 # adding lld to clang-bootstrap-deps without having it enabled in
581 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
582 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
583 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
585 add_dependencies(clang-bootstrap-deps lld)
589 # Build llvm-rc and llvm-mt which are needed by the Windows build.
590 add_dependencies(clang-bootstrap-deps llvm-rc)
591 if(LLVM_ENABLE_LIBXML2)
592 add_dependencies(clang-bootstrap-deps llvm-mt)
596 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
597 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
599 add_dependencies(clang-bootstrap-deps LTO)
600 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
601 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
602 # so that the host object file tools will use the just-built libLTO.
603 # However if System Integrity Protection is enabled the DYLD variables
604 # will be scrubbed from the environment of any base system commands. This
605 # includes /bin/sh, which ninja uses when executing build commands. To
606 # work around the envar being filtered away we pass it in as a CMake
607 # variable, and have LLVM's CMake append the envar to the archiver calls.
608 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
609 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
611 add_dependencies(clang-bootstrap-deps llvm-lib)
612 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
614 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
615 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
616 add_dependencies(clang-bootstrap-deps LLVMgold)
618 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
619 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
623 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
624 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
627 add_custom_target(${NEXT_CLANG_STAGE}-clear
628 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
631 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
632 DEPENDS clang-bootstrap-deps
633 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
634 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
635 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
636 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
637 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
640 if(CMAKE_VERBOSE_MAKEFILE)
641 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
644 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
652 CLANG_VERSION_PATCHLEVEL
656 CLANG_REPOSITORY_STRING
658 CMAKE_OSX_ARCHITECTURES
661 LLVM_ENABLE_RUNTIMES)
663 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
664 # because the next stage will use the same compiler used to build this stage.
665 if(NOT LLVM_BUILD_INSTRUMENTED)
666 if(TARGET compiler-rt)
667 add_dependencies(clang-bootstrap-deps compiler-rt)
669 if(TARGET cxx-headers)
670 add_dependencies(clang-bootstrap-deps cxx-headers)
674 set(C_COMPILER "clang")
675 set(CXX_COMPILER "clang++")
677 set(C_COMPILER "clang-cl.exe")
678 set(CXX_COMPILER "clang-cl.exe")
682 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
683 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
684 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
685 -DCMAKE_ASM_COMPILER_ID=Clang)
687 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
688 # otherwise it defaults the linker to be link.exe.
689 if(BOOTSTRAP_LLVM_ENABLE_LLD)
690 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
691 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
695 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
696 set(${CLANG_STAGE}_TABLEGEN
697 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
698 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
699 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
700 if(BOOTSTRAP_LLVM_ENABLE_LLD)
701 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
703 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
704 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
705 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
706 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
708 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
709 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
710 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
711 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
715 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
716 add_dependencies(clang-bootstrap-deps llvm-profdata)
717 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
720 if(LLVM_BUILD_INSTRUMENTED)
721 add_dependencies(clang-bootstrap-deps generate-profdata)
722 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
723 # Use the current tools for LTO instead of the instrumented ones
724 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
733 set(COMPILER_OPTIONS)
739 # Populate the passthrough variables
740 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
741 if(DEFINED ${variableName})
742 if("${${variableName}}" STREQUAL "")
745 string(REPLACE ";" "|" value "${${variableName}}")
747 list(APPEND PASSTHROUGH_VARIABLES
748 -D${variableName}=${value})
752 # Find all variables that start with BOOTSTRAP_ and populate a variable with
754 get_cmake_property(variableNames VARIABLES)
755 foreach(variableName ${variableNames})
756 if(variableName MATCHES "^BOOTSTRAP_")
757 string(SUBSTRING ${variableName} 10 -1 varName)
758 string(REPLACE ";" "|" value "${${variableName}}")
759 list(APPEND PASSTHROUGH_VARIABLES
760 -D${varName}=${value})
762 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
763 list(APPEND PASSTHROUGH_VARIABLES
764 -D${variableName}=${${variableName}})
768 # Build arguments for native tool used in CMake.
769 set(build_configuration "$<CONFIG>")
770 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
771 if(NOT build_tool_args STREQUAL "")
772 string(PREPEND build_tool_args "-- ")
773 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
776 ExternalProject_Add(${NEXT_CLANG_STAGE}
777 DEPENDS clang-bootstrap-deps
778 PREFIX ${NEXT_CLANG_STAGE}
779 SOURCE_DIR ${CMAKE_SOURCE_DIR}
780 STAMP_DIR ${STAMP_DIR}
781 BINARY_DIR ${BINARY_DIR}
784 # We shouldn't need to set this here, but INSTALL_DIR doesn't
785 # seem to work, so instead I'm passing this through
786 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
787 ${PASSTHROUGH_VARIABLES}
788 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
789 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
791 ${${CLANG_STAGE}_TABLEGEN}
792 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
793 ${${CLANG_STAGE}_LINKER}
795 ${${CLANG_STAGE}_RANLIB}
796 ${${CLANG_STAGE}_OBJCOPY}
797 ${${CLANG_STAGE}_STRIP}
798 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
799 --config ${build_configuration}
802 STEP_TARGETS configure build
803 USES_TERMINAL_CONFIGURE 1
804 USES_TERMINAL_BUILD 1
805 USES_TERMINAL_INSTALL 1
809 # exclude really-install from main target
810 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
811 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
812 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
813 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
817 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
818 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
820 if(NOT CLANG_BOOTSTRAP_TARGETS)
821 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
823 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
824 # Install targets have side effects, so we always want to execute them.
825 # "install" is reserved by CMake and can't be used as a step name for
826 # ExternalProject_Add_Step, so we can match against "^install-" instead of
827 # "^install" to get a tighter match. CMake's installation scripts already
828 # skip up-to-date files, so there's no behavior change if you install to the
829 # same destination multiple times.
830 if(target MATCHES "^install-")
836 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
837 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
838 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
840 ALWAYS ${step_always}
845 if(target MATCHES "^stage[0-9]*")
846 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
849 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
853 if (CLANG_BOLT_INSTRUMENT AND NOT LLVM_BUILD_INSTRUMENTED)
854 set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
855 set(CLANG_INSTRUMENTED ${CLANG_PATH}-bolt.inst)
856 set(BOLT_FDATA ${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/prof.fdata)
858 # Instrument clang with BOLT
859 add_custom_target(clang-instrumented
860 DEPENDS ${CLANG_INSTRUMENTED}
862 add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
863 DEPENDS clang llvm-bolt
864 COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
865 -instrument --instrumentation-file-append-pid
866 --instrumentation-file=${BOLT_FDATA}
867 COMMENT "Instrumenting clang binary with BOLT"
871 # Optimize original (pre-bolt) Clang using the collected profile
872 set(CLANG_OPTIMIZED ${CMAKE_CURRENT_BINARY_DIR}/clang.bolt)
873 add_custom_target(clang-bolt
874 DEPENDS ${CLANG_OPTIMIZED}
876 add_custom_command(OUTPUT ${CLANG_OPTIMIZED}
877 DEPENDS clang-bolt-profile
878 COMMAND llvm-bolt ${CLANG_PATH}
879 -o ${CLANG_OPTIMIZED}
881 -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions
882 -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack
883 COMMAND ${CMAKE_COMMAND} -E rename ${CLANG_OPTIMIZED} $<TARGET_FILE:clang>
884 COMMENT "Optimizing Clang with BOLT"
889 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
890 add_subdirectory(utils/ClangVisualizers)
892 add_subdirectory(utils/hmaptool)
894 if(CLANG_BUILT_STANDALONE)
895 llvm_distribution_add_targets()
896 process_llvm_pass_plugins()
899 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
902 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
903 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)