1 cmake_minimum_required(VERSION 3.13.4)
3 # If we are not building as a part of LLVM, build Clang as an
4 # standalone project, using LLVM as an external library:
5 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
7 set(CLANG_BUILT_STANDALONE TRUE)
10 # Must go below project(..)
11 include(GNUInstallDirs)
13 if(CLANG_BUILT_STANDALONE)
14 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
15 set(CMAKE_CXX_STANDARD_REQUIRED YES)
16 set(CMAKE_CXX_EXTENSIONS NO)
19 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
20 CACHE BOOL "Enable assertions")
21 # Assertions should follow llvm-config's.
22 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
25 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
26 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
28 # Turn into CACHE PATHs for overwritting
29 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
30 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
31 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
32 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
33 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
35 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
38 # They are used as destination of target generators.
39 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
40 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
42 # DLL platform -- put DLLs into bin.
43 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
45 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
48 option(LLVM_INSTALL_TOOLCHAIN_ONLY
49 "Only include toolchain files in the 'install' target." OFF)
51 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
52 "Set to ON to force using an old, unsupported host toolchain." OFF)
53 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
54 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
58 include(HandleLLVMOptions)
59 include(VersionFromVCS)
60 include(GetErrcMessages)
61 include(LLVMDistributionSupport)
63 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
64 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
65 "Default URL where bug reports are to be submitted.")
67 if (NOT DEFINED LLVM_INCLUDE_TESTS)
68 set(LLVM_INCLUDE_TESTS ON)
71 include_directories(${LLVM_INCLUDE_DIRS})
72 link_directories("${LLVM_LIBRARY_DIR}")
74 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
75 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
76 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
78 if(LLVM_INCLUDE_TESTS)
79 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
80 COMPONENTS Interpreter)
82 # Check prebuilt llvm/utils.
83 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
84 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
85 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
86 set(LLVM_UTILS_PROVIDED ON)
89 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
90 # Note: path not really used, except for checking if lit was found
91 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
92 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
93 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
95 if(NOT LLVM_UTILS_PROVIDED)
96 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
97 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
98 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
99 set(LLVM_UTILS_PROVIDED ON)
100 set(CLANG_TEST_DEPS FileCheck count not)
102 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
103 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
104 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
105 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
106 add_subdirectory(${UNITTEST_DIR} utils/unittest)
109 # Seek installed Lit.
110 find_program(LLVM_LIT
111 NAMES llvm-lit lit.py lit
112 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
113 DOC "Path to lit.py")
117 # Define the default arguments to use with 'lit', and an option for the user
119 set(LIT_ARGS_DEFAULT "-sv")
121 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
123 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
125 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
127 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
128 if( WIN32 AND NOT CYGWIN )
129 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
132 set(LLVM_INCLUDE_TESTS OFF)
135 umbrella_lit_testsuite_begin(check-all)
136 endif() # LLVM_INCLUDE_TESTS
138 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
140 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
143 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
144 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
147 # Make sure that our source directory is on the current cmake module path so that
148 # we can include cmake files from this directory.
149 list(INSERT CMAKE_MODULE_PATH 0
150 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
151 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
154 if(LLVM_ENABLE_LIBXML2)
155 # Don't look for libxml if we're using MSan, since uninstrumented third party
156 # code may call MSan interceptors like strlen, leading to false positives.
157 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
158 set (LIBXML2_FOUND 0)
159 find_package(LibXml2 2.5.3 QUIET)
161 set(CLANG_HAVE_LIBXML 1)
166 include(CheckIncludeFile)
167 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
169 set(CLANG_RESOURCE_DIR "" CACHE STRING
170 "Relative directory from the Clang binary to its resource files.")
172 set(C_INCLUDE_DIRS "" CACHE STRING
173 "Colon separated list of directories clang will search for headers.")
175 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
176 set(DEFAULT_SYSROOT "" CACHE STRING
177 "Default <path> to all compiler invocations for --sysroot=<path>." )
179 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
181 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
182 "enable x86 relax relocations by default")
184 set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
185 "Enable IEEE binary128 as default long double format on PowerPC Linux.")
187 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
188 "Whether clang should use a new process for the CC1 invocation")
190 option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
192 # Manually handle default so we can change the meaning of a cached default.
193 set(CLANG_ENABLE_OPAQUE_POINTERS "DEFAULT" CACHE STRING
194 "Enable opaque pointers by default")
195 if(CLANG_ENABLE_OPAQUE_POINTERS STREQUAL "DEFAULT")
196 set(CLANG_ENABLE_OPAQUE_POINTERS_INTERNAL ON)
197 elseif(CLANG_ENABLE_OPAQUE_POINTERS)
198 set(CLANG_ENABLE_OPAQUE_POINTERS_INTERNAL ON)
200 set(CLANG_ENABLE_OPAQUE_POINTERS_INTERNAL OFF)
203 # TODO: verify the values against LangStandards.def?
204 set(CLANG_DEFAULT_STD_C "" CACHE STRING
205 "Default standard to use for C/ObjC code (IDENT from LangStandards.def, empty for platform default)")
206 set(CLANG_DEFAULT_STD_CXX "" CACHE STRING
207 "Default standard to use for C++/ObjC++ code (IDENT from LangStandards.def, empty for platform default)")
209 set(CLANG_DEFAULT_LINKER "" CACHE STRING
210 "Default linker to use (linker name or absolute path, empty for platform default)")
212 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
213 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
214 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
215 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
216 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
217 message(WARNING "Resetting default C++ stdlib to use platform default")
218 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
219 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
222 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
223 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
224 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
225 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
226 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
227 message(WARNING "Resetting default rtlib to use platform default")
228 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
229 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
232 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
233 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
234 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
235 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
236 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
240 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
241 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
242 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
243 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
244 message(WARNING "Resetting default unwindlib to use platform default")
245 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
246 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
249 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
250 "Default objcopy executable to use.")
252 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
253 "Default OpenMP runtime used by -fopenmp.")
255 # OpenMP offloading requires at least sm_35 because we use shuffle instructions
256 # to generate efficient code for reductions and the atomicMax instruction on
257 # 64-bit integers in the implementation of conditional lastprivate.
258 set(CUDA_ARCH_FLAGS "sm_35")
260 # Try to find the highest Nvidia GPU architecture the system supports
261 if (NOT DEFINED CLANG_OPENMP_NVPTX_DEFAULT_ARCH)
262 find_package(CUDA QUIET)
264 cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS)
267 set(CUDA_ARCH_FLAGS ${CLANG_OPENMP_NVPTX_DEFAULT_ARCH})
270 string(REGEX MATCH "sm_([0-9]+)" CUDA_ARCH_MATCH ${CUDA_ARCH_FLAGS})
271 if (NOT DEFINED CUDA_ARCH_MATCH OR "${CMAKE_MATCH_1}" LESS 35)
272 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH "sm_35" CACHE STRING
273 "Default architecture for OpenMP offloading to Nvidia GPUs." FORCE)
274 message(WARNING "Resetting default architecture for OpenMP offloading to Nvidia GPUs to sm_35")
276 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH ${CUDA_ARCH_MATCH} CACHE STRING
277 "Default architecture for OpenMP offloading to Nvidia GPUs.")
280 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
282 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
283 "Vendor-specific text for showing with version information.")
285 set(CLANG_REPOSITORY_STRING "" CACHE STRING
286 "Vendor-specific text for showing the repository the source is taken from.")
288 if(CLANG_REPOSITORY_STRING)
289 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
292 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
293 "Vendor-specific uti.")
295 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
296 "Python versions to install libclang python bindings for")
298 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
299 "Link tools against libclang-cpp.so")
301 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
302 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
303 "LLVM_LINK_LLVM_DYLIB=OFF")
306 # The libdir suffix must exactly match whatever LLVM's configuration used.
307 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
309 set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
310 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
311 mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
313 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
314 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
316 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
317 message(FATAL_ERROR "In-source builds are not allowed. "
318 "Please create a directory and run cmake "
319 "from there, passing the path to this source directory as the last argument. "
320 "This process created the file `CMakeCache.txt' and the directory "
321 "`CMakeFiles'. Please delete them.")
324 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
325 if(NOT DEFINED CLANG_VERSION_MAJOR)
326 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
328 if(NOT DEFINED CLANG_VERSION_MINOR)
329 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
331 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
332 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
334 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
335 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
336 message(STATUS "Clang version: ${CLANG_VERSION}")
338 # Configure the Version.inc file.
340 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
341 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
343 # Add appropriate flags for GCC
344 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
345 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
346 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
347 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
350 # Enable -pedantic for Clang even if it's not enabled for LLVM.
351 if (NOT LLVM_ENABLE_PEDANTIC)
352 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
355 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
356 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
357 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
361 # Determine HOST_LINK_VERSION on Darwin.
362 set(HOST_LINK_VERSION)
363 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
366 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
367 RESULT_VARIABLE HAD_ERROR
368 OUTPUT_VARIABLE LD_V_OUTPUT
371 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
373 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
374 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
375 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
376 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
378 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
381 include(CMakeParseArguments)
384 set(CMAKE_INCLUDE_CURRENT_DIR ON)
386 include_directories(BEFORE
387 ${CMAKE_CURRENT_BINARY_DIR}/include
388 ${CMAKE_CURRENT_SOURCE_DIR}/include
391 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
392 install(DIRECTORY include/clang include/clang-c
393 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
394 COMPONENT clang-headers
398 PATTERN "config.h" EXCLUDE
401 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
402 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
403 COMPONENT clang-headers
405 PATTERN "CMakeFiles" EXCLUDE
410 # Installing the headers needs to depend on generating any public
411 # tablegen'd headers.
412 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
413 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
414 if(NOT LLVM_ENABLE_IDE)
415 add_llvm_install_targets(install-clang-headers
416 DEPENDS clang-headers
417 COMPONENT clang-headers)
420 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
421 install(PROGRAMS utils/bash-autocomplete.sh
422 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
423 COMPONENT bash-autocomplete)
424 if(NOT LLVM_ENABLE_IDE)
425 add_llvm_install_targets(install-bash-autocomplete
426 DEPENDS bash-autocomplete
427 COMPONENT bash-autocomplete)
431 add_definitions( -D_GNU_SOURCE )
433 option(CLANG_BUILD_TOOLS
434 "Build the Clang tools. If OFF, just generate build targets." ON)
436 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
437 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
439 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
441 CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
442 "Build clang with plugin support" ON
443 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
445 # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
446 # itself, which is not possible in many platforms because of current limitations in
447 # JIT stack. (more platforms need to be supported by JITLink)
448 if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
449 set(HAVE_CLANG_REPL_SUPPORT ON)
452 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
453 option(CLANG_ENABLE_STATIC_ANALYZER
454 "Include static analyzer in clang binary." ON)
456 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
458 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
459 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
462 if(CLANG_ENABLE_ARCMT)
463 set(CLANG_ENABLE_OBJC_REWRITER ON)
466 # Clang version information
467 set(CLANG_EXECUTABLE_VERSION
468 "${CLANG_VERSION_MAJOR}" CACHE STRING
469 "Major version number that will be appended to the clang executable name")
470 set(LIBCLANG_LIBRARY_VERSION
471 "${CLANG_VERSION_MAJOR}" CACHE STRING
472 "Major version number that will be appended to the libclang library")
473 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
475 option(CLANG_INCLUDE_TESTS
476 "Generate build targets for the Clang unit tests."
477 ${LLVM_INCLUDE_TESTS})
479 add_subdirectory(utils/TableGen)
481 add_subdirectory(include)
483 # All targets below may depend on all tablegen'd files.
484 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
485 add_custom_target(clang-tablegen-targets
488 ${CLANG_TABLEGEN_TARGETS})
489 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
490 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
492 # Force target to be built as soon as possible. Clang modules builds depend
493 # header-wise on it as they ship all headers from the umbrella folders. Building
494 # an entire module might include header, which depends on intrinsics_gen.
495 if(LLVM_ENABLE_MODULES)
496 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
499 add_subdirectory(lib)
500 add_subdirectory(tools)
501 add_subdirectory(runtime)
503 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
504 add_subdirectory(examples)
507 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
508 # default value get updated to the new default.
509 if(CLANG_ORDER_FILE STREQUAL "")
510 unset(CLANG_ORDER_FILE CACHE)
511 unset(CLANG_ORDER_FILE)
515 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
516 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
518 if(NOT EXISTS ${CLANG_ORDER_FILE})
519 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
520 if(PATH_START EQUAL 0)
521 file(WRITE ${CLANG_ORDER_FILE} "\n")
523 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
529 if( CLANG_INCLUDE_TESTS )
530 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
531 add_subdirectory(unittests)
532 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
533 list(APPEND CLANG_TEST_PARAMS
534 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
537 add_subdirectory(test)
538 add_subdirectory(bindings/python/tests)
540 if(CLANG_BUILT_STANDALONE)
541 umbrella_lit_testsuite_end(check-all)
543 add_subdirectory(utils/perf-training)
546 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
547 ${LLVM_INCLUDE_DOCS})
548 if( CLANG_INCLUDE_DOCS )
549 add_subdirectory(docs)
552 # Custom target to install all clang libraries.
553 add_custom_target(clang-libraries)
554 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
556 if(NOT LLVM_ENABLE_IDE)
557 add_llvm_install_targets(install-clang-libraries
558 DEPENDS clang-libraries
559 COMPONENT clang-libraries)
562 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
564 list(REMOVE_DUPLICATES CLANG_LIBS)
565 foreach(lib ${CLANG_LIBS})
566 add_dependencies(clang-libraries ${lib})
567 if(NOT LLVM_ENABLE_IDE)
568 add_dependencies(install-clang-libraries install-${lib})
569 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
574 add_subdirectory(cmake/modules)
577 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
580 if (CLANG_ENABLE_BOOTSTRAP)
581 include(ExternalProject)
583 add_custom_target(clang-bootstrap-deps DEPENDS clang)
586 set(CLANG_STAGE stage1)
589 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
591 if(NOT LLVM_BUILD_INSTRUMENTED)
592 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
593 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
595 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
598 set(NEXT_CLANG_STAGE bootstrap)
601 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
602 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
604 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
607 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
608 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
610 if(BOOTSTRAP_LLVM_ENABLE_LLD)
611 # adding lld to clang-bootstrap-deps without having it enabled in
612 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
613 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
614 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
616 add_dependencies(clang-bootstrap-deps lld)
619 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
620 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
622 add_dependencies(clang-bootstrap-deps LTO)
623 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
624 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
625 # so that the host object file tools will use the just-built libLTO.
626 # However if System Integrity Protection is enabled the DYLD variables
627 # will be scrubbed from the environment of any base system commands. This
628 # includes /bin/sh, which ninja uses when executing build commands. To
629 # work around the envar being filtered away we pass it in as a CMake
630 # variable, and have LLVM's CMake append the envar to the archiver calls.
631 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
632 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
634 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
635 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
636 add_dependencies(clang-bootstrap-deps LLVMgold)
638 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
639 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
643 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
644 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
647 add_custom_target(${NEXT_CLANG_STAGE}-clear
648 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
651 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
652 DEPENDS clang-bootstrap-deps
653 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
654 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
655 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
656 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
657 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
660 if(CMAKE_VERBOSE_MAKEFILE)
661 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
664 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
672 CLANG_VERSION_PATCHLEVEL
676 CLANG_REPOSITORY_STRING
677 CMAKE_C_COMPILER_LAUNCHER
678 CMAKE_CXX_COMPILER_LAUNCHER
680 CMAKE_OSX_ARCHITECTURES
683 LLVM_ENABLE_RUNTIMES)
685 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
686 # because the next stage will use the same compiler used to build this stage.
687 if(NOT LLVM_BUILD_INSTRUMENTED)
688 if(TARGET compiler-rt)
689 add_dependencies(clang-bootstrap-deps compiler-rt)
691 if(TARGET cxx-headers)
692 add_dependencies(clang-bootstrap-deps cxx-headers)
696 set(C_COMPILER "clang")
697 set(CXX_COMPILER "clang++")
699 set(C_COMPILER "clang-cl.exe")
700 set(CXX_COMPILER "clang-cl.exe")
704 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
705 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
706 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
707 -DCMAKE_ASM_COMPILER_ID=Clang)
709 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
710 # otherwise it defaults the linker to be link.exe.
711 if(BOOTSTRAP_LLVM_ENABLE_LLD)
712 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
713 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
717 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
718 set(${CLANG_STAGE}_TABLEGEN
719 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
720 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
721 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
722 if(BOOTSTRAP_LLVM_ENABLE_LLD)
723 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
725 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
726 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
727 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
728 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
730 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
731 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
732 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
733 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
737 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
738 add_dependencies(clang-bootstrap-deps llvm-profdata)
739 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
742 if(LLVM_BUILD_INSTRUMENTED)
743 add_dependencies(clang-bootstrap-deps generate-profdata)
744 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
745 # Use the current tools for LTO instead of the instrumented ones
746 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
755 set(COMPILER_OPTIONS)
761 # Populate the passthrough variables
762 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
763 if(DEFINED ${variableName})
764 if("${${variableName}}" STREQUAL "")
767 string(REPLACE ";" "|" value "${${variableName}}")
769 list(APPEND PASSTHROUGH_VARIABLES
770 -D${variableName}=${value})
774 # Find all variables that start with BOOTSTRAP_ and populate a variable with
776 get_cmake_property(variableNames VARIABLES)
777 foreach(variableName ${variableNames})
778 if(variableName MATCHES "^BOOTSTRAP_")
779 string(SUBSTRING ${variableName} 10 -1 varName)
780 string(REPLACE ";" "|" value "${${variableName}}")
781 list(APPEND PASSTHROUGH_VARIABLES
782 -D${varName}=${value})
784 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
785 list(APPEND PASSTHROUGH_VARIABLES
786 -D${variableName}=${${variableName}})
790 # Build arguments for native tool used in CMake.
791 set(build_configuration "$<CONFIG>")
792 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
793 if(NOT build_tool_args STREQUAL "")
794 string(PREPEND build_tool_args "-- ")
795 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
798 ExternalProject_Add(${NEXT_CLANG_STAGE}
799 DEPENDS clang-bootstrap-deps
800 PREFIX ${NEXT_CLANG_STAGE}
801 SOURCE_DIR ${CMAKE_SOURCE_DIR}
802 STAMP_DIR ${STAMP_DIR}
803 BINARY_DIR ${BINARY_DIR}
806 # We shouldn't need to set this here, but INSTALL_DIR doesn't
807 # seem to work, so instead I'm passing this through
808 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
809 ${PASSTHROUGH_VARIABLES}
810 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
811 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
813 ${${CLANG_STAGE}_TABLEGEN}
814 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
815 ${${CLANG_STAGE}_LINKER}
817 ${${CLANG_STAGE}_RANLIB}
818 ${${CLANG_STAGE}_OBJCOPY}
819 ${${CLANG_STAGE}_STRIP}
820 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
821 --config ${build_configuration}
824 STEP_TARGETS configure build
825 USES_TERMINAL_CONFIGURE 1
826 USES_TERMINAL_BUILD 1
827 USES_TERMINAL_INSTALL 1
831 # exclude really-install from main target
832 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
833 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
834 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
835 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
839 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
840 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
842 if(NOT CLANG_BOOTSTRAP_TARGETS)
843 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
845 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
846 # Install targets have side effects, so we always want to execute them.
847 # "install" is reserved by CMake and can't be used as a step name for
848 # ExternalProject_Add_Step, so we can match against "^install-" instead of
849 # "^install" to get a tighter match. CMake's installation scripts already
850 # skip up-to-date files, so there's no behavior change if you install to the
851 # same destination multiple times.
852 if(target MATCHES "^install-")
858 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
859 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
860 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
862 ALWAYS ${step_always}
867 if(target MATCHES "^stage[0-9]*")
868 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
871 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
875 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
876 add_subdirectory(utils/ClangVisualizers)
878 add_subdirectory(utils/hmaptool)
880 if(CLANG_BUILT_STANDALONE)
881 llvm_distribution_add_targets()
882 process_llvm_pass_plugins()
885 set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
888 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
889 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)