1 cmake_minimum_required(VERSION 3.13.4)
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)
14 if ("${CMAKE_VERSION}" VERSION_LESS "3.20.0")
16 "Your CMake version is ${CMAKE_VERSION}. Starting with LLVM 17.0.0, the "
17 "minimum version of CMake required to build LLVM will become 3.20.0, and "
18 "using an older CMake will become an error. Please upgrade your CMake to "
19 "at least 3.20.0 now to avoid issues in the future!")
23 # Must go below project(..)
24 include(GNUInstallDirs)
26 if(CLANG_BUILT_STANDALONE)
27 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
28 set(CMAKE_CXX_STANDARD_REQUIRED YES)
29 set(CMAKE_CXX_EXTENSIONS NO)
32 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
33 CACHE BOOL "Enable assertions")
34 # Assertions should follow llvm-config's.
35 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
38 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
39 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
41 # Turn into CACHE PATHs for overwritting
42 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
43 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
44 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
45 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
46 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
48 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
51 # They are used as destination of target generators.
52 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
53 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
55 # DLL platform -- put DLLs into bin.
56 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
58 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
61 option(LLVM_INSTALL_TOOLCHAIN_ONLY
62 "Only include toolchain files in the 'install' target." OFF)
64 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
65 "Set to ON to force using an old, unsupported host toolchain." OFF)
66 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
67 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
71 include(HandleLLVMOptions)
72 include(VersionFromVCS)
74 include(GetErrcMessages)
75 include(LLVMDistributionSupport)
77 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
78 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
79 "Default URL where bug reports are to be submitted.")
81 if (NOT DEFINED LLVM_INCLUDE_TESTS)
82 set(LLVM_INCLUDE_TESTS ON)
85 include_directories(${LLVM_INCLUDE_DIRS})
86 link_directories("${LLVM_LIBRARY_DIR}")
88 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
89 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
90 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
92 if(LLVM_INCLUDE_TESTS)
93 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
94 COMPONENTS Interpreter)
96 # Check prebuilt llvm/utils.
97 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
98 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
99 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
100 set(LLVM_UTILS_PROVIDED ON)
103 # Seek installed Lit.
104 find_program(LLVM_LIT
105 NAMES llvm-lit lit.py lit
106 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
107 DOC "Path to lit.py")
109 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
110 # Note: path not really used, except for checking if lit was found
111 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
112 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
114 if(NOT LLVM_UTILS_PROVIDED)
115 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
116 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
117 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
118 set(LLVM_UTILS_PROVIDED ON)
119 set(CLANG_TEST_DEPS FileCheck count not)
121 set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
122 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
123 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
124 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
125 add_subdirectory(${UNITTEST_DIR} third-party/unittest)
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 # Make sure that our source directory is on the current cmake module path so that
153 # we can include cmake files from this directory.
154 list(INSERT CMAKE_MODULE_PATH 0
155 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
156 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
159 # This allows disabling clang's XML dependency even if LLVM finds libxml2.
160 # By default, clang depends on libxml2 if LLVM does.
161 option(CLANG_ENABLE_LIBXML2 "Whether libclang may depend on libxml2"
162 ${LLVM_ENABLE_LIBXML2})
164 if(CLANG_ENABLE_LIBXML2)
165 # Don't look for libxml if we're using MSan, since uninstrumented third party
166 # code may call MSan interceptors like strlen, leading to false positives.
167 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
168 set (LIBXML2_FOUND 0)
169 find_package(LibXml2 2.5.3 QUIET)
171 set(CLANG_HAVE_LIBXML 1)
176 include(CheckIncludeFile)
177 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
179 set(CLANG_RESOURCE_DIR "" CACHE STRING
180 "Relative directory from the Clang binary to its resource files.")
182 set(C_INCLUDE_DIRS "" CACHE STRING
183 "Colon separated list of directories clang will search for headers.")
185 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
186 set(DEFAULT_SYSROOT "" CACHE STRING
187 "Default <path> to all compiler invocations for --sysroot=<path>." )
189 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
191 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
192 "enable x86 relax relocations by default")
194 set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
195 "Enable IEEE binary128 as default long double format on PowerPC Linux.")
197 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
198 "Whether clang should use a new process for the CC1 invocation")
200 option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
202 set(CLANG_DEFAULT_LINKER "" CACHE STRING
203 "Default linker to use (linker name or absolute path, empty for platform default)")
205 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
206 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
207 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
208 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
209 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
210 message(WARNING "Resetting default C++ stdlib to use platform default")
211 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
212 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
215 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
216 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
217 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
218 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
219 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
220 message(WARNING "Resetting default rtlib to use platform default")
221 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
222 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
225 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
226 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
227 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
228 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
229 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
233 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
234 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
235 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
236 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
237 message(WARNING "Resetting default unwindlib to use platform default")
238 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
239 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
242 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
243 "Default objcopy executable to use.")
245 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
246 "Default OpenMP runtime used by -fopenmp.")
248 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
250 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
251 "Vendor-specific text for showing with version information.")
253 set(CLANG_REPOSITORY_STRING "" CACHE STRING
254 "Vendor-specific text for showing the repository the source is taken from.")
256 if(CLANG_REPOSITORY_STRING)
257 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
260 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
261 "Vendor-specific uti.")
263 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
264 "Python versions to install libclang python bindings for")
266 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
267 "Link tools against libclang-cpp.so")
269 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
270 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
271 "LLVM_LINK_LLVM_DYLIB=OFF")
274 # The libdir suffix must exactly match whatever LLVM's configuration used.
275 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
277 set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
278 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
279 mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
281 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
282 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
284 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
285 message(FATAL_ERROR "In-source builds are not allowed. "
286 "Please create a directory and run cmake "
287 "from there, passing the path to this source directory as the last argument. "
288 "This process created the file `CMakeCache.txt' and the directory "
289 "`CMakeFiles'. Please delete them.")
292 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
293 if(NOT DEFINED CLANG_VERSION_MAJOR)
294 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
296 if(NOT DEFINED CLANG_VERSION_MINOR)
297 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
299 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
300 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
302 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
303 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
304 message(STATUS "Clang version: ${CLANG_VERSION}")
306 # Configure the Version.inc file.
308 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
309 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
311 # Add appropriate flags for GCC
312 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
313 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
314 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
315 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
318 # Enable -pedantic for Clang even if it's not enabled for LLVM.
319 if (NOT LLVM_ENABLE_PEDANTIC)
320 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
323 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
324 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
325 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
329 # Determine HOST_LINK_VERSION on Darwin.
330 set(HOST_LINK_VERSION)
331 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
334 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
335 RESULT_VARIABLE HAD_ERROR
336 OUTPUT_VARIABLE LD_V_OUTPUT
339 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
341 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
342 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
343 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
344 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
346 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
349 include(CMakeParseArguments)
352 set(CMAKE_INCLUDE_CURRENT_DIR ON)
354 include_directories(BEFORE
355 ${CMAKE_CURRENT_BINARY_DIR}/include
356 ${CMAKE_CURRENT_SOURCE_DIR}/include
359 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
360 install(DIRECTORY include/clang include/clang-c
361 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
362 COMPONENT clang-headers
366 PATTERN "config.h" EXCLUDE
369 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
370 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
371 COMPONENT clang-headers
373 PATTERN "CMakeFiles" EXCLUDE
378 # Installing the headers needs to depend on generating any public
379 # tablegen'd headers.
380 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
381 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
382 if(NOT LLVM_ENABLE_IDE)
383 add_llvm_install_targets(install-clang-headers
384 DEPENDS clang-headers
385 COMPONENT clang-headers)
388 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
389 install(FILES utils/bash-autocomplete.sh
390 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
391 COMPONENT bash-autocomplete)
392 if(NOT LLVM_ENABLE_IDE)
393 add_llvm_install_targets(install-bash-autocomplete
394 DEPENDS bash-autocomplete
395 COMPONENT bash-autocomplete)
399 option(CLANG_BUILD_TOOLS
400 "Build the Clang tools. If OFF, just generate build targets." ON)
402 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
403 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
405 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
407 CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
408 "Build clang with plugin support" ON
409 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
411 # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
412 # itself, which is not possible in many platforms because of current limitations in
413 # JIT stack. (more platforms need to be supported by JITLink)
414 if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
415 set(HAVE_CLANG_REPL_SUPPORT ON)
418 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
419 option(CLANG_ENABLE_STATIC_ANALYZER
420 "Include static analyzer in clang binary." ON)
422 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
424 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
425 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
428 if(CLANG_ENABLE_ARCMT)
429 set(CLANG_ENABLE_OBJC_REWRITER ON)
432 # This option is a stop-gap, we should commit to removing this as
433 # soon as possible. See discussion:
434 # https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
435 option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
436 "Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
438 # Clang version information
439 set(CLANG_EXECUTABLE_VERSION
440 "${CLANG_VERSION_MAJOR}" CACHE STRING
441 "Major version number that will be appended to the clang executable name")
442 set(LIBCLANG_LIBRARY_VERSION
443 "${CLANG_VERSION_MAJOR}" CACHE STRING
444 "Major version number that will be appended to the libclang library")
445 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
447 option(CLANG_INCLUDE_TESTS
448 "Generate build targets for the Clang unit tests."
449 ${LLVM_INCLUDE_TESTS})
451 option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
452 # While HLSL support is experimental this should stay hidden.
453 mark_as_advanced(CLANG_ENABLE_HLSL)
455 add_subdirectory(utils/TableGen)
457 # Export CLANG_TABLEGEN_EXE for use by flang docs.
458 set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
460 add_subdirectory(include)
462 # All targets below may depend on all tablegen'd files.
463 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
464 add_custom_target(clang-tablegen-targets
467 ${CLANG_TABLEGEN_TARGETS})
468 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
469 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
471 # Force target to be built as soon as possible. Clang modules builds depend
472 # header-wise on it as they ship all headers from the umbrella folders. Building
473 # an entire module might include header, which depends on intrinsics_gen.
474 if(LLVM_ENABLE_MODULES)
475 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
478 add_subdirectory(lib)
479 add_subdirectory(tools)
480 add_subdirectory(runtime)
482 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
483 add_subdirectory(examples)
486 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
487 # default value get updated to the new default.
488 if(CLANG_ORDER_FILE STREQUAL "")
489 unset(CLANG_ORDER_FILE CACHE)
490 unset(CLANG_ORDER_FILE)
494 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
495 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
497 if(NOT EXISTS ${CLANG_ORDER_FILE})
498 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
499 if(PATH_START EQUAL 0)
500 file(WRITE ${CLANG_ORDER_FILE} "\n")
502 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
508 if( CLANG_INCLUDE_TESTS )
509 if(EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
510 add_subdirectory(unittests)
511 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
512 list(APPEND CLANG_TEST_PARAMS
513 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
516 add_subdirectory(test)
517 add_subdirectory(bindings/python/tests)
519 if(CLANG_BUILT_STANDALONE)
520 umbrella_lit_testsuite_end(check-all)
522 add_subdirectory(utils/perf-training)
525 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
526 ${LLVM_INCLUDE_DOCS})
527 if( CLANG_INCLUDE_DOCS )
528 add_subdirectory(docs)
531 # Custom target to install all clang libraries.
532 add_custom_target(clang-libraries)
533 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
535 if(NOT LLVM_ENABLE_IDE)
536 add_llvm_install_targets(install-clang-libraries
537 DEPENDS clang-libraries
538 COMPONENT clang-libraries)
541 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
543 list(REMOVE_DUPLICATES CLANG_LIBS)
544 foreach(lib ${CLANG_LIBS})
545 add_dependencies(clang-libraries ${lib})
546 if(NOT LLVM_ENABLE_IDE)
547 add_dependencies(install-clang-libraries install-${lib})
548 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
553 add_subdirectory(cmake/modules)
556 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
559 if (CLANG_ENABLE_BOOTSTRAP)
560 include(ExternalProject)
562 add_custom_target(clang-bootstrap-deps DEPENDS clang)
565 set(CLANG_STAGE stage1)
568 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
570 if(NOT LLVM_BUILD_INSTRUMENTED)
571 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
572 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
574 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
577 set(NEXT_CLANG_STAGE bootstrap)
580 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
581 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
583 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
586 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
587 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
589 if(BOOTSTRAP_LLVM_ENABLE_LLD)
590 # adding lld to clang-bootstrap-deps without having it enabled in
591 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
592 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
593 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
595 add_dependencies(clang-bootstrap-deps lld)
599 # Build llvm-rc and llvm-mt which are needed by the Windows build.
600 add_dependencies(clang-bootstrap-deps llvm-rc)
601 if(LLVM_ENABLE_LIBXML2)
602 add_dependencies(clang-bootstrap-deps llvm-mt)
606 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
607 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
609 add_dependencies(clang-bootstrap-deps LTO)
610 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
611 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
612 # so that the host object file tools will use the just-built libLTO.
613 # However if System Integrity Protection is enabled the DYLD variables
614 # will be scrubbed from the environment of any base system commands. This
615 # includes /bin/sh, which ninja uses when executing build commands. To
616 # work around the envar being filtered away we pass it in as a CMake
617 # variable, and have LLVM's CMake append the envar to the archiver calls.
618 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
619 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
621 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
622 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
623 add_dependencies(clang-bootstrap-deps LLVMgold)
625 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
626 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
630 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
631 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
634 add_custom_target(${NEXT_CLANG_STAGE}-clear
635 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
638 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
639 DEPENDS clang-bootstrap-deps
640 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
641 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
642 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
643 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
644 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
647 if(CMAKE_VERBOSE_MAKEFILE)
648 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
651 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
659 CLANG_VERSION_PATCHLEVEL
663 CLANG_REPOSITORY_STRING
664 CMAKE_C_COMPILER_LAUNCHER
665 CMAKE_CXX_COMPILER_LAUNCHER
667 CMAKE_OSX_ARCHITECTURES
670 LLVM_ENABLE_RUNTIMES)
672 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
673 # because the next stage will use the same compiler used to build this stage.
674 if(NOT LLVM_BUILD_INSTRUMENTED)
675 if(TARGET compiler-rt)
676 add_dependencies(clang-bootstrap-deps compiler-rt)
678 if(TARGET cxx-headers)
679 add_dependencies(clang-bootstrap-deps cxx-headers)
683 set(C_COMPILER "clang")
684 set(CXX_COMPILER "clang++")
686 set(C_COMPILER "clang-cl.exe")
687 set(CXX_COMPILER "clang-cl.exe")
691 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
692 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
693 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
694 -DCMAKE_ASM_COMPILER_ID=Clang)
696 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
697 # otherwise it defaults the linker to be link.exe.
698 if(BOOTSTRAP_LLVM_ENABLE_LLD)
699 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
700 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
704 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
705 set(${CLANG_STAGE}_TABLEGEN
706 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
707 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
708 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
709 if(BOOTSTRAP_LLVM_ENABLE_LLD)
710 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
712 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
713 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
714 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
715 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
717 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
718 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
719 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
720 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
724 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
725 add_dependencies(clang-bootstrap-deps llvm-profdata)
726 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
729 if(LLVM_BUILD_INSTRUMENTED)
730 add_dependencies(clang-bootstrap-deps generate-profdata)
731 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
732 # Use the current tools for LTO instead of the instrumented ones
733 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
742 set(COMPILER_OPTIONS)
748 # Populate the passthrough variables
749 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
750 if(DEFINED ${variableName})
751 if("${${variableName}}" STREQUAL "")
754 string(REPLACE ";" "|" value "${${variableName}}")
756 list(APPEND PASSTHROUGH_VARIABLES
757 -D${variableName}=${value})
761 # Find all variables that start with BOOTSTRAP_ and populate a variable with
763 get_cmake_property(variableNames VARIABLES)
764 foreach(variableName ${variableNames})
765 if(variableName MATCHES "^BOOTSTRAP_")
766 string(SUBSTRING ${variableName} 10 -1 varName)
767 string(REPLACE ";" "|" value "${${variableName}}")
768 list(APPEND PASSTHROUGH_VARIABLES
769 -D${varName}=${value})
771 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
772 list(APPEND PASSTHROUGH_VARIABLES
773 -D${variableName}=${${variableName}})
777 # Build arguments for native tool used in CMake.
778 set(build_configuration "$<CONFIG>")
779 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
780 if(NOT build_tool_args STREQUAL "")
781 string(PREPEND build_tool_args "-- ")
782 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
785 ExternalProject_Add(${NEXT_CLANG_STAGE}
786 DEPENDS clang-bootstrap-deps
787 PREFIX ${NEXT_CLANG_STAGE}
788 SOURCE_DIR ${CMAKE_SOURCE_DIR}
789 STAMP_DIR ${STAMP_DIR}
790 BINARY_DIR ${BINARY_DIR}
793 # We shouldn't need to set this here, but INSTALL_DIR doesn't
794 # seem to work, so instead I'm passing this through
795 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
796 ${PASSTHROUGH_VARIABLES}
797 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
798 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
800 ${${CLANG_STAGE}_TABLEGEN}
801 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
802 ${${CLANG_STAGE}_LINKER}
804 ${${CLANG_STAGE}_RANLIB}
805 ${${CLANG_STAGE}_OBJCOPY}
806 ${${CLANG_STAGE}_STRIP}
807 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
808 --config ${build_configuration}
811 STEP_TARGETS configure build
812 USES_TERMINAL_CONFIGURE 1
813 USES_TERMINAL_BUILD 1
814 USES_TERMINAL_INSTALL 1
818 # exclude really-install from main target
819 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
820 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
821 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
822 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
826 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
827 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
829 if(NOT CLANG_BOOTSTRAP_TARGETS)
830 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
832 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
833 # Install targets have side effects, so we always want to execute them.
834 # "install" is reserved by CMake and can't be used as a step name for
835 # ExternalProject_Add_Step, so we can match against "^install-" instead of
836 # "^install" to get a tighter match. CMake's installation scripts already
837 # skip up-to-date files, so there's no behavior change if you install to the
838 # same destination multiple times.
839 if(target MATCHES "^install-")
845 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
846 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
847 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
849 ALWAYS ${step_always}
854 if(target MATCHES "^stage[0-9]*")
855 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
858 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
862 if (CLANG_BOLT_INSTRUMENT AND NOT LLVM_BUILD_INSTRUMENTED)
863 set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
864 set(CLANGXX_PATH ${CLANG_PATH}++)
865 set(CLANG_INSTRUMENTED ${CLANG_PATH}-bolt.inst)
866 set(CLANGXX_INSTRUMENTED ${CLANGXX_PATH}-bolt.inst)
868 # Instrument clang with BOLT
869 add_custom_target(clang-instrumented
870 DEPENDS ${CLANG_INSTRUMENTED}
872 add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
873 DEPENDS clang llvm-bolt
874 COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
875 -instrument --instrumentation-file-append-pid
876 --instrumentation-file=${CMAKE_CURRENT_BINARY_DIR}/prof.fdata
877 COMMENT "Instrumenting clang binary with BOLT"
881 # Make a symlink from clang-bolt.inst to clang++-bolt.inst
882 add_custom_target(clang++-instrumented
883 DEPENDS ${CLANGXX_INSTRUMENTED}
885 add_custom_command(OUTPUT ${CLANGXX_INSTRUMENTED}
886 DEPENDS clang-instrumented
887 COMMAND ${CMAKE_COMMAND} -E create_symlink
888 ${CLANG_INSTRUMENTED}
889 ${CLANGXX_INSTRUMENTED}
890 COMMENT "Creating symlink from BOLT instrumented clang to clang++"
894 # Build specified targets with instrumented Clang to collect the profile
895 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt-instrumented-clang-stamps/)
896 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt-instrumented-clang-bins/)
897 set(build_configuration "$<CONFIG>")
898 include(ExternalProject)
899 ExternalProject_Add(bolt-instrumentation-profile
900 DEPENDS clang++-instrumented
901 PREFIX bolt-instrumentation-profile
902 SOURCE_DIR ${CMAKE_SOURCE_DIR}
903 STAMP_DIR ${STAMP_DIR}
904 BINARY_DIR ${BINARY_DIR}
907 ${CLANG_BOLT_INSTRUMENT_EXTRA_CMAKE_FLAGS}
908 # We shouldn't need to set this here, but INSTALL_DIR doesn't
909 # seem to work, so instead I'm passing this through
910 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
911 -DCMAKE_C_COMPILER=${CLANG_INSTRUMENTED}
912 -DCMAKE_CXX_COMPILER=${CLANGXX_INSTRUMENTED}
913 -DCMAKE_ASM_COMPILER=${CLANG_INSTRUMENTED}
914 -DCMAKE_ASM_COMPILER_ID=Clang
915 -DCMAKE_BUILD_TYPE=Release
916 -DLLVM_ENABLE_PROJECTS=${CLANG_BOLT_INSTRUMENT_PROJECTS}
917 -DLLVM_TARGETS_TO_BUILD=${LLVM_TARGETS_TO_BUILD}
918 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
919 --config ${build_configuration}
920 --target ${CLANG_BOLT_INSTRUMENT_TARGETS}
922 STEP_TARGETS configure build
923 USES_TERMINAL_CONFIGURE 1
924 USES_TERMINAL_BUILD 1
925 USES_TERMINAL_INSTALL 1
928 # Merge profiles into one using merge-fdata
929 add_custom_target(clang-bolt-profile
930 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata
932 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata
933 DEPENDS merge-fdata bolt-instrumentation-profile-build
934 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
935 COMMAND ${Python3_EXECUTABLE}
936 ${CMAKE_CURRENT_SOURCE_DIR}/utils/perf-training/perf-helper.py merge-fdata
937 $<TARGET_FILE:merge-fdata> ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata
938 ${CMAKE_CURRENT_BINARY_DIR}
939 COMMENT "Preparing BOLT profile"
943 # Optimize original (pre-bolt) Clang using the collected profile
944 set(CLANG_OPTIMIZED ${CMAKE_CURRENT_BINARY_DIR}/clang.bolt)
945 add_custom_target(clang-bolt
946 DEPENDS ${CLANG_OPTIMIZED}
948 add_custom_command(OUTPUT ${CLANG_OPTIMIZED}
949 DEPENDS clang-bolt-profile
950 COMMAND llvm-bolt ${CLANG_PATH}
951 -o ${CLANG_OPTIMIZED}
952 -data ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata
953 -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions
954 -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack
955 COMMAND ${CMAKE_COMMAND} -E rename ${CLANG_OPTIMIZED} $<TARGET_FILE:clang>
956 COMMENT "Optimizing Clang with BOLT"
961 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
962 add_subdirectory(utils/ClangVisualizers)
964 add_subdirectory(utils/hmaptool)
966 if(CLANG_BUILT_STANDALONE)
967 llvm_distribution_add_targets()
968 process_llvm_pass_plugins()
971 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
974 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
975 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)