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)
8 set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard to conform to")
9 set(CMAKE_CXX_STANDARD_REQUIRED YES)
10 set(CMAKE_CXX_EXTENSIONS NO)
12 # Rely on llvm-config.
13 set(LLVM_CONFIG_OUTPUT)
15 set (LLVM_CONFIG_FOUND 1)
16 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
17 message(DEPRECATION "Using llvm-config to detect the LLVM installation is \
18 deprecated. The installed cmake files should be used \
19 instead. CMake should be able to detect your LLVM install \
20 automatically, but you can also use LLVM_DIR to specify \
21 the path containing LLVMConfig.cmake.")
22 set(CONFIG_COMMAND ${LLVM_CONFIG}
32 COMMAND ${CONFIG_COMMAND}
33 RESULT_VARIABLE HAD_ERROR
34 OUTPUT_VARIABLE LLVM_CONFIG_OUTPUT
38 "[ \t]*[\r\n]+[ \t]*" ";"
39 LLVM_CONFIG_OUTPUT ${LLVM_CONFIG_OUTPUT})
41 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
42 message(STATUS "${CONFIG_COMMAND_STR}")
43 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
46 list(GET LLVM_CONFIG_OUTPUT 0 MAIN_INCLUDE_DIR)
47 list(GET LLVM_CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
48 list(GET LLVM_CONFIG_OUTPUT 2 MAIN_SRC_DIR)
49 list(GET LLVM_CONFIG_OUTPUT 3 LLVM_CONFIG_CMAKE_DIR)
50 list(GET LLVM_CONFIG_OUTPUT 4 TOOLS_BINARY_DIR)
51 list(GET LLVM_CONFIG_OUTPUT 5 LIBRARY_DIR)
52 list(GET LLVM_CONFIG_OUTPUT 6 ENABLE_ASSERTIONS)
54 # Normalize LLVM_CMAKE_DIR. --cmakedir might contain backslashes.
55 # CMake assumes slashes as PATH.
56 file(TO_CMAKE_PATH ${LLVM_CONFIG_CMAKE_DIR} LLVM_CMAKE_DIR)
61 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
62 CACHE BOOL "Enable assertions")
63 # Assertions should follow llvm-config's.
64 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
67 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
68 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
70 # We can't check LLVM_CONFIG here, because find_package(LLVM ...) also sets
72 if (NOT LLVM_CONFIG_FOUND)
73 # Pull values from LLVMConfig.cmake. We can drop this once the llvm-config
75 set(MAIN_INCLUDE_DIR ${LLVM_INCLUDE_DIR})
76 set(LLVM_OBJ_DIR ${LLVM_BINARY_DIR})
77 set(TOOLS_BINARY_DIR ${LLVM_TOOLS_BINARY_DIR})
78 set(LIBRARY_DIR ${LLVM_LIBRARY_DIR})
81 set(LLVM_MAIN_INCLUDE_DIR ${MAIN_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
82 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
83 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
84 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
85 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
87 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
90 # They are used as destination of target generators.
91 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
92 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
94 # DLL platform -- put DLLs into bin.
95 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
97 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
100 option(LLVM_INSTALL_TOOLCHAIN_ONLY
101 "Only include toolchain files in the 'install' target." OFF)
103 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
104 "Set to ON to force using an old, unsupported host toolchain." OFF)
105 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
106 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
110 include(HandleLLVMOptions)
111 include(VersionFromVCS)
112 include(GetErrcMessages)
113 include(LLVMDistributionSupport)
115 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
116 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
117 "Default URL where bug reports are to be submitted.")
119 if (NOT DEFINED LLVM_INCLUDE_TESTS)
120 set(LLVM_INCLUDE_TESTS ON)
123 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
124 link_directories("${LLVM_LIBRARY_DIR}")
126 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
127 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
128 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
130 if(LLVM_INCLUDE_TESTS)
131 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
132 COMPONENTS Interpreter)
134 # Check prebuilt llvm/utils.
135 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
136 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
137 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
138 set(LLVM_UTILS_PROVIDED ON)
141 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
142 # Note: path not really used, except for checking if lit was found
143 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
144 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
145 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
147 if(NOT LLVM_UTILS_PROVIDED)
148 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
149 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
150 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
151 set(LLVM_UTILS_PROVIDED ON)
152 set(CLANG_TEST_DEPS FileCheck count not)
154 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
155 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
156 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
157 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
158 add_subdirectory(${UNITTEST_DIR} utils/unittest)
161 # Seek installed Lit.
162 find_program(LLVM_LIT
163 NAMES llvm-lit lit.py lit
164 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
165 DOC "Path to lit.py")
169 # Define the default arguments to use with 'lit', and an option for the user
171 set(LIT_ARGS_DEFAULT "-sv")
173 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
175 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
177 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
179 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
180 if( WIN32 AND NOT CYGWIN )
181 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
184 set(LLVM_INCLUDE_TESTS OFF)
188 set(CLANG_BUILT_STANDALONE TRUE)
190 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
192 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
195 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
196 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
199 # Make sure that our source directory is on the current cmake module path so that
200 # we can include cmake files from this directory.
201 list(INSERT CMAKE_MODULE_PATH 0
202 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
203 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
206 if(LLVM_ENABLE_LIBXML2)
207 # Don't look for libxml if we're using MSan, since uninstrumented third party
208 # code may call MSan interceptors like strlen, leading to false positives.
209 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
210 set (LIBXML2_FOUND 0)
211 find_package(LibXml2 2.5.3 QUIET)
213 set(CLANG_HAVE_LIBXML 1)
218 include(CheckIncludeFile)
219 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
221 set(CLANG_RESOURCE_DIR "" CACHE STRING
222 "Relative directory from the Clang binary to its resource files.")
224 set(C_INCLUDE_DIRS "" CACHE STRING
225 "Colon separated list of directories clang will search for headers.")
227 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
228 set(DEFAULT_SYSROOT "" CACHE STRING
229 "Default <path> to all compiler invocations for --sysroot=<path>." )
231 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
233 set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
234 "enable x86 relax relocations by default")
236 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
237 "Whether clang should use a new process for the CC1 invocation")
239 option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on Linux" OFF)
241 # TODO: verify the values against LangStandards.def?
242 set(CLANG_DEFAULT_STD_C "" CACHE STRING
243 "Default standard to use for C/ObjC code (IDENT from LangStandards.def, empty for platform default)")
244 set(CLANG_DEFAULT_STD_CXX "" CACHE STRING
245 "Default standard to use for C++/ObjC++ code (IDENT from LangStandards.def, empty for platform default)")
247 set(CLANG_DEFAULT_LINKER "" CACHE STRING
248 "Default linker to use (linker name or absolute path, empty for platform default)")
250 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
251 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
252 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
253 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
254 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
255 message(WARNING "Resetting default C++ stdlib to use platform default")
256 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
257 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
260 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
261 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
262 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
263 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
264 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
265 message(WARNING "Resetting default rtlib to use platform default")
266 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
267 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
270 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
271 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
272 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
273 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
274 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
278 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
279 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
280 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
281 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
282 message(WARNING "Resetting default unwindlib to use platform default")
283 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
284 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
287 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
288 "Default objcopy executable to use.")
290 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
291 "Default OpenMP runtime used by -fopenmp.")
293 # OpenMP offloading requires at least sm_35 because we use shuffle instructions
294 # to generate efficient code for reductions and the atomicMax instruction on
295 # 64-bit integers in the implementation of conditional lastprivate.
296 set(CUDA_ARCH_FLAGS "sm_35")
298 # Try to find the highest Nvidia GPU architecture the system supports
299 if (NOT DEFINED CLANG_OPENMP_NVPTX_DEFAULT_ARCH)
300 find_package(CUDA QUIET)
302 cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS)
305 set(CUDA_ARCH_FLAGS ${CLANG_OPENMP_NVPTX_DEFAULT_ARCH})
308 string(REGEX MATCH "sm_([0-9]+)" CUDA_ARCH_MATCH ${CUDA_ARCH_FLAGS})
309 if (NOT DEFINED CUDA_ARCH_MATCH OR "${CMAKE_MATCH_1}" LESS 35)
310 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH "sm_35" CACHE STRING
311 "Default architecture for OpenMP offloading to Nvidia GPUs." FORCE)
312 message(WARNING "Resetting default architecture for OpenMP offloading to Nvidia GPUs to sm_35")
314 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH ${CUDA_ARCH_MATCH} CACHE STRING
315 "Default architecture for OpenMP offloading to Nvidia GPUs.")
318 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
320 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
321 "Vendor-specific text for showing with version information.")
323 set(CLANG_REPOSITORY_STRING "" CACHE STRING
324 "Vendor-specific text for showing the repository the source is taken from.")
326 if(CLANG_REPOSITORY_STRING)
327 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
330 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
331 "Vendor-specific uti.")
333 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
334 "Python versions to install libclang python bindings for")
336 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
337 "Link tools against libclang-cpp.so")
339 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
340 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
341 "LLVM_LINK_LLVM_DYLIB=OFF")
344 # The libdir suffix must exactly match whatever LLVM's configuration used.
345 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
347 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
348 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
350 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
351 message(FATAL_ERROR "In-source builds are not allowed. "
352 "Please create a directory and run cmake "
353 "from there, passing the path to this source directory as the last argument. "
354 "This process created the file `CMakeCache.txt' and the directory "
355 "`CMakeFiles'. Please delete them.")
358 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
359 if(NOT DEFINED CLANG_VERSION_MAJOR)
360 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
362 if(NOT DEFINED CLANG_VERSION_MINOR)
363 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
365 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
366 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
368 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
369 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
370 message(STATUS "Clang version: ${CLANG_VERSION}")
372 # Configure the Version.inc file.
374 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
375 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
377 # Add appropriate flags for GCC
378 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
379 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
380 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
381 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
384 # Enable -pedantic for Clang even if it's not enabled for LLVM.
385 if (NOT LLVM_ENABLE_PEDANTIC)
386 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
389 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
390 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
391 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
395 # Determine HOST_LINK_VERSION on Darwin.
396 set(HOST_LINK_VERSION)
400 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
401 RESULT_VARIABLE HAD_ERROR
402 OUTPUT_VARIABLE LD_V_OUTPUT
405 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
407 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
408 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
409 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
410 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
412 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
415 include(CMakeParseArguments)
418 set(CMAKE_INCLUDE_CURRENT_DIR ON)
420 include_directories(BEFORE
421 ${CMAKE_CURRENT_BINARY_DIR}/include
422 ${CMAKE_CURRENT_SOURCE_DIR}/include
425 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
426 install(DIRECTORY include/clang include/clang-c
428 COMPONENT clang-headers
432 PATTERN "config.h" EXCLUDE
435 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
437 COMPONENT clang-headers
439 PATTERN "CMakeFiles" EXCLUDE
444 # Installing the headers needs to depend on generating any public
445 # tablegen'd headers.
446 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
447 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
448 if(NOT LLVM_ENABLE_IDE)
449 add_llvm_install_targets(install-clang-headers
450 DEPENDS clang-headers
451 COMPONENT clang-headers)
454 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
455 install(PROGRAMS utils/bash-autocomplete.sh
456 DESTINATION share/clang
457 COMPONENT bash-autocomplete)
458 if(NOT LLVM_ENABLE_IDE)
459 add_llvm_install_targets(install-bash-autocomplete
460 DEPENDS bash-autocomplete
461 COMPONENT bash-autocomplete)
465 add_definitions( -D_GNU_SOURCE )
467 option(CLANG_BUILD_TOOLS
468 "Build the Clang tools. If OFF, just generate build targets." ON)
470 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
471 option(CLANG_ENABLE_STATIC_ANALYZER
472 "Include static analyzer in clang binary." ON)
474 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
476 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
477 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
480 if(CLANG_ENABLE_ARCMT)
481 set(CLANG_ENABLE_OBJC_REWRITER ON)
484 # Clang version information
485 set(CLANG_EXECUTABLE_VERSION
486 "${CLANG_VERSION_MAJOR}" CACHE STRING
487 "Major version number that will be appended to the clang executable name")
488 set(LIBCLANG_LIBRARY_VERSION
489 "${CLANG_VERSION_MAJOR}" CACHE STRING
490 "Major version number that will be appended to the libclang library")
491 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
493 option(CLANG_INCLUDE_TESTS
494 "Generate build targets for the Clang unit tests."
495 ${LLVM_INCLUDE_TESTS})
497 add_subdirectory(utils/TableGen)
499 add_subdirectory(include)
501 # All targets below may depend on all tablegen'd files.
502 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
503 add_custom_target(clang-tablegen-targets
506 ${CLANG_TABLEGEN_TARGETS})
507 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
508 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
510 # Force target to be built as soon as possible. Clang modules builds depend
511 # header-wise on it as they ship all headers from the umbrella folders. Building
512 # an entire module might include header, which depends on intrinsics_gen.
513 if(LLVM_ENABLE_MODULES)
514 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
517 add_subdirectory(lib)
518 add_subdirectory(tools)
519 add_subdirectory(runtime)
521 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
522 add_subdirectory(examples)
525 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
526 # default value get updated to the new default.
527 if(CLANG_ORDER_FILE STREQUAL "")
528 unset(CLANG_ORDER_FILE CACHE)
529 unset(CLANG_ORDER_FILE)
533 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
534 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
536 if(NOT EXISTS ${CLANG_ORDER_FILE})
537 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
538 if(PATH_START EQUAL 0)
539 file(WRITE ${CLANG_ORDER_FILE} "\n")
541 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
547 if( CLANG_INCLUDE_TESTS )
548 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
549 add_subdirectory(unittests)
550 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
551 list(APPEND CLANG_TEST_PARAMS
552 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
555 add_subdirectory(test)
556 add_subdirectory(bindings/python/tests)
558 if(CLANG_BUILT_STANDALONE)
559 # Add a global check rule now that all subdirectories have been traversed
560 # and we know the total set of lit testsuites.
561 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
562 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
563 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
564 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
565 get_property(LLVM_ADDITIONAL_TEST_TARGETS
566 GLOBAL PROPERTY LLVM_ADDITIONAL_TEST_TARGETS)
567 add_lit_target(check-all
568 "Running all regression tests"
569 ${LLVM_LIT_TESTSUITES}
570 PARAMS ${LLVM_LIT_PARAMS}
571 DEPENDS ${LLVM_LIT_DEPENDS} ${LLVM_ADDITIONAL_TEST_TARGETS}
572 ARGS ${LLVM_LIT_EXTRA_ARGS}
575 add_subdirectory(utils/perf-training)
578 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
579 ${LLVM_INCLUDE_DOCS})
580 if( CLANG_INCLUDE_DOCS )
581 add_subdirectory(docs)
584 # Custom target to install all clang libraries.
585 add_custom_target(clang-libraries)
586 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
588 if(NOT LLVM_ENABLE_IDE)
589 add_llvm_install_targets(install-clang-libraries
590 DEPENDS clang-libraries
591 COMPONENT clang-libraries)
594 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
596 list(REMOVE_DUPLICATES CLANG_LIBS)
597 foreach(lib ${CLANG_LIBS})
598 add_dependencies(clang-libraries ${lib})
599 if(NOT LLVM_ENABLE_IDE)
600 add_dependencies(install-clang-libraries install-${lib})
601 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
606 add_subdirectory(cmake/modules)
609 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
612 if (CLANG_ENABLE_BOOTSTRAP)
613 include(ExternalProject)
615 add_custom_target(clang-bootstrap-deps DEPENDS clang)
618 set(CLANG_STAGE stage1)
621 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
623 if(NOT LLVM_BUILD_INSTRUMENTED)
624 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
625 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
627 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
630 set(NEXT_CLANG_STAGE bootstrap)
633 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
634 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
636 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
639 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
640 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
642 if(BOOTSTRAP_LLVM_ENABLE_LLD)
643 # adding lld to clang-bootstrap-deps without having it enabled in
644 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
645 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
646 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
648 add_dependencies(clang-bootstrap-deps lld)
651 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
652 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
654 add_dependencies(clang-bootstrap-deps LTO)
655 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
656 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
657 # so that the host object file tools will use the just-built libLTO.
658 # However if System Integrity Protection is enabled the DYLD variables
659 # will be scrubbed from the environment of any base system commands. This
660 # includes /bin/sh, which ninja uses when executing build commands. To
661 # work around the envar being filtered away we pass it in as a CMake
662 # variable, and have LLVM's CMake append the envar to the archiver calls.
663 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
664 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
666 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
667 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
668 add_dependencies(clang-bootstrap-deps LLVMgold)
670 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
671 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
675 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
676 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
679 add_custom_target(${NEXT_CLANG_STAGE}-clear
680 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
683 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
684 DEPENDS clang-bootstrap-deps
685 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
686 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
687 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
688 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
689 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
692 if(CMAKE_VERBOSE_MAKEFILE)
693 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
696 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
704 CLANG_VERSION_PATCHLEVEL
708 CLANG_REPOSITORY_STRING
709 CMAKE_C_COMPILER_LAUNCHER
710 CMAKE_CXX_COMPILER_LAUNCHER
712 CMAKE_OSX_ARCHITECTURES
715 LLVM_ENABLE_RUNTIMES)
717 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
718 # because the next stage will use the same compiler used to build this stage.
719 if(NOT LLVM_BUILD_INSTRUMENTED)
720 if(TARGET compiler-rt)
721 add_dependencies(clang-bootstrap-deps compiler-rt)
723 if(TARGET cxx-headers)
724 add_dependencies(clang-bootstrap-deps cxx-headers)
728 set(C_COMPILER "clang")
729 set(CXX_COMPILER "clang++")
731 set(C_COMPILER "clang-cl.exe")
732 set(CXX_COMPILER "clang-cl.exe")
736 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
737 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
738 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
739 -DCMAKE_ASM_COMPILER_ID=Clang)
741 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
742 # otherwise it defaults the linker to be link.exe.
743 if(BOOTSTRAP_LLVM_ENABLE_LLD)
744 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
745 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
749 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
750 set(${CLANG_STAGE}_CONFIG -DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config)
751 set(${CLANG_STAGE}_TABLEGEN
752 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
753 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
754 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
755 if(BOOTSTRAP_LLVM_ENABLE_LLD)
756 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
758 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
759 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
760 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
761 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
763 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
764 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
765 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
766 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
770 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
771 add_dependencies(clang-bootstrap-deps llvm-profdata)
772 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
775 if(LLVM_BUILD_INSTRUMENTED)
776 add_dependencies(clang-bootstrap-deps generate-profdata)
777 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
778 # Use the current tools for LTO instead of the instrumented ones
779 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
788 set(COMPILER_OPTIONS)
794 # Find all variables that start with BOOTSTRAP_ and populate a variable with
796 get_cmake_property(variableNames VARIABLES)
797 foreach(variableName ${variableNames})
798 if(variableName MATCHES "^BOOTSTRAP_")
799 string(SUBSTRING ${variableName} 10 -1 varName)
800 string(REPLACE ";" "|" value "${${variableName}}")
801 list(APPEND PASSTHROUGH_VARIABLES
802 -D${varName}=${value})
804 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
805 list(APPEND PASSTHROUGH_VARIABLES
806 -D${variableName}=${${variableName}})
810 # Populate the passthrough variables
811 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
812 if(DEFINED ${variableName})
813 if("${${variableName}}" STREQUAL "")
816 string(REPLACE ";" "|" value "${${variableName}}")
818 list(APPEND PASSTHROUGH_VARIABLES
819 -D${variableName}=${value})
823 ExternalProject_Add(${NEXT_CLANG_STAGE}
824 DEPENDS clang-bootstrap-deps
825 PREFIX ${NEXT_CLANG_STAGE}
826 SOURCE_DIR ${CMAKE_SOURCE_DIR}
827 STAMP_DIR ${STAMP_DIR}
828 BINARY_DIR ${BINARY_DIR}
831 # We shouldn't need to set this here, but INSTALL_DIR doesn't
832 # seem to work, so instead I'm passing this through
833 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
834 ${PASSTHROUGH_VARIABLES}
835 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
836 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
838 ${${CLANG_STAGE}_CONFIG}
839 ${${CLANG_STAGE}_TABLEGEN}
840 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
841 ${${CLANG_STAGE}_LINKER}
843 ${${CLANG_STAGE}_RANLIB}
844 ${${CLANG_STAGE}_OBJCOPY}
845 ${${CLANG_STAGE}_STRIP}
847 STEP_TARGETS configure build
848 USES_TERMINAL_CONFIGURE 1
849 USES_TERMINAL_BUILD 1
850 USES_TERMINAL_INSTALL 1
854 # exclude really-install from main target
855 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
856 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
857 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
858 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
862 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
863 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
865 if(NOT CLANG_BOOTSTRAP_TARGETS)
866 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
868 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
869 # Install targets have side effects, so we always want to execute them.
870 # "install" is reserved by CMake and can't be used as a step name for
871 # ExternalProject_Add_Step, so we can match against "^install-" instead of
872 # "^install" to get a tighter match. CMake's installation scripts already
873 # skip up-to-date files, so there's no behavior change if you install to the
874 # same destination multiple times.
875 if(target MATCHES "^install-")
881 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
882 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
883 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
885 ALWAYS ${step_always}
890 if(target MATCHES "^stage[0-9]*")
891 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
894 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
898 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
899 add_subdirectory(utils/ClangVisualizers)
901 add_subdirectory(utils/hmaptool)
903 if(CLANG_BUILT_STANDALONE)
904 llvm_distribution_add_targets()
905 process_llvm_pass_plugins()
909 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
910 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)