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}")
365 include(CMakeParseArguments)
368 set(CMAKE_INCLUDE_CURRENT_DIR ON)
370 include_directories(BEFORE
371 ${CMAKE_CURRENT_BINARY_DIR}/include
372 ${CMAKE_CURRENT_SOURCE_DIR}/include
375 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
376 install(DIRECTORY include/clang include/clang-c
377 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
378 COMPONENT clang-headers
382 PATTERN "config.h" EXCLUDE
385 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
386 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
387 COMPONENT clang-headers
389 PATTERN "CMakeFiles" EXCLUDE
394 # Installing the headers needs to depend on generating any public
395 # tablegen'd headers.
396 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
397 set_target_properties(clang-headers PROPERTIES FOLDER "Clang/Resources")
398 if(NOT LLVM_ENABLE_IDE)
399 add_llvm_install_targets(install-clang-headers
400 DEPENDS clang-headers
401 COMPONENT clang-headers)
404 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
405 set_target_properties(bash-autocomplete PROPERTIES FOLDER "Clang/Misc")
406 install(FILES utils/bash-autocomplete.sh
407 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
408 COMPONENT bash-autocomplete)
409 if(NOT LLVM_ENABLE_IDE)
410 add_llvm_install_targets(install-bash-autocomplete
411 DEPENDS bash-autocomplete
412 COMPONENT bash-autocomplete)
416 option(CLANG_BUILD_TOOLS
417 "Build the Clang tools. If OFF, just generate build targets." ON)
419 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
420 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
422 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
424 CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
425 "Build clang with plugin support" ON
426 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
428 # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
429 # itself, which is not possible in many platforms because of current limitations in
430 # JIT stack. (more platforms need to be supported by JITLink)
431 if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
432 set(HAVE_CLANG_REPL_SUPPORT ON)
435 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
436 option(CLANG_ENABLE_STATIC_ANALYZER
437 "Include static analyzer in clang binary." ON)
439 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
441 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
442 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
445 if(CLANG_ENABLE_ARCMT)
446 set(CLANG_ENABLE_OBJC_REWRITER ON)
449 # This option is a stop-gap, we should commit to removing this as
450 # soon as possible. See discussion:
451 # https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
452 option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
453 "Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
455 # Clang version information
456 set(CLANG_EXECUTABLE_VERSION
457 "${CLANG_VERSION_MAJOR}" CACHE STRING
458 "Major version number that will be appended to the clang executable name")
459 set(LIBCLANG_LIBRARY_VERSION
460 "${CLANG_VERSION_MAJOR}" CACHE STRING
461 "Major version number that will be appended to the libclang library")
462 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
464 option(CLANG_INCLUDE_TESTS
465 "Generate build targets for the Clang unit tests."
466 ${LLVM_INCLUDE_TESTS})
468 option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
469 # While HLSL support is experimental this should stay hidden.
470 mark_as_advanced(CLANG_ENABLE_HLSL)
472 add_subdirectory(utils/TableGen)
474 # Export CLANG_TABLEGEN_EXE for use by flang docs.
475 set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
477 add_subdirectory(include)
479 # All targets below may depend on all tablegen'd files.
480 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
481 add_custom_target(clang-tablegen-targets
485 ${CLANG_TABLEGEN_TARGETS})
486 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Clang/Tablegenning/Targets")
487 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
489 # Force target to be built as soon as possible. Clang modules builds depend
490 # header-wise on it as they ship all headers from the umbrella folders. Building
491 # an entire module might include header, which depends on intrinsics_gen.
492 if(LLVM_ENABLE_MODULES)
493 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
496 add_subdirectory(lib)
497 add_subdirectory(tools)
498 add_subdirectory(runtime)
500 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
501 add_subdirectory(examples)
504 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
505 # default value get updated to the new default.
506 if(CLANG_ORDER_FILE STREQUAL "")
507 unset(CLANG_ORDER_FILE CACHE)
508 unset(CLANG_ORDER_FILE)
512 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
513 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
515 if(NOT EXISTS ${CLANG_ORDER_FILE})
516 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
517 if(PATH_START EQUAL 0)
518 file(WRITE ${CLANG_ORDER_FILE} "\n")
520 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
526 if( CLANG_INCLUDE_TESTS )
529 add_subdirectory(unittests)
530 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
531 list(APPEND CLANG_TEST_PARAMS
532 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
534 add_subdirectory(test)
535 add_subdirectory(bindings/python/tests)
537 if(CLANG_BUILT_STANDALONE)
538 umbrella_lit_testsuite_end(check-all)
540 add_subdirectory(utils/perf-training)
543 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
544 ${LLVM_INCLUDE_DOCS})
545 if( CLANG_INCLUDE_DOCS )
546 add_subdirectory(docs)
549 # Custom target to install all clang libraries.
550 add_custom_target(clang-libraries)
551 set_target_properties(clang-libraries PROPERTIES FOLDER "Clang/Install")
553 if(NOT LLVM_ENABLE_IDE)
554 add_llvm_install_targets(install-clang-libraries
555 DEPENDS clang-libraries
556 COMPONENT clang-libraries)
559 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
561 list(REMOVE_DUPLICATES CLANG_LIBS)
562 foreach(lib ${CLANG_LIBS})
563 add_dependencies(clang-libraries ${lib})
564 if(NOT LLVM_ENABLE_IDE)
565 add_dependencies(install-clang-libraries install-${lib})
566 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
571 add_subdirectory(cmake/modules)
574 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
577 if (CLANG_ENABLE_BOOTSTRAP)
578 include(ExternalProject)
580 add_custom_target(clang-bootstrap-deps DEPENDS clang)
583 set(CLANG_STAGE stage1)
586 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
588 if(NOT LLVM_BUILD_INSTRUMENTED)
589 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
590 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
592 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
595 set(NEXT_CLANG_STAGE bootstrap)
598 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
599 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
601 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
604 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
605 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
607 if(BOOTSTRAP_LLVM_ENABLE_LLD)
608 # adding lld to clang-bootstrap-deps without having it enabled in
609 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
610 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
611 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
613 add_dependencies(clang-bootstrap-deps lld)
617 # Build llvm-rc and llvm-mt which are needed by the Windows build.
618 add_dependencies(clang-bootstrap-deps llvm-rc)
619 if(LLVM_ENABLE_LIBXML2)
620 add_dependencies(clang-bootstrap-deps llvm-mt)
624 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
625 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
627 add_dependencies(clang-bootstrap-deps LTO)
628 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
629 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
630 # so that the host object file tools will use the just-built libLTO.
631 # However if System Integrity Protection is enabled the DYLD variables
632 # will be scrubbed from the environment of any base system commands. This
633 # includes /bin/sh, which ninja uses when executing build commands. To
634 # work around the envar being filtered away we pass it in as a CMake
635 # variable, and have LLVM's CMake append the envar to the archiver calls.
636 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
637 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
639 add_dependencies(clang-bootstrap-deps llvm-lib)
640 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
642 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
643 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
644 add_dependencies(clang-bootstrap-deps LLVMgold)
646 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
647 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
651 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
652 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
655 add_custom_target(${NEXT_CLANG_STAGE}-clear
656 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
659 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
660 DEPENDS clang-bootstrap-deps
661 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
662 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
663 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
664 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
665 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
668 if(CMAKE_VERBOSE_MAKEFILE)
669 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
672 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
680 CLANG_VERSION_PATCHLEVEL
685 CLANG_REPOSITORY_STRING
687 CMAKE_OSX_ARCHITECTURES
690 LLVM_ENABLE_RUNTIMES)
692 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
693 # because the next stage will use the same compiler used to build this stage.
694 if(NOT LLVM_BUILD_INSTRUMENTED)
695 if(TARGET compiler-rt)
696 add_dependencies(clang-bootstrap-deps compiler-rt)
698 if(TARGET cxx-headers)
699 add_dependencies(clang-bootstrap-deps cxx-headers)
703 set(C_COMPILER "clang")
704 set(CXX_COMPILER "clang++")
706 set(C_COMPILER "clang-cl.exe")
707 set(CXX_COMPILER "clang-cl.exe")
711 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
712 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
713 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
714 -DCMAKE_ASM_COMPILER_ID=Clang)
716 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
717 # otherwise it defaults the linker to be link.exe.
718 if(BOOTSTRAP_LLVM_ENABLE_LLD)
719 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
720 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
724 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
725 set(${CLANG_STAGE}_TABLEGEN
726 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
727 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
728 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
729 if(BOOTSTRAP_LLVM_ENABLE_LLD)
730 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
732 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
733 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
734 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
735 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
737 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
738 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
739 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
740 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
744 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
745 add_dependencies(clang-bootstrap-deps llvm-profdata)
746 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
749 if(LLVM_BUILD_INSTRUMENTED)
750 add_dependencies(clang-bootstrap-deps generate-profdata)
751 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
752 # Use the current tools for LTO instead of the instrumented ones
753 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
762 set(COMPILER_OPTIONS)
768 # Populate the passthrough variables
769 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
770 if(DEFINED ${variableName})
771 if("${${variableName}}" STREQUAL "")
774 string(REPLACE ";" "|" value "${${variableName}}")
776 list(APPEND PASSTHROUGH_VARIABLES
777 -D${variableName}=${value})
781 # Find all variables that start with BOOTSTRAP_ and populate a variable with
783 get_cmake_property(variableNames VARIABLES)
784 foreach(variableName ${variableNames})
785 if(variableName MATCHES "^BOOTSTRAP_")
786 string(SUBSTRING ${variableName} 10 -1 varName)
787 string(REPLACE ";" "|" value "${${variableName}}")
788 list(APPEND PASSTHROUGH_VARIABLES
789 -D${varName}=${value})
791 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
792 list(APPEND PASSTHROUGH_VARIABLES
793 -D${variableName}=${${variableName}})
797 # Build arguments for native tool used in CMake.
798 set(build_configuration "$<CONFIG>")
799 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
800 if(NOT build_tool_args STREQUAL "")
801 string(PREPEND build_tool_args "-- ")
802 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
805 ExternalProject_Add(${NEXT_CLANG_STAGE}
806 DEPENDS clang-bootstrap-deps
807 PREFIX ${NEXT_CLANG_STAGE}
808 SOURCE_DIR ${CMAKE_SOURCE_DIR}
809 STAMP_DIR ${STAMP_DIR}
810 BINARY_DIR ${BINARY_DIR}
813 # We shouldn't need to set this here, but INSTALL_DIR doesn't
814 # seem to work, so instead I'm passing this through
815 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
816 ${PASSTHROUGH_VARIABLES}
817 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
818 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
820 ${${CLANG_STAGE}_TABLEGEN}
821 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
822 ${${CLANG_STAGE}_LINKER}
824 ${${CLANG_STAGE}_RANLIB}
825 ${${CLANG_STAGE}_OBJCOPY}
826 ${${CLANG_STAGE}_STRIP}
827 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
828 --config ${build_configuration}
831 STEP_TARGETS configure build
832 USES_TERMINAL_CONFIGURE 1
833 USES_TERMINAL_BUILD 1
834 USES_TERMINAL_INSTALL 1
838 # exclude really-install from main target
839 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
840 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
841 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
842 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
846 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
847 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
849 if(NOT CLANG_BOOTSTRAP_TARGETS)
850 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
852 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
854 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
855 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
856 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
858 # We need to set ALWAYS to ON here, otherwise these targets won't be
859 # built on a second invocation of ninja. The targets have their own
860 # logic to determine if they should build or not so setting ALWAYS ON
861 # here does not mean the targets will always rebuild it just means that
862 # they will check their dependenices and see if they need to be built.
868 if(target MATCHES "^stage[0-9]*")
869 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
872 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
876 set(CLANG_BOLT OFF CACHE STRING "Apply BOLT optimization to Clang. \
877 May be specified as Instrument or Perf or LBR to use a particular profiling \
879 string(TOUPPER "${CLANG_BOLT}" CLANG_BOLT)
881 if (CLANG_BOLT AND NOT LLVM_BUILD_INSTRUMENTED)
882 set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
883 set(CLANG_INSTRUMENTED ${LLVM_RUNTIME_OUTPUT_INTDIR}/${CLANG_BOLT_INSTRUMENTED})
884 set(BOLT_FDATA ${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/prof.fdata)
886 # Pass extra flag in no-LBR mode
887 if (CLANG_BOLT STREQUAL "PERF")
888 set(BOLT_NO_LBR "-nl")
891 if (CLANG_BOLT STREQUAL "INSTRUMENT")
892 # Instrument clang with BOLT
893 add_custom_target(clang-instrumented
894 DEPENDS ${CLANG_INSTRUMENTED}
896 add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
897 DEPENDS clang llvm-bolt
898 COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
899 -instrument --instrumentation-file-append-pid
900 --instrumentation-file=${BOLT_FDATA}
901 COMMENT "Instrumenting clang binary with BOLT"
905 add_custom_target(clang-bolt-training-deps DEPENDS clang-instrumented)
907 add_custom_target(clang-bolt-training-deps DEPENDS clang)
910 # Optimize original (pre-bolt) Clang using the collected profile
911 add_custom_target(clang-bolt
912 DEPENDS clang-bolt-profile
913 COMMAND ${CMAKE_COMMAND} -E rename $<TARGET_FILE:clang> ${CLANG_PATH}-prebolt
914 COMMAND ${CMAKE_COMMAND} -E create_symlink ${CLANG_PATH}-prebolt ${CLANG_PATH}++-prebolt
915 COMMAND llvm-bolt ${CLANG_PATH}-prebolt
916 -o $<TARGET_FILE:clang>
918 -reorder-blocks=ext-tsp -reorder-functions=cdsort -split-functions
919 -split-all-cold -split-eh -dyno-stats -use-gnu-stack
920 -update-debug-sections
922 COMMENT "Optimizing Clang with BOLT"
928 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
929 add_subdirectory(utils/ClangVisualizers)
931 add_subdirectory(utils/hmaptool)
933 if(CLANG_BUILT_STANDALONE)
934 llvm_distribution_add_targets()
935 process_llvm_pass_plugins()
938 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
941 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
942 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)