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 # This check requires _GNU_SOURCE on linux
171 check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
172 if( CLANG_HAVE_DLFCN_H )
173 include(CheckLibraryExists)
174 include(CheckSymbolExists)
175 check_library_exists(dl dlopen "" HAVE_LIBDL)
177 list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
179 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
180 check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
181 list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
183 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
187 set(CLANG_RESOURCE_DIR "" CACHE STRING
188 "Relative directory from the Clang binary to its resource files.")
190 set(C_INCLUDE_DIRS "" CACHE STRING
191 "Colon separated list of directories clang will search for headers.")
193 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
194 set(DEFAULT_SYSROOT "" CACHE STRING
195 "Default <path> to all compiler invocations for --sysroot=<path>." )
196 if(GCC_INSTALL_PREFIX)
197 message(WARNING "GCC_INSTALL_PREFIX is deprecated and will be removed. Use "
198 "configuration files (https://clang.llvm.org/docs/UsersManual.html#configuration-files)"
199 "to specify the default --gcc-install-dir= or --gcc-triple=. --gcc-toolchain= is discouraged. "
200 "See https://github.com/llvm/llvm-project/pull/77537 for detail.")
203 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
205 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
206 "enable x86 relax relocations by default")
208 set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
209 "Enable IEEE binary128 as default long double format on PowerPC Linux.")
211 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
212 "Whether clang should use a new process for the CC1 invocation")
214 option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
216 set(CLANG_DEFAULT_LINKER "" CACHE STRING
217 "Default linker to use (linker name or absolute path, empty for platform default)")
219 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
220 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
221 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
222 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
223 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
224 message(WARNING "Resetting default C++ stdlib to use platform default")
225 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
226 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
229 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
230 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
231 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
232 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
233 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
234 message(WARNING "Resetting default rtlib to use platform default")
235 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
236 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
239 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
240 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
241 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
242 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
243 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
247 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
248 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
249 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
250 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
251 message(WARNING "Resetting default unwindlib to use platform default")
252 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
253 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
256 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
257 "Default objcopy executable to use.")
259 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
260 "Default OpenMP runtime used by -fopenmp.")
262 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
264 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
265 "Vendor-specific text for showing with version information.")
267 set(CLANG_REPOSITORY_STRING "" CACHE STRING
268 "Vendor-specific text for showing the repository the source is taken from.")
270 if(CLANG_REPOSITORY_STRING)
271 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
274 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
275 "Vendor-specific uti.")
277 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
278 "Python versions to install libclang python bindings for")
280 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
281 "Link tools against libclang-cpp.so")
283 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
284 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
285 "LLVM_LINK_LLVM_DYLIB=OFF")
288 # The libdir suffix must exactly match whatever LLVM's configuration used.
289 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
291 set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
292 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
293 mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
295 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
296 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
298 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
299 message(FATAL_ERROR "In-source builds are not allowed. "
300 "Please create a directory and run cmake "
301 "from there, passing the path to this source directory as the last argument. "
302 "This process created the file `CMakeCache.txt' and the directory "
303 "`CMakeFiles'. Please delete them.")
306 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
307 if(NOT DEFINED CLANG_VERSION_MAJOR)
308 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
310 if(NOT DEFINED CLANG_VERSION_MINOR)
311 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
313 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
314 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
316 if(NOT DEFINED CLANG_VERSION_SUFFIX)
317 set(CLANG_VERSION_SUFFIX ${LLVM_VERSION_SUFFIX})
319 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}${CLANG_VERSION_SUFFIX}")
320 message(STATUS "Clang version: ${CLANG_VERSION}")
322 # Configure the Version.inc file.
324 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
325 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
327 # Add appropriate flags for GCC
328 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
329 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
330 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
331 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
334 # Enable -pedantic for Clang even if it's not enabled for LLVM.
335 if (NOT LLVM_ENABLE_PEDANTIC)
336 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
339 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
340 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
341 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
345 # Determine HOST_LINK_VERSION on Darwin.
346 set(HOST_LINK_VERSION)
347 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
350 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
351 RESULT_VARIABLE HAD_ERROR
352 OUTPUT_VARIABLE LD_V_OUTPUT
355 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
357 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
358 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
359 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
360 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
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 "Misc")
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 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 "Misc")
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 )
526 add_subdirectory(unittests)
527 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
528 list(APPEND CLANG_TEST_PARAMS
529 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
531 add_subdirectory(test)
532 add_subdirectory(bindings/python/tests)
534 if(CLANG_BUILT_STANDALONE)
535 umbrella_lit_testsuite_end(check-all)
537 add_subdirectory(utils/perf-training)
540 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
541 ${LLVM_INCLUDE_DOCS})
542 if( CLANG_INCLUDE_DOCS )
543 add_subdirectory(docs)
546 # Custom target to install all clang libraries.
547 add_custom_target(clang-libraries)
548 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
550 if(NOT LLVM_ENABLE_IDE)
551 add_llvm_install_targets(install-clang-libraries
552 DEPENDS clang-libraries
553 COMPONENT clang-libraries)
556 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
558 list(REMOVE_DUPLICATES CLANG_LIBS)
559 foreach(lib ${CLANG_LIBS})
560 add_dependencies(clang-libraries ${lib})
561 if(NOT LLVM_ENABLE_IDE)
562 add_dependencies(install-clang-libraries install-${lib})
563 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
568 add_subdirectory(cmake/modules)
571 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
574 if (CLANG_ENABLE_BOOTSTRAP)
575 include(ExternalProject)
577 add_custom_target(clang-bootstrap-deps DEPENDS clang)
580 set(CLANG_STAGE stage1)
583 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
585 if(NOT LLVM_BUILD_INSTRUMENTED)
586 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
587 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
589 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
592 set(NEXT_CLANG_STAGE bootstrap)
595 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
596 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
598 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
601 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
602 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
604 if(BOOTSTRAP_LLVM_ENABLE_LLD)
605 # adding lld to clang-bootstrap-deps without having it enabled in
606 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
607 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
608 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
610 add_dependencies(clang-bootstrap-deps lld)
614 # Build llvm-rc and llvm-mt which are needed by the Windows build.
615 add_dependencies(clang-bootstrap-deps llvm-rc)
616 if(LLVM_ENABLE_LIBXML2)
617 add_dependencies(clang-bootstrap-deps llvm-mt)
621 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
622 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
624 add_dependencies(clang-bootstrap-deps LTO)
625 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
626 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
627 # so that the host object file tools will use the just-built libLTO.
628 # However if System Integrity Protection is enabled the DYLD variables
629 # will be scrubbed from the environment of any base system commands. This
630 # includes /bin/sh, which ninja uses when executing build commands. To
631 # work around the envar being filtered away we pass it in as a CMake
632 # variable, and have LLVM's CMake append the envar to the archiver calls.
633 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
634 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
636 add_dependencies(clang-bootstrap-deps llvm-lib)
637 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
639 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
640 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
641 add_dependencies(clang-bootstrap-deps LLVMgold)
643 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
644 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
648 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
649 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
652 add_custom_target(${NEXT_CLANG_STAGE}-clear
653 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
656 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
657 DEPENDS clang-bootstrap-deps
658 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
659 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
660 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
661 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
662 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
665 if(CMAKE_VERBOSE_MAKEFILE)
666 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
669 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
677 CLANG_VERSION_PATCHLEVEL
682 CLANG_REPOSITORY_STRING
684 CMAKE_OSX_ARCHITECTURES
687 LLVM_ENABLE_RUNTIMES)
689 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
690 # because the next stage will use the same compiler used to build this stage.
691 if(NOT LLVM_BUILD_INSTRUMENTED)
692 if(TARGET compiler-rt)
693 add_dependencies(clang-bootstrap-deps compiler-rt)
695 if(TARGET cxx-headers)
696 add_dependencies(clang-bootstrap-deps cxx-headers)
700 set(C_COMPILER "clang")
701 set(CXX_COMPILER "clang++")
703 set(C_COMPILER "clang-cl.exe")
704 set(CXX_COMPILER "clang-cl.exe")
708 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
709 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
710 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
711 -DCMAKE_ASM_COMPILER_ID=Clang)
713 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
714 # otherwise it defaults the linker to be link.exe.
715 if(BOOTSTRAP_LLVM_ENABLE_LLD)
716 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
717 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
721 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
722 set(${CLANG_STAGE}_TABLEGEN
723 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
724 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
725 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
726 if(BOOTSTRAP_LLVM_ENABLE_LLD)
727 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
729 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
730 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
731 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
732 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
734 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
735 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
736 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
737 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
741 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
742 add_dependencies(clang-bootstrap-deps llvm-profdata)
743 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
746 if(LLVM_BUILD_INSTRUMENTED)
747 add_dependencies(clang-bootstrap-deps generate-profdata)
748 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
749 # Use the current tools for LTO instead of the instrumented ones
750 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
759 set(COMPILER_OPTIONS)
765 # Populate the passthrough variables
766 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
767 if(DEFINED ${variableName})
768 if("${${variableName}}" STREQUAL "")
771 string(REPLACE ";" "|" value "${${variableName}}")
773 list(APPEND PASSTHROUGH_VARIABLES
774 -D${variableName}=${value})
778 # Find all variables that start with BOOTSTRAP_ and populate a variable with
780 get_cmake_property(variableNames VARIABLES)
781 foreach(variableName ${variableNames})
782 if(variableName MATCHES "^BOOTSTRAP_")
783 string(SUBSTRING ${variableName} 10 -1 varName)
784 string(REPLACE ";" "|" value "${${variableName}}")
785 list(APPEND PASSTHROUGH_VARIABLES
786 -D${varName}=${value})
788 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
789 list(APPEND PASSTHROUGH_VARIABLES
790 -D${variableName}=${${variableName}})
794 # Build arguments for native tool used in CMake.
795 set(build_configuration "$<CONFIG>")
796 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
797 if(NOT build_tool_args STREQUAL "")
798 string(PREPEND build_tool_args "-- ")
799 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
802 ExternalProject_Add(${NEXT_CLANG_STAGE}
803 DEPENDS clang-bootstrap-deps
804 PREFIX ${NEXT_CLANG_STAGE}
805 SOURCE_DIR ${CMAKE_SOURCE_DIR}
806 STAMP_DIR ${STAMP_DIR}
807 BINARY_DIR ${BINARY_DIR}
810 # We shouldn't need to set this here, but INSTALL_DIR doesn't
811 # seem to work, so instead I'm passing this through
812 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
813 ${PASSTHROUGH_VARIABLES}
814 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
815 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
817 ${${CLANG_STAGE}_TABLEGEN}
818 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
819 ${${CLANG_STAGE}_LINKER}
821 ${${CLANG_STAGE}_RANLIB}
822 ${${CLANG_STAGE}_OBJCOPY}
823 ${${CLANG_STAGE}_STRIP}
824 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
825 --config ${build_configuration}
828 STEP_TARGETS configure build
829 USES_TERMINAL_CONFIGURE 1
830 USES_TERMINAL_BUILD 1
831 USES_TERMINAL_INSTALL 1
835 # exclude really-install from main target
836 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
837 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
838 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
839 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
843 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
844 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
846 if(NOT CLANG_BOOTSTRAP_TARGETS)
847 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
849 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
850 # Install targets have side effects, so we always want to execute them.
851 # "install" is reserved by CMake and can't be used as a step name for
852 # ExternalProject_Add_Step, so we can match against "^install-" instead of
853 # "^install" to get a tighter match. CMake's installation scripts already
854 # skip up-to-date files, so there's no behavior change if you install to the
855 # same destination multiple times.
856 if(target MATCHES "^install-")
862 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
863 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
864 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
866 ALWAYS ${step_always}
871 if(target MATCHES "^stage[0-9]*")
872 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
875 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
879 if (CLANG_BOLT_INSTRUMENT AND NOT LLVM_BUILD_INSTRUMENTED)
880 set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
881 set(CLANG_INSTRUMENTED ${CLANG_PATH}-bolt.inst)
882 set(BOLT_FDATA ${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/prof.fdata)
884 # Instrument clang with BOLT
885 add_custom_target(clang-instrumented
886 DEPENDS ${CLANG_INSTRUMENTED}
888 add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
889 DEPENDS clang llvm-bolt
890 COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
891 -instrument --instrumentation-file-append-pid
892 --instrumentation-file=${BOLT_FDATA}
893 COMMENT "Instrumenting clang binary with BOLT"
897 # Optimize original (pre-bolt) Clang using the collected profile
898 set(CLANG_OPTIMIZED ${CMAKE_CURRENT_BINARY_DIR}/clang.bolt)
899 add_custom_target(clang-bolt
900 DEPENDS ${CLANG_OPTIMIZED}
902 add_custom_command(OUTPUT ${CLANG_OPTIMIZED}
903 DEPENDS clang-bolt-profile
904 COMMAND llvm-bolt ${CLANG_PATH}
905 -o ${CLANG_OPTIMIZED}
907 -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions
908 -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack
909 COMMAND ${CMAKE_COMMAND} -E rename ${CLANG_OPTIMIZED} $<TARGET_FILE:clang>
910 COMMENT "Optimizing Clang with BOLT"
915 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
916 add_subdirectory(utils/ClangVisualizers)
918 add_subdirectory(utils/hmaptool)
920 if(CLANG_BUILT_STANDALONE)
921 llvm_distribution_add_targets()
922 process_llvm_pass_plugins()
925 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
928 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
929 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)