1 cmake_minimum_required(VERSION 3.20.0)
2 set(LLVM_SUBPROJECT_TITLE "Clang")
4 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
10 # If we are not building as a part of LLVM, build Clang as an
11 # standalone project, using LLVM as an external library:
12 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
14 set(CLANG_BUILT_STANDALONE TRUE)
17 # Make sure that our source directory is on the current cmake module path so that
18 # we can include cmake files from this directory.
19 list(INSERT CMAKE_MODULE_PATH 0
20 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
21 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
24 # Must go below project(..)
25 include(GNUInstallDirs)
26 include(GetDarwinLinkerVersion)
28 if(CLANG_BUILT_STANDALONE)
29 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
30 set(CMAKE_CXX_STANDARD_REQUIRED YES)
31 set(CMAKE_CXX_EXTENSIONS NO)
34 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
35 CACHE BOOL "Enable assertions")
36 # Assertions should follow llvm-config's.
37 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
40 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
41 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
43 # Turn into CACHE PATHs for overwritting
44 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
45 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
46 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
47 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
48 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
50 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
53 # They are used as destination of target generators.
54 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
55 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
57 # DLL platform -- put DLLs into bin.
58 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
60 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
63 option(LLVM_INSTALL_TOOLCHAIN_ONLY
64 "Only include toolchain files in the 'install' target." OFF)
66 option(LLVM_FORCE_USE_OLD_TOOLCHAIN
67 "Set to ON to force using an old, unsupported host toolchain." OFF)
68 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
69 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
73 include(HandleLLVMOptions)
74 include(VersionFromVCS)
76 include(GetErrcMessages)
77 include(LLVMDistributionSupport)
79 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
80 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
81 "Default URL where bug reports are to be submitted.")
83 if (NOT DEFINED LLVM_INCLUDE_TESTS)
84 set(LLVM_INCLUDE_TESTS ON)
87 include_directories(${LLVM_INCLUDE_DIRS})
88 link_directories("${LLVM_LIBRARY_DIR}")
90 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
91 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
92 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
94 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
95 COMPONENTS Interpreter)
97 if(LLVM_INCLUDE_TESTS)
98 # Check prebuilt llvm/utils.
99 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
100 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
101 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
102 set(LLVM_UTILS_PROVIDED ON)
105 # Seek installed Lit.
106 find_program(LLVM_LIT
107 NAMES llvm-lit lit.py lit
108 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
109 DOC "Path to lit.py")
111 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
112 # Note: path not really used, except for checking if lit was found
113 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
114 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
116 if(NOT LLVM_UTILS_PROVIDED)
117 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
118 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
119 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
120 set(LLVM_UTILS_PROVIDED ON)
121 set(CLANG_TEST_DEPS FileCheck count not)
125 if (NOT TARGET llvm_gtest)
126 message(FATAL_ERROR "llvm-gtest not found. Please install llvm-gtest or disable tests with -DLLVM_INCLUDE_TESTS=OFF")
130 # Define the default arguments to use with 'lit', and an option for the user
132 set(LIT_ARGS_DEFAULT "-sv")
134 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
136 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
138 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
140 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
141 if( WIN32 AND NOT CYGWIN )
142 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
145 set(LLVM_INCLUDE_TESTS OFF)
148 umbrella_lit_testsuite_begin(check-all)
149 endif() # LLVM_INCLUDE_TESTS
152 # This allows disabling clang's XML dependency even if LLVM finds libxml2.
153 # By default, clang depends on libxml2 if LLVM does.
154 option(CLANG_ENABLE_LIBXML2 "Whether libclang may depend on libxml2"
155 ${LLVM_ENABLE_LIBXML2})
157 if(CLANG_ENABLE_LIBXML2)
158 # Don't look for libxml if we're using MSan, since uninstrumented third party
159 # code may call MSan interceptors like strlen, leading to false positives.
160 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
161 set (LIBXML2_FOUND 0)
162 find_package(LibXml2 2.5.3 QUIET)
164 set(CLANG_HAVE_LIBXML 1)
170 if (CLANG_BUILT_STANDALONE)
172 "ClangIR is not yet supported in the standalone build.")
174 if (NOT "${LLVM_ENABLE_PROJECTS}" MATCHES "MLIR|mlir")
176 "Cannot build ClangIR without MLIR in LLVM_ENABLE_PROJECTS")
180 include(CheckIncludeFile)
181 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
183 # This check requires _GNU_SOURCE on linux
184 check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
185 if( CLANG_HAVE_DLFCN_H )
186 include(CheckLibraryExists)
187 include(CheckSymbolExists)
188 check_library_exists(dl dlopen "" HAVE_LIBDL)
190 list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
192 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
193 check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
194 list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
196 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
200 set(CLANG_RESOURCE_DIR "" CACHE STRING
201 "Relative directory from the Clang binary to its resource files.")
203 set(C_INCLUDE_DIRS "" CACHE STRING
204 "Colon separated list of directories clang will search for headers.")
206 set(USE_DEPRECATED_GCC_INSTALL_PREFIX OFF CACHE BOOL "Temporary workaround before GCC_INSTALL_PREFIX is completely removed")
207 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
208 set(DEFAULT_SYSROOT "" CACHE STRING
209 "Default <path> to all compiler invocations for --sysroot=<path>." )
210 if(GCC_INSTALL_PREFIX AND NOT USE_DEPRECATED_GCC_INSTALL_PREFIX)
211 message(FATAL_ERROR "GCC_INSTALL_PREFIX is deprecated and will be removed. Use "
212 "configuration files (https://clang.llvm.org/docs/UsersManual.html#configuration-files)"
213 "to specify the default --gcc-install-dir= or --gcc-triple=. --gcc-toolchain= is discouraged. "
214 "See https://github.com/llvm/llvm-project/pull/77537 for detail.")
217 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
219 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
220 "enable x86 relax relocations by default")
222 set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
223 "Enable IEEE binary128 as default long double format on PowerPC Linux.")
225 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
226 "Whether clang should use a new process for the CC1 invocation")
228 option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
230 set(CLANG_DEFAULT_LINKER "" CACHE STRING
231 "Default linker to use (linker name or absolute path, empty for platform default)")
233 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
234 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
235 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
236 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
237 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
238 message(WARNING "Resetting default C++ stdlib to use platform default")
239 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
240 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
243 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
244 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
245 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
246 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
247 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
248 message(WARNING "Resetting default rtlib to use platform default")
249 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
250 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
253 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
254 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
255 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
256 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
257 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
261 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
262 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
263 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
264 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
265 message(WARNING "Resetting default unwindlib to use platform default")
266 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
267 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
270 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
271 "Default objcopy executable to use.")
273 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
274 "Default OpenMP runtime used by -fopenmp.")
276 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
278 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
279 "Vendor-specific text for showing with version information.")
281 set(CLANG_REPOSITORY_STRING "" CACHE STRING
282 "Vendor-specific text for showing the repository the source is taken from.")
284 if(CLANG_REPOSITORY_STRING)
285 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
288 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
289 "Vendor-specific uti.")
291 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
292 "Python versions to install libclang python bindings for")
294 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
295 "Link tools against libclang-cpp.so")
297 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
298 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
299 "LLVM_LINK_LLVM_DYLIB=OFF")
302 # The libdir suffix must exactly match whatever LLVM's configuration used.
303 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
305 set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
306 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
307 mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
309 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
310 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
312 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
313 message(FATAL_ERROR "In-source builds are not allowed. "
314 "Please create a directory and run cmake "
315 "from there, passing the path to this source directory as the last argument. "
316 "This process created the file `CMakeCache.txt' and the directory "
317 "`CMakeFiles'. Please delete them.")
320 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
321 if(NOT DEFINED CLANG_VERSION_MAJOR)
322 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
324 if(NOT DEFINED CLANG_VERSION_MINOR)
325 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
327 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
328 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
330 if(NOT DEFINED CLANG_VERSION_SUFFIX)
331 set(CLANG_VERSION_SUFFIX ${LLVM_VERSION_SUFFIX})
333 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}${CLANG_VERSION_SUFFIX}")
334 message(STATUS "Clang version: ${CLANG_VERSION}")
336 # Configure the Version.inc file.
338 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
339 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
341 # Add appropriate flags for GCC
342 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
343 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
344 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
345 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
348 # Enable -pedantic for Clang even if it's not enabled for LLVM.
349 if (NOT LLVM_ENABLE_PEDANTIC)
350 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
353 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
354 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
358 # Determine HOST_LINK_VERSION on Darwin.
359 set(HOST_LINK_VERSION)
360 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
361 get_darwin_linker_version(HOST_LINK_VERSION)
362 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
367 set(CMAKE_INCLUDE_CURRENT_DIR ON)
369 include_directories(BEFORE
370 ${CMAKE_CURRENT_BINARY_DIR}/include
371 ${CMAKE_CURRENT_SOURCE_DIR}/include
374 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
375 install(DIRECTORY include/clang include/clang-c
376 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
377 COMPONENT clang-headers
381 PATTERN "config.h" EXCLUDE
384 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
385 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
386 COMPONENT clang-headers
388 PATTERN "CMakeFiles" EXCLUDE
393 # Installing the headers needs to depend on generating any public
394 # tablegen'd headers.
395 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
396 set_target_properties(clang-headers PROPERTIES FOLDER "Clang/Resources")
397 if(NOT LLVM_ENABLE_IDE)
398 add_llvm_install_targets(install-clang-headers
399 DEPENDS clang-headers
400 COMPONENT clang-headers)
403 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
404 set_target_properties(bash-autocomplete PROPERTIES FOLDER "Clang/Misc")
405 install(FILES utils/bash-autocomplete.sh
406 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
407 COMPONENT bash-autocomplete)
408 if(NOT LLVM_ENABLE_IDE)
409 add_llvm_install_targets(install-bash-autocomplete
410 DEPENDS bash-autocomplete
411 COMPONENT bash-autocomplete)
415 option(CLANG_BUILD_TOOLS
416 "Build the Clang tools. If OFF, just generate build targets." ON)
418 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
419 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
421 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
423 CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
424 "Build clang with plugin support" ON
425 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
427 # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
428 # itself, which is not possible in many platforms because of current limitations in
429 # JIT stack. (more platforms need to be supported by JITLink)
430 if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
431 set(HAVE_CLANG_REPL_SUPPORT ON)
434 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
435 option(CLANG_ENABLE_STATIC_ANALYZER
436 "Include static analyzer in clang binary." ON)
438 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
440 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
441 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
444 if(CLANG_ENABLE_ARCMT)
445 set(CLANG_ENABLE_OBJC_REWRITER ON)
448 # This option is a stop-gap, we should commit to removing this as
449 # soon as possible. See discussion:
450 # https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
451 option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
452 "Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
454 # Clang version information
455 set(CLANG_EXECUTABLE_VERSION
456 "${CLANG_VERSION_MAJOR}" CACHE STRING
457 "Major version number that will be appended to the clang executable name")
458 set(LIBCLANG_LIBRARY_VERSION
459 "${CLANG_VERSION_MAJOR}" CACHE STRING
460 "Major version number that will be appended to the libclang library")
461 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
463 option(CLANG_INCLUDE_TESTS
464 "Generate build targets for the Clang unit tests."
465 ${LLVM_INCLUDE_TESTS})
467 option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
468 # While HLSL support is experimental this should stay hidden.
469 mark_as_advanced(CLANG_ENABLE_HLSL)
471 add_subdirectory(utils/TableGen)
473 # Export CLANG_TABLEGEN_EXE for use by flang docs.
474 set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
476 add_subdirectory(include)
478 # All targets below may depend on all tablegen'd files.
479 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
480 add_custom_target(clang-tablegen-targets
484 ${CLANG_TABLEGEN_TARGETS})
485 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Clang/Tablegenning/Targets")
486 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
488 # Force target to be built as soon as possible. Clang modules builds depend
489 # header-wise on it as they ship all headers from the umbrella folders. Building
490 # an entire module might include header, which depends on intrinsics_gen.
491 if(LLVM_ENABLE_MODULES)
492 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
495 add_subdirectory(lib)
496 add_subdirectory(tools)
497 add_subdirectory(runtime)
499 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
500 add_subdirectory(examples)
503 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
504 # default value get updated to the new default.
505 if(CLANG_ORDER_FILE STREQUAL "")
506 unset(CLANG_ORDER_FILE CACHE)
507 unset(CLANG_ORDER_FILE)
511 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
512 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
514 if(NOT EXISTS ${CLANG_ORDER_FILE})
515 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
516 if(PATH_START EQUAL 0)
517 file(WRITE ${CLANG_ORDER_FILE} "\n")
519 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
525 if( CLANG_INCLUDE_TESTS )
528 add_subdirectory(unittests)
529 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
530 list(APPEND CLANG_TEST_PARAMS
531 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
533 add_subdirectory(test)
534 add_subdirectory(bindings/python/tests)
536 if(CLANG_BUILT_STANDALONE)
537 umbrella_lit_testsuite_end(check-all)
539 add_subdirectory(utils/perf-training)
542 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
543 ${LLVM_INCLUDE_DOCS})
544 if( CLANG_INCLUDE_DOCS )
545 add_subdirectory(docs)
548 # Custom target to install all clang libraries.
549 add_custom_target(clang-libraries)
550 set_target_properties(clang-libraries PROPERTIES FOLDER "Clang/Install")
552 if(NOT LLVM_ENABLE_IDE)
553 add_llvm_install_targets(install-clang-libraries
554 DEPENDS clang-libraries
555 COMPONENT clang-libraries)
558 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
560 list(REMOVE_DUPLICATES CLANG_LIBS)
561 foreach(lib ${CLANG_LIBS})
562 add_dependencies(clang-libraries ${lib})
563 if(NOT LLVM_ENABLE_IDE)
564 add_dependencies(install-clang-libraries install-${lib})
565 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
570 add_subdirectory(cmake/modules)
573 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
576 if (CLANG_ENABLE_BOOTSTRAP)
577 include(ExternalProject)
579 add_custom_target(clang-bootstrap-deps DEPENDS clang)
582 set(CLANG_STAGE stage1)
585 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
587 if(NOT LLVM_BUILD_INSTRUMENTED)
588 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
589 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
591 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
594 set(NEXT_CLANG_STAGE bootstrap)
597 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
598 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
600 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
603 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
604 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
606 if(BOOTSTRAP_LLVM_ENABLE_LLD)
607 # adding lld to clang-bootstrap-deps without having it enabled in
608 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
609 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
610 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
612 add_dependencies(clang-bootstrap-deps lld)
616 # Build llvm-rc and llvm-mt which are needed by the Windows build.
617 add_dependencies(clang-bootstrap-deps llvm-rc)
618 if(LLVM_ENABLE_LIBXML2)
619 add_dependencies(clang-bootstrap-deps llvm-mt)
623 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
624 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
626 add_dependencies(clang-bootstrap-deps LTO)
627 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
628 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
629 # so that the host object file tools will use the just-built libLTO.
630 # However if System Integrity Protection is enabled the DYLD variables
631 # will be scrubbed from the environment of any base system commands. This
632 # includes /bin/sh, which ninja uses when executing build commands. To
633 # work around the envar being filtered away we pass it in as a CMake
634 # variable, and have LLVM's CMake append the envar to the archiver calls.
635 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
636 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
638 add_dependencies(clang-bootstrap-deps llvm-lib)
639 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
641 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
642 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
643 add_dependencies(clang-bootstrap-deps LLVMgold)
645 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
646 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
650 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
651 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
654 add_custom_target(${NEXT_CLANG_STAGE}-clear
655 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
658 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
659 DEPENDS clang-bootstrap-deps
660 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
661 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
662 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
663 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
664 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
667 if(CMAKE_VERBOSE_MAKEFILE)
668 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
671 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
679 CLANG_VERSION_PATCHLEVEL
684 CLANG_REPOSITORY_STRING
686 CMAKE_OSX_ARCHITECTURES
689 LLVM_ENABLE_RUNTIMES)
691 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
692 # because the next stage will use the same compiler used to build this stage.
693 if(NOT LLVM_BUILD_INSTRUMENTED)
694 if(TARGET compiler-rt)
695 add_dependencies(clang-bootstrap-deps compiler-rt)
697 if(TARGET cxx-headers)
698 add_dependencies(clang-bootstrap-deps cxx-headers)
702 set(C_COMPILER "clang")
703 set(CXX_COMPILER "clang++")
705 set(C_COMPILER "clang-cl.exe")
706 set(CXX_COMPILER "clang-cl.exe")
710 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
711 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
712 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
713 -DCMAKE_ASM_COMPILER_ID=Clang)
715 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
716 # otherwise it defaults the linker to be link.exe.
717 if(BOOTSTRAP_LLVM_ENABLE_LLD)
718 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
719 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
723 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
724 set(${CLANG_STAGE}_TABLEGEN
725 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
726 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
727 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
728 if(BOOTSTRAP_LLVM_ENABLE_LLD)
729 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
731 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
732 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
733 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
734 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
736 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
737 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
738 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
739 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
743 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
744 add_dependencies(clang-bootstrap-deps llvm-profdata)
745 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
748 if(LLVM_BUILD_INSTRUMENTED)
749 add_dependencies(clang-bootstrap-deps generate-profdata)
750 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
751 # Use the current tools for LTO instead of the instrumented ones
752 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
761 set(COMPILER_OPTIONS)
767 # Populate the passthrough variables
768 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
769 if(DEFINED ${variableName})
770 if("${${variableName}}" STREQUAL "")
773 string(REPLACE ";" "|" value "${${variableName}}")
775 list(APPEND PASSTHROUGH_VARIABLES
776 -D${variableName}=${value})
780 # Find all variables that start with BOOTSTRAP_ and populate a variable with
782 get_cmake_property(variableNames VARIABLES)
783 foreach(variableName ${variableNames})
784 if(variableName MATCHES "^BOOTSTRAP_")
785 string(SUBSTRING ${variableName} 10 -1 varName)
786 string(REPLACE ";" "|" value "${${variableName}}")
787 list(APPEND PASSTHROUGH_VARIABLES
788 -D${varName}=${value})
790 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
791 list(APPEND PASSTHROUGH_VARIABLES
792 -D${variableName}=${${variableName}})
796 # Build arguments for native tool used in CMake.
797 set(build_configuration "$<CONFIG>")
798 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
799 if(NOT build_tool_args STREQUAL "")
800 string(PREPEND build_tool_args "-- ")
801 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
804 ExternalProject_Add(${NEXT_CLANG_STAGE}
805 DEPENDS clang-bootstrap-deps
806 PREFIX ${NEXT_CLANG_STAGE}
807 SOURCE_DIR ${CMAKE_SOURCE_DIR}
808 STAMP_DIR ${STAMP_DIR}
809 BINARY_DIR ${BINARY_DIR}
812 # We shouldn't need to set this here, but INSTALL_DIR doesn't
813 # seem to work, so instead I'm passing this through
814 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
815 ${PASSTHROUGH_VARIABLES}
816 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
817 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
819 ${${CLANG_STAGE}_TABLEGEN}
820 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
821 ${${CLANG_STAGE}_LINKER}
823 ${${CLANG_STAGE}_RANLIB}
824 ${${CLANG_STAGE}_OBJCOPY}
825 ${${CLANG_STAGE}_STRIP}
826 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
827 --config ${build_configuration}
830 STEP_TARGETS configure build
831 USES_TERMINAL_CONFIGURE 1
832 USES_TERMINAL_BUILD 1
833 USES_TERMINAL_INSTALL 1
837 # exclude really-install from main target
838 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
839 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
840 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
841 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
845 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
846 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
848 if(NOT CLANG_BOOTSTRAP_TARGETS)
849 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
851 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
853 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
854 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
855 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
857 # We need to set ALWAYS to ON here, otherwise these targets won't be
858 # built on a second invocation of ninja. The targets have their own
859 # logic to determine if they should build or not so setting ALWAYS ON
860 # here does not mean the targets will always rebuild it just means that
861 # they will check their dependenices and see if they need to be built.
867 if(target MATCHES "^stage[0-9]*")
868 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
871 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
875 set(CLANG_BOLT OFF CACHE STRING "Apply BOLT optimization to Clang. \
876 May be specified as Instrument or Perf or LBR to use a particular profiling \
878 string(TOUPPER "${CLANG_BOLT}" CLANG_BOLT)
880 if (CLANG_BOLT AND NOT LLVM_BUILD_INSTRUMENTED)
881 set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
882 set(CLANG_INSTRUMENTED ${LLVM_RUNTIME_OUTPUT_INTDIR}/${CLANG_BOLT_INSTRUMENTED})
883 set(BOLT_FDATA ${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/prof.fdata)
885 # Pass extra flag in no-LBR mode
886 if (CLANG_BOLT STREQUAL "PERF")
887 set(BOLT_NO_LBR "-nl")
890 if (CLANG_BOLT STREQUAL "INSTRUMENT")
891 # Instrument clang with BOLT
892 add_custom_target(clang-instrumented
893 DEPENDS ${CLANG_INSTRUMENTED}
895 add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
896 DEPENDS clang llvm-bolt
897 COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
898 -instrument --instrumentation-file-append-pid
899 --instrumentation-file=${BOLT_FDATA}
900 COMMENT "Instrumenting clang binary with BOLT"
904 add_custom_target(clang-bolt-training-deps DEPENDS clang-instrumented)
906 add_custom_target(clang-bolt-training-deps DEPENDS clang)
909 # Optimize original (pre-bolt) Clang using the collected profile
910 add_custom_target(clang-bolt
911 DEPENDS clang-bolt-profile
912 COMMAND ${CMAKE_COMMAND} -E rename $<TARGET_FILE:clang> ${CLANG_PATH}-prebolt
913 COMMAND ${CMAKE_COMMAND} -E create_symlink ${CLANG_PATH}-prebolt ${CLANG_PATH}++-prebolt
914 COMMAND llvm-bolt ${CLANG_PATH}-prebolt
915 -o $<TARGET_FILE:clang>
917 -reorder-blocks=ext-tsp -reorder-functions=cdsort -split-functions
918 -split-all-cold -split-eh -dyno-stats -use-gnu-stack
919 -update-debug-sections
921 COMMENT "Optimizing Clang with BOLT"
927 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
928 add_subdirectory(utils/ClangVisualizers)
930 add_subdirectory(utils/hmaptool)
932 if(CLANG_BUILT_STANDALONE)
933 llvm_distribution_add_targets()
934 process_llvm_pass_plugins()
937 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
940 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
941 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)