1 cmake_minimum_required(VERSION 3.4.3)
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 )
10 find_program(LLVM_CONFIG "llvm-config")
12 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
13 set(CONFIG_COMMAND ${LLVM_CONFIG}
21 COMMAND ${CONFIG_COMMAND}
22 RESULT_VARIABLE HAD_ERROR
23 OUTPUT_VARIABLE CONFIG_OUTPUT
27 "[ \t]*[\r\n]+[ \t]*" ";"
28 CONFIG_OUTPUT ${CONFIG_OUTPUT})
30 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
31 message(STATUS "${CONFIG_COMMAND_STR}")
32 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
35 message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
38 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
39 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
40 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
41 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
42 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
43 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
46 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
47 CACHE BOOL "Enable assertions")
48 # Assertions should follow llvm-config's.
49 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
52 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
53 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
54 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
55 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
56 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
58 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
61 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
62 set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
63 if(EXISTS ${LLVMCONFIG_FILE})
64 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
65 include(${LLVMCONFIG_FILE})
67 message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
70 # They are used as destination of target generators.
71 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
72 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
74 # DLL platform -- put DLLs into bin.
75 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
77 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
80 option(LLVM_INSTALL_TOOLCHAIN_ONLY
81 "Only include toolchain files in the 'install' target." OFF)
83 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
84 "Set to ON to force using an old, unsupported host toolchain." OFF)
85 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
89 include(HandleLLVMOptions)
90 include(VersionFromVCS)
92 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
94 if (NOT DEFINED LLVM_INCLUDE_TESTS)
95 set(LLVM_INCLUDE_TESTS ON)
98 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
99 link_directories("${LLVM_LIBRARY_DIR}")
101 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
102 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
103 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
105 if(LLVM_INCLUDE_TESTS)
106 set(Python_ADDITIONAL_VERSIONS 2.7)
107 include(FindPythonInterp)
108 if(NOT PYTHONINTERP_FOUND)
110 "Unable to find Python interpreter, required for builds and testing.
112 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
115 if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
116 message(FATAL_ERROR "Python 2.7 or newer is required")
119 # Check prebuilt llvm/utils.
120 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
121 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
122 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
123 set(LLVM_UTILS_PROVIDED ON)
126 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
127 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
128 if(NOT LLVM_UTILS_PROVIDED)
129 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
130 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
131 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
132 set(LLVM_UTILS_PROVIDED ON)
133 set(CLANG_TEST_DEPS FileCheck count not)
135 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
136 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
137 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
138 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
139 add_subdirectory(${UNITTEST_DIR} utils/unittest)
142 # Seek installed Lit.
143 find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
144 DOC "Path to lit.py")
148 # Define the default arguments to use with 'lit', and an option for the user
150 set(LIT_ARGS_DEFAULT "-sv")
152 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
154 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
156 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
157 if( WIN32 AND NOT CYGWIN )
158 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
161 set(LLVM_INCLUDE_TESTS OFF)
165 set( CLANG_BUILT_STANDALONE 1 )
166 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
168 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
171 find_package(LibXml2 2.5.3 QUIET)
173 set(CLANG_HAVE_LIBXML 1)
176 set(CLANG_RESOURCE_DIR "" CACHE STRING
177 "Relative directory from the Clang binary to its resource files.")
179 set(C_INCLUDE_DIRS "" CACHE STRING
180 "Colon separated list of directories clang will search for headers.")
182 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
183 set(DEFAULT_SYSROOT "" CACHE PATH
184 "Default <path> to all compiler invocations for --sysroot=<path>." )
186 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
188 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
189 "Default C++ stdlib to use (empty for architecture default, \"libstdc++\" or \"libc++\"")
190 if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
191 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
192 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
193 message(WARNING "Resetting default C++ stdlib to use architecture default")
194 set(CLANG_DEFAULT_CXX_STDLIB "")
197 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
198 "Default OpenMP runtime used by -fopenmp.")
200 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
201 "Vendor-specific text for showing with version information.")
204 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
207 set(CLANG_REPOSITORY_STRING "" CACHE STRING
208 "Vendor-specific text for showing the repository the source is taken from.")
210 if(CLANG_REPOSITORY_STRING)
211 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
214 option(CLANG_APPEND_VC_REV
215 "Append the version control system revision id to clang version spew" OFF)
216 if(CLANG_APPEND_VC_REV)
218 # This macro will set SVN_REVISION in the parent scope
219 add_version_info_from_vcs(VERSION_VAR)
223 add_definitions(-DSVN_REVISION="${SVN_REVISION}")
227 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
228 "Vendor-specific uti.")
230 # The libdir suffix must exactly match whatever LLVM's configuration used.
231 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
233 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
234 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
236 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
237 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
238 "the makefiles distributed with LLVM. Please create a directory and run cmake "
239 "from there, passing the path to this source directory as the last argument. "
240 "This process created the file `CMakeCache.txt' and the directory "
241 "`CMakeFiles'. Please delete them.")
244 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
246 tablegenned_files_on_include_dir
247 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
248 if( tablegenned_files_on_include_dir )
249 message(FATAL_ERROR "Apparently there is a previous in-source build, "
250 "probably as the result of running `configure' and `make' on "
251 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
252 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
256 # Compute the Clang version from the LLVM version.
257 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
259 message(STATUS "Clang version: ${CLANG_VERSION}")
261 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
263 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
265 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
266 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
267 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
270 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
273 # Configure the Version.inc file.
275 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
276 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
278 # Add appropriate flags for GCC
279 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
280 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
281 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
282 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
285 # Enable -pedantic for Clang even if it's not enabled for LLVM.
286 if (NOT LLVM_ENABLE_PEDANTIC)
287 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
290 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
291 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
292 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
296 # Determine HOST_LINK_VERSION on Darwin.
297 set(HOST_LINK_VERSION)
301 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
302 RESULT_VARIABLE HAD_ERROR
303 OUTPUT_VARIABLE LD_V_OUTPUT
306 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
307 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
308 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
309 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
312 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
317 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
318 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
320 include(CMakeParseArguments)
322 function(clang_tablegen)
324 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
325 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
327 # Generates a custom command for invoking tblgen as
329 # tblgen source-file -o=output-file tablegen-arg ...
331 # and, if cmake-target-name is provided, creates a custom target for
332 # executing the custom command depending on output-file. It is
333 # possible to list more files to depend after DEPENDS.
335 cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
338 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
341 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
342 tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS})
345 add_public_tablegen_target(${CTG_TARGET})
346 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
347 set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
349 endfunction(clang_tablegen)
351 macro(set_clang_windows_version_resource_properties name)
352 if(DEFINED windows_resource_file)
353 set_windows_version_resource_properties(${name} ${windows_resource_file}
354 VERSION_MAJOR ${CLANG_VERSION_MAJOR}
355 VERSION_MINOR ${CLANG_VERSION_MINOR}
356 VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
357 VERSION_STRING "${CLANG_VERSION} (${BACKEND_PACKAGE_STRING})"
358 PRODUCT_NAME "clang")
362 macro(add_clang_subdirectory name)
363 add_llvm_subdirectory(CLANG TOOL ${name})
366 macro(add_clang_library name)
367 cmake_parse_arguments(ARG
373 if(MSVC_IDE OR XCODE)
375 file(RELATIVE_PATH lib_path
376 ${CLANG_SOURCE_DIR}/lib/
377 ${CMAKE_CURRENT_SOURCE_DIR}
379 if(NOT lib_path MATCHES "^[.][.]")
380 file( GLOB_RECURSE headers
381 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
382 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
384 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
386 file( GLOB_RECURSE tds
387 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
389 source_group("TableGen descriptions" FILES ${tds})
390 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
393 set(srcs ${headers} ${tds})
396 endif(MSVC_IDE OR XCODE)
397 if(srcs OR ARG_ADDITIONAL_HEADERS)
401 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
405 set(ARG_ENABLE_SHARED SHARED)
407 llvm_add_library(${name} ${ARG_ENABLE_SHARED} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
410 target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
412 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
413 install(TARGETS ${name}
416 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
417 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
418 RUNTIME DESTINATION bin)
420 if (${ARG_SHARED} AND NOT CMAKE_CONFIGURATION_TYPES)
421 add_custom_target(install-${name}
423 COMMAND "${CMAKE_COMMAND}"
424 -DCMAKE_INSTALL_COMPONENT=${name}
425 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
428 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
430 # Add empty "phony" target
431 add_custom_target(${name})
434 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
435 set_clang_windows_version_resource_properties(${name})
436 endmacro(add_clang_library)
438 macro(add_clang_executable name)
439 add_llvm_executable( ${name} ${ARGN} )
440 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
441 set_clang_windows_version_resource_properties(${name})
442 endmacro(add_clang_executable)
444 macro(add_clang_tool name)
445 add_clang_executable(${name} ${ARGN})
446 install(TARGETS ${name}
447 RUNTIME DESTINATION bin
450 if(NOT CMAKE_CONFIGURATION_TYPES)
451 add_custom_target(install-${name}
453 COMMAND "${CMAKE_COMMAND}"
454 -DCMAKE_INSTALL_COMPONENT=${name}
455 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
459 macro(add_clang_symlink name dest)
460 add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
461 # Always generate install targets
462 llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
465 set(CMAKE_INCLUDE_CURRENT_DIR ON)
467 include_directories(BEFORE
468 ${CMAKE_CURRENT_BINARY_DIR}/include
469 ${CMAKE_CURRENT_SOURCE_DIR}/include
472 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
473 install(DIRECTORY include/clang include/clang-c
478 PATTERN "config.h" EXCLUDE
479 PATTERN ".svn" EXCLUDE
482 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
485 PATTERN "CMakeFiles" EXCLUDE
491 add_definitions( -D_GNU_SOURCE )
493 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
494 if (CLANG_ENABLE_ARCMT)
495 set(ENABLE_CLANG_ARCMT "1")
497 set(ENABLE_CLANG_ARCMT "0")
500 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
501 if (CLANG_ENABLE_STATIC_ANALYZER)
502 set(ENABLE_CLANG_STATIC_ANALYZER "1")
504 set(ENABLE_CLANG_STATIC_ANALYZER "0")
507 if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
508 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
511 if(CLANG_ENABLE_ARCMT)
512 add_definitions(-DCLANG_ENABLE_ARCMT)
513 add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
515 if(CLANG_ENABLE_STATIC_ANALYZER)
516 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
519 # Clang version information
520 set(CLANG_EXECUTABLE_VERSION
521 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
522 "Version number that will be placed into the clang executable, in the form XX.YY")
523 set(LIBCLANG_LIBRARY_VERSION
524 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
525 "Version number that will be placed into the libclang library , in the form XX.YY")
526 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
528 option(CLANG_INCLUDE_TESTS
529 "Generate build targets for the Clang unit tests."
530 ${LLVM_INCLUDE_TESTS})
532 add_subdirectory(utils/TableGen)
534 add_subdirectory(include)
536 # All targets below may depend on all tablegen'd files.
537 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
538 list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
540 add_subdirectory(lib)
541 add_subdirectory(tools)
542 add_subdirectory(runtime)
544 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
545 if (CLANG_BUILD_EXAMPLES)
546 set(ENABLE_CLANG_EXAMPLES "1")
548 set(ENABLE_CLANG_EXAMPLES "0")
550 add_subdirectory(examples)
552 if( CLANG_INCLUDE_TESTS )
553 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
554 add_subdirectory(unittests)
555 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
556 list(APPEND CLANG_TEST_PARAMS
557 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
560 add_subdirectory(test)
562 if(CLANG_BUILT_STANDALONE)
563 # Add a global check rule now that all subdirectories have been traversed
564 # and we know the total set of lit testsuites.
565 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
566 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
567 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
568 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
569 add_lit_target(check-all
570 "Running all regression tests"
571 ${LLVM_LIT_TESTSUITES}
572 PARAMS ${LLVM_LIT_PARAMS}
573 DEPENDS ${LLVM_LIT_DEPENDS}
574 ARGS ${LLVM_LIT_EXTRA_ARGS}
577 add_subdirectory(utils/perf-training)
580 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
581 ${LLVM_INCLUDE_DOCS})
582 if( CLANG_INCLUDE_DOCS )
583 add_subdirectory(docs)
588 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
589 # default value get updated to the new default.
590 if(CLANG_ORDER_FILE STREQUAL "")
591 unset(CLANG_ORDER_FILE CACHE)
592 unset(CLANG_ORDER_FILE)
596 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
597 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
599 if(CLANG_ORDER_FILE AND NOT EXISTS ${CLANG_ORDER_FILE})
600 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
601 if(PATH_START EQUAL 0)
602 file(WRITE ${CLANG_ORDER_FILE} "\n")
604 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
609 # Generate a list of CMake library targets so that other CMake projects can
610 # link against them. LLVM calls its version of this file LLVMExports.cmake, but
611 # the usual CMake convention seems to be ${Project}Targets.cmake.
612 set(CLANG_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/clang)
613 set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
614 get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
615 export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
617 # Install a <prefix>/lib/cmake/clang/ClangConfig.cmake file so that
618 # find_package(Clang) works. Install the target list with it.
619 install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR})
622 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
623 DESTINATION lib${LLVM_LIBDIR_SUFFIX}/cmake/clang)
625 # Also copy ClangConfig.cmake to the build directory so that dependent projects
626 # can build against a build directory of Clang more easily.
628 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
629 ${CLANG_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang/ClangConfig.cmake
632 if (CLANG_ENABLE_BOOTSTRAP)
633 include(ExternalProject)
636 set(CLANG_STAGE stage1)
637 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
640 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
642 if(NOT LLVM_BUILD_INSTRUMENTED)
643 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
644 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
646 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
649 set(NEXT_CLANG_STAGE bootstrap)
652 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
653 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
655 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
658 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
659 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
660 set(cmake_command ${CMAKE_COMMAND})
662 # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
663 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
666 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
667 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
668 # so that the host object file tools will use the just-built libLTO.
669 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
670 set(cmake_command ${CMAKE_COMMAND} -E env DYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR} ${CMAKE_COMMAND})
672 list(APPEND LTO_DEP LLVMgold llvm-ar llvm-ranlib)
673 set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
674 set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
678 add_custom_target(${NEXT_CLANG_STAGE}-clear
679 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
682 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
683 DEPENDS clang ${LTO_DEP}
684 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
685 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
686 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
687 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
688 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
691 if(CMAKE_VERBOSE_MAKEFILE)
692 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
695 set(BOOTSTRAP_DEFAULT_PASSTHROUGH
702 CLANG_REPOSITORY_STRING
705 if(TARGET compiler-rt)
706 set(RUNTIME_DEP compiler-rt)
710 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
711 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
712 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
714 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
715 set(PGO_DEP llvm-profdata)
716 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
719 if(LLVM_BUILD_INSTRUMENTED)
720 set(PGO_DEP generate-profdata)
721 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
723 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
724 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
725 -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER})
726 set(RUNTIME_DEP) # Don't set runtime dependencies
729 # Find all variables that start with BOOTSTRAP_ and populate a variable with
731 get_cmake_property(variableNames VARIABLES)
732 foreach(variableName ${variableNames})
733 if(variableName MATCHES "^BOOTSTRAP_")
734 string(SUBSTRING ${variableName} 10 -1 varName)
735 string(REPLACE ";" "\;" value "${${variableName}}")
736 list(APPEND PASSTHROUGH_VARIABLES
737 -D${varName}=${value})
739 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
740 list(APPEND PASSTHROUGH_VARIABLES
741 -D${variableName}=${${variableName}})
745 # Populate the passthrough variables
746 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${BOOTSTRAP_DEFAULT_PASSTHROUGH})
748 string(REPLACE ";" "\;" value ${${variableName}})
749 list(APPEND PASSTHROUGH_VARIABLES
750 -D${variableName}=${value})
754 ExternalProject_Add(${NEXT_CLANG_STAGE}
755 DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP} ${PGO_DEP}
756 PREFIX ${NEXT_CLANG_STAGE}
757 SOURCE_DIR ${CMAKE_SOURCE_DIR}
758 STAMP_DIR ${STAMP_DIR}
759 BINARY_DIR ${BINARY_DIR}
762 # We shouldn't need to set this here, but INSTALL_DIR doesn't
763 # seem to work, so instead I'm passing this through
764 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
765 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
766 ${PASSTHROUGH_VARIABLES}
767 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
769 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
770 CMAKE_COMMAND ${cmake_command}
772 STEP_TARGETS configure build
773 USES_TERMINAL_CONFIGURE 1
774 USES_TERMINAL_BUILD 1
775 USES_TERMINAL_INSTALL 1
778 # exclude really-install from main target
779 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
780 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
781 COMMAND ${cmake_command} --build <BINARY_DIR> --target install
782 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
786 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
787 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
789 if(NOT CLANG_BOOTSTRAP_TARGETS)
790 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
792 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
793 # exclude from main target
794 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
796 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
797 COMMAND ${cmake_command} --build <BINARY_DIR> --target ${target}
798 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
803 if(target MATCHES "^stage[0-9]*")
804 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
807 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
811 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
812 add_subdirectory(utils/ClangVisualizers)