1 cmake_minimum_required(VERSION 3.4.3)
4 cmake_policy(SET CMP0075 NEW)
7 # If we are not building as a part of LLVM, build Clang as an
8 # standalone project, using LLVM as an external library:
9 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
12 # Rely on llvm-config.
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}
31 COMMAND ${CONFIG_COMMAND}
32 RESULT_VARIABLE HAD_ERROR
33 OUTPUT_VARIABLE CONFIG_OUTPUT
37 "[ \t]*[\r\n]+[ \t]*" ";"
38 CONFIG_OUTPUT ${CONFIG_OUTPUT})
40 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
41 message(STATUS "${CONFIG_COMMAND_STR}")
42 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
45 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
46 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
47 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
48 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
49 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
50 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
51 list(GET CONFIG_OUTPUT 6 LLVM_CONFIG_CMAKE_PATH)
53 # Normalize LLVM_CMAKE_PATH. --cmakedir might contain backslashes.
54 # CMake assumes slashes as PATH.
55 file(TO_CMAKE_PATH ${LLVM_CONFIG_CMAKE_PATH} LLVM_CMAKE_PATH)
60 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
61 CACHE BOOL "Enable assertions")
62 # Assertions should follow llvm-config's.
63 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
66 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_PATH}")
67 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
69 # We can't check LLVM_CONFIG here, because find_package(LLVM ...) also sets
71 if (NOT LLVM_CONFIG_FOUND)
72 # Pull values from LLVMConfig.cmake. We can drop this once the llvm-config
74 set(TOOLS_BINARY_DIR ${LLVM_TOOLS_BINARY_DIR})
75 set(LIBRARY_DIR ${LLVM_LIBRARY_DIR})
76 set(INCLUDE_DIR ${LLVM_INCLUDE_DIR})
77 set(LLVM_OBJ_DIR ${LLVM_BINARY_DIR})
78 # The LLVM_CMAKE_PATH variable is set when doing non-standalone builds and
79 # used in this project, so we need to make sure we set this value.
80 # FIXME: LLVM_CMAKE_DIR comes from LLVMConfig.cmake. We should rename
81 # LLVM_CMAKE_PATH to LLVM_CMAKE_DIR throughout the project.
82 set(LLVM_CMAKE_PATH ${LLVM_CMAKE_DIR})
85 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
86 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
87 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
88 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
89 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
91 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
94 # They are used as destination of target generators.
95 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
96 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
98 # DLL platform -- put DLLs into bin.
99 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
101 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
104 option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
105 option(LLVM_INSTALL_TOOLCHAIN_ONLY
106 "Only include toolchain files in the 'install' target." OFF)
108 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
109 "Set to ON to force using an old, unsupported host toolchain." OFF)
110 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
111 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
115 include(HandleLLVMOptions)
116 include(VersionFromVCS)
117 include(LLVMDistributionSupport)
119 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
121 if (NOT DEFINED LLVM_INCLUDE_TESTS)
122 set(LLVM_INCLUDE_TESTS ON)
125 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
126 link_directories("${LLVM_LIBRARY_DIR}")
128 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
129 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
130 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
132 if(LLVM_INCLUDE_TESTS)
133 include(FindPythonInterp)
134 if(NOT PYTHONINTERP_FOUND)
136 "Unable to find Python interpreter, required for builds and testing.
138 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
141 if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
142 message(FATAL_ERROR "Python 2.7 or newer is required")
145 # Check prebuilt llvm/utils.
146 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
147 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
148 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
149 set(LLVM_UTILS_PROVIDED ON)
152 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
153 # Note: path not really used, except for checking if lit was found
154 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
155 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
156 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
158 if(NOT LLVM_UTILS_PROVIDED)
159 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
160 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
161 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
162 set(LLVM_UTILS_PROVIDED ON)
163 set(CLANG_TEST_DEPS FileCheck count not)
165 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
166 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
167 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
168 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
169 add_subdirectory(${UNITTEST_DIR} utils/unittest)
172 # Seek installed Lit.
173 find_program(LLVM_LIT
174 NAMES llvm-lit lit.py lit
175 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
176 DOC "Path to lit.py")
180 # Define the default arguments to use with 'lit', and an option for the user
182 set(LIT_ARGS_DEFAULT "-sv")
184 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
186 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
188 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
189 if( WIN32 AND NOT CYGWIN )
190 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
193 set(LLVM_INCLUDE_TESTS OFF)
197 set( CLANG_BUILT_STANDALONE 1 )
198 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
200 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
203 # Make sure that our source directory is on the current cmake module path so that
204 # we can include cmake files from this directory.
205 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
207 if(LLVM_ENABLE_LIBXML2)
208 # Don't look for libxml if we're using MSan, since uninstrumented third party
209 # code may call MSan interceptors like strlen, leading to false positives.
210 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
211 set (LIBXML2_FOUND 0)
212 find_package(LibXml2 2.5.3 QUIET)
214 set(CLANG_HAVE_LIBXML 1)
219 include(CheckIncludeFile)
220 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
222 set(CLANG_RESOURCE_DIR "" CACHE STRING
223 "Relative directory from the Clang binary to its resource files.")
225 set(C_INCLUDE_DIRS "" CACHE STRING
226 "Colon separated list of directories clang will search for headers.")
228 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
229 set(DEFAULT_SYSROOT "" CACHE PATH
230 "Default <path> to all compiler invocations for --sysroot=<path>." )
232 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
234 set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
235 "enable x86 relax relocations by default")
237 set(ENABLE_EXPERIMENTAL_NEW_PASS_MANAGER FALSE CACHE BOOL
238 "Enable the experimental new pass manager by default.")
240 set(CLANG_SPAWN_CC1 OFF CACHE BOOL
241 "Whether clang should use a new process for the CC1 invocation")
243 # TODO: verify the values against LangStandards.def?
244 set(CLANG_DEFAULT_STD_C "" CACHE STRING
245 "Default standard to use for C/ObjC code (IDENT from LangStandards.def, empty for platform default)")
246 set(CLANG_DEFAULT_STD_CXX "" CACHE STRING
247 "Default standard to use for C++/ObjC++ code (IDENT from LangStandards.def, empty for platform default)")
249 set(CLANG_DEFAULT_LINKER "" CACHE STRING
250 "Default linker to use (linker name or absolute path, empty for platform default)")
252 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
253 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
254 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
255 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
256 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
257 message(WARNING "Resetting default C++ stdlib to use platform default")
258 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
259 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
262 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
263 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
264 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
265 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
266 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
267 message(WARNING "Resetting default rtlib to use platform default")
268 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
269 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
272 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
273 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
274 if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
275 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
276 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
277 elseif (CLANG_DEFAULT_RTLIBS STREQUAL "libunwind")
278 set (CLANG_DEFAULT_UNWINDLIB "none" CACHE STRING "" FORCE)
282 if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
283 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
284 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
285 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
286 message(WARNING "Resetting default unwindlib to use platform default")
287 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
288 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty for none)" FORCE)
291 set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
292 "Default objcopy executable to use.")
294 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
295 "Default OpenMP runtime used by -fopenmp.")
297 # OpenMP offloading requires at least sm_35 because we use shuffle instructions
298 # to generate efficient code for reductions and the atomicMax instruction on
299 # 64-bit integers in the implementation of conditional lastprivate.
300 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH "sm_35" CACHE STRING
301 "Default architecture for OpenMP offloading to Nvidia GPUs.")
302 string(REGEX MATCH "^sm_([0-9]+)$" MATCHED_ARCH "${CLANG_OPENMP_NVPTX_DEFAULT_ARCH}")
303 if (NOT DEFINED MATCHED_ARCH OR "${CMAKE_MATCH_1}" LESS 35)
304 message(WARNING "Resetting default architecture for OpenMP offloading to Nvidia GPUs to sm_35")
305 set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH "sm_35" CACHE STRING
306 "Default architecture for OpenMP offloading to Nvidia GPUs." FORCE)
309 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
310 "Vendor-specific text for showing with version information.")
313 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
316 set(CLANG_REPOSITORY_STRING "" CACHE STRING
317 "Vendor-specific text for showing the repository the source is taken from.")
319 if(CLANG_REPOSITORY_STRING)
320 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
323 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
324 "Vendor-specific uti.")
326 set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
327 "Python versions to install libclang python bindings for")
329 set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
330 "Link tools against libclang-cpp.so")
332 if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
333 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
334 "LLVM_LINK_LLVM_DYLIB=OFF")
337 # The libdir suffix must exactly match whatever LLVM's configuration used.
338 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
340 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
341 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
343 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
344 message(FATAL_ERROR "In-source builds are not allowed. "
345 "Please create a directory and run cmake "
346 "from there, passing the path to this source directory as the last argument. "
347 "This process created the file `CMakeCache.txt' and the directory "
348 "`CMakeFiles'. Please delete them.")
351 # If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
352 if(NOT DEFINED CLANG_VERSION_MAJOR)
353 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
355 if(NOT DEFINED CLANG_VERSION_MINOR)
356 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
358 if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
359 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
361 # Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
362 set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
363 message(STATUS "Clang version: ${CLANG_VERSION}")
365 # Configure the Version.inc file.
367 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
368 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
370 # Add appropriate flags for GCC
371 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
372 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
373 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
374 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
377 # Enable -pedantic for Clang even if it's not enabled for LLVM.
378 if (NOT LLVM_ENABLE_PEDANTIC)
379 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
382 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
383 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
384 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
388 # Determine HOST_LINK_VERSION on Darwin.
389 set(HOST_LINK_VERSION)
393 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
394 RESULT_VARIABLE HAD_ERROR
395 OUTPUT_VARIABLE LD_V_OUTPUT
398 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
399 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
400 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
401 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
404 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
408 include(CMakeParseArguments)
411 set(CMAKE_INCLUDE_CURRENT_DIR ON)
413 include_directories(BEFORE
414 ${CMAKE_CURRENT_BINARY_DIR}/include
415 ${CMAKE_CURRENT_SOURCE_DIR}/include
418 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
419 install(DIRECTORY include/clang include/clang-c
421 COMPONENT clang-headers
425 PATTERN "config.h" EXCLUDE
426 PATTERN ".svn" EXCLUDE
429 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
431 COMPONENT clang-headers
433 PATTERN "CMakeFiles" EXCLUDE
438 # Installing the headers needs to depend on generating any public
439 # tablegen'd headers.
440 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
441 set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
442 if(NOT LLVM_ENABLE_IDE)
443 add_llvm_install_targets(install-clang-headers
444 DEPENDS clang-headers
445 COMPONENT clang-headers)
448 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
449 install(PROGRAMS utils/bash-autocomplete.sh
450 DESTINATION share/clang
451 COMPONENT bash-autocomplete)
452 if(NOT LLVM_ENABLE_IDE)
453 add_llvm_install_targets(install-bash-autocomplete
454 DEPENDS bash-autocomplete
455 COMPONENT bash-autocomplete)
459 add_definitions( -D_GNU_SOURCE )
461 option(CLANG_BUILD_TOOLS
462 "Build the Clang tools. If OFF, just generate build targets." ON)
464 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
465 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
467 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
469 if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
470 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
473 if(CLANG_ENABLE_ARCMT)
474 set(CLANG_ENABLE_OBJC_REWRITER ON)
477 # Clang version information
478 set(CLANG_EXECUTABLE_VERSION
479 "${CLANG_VERSION_MAJOR}" CACHE STRING
480 "Major version number that will be appended to the clang executable name")
481 set(LIBCLANG_LIBRARY_VERSION
482 "${CLANG_VERSION_MAJOR}" CACHE STRING
483 "Major version number that will be appended to the libclang library")
484 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
486 option(CLANG_INCLUDE_TESTS
487 "Generate build targets for the Clang unit tests."
488 ${LLVM_INCLUDE_TESTS})
490 add_subdirectory(utils/TableGen)
492 add_subdirectory(include)
494 # All targets below may depend on all tablegen'd files.
495 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
496 add_custom_target(clang-tablegen-targets DEPENDS ${CLANG_TABLEGEN_TARGETS})
497 set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
498 list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
500 # Force target to be built as soon as possible. Clang modules builds depend
501 # header-wise on it as they ship all headers from the umbrella folders. Building
502 # an entire module might include header, which depends on intrinsics_gen.
503 if(LLVM_ENABLE_MODULES AND NOT CLANG_BUILT_STANDALONE)
504 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
507 add_subdirectory(lib)
508 add_subdirectory(tools)
509 add_subdirectory(runtime)
511 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
512 add_subdirectory(examples)
515 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
516 # default value get updated to the new default.
517 if(CLANG_ORDER_FILE STREQUAL "")
518 unset(CLANG_ORDER_FILE CACHE)
519 unset(CLANG_ORDER_FILE)
523 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
524 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
526 if(NOT EXISTS ${CLANG_ORDER_FILE})
527 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
528 if(PATH_START EQUAL 0)
529 file(WRITE ${CLANG_ORDER_FILE} "\n")
531 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
537 if( CLANG_INCLUDE_TESTS )
538 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
539 add_subdirectory(unittests)
540 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
541 list(APPEND CLANG_TEST_PARAMS
542 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
545 add_subdirectory(test)
546 add_subdirectory(bindings/python/tests)
548 if(CLANG_BUILT_STANDALONE)
549 # Add a global check rule now that all subdirectories have been traversed
550 # and we know the total set of lit testsuites.
551 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
552 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
553 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
554 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
555 get_property(LLVM_ADDITIONAL_TEST_TARGETS
556 GLOBAL PROPERTY LLVM_ADDITIONAL_TEST_TARGETS)
557 add_lit_target(check-all
558 "Running all regression tests"
559 ${LLVM_LIT_TESTSUITES}
560 PARAMS ${LLVM_LIT_PARAMS}
561 DEPENDS ${LLVM_LIT_DEPENDS} ${LLVM_ADDITIONAL_TEST_TARGETS}
562 ARGS ${LLVM_LIT_EXTRA_ARGS}
565 add_subdirectory(utils/perf-training)
568 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
569 ${LLVM_INCLUDE_DOCS})
570 if( CLANG_INCLUDE_DOCS )
571 add_subdirectory(docs)
574 # Custom target to install all clang libraries.
575 add_custom_target(clang-libraries)
576 set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
578 if(NOT LLVM_ENABLE_IDE)
579 add_llvm_install_targets(install-clang-libraries
580 DEPENDS clang-libraries
581 COMPONENT clang-libraries)
584 get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
586 list(REMOVE_DUPLICATES CLANG_LIBS)
587 foreach(lib ${CLANG_LIBS})
588 add_dependencies(clang-libraries ${lib})
589 if(NOT LLVM_ENABLE_IDE)
590 add_dependencies(install-clang-libraries install-${lib})
595 add_subdirectory(cmake/modules)
598 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
601 if (CLANG_ENABLE_BOOTSTRAP)
602 include(ExternalProject)
604 add_custom_target(clang-bootstrap-deps DEPENDS clang)
607 set(CLANG_STAGE stage1)
610 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
612 if(NOT LLVM_BUILD_INSTRUMENTED)
613 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
614 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
616 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
619 set(NEXT_CLANG_STAGE bootstrap)
622 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
623 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
625 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
628 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
629 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
631 if(BOOTSTRAP_LLVM_ENABLE_LLD)
632 add_dependencies(clang-bootstrap-deps lld)
635 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
636 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
638 add_dependencies(clang-bootstrap-deps LTO)
639 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
640 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
641 # so that the host object file tools will use the just-built libLTO.
642 # However if System Integrity Protection is enabled the DYLD variables
643 # will be scrubbed from the environment of any base system commands. This
644 # includes /bin/sh, which ninja uses when executing build commands. To
645 # work around the envar being filtered away we pass it in as a CMake
646 # variable, and have LLVM's CMake append the envar to the archiver calls.
647 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
648 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
650 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
651 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
652 add_dependencies(clang-bootstrap-deps LLVMgold)
654 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
655 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
659 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
660 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
663 add_custom_target(${NEXT_CLANG_STAGE}-clear
664 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
667 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
668 DEPENDS clang-bootstrap-deps
669 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
670 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
671 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
672 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
673 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
676 if(CMAKE_VERBOSE_MAKEFILE)
677 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
680 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
688 CLANG_VERSION_PATCHLEVEL
691 CLANG_REPOSITORY_STRING
692 CMAKE_C_COMPILER_LAUNCHER
693 CMAKE_CXX_COMPILER_LAUNCHER
695 CMAKE_OSX_ARCHITECTURES
697 LLVM_ENABLE_RUNTIMES)
699 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
700 # because the next stage will use the same compiler used to build this stage.
701 if(NOT LLVM_BUILD_INSTRUMENTED)
702 if(TARGET compiler-rt)
703 add_dependencies(clang-bootstrap-deps compiler-rt)
705 if(TARGET cxx-headers)
706 add_dependencies(clang-bootstrap-deps cxx-headers)
710 set(C_COMPILER "clang")
711 set(CXX_COMPILER "clang++")
713 set(C_COMPILER "clang-cl.exe")
714 set(CXX_COMPILER "clang-cl.exe")
718 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
719 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
720 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
721 -DCMAKE_ASM_COMPILER_ID=Clang)
723 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
724 set(${CLANG_STAGE}_CONFIG -DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config)
725 set(${CLANG_STAGE}_TABLEGEN
726 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
727 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
728 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
729 if(BOOTSTRAP_LLVM_ENABLE_LLD)
730 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
732 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
733 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
734 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
735 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
737 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
738 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
739 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
743 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
744 add_dependencies(clang-bootstrap-deps llvm-profdata)
745 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
748 if(LLVM_BUILD_INSTRUMENTED)
749 add_dependencies(clang-bootstrap-deps generate-profdata)
750 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
751 # Use the current tools for LTO instead of the instrumented ones
752 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
761 set(COMPILER_OPTIONS)
767 # Find all variables that start with BOOTSTRAP_ and populate a variable with
769 get_cmake_property(variableNames VARIABLES)
770 foreach(variableName ${variableNames})
771 if(variableName MATCHES "^BOOTSTRAP_")
772 string(SUBSTRING ${variableName} 10 -1 varName)
773 string(REPLACE ";" "|" value "${${variableName}}")
774 list(APPEND PASSTHROUGH_VARIABLES
775 -D${varName}=${value})
777 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
778 list(APPEND PASSTHROUGH_VARIABLES
779 -D${variableName}=${${variableName}})
783 # Populate the passthrough variables
784 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
785 if(DEFINED ${variableName})
786 if("${${variableName}}" STREQUAL "")
789 string(REPLACE ";" "|" value "${${variableName}}")
791 list(APPEND PASSTHROUGH_VARIABLES
792 -D${variableName}=${value})
796 ExternalProject_Add(${NEXT_CLANG_STAGE}
797 DEPENDS clang-bootstrap-deps
798 PREFIX ${NEXT_CLANG_STAGE}
799 SOURCE_DIR ${CMAKE_SOURCE_DIR}
800 STAMP_DIR ${STAMP_DIR}
801 BINARY_DIR ${BINARY_DIR}
804 # We shouldn't need to set this here, but INSTALL_DIR doesn't
805 # seem to work, so instead I'm passing this through
806 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
807 ${PASSTHROUGH_VARIABLES}
808 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
809 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
811 ${${CLANG_STAGE}_CONFIG}
812 ${${CLANG_STAGE}_TABLEGEN}
813 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
814 ${${CLANG_STAGE}_LINKER}
816 ${${CLANG_STAGE}_RANLIB}
817 ${${CLANG_STAGE}_OBJCOPY}
818 ${${CLANG_STAGE}_STRIP}
820 STEP_TARGETS configure build
821 USES_TERMINAL_CONFIGURE 1
822 USES_TERMINAL_BUILD 1
823 USES_TERMINAL_INSTALL 1
827 # exclude really-install from main target
828 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
829 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
830 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
831 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
835 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
836 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
838 if(NOT CLANG_BOOTSTRAP_TARGETS)
839 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
841 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
842 # exclude from main target
843 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
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}'"
852 if(target MATCHES "^stage[0-9]*")
853 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
856 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
860 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
861 add_subdirectory(utils/ClangVisualizers)
863 add_subdirectory(utils/hmaptool)
865 if(CLANG_BUILT_STANDALONE)
866 llvm_distribution_add_targets()
870 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
871 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)