1cmake_minimum_required(VERSION 3.13.4)
2
3if(POLICY CMP0075)
4  cmake_policy(SET CMP0075 NEW)
5endif()
6
7# If we are not building as a part of LLVM, build Clang as an
8# standalone project, using LLVM as an external library:
9if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
10  project(Clang)
11
12  set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard to conform to")
13  set(CMAKE_CXX_STANDARD_REQUIRED YES)
14  set(CMAKE_CXX_EXTENSIONS NO)
15
16  # Rely on llvm-config.
17  set(CONFIG_OUTPUT)
18  if(LLVM_CONFIG)
19    set (LLVM_CONFIG_FOUND 1)
20    message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
21    message(DEPRECATION "Using llvm-config to detect the LLVM installation is \
22          deprecated.  The installed cmake files should be used \
23          instead.  CMake should be able to detect your LLVM install \
24          automatically, but you can also use LLVM_DIR to specify \
25          the path containing LLVMConfig.cmake.")
26    set(CONFIG_COMMAND ${LLVM_CONFIG}
27      "--assertion-mode"
28      "--bindir"
29      "--libdir"
30      "--includedir"
31      "--prefix"
32      "--src-root"
33      "--cmakedir")
34    execute_process(
35      COMMAND ${CONFIG_COMMAND}
36      RESULT_VARIABLE HAD_ERROR
37      OUTPUT_VARIABLE CONFIG_OUTPUT
38    )
39    if(NOT HAD_ERROR)
40      string(REGEX REPLACE
41        "[ \t]*[\r\n]+[ \t]*" ";"
42        CONFIG_OUTPUT ${CONFIG_OUTPUT})
43    else()
44      string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
45      message(STATUS "${CONFIG_COMMAND_STR}")
46      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
47    endif()
48
49    list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
50    list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
51    list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
52    list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
53    list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
54    list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
55    list(GET CONFIG_OUTPUT 6 LLVM_CONFIG_CMAKE_PATH)
56
57    # Normalize LLVM_CMAKE_PATH. --cmakedir might contain backslashes.
58    # CMake assumes slashes as PATH.
59    file(TO_CMAKE_PATH ${LLVM_CONFIG_CMAKE_PATH} LLVM_CMAKE_PATH)
60  endif()
61
62
63  if(NOT MSVC_IDE)
64    set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
65      CACHE BOOL "Enable assertions")
66    # Assertions should follow llvm-config's.
67    mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
68  endif()
69
70  find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_PATH}")
71  list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
72
73  # We can't check LLVM_CONFIG here, because find_package(LLVM ...) also sets
74  # LLVM_CONFIG.
75  if (NOT LLVM_CONFIG_FOUND)
76    # Pull values from LLVMConfig.cmake.  We can drop this once the llvm-config
77    # path is removed.
78    set(TOOLS_BINARY_DIR ${LLVM_TOOLS_BINARY_DIR})
79    set(LIBRARY_DIR ${LLVM_LIBRARY_DIR})
80    set(INCLUDE_DIR ${LLVM_INCLUDE_DIR})
81    set(LLVM_OBJ_DIR ${LLVM_BINARY_DIR})
82    # The LLVM_CMAKE_PATH variable is set when doing non-standalone builds and
83    # used in this project, so we need to make sure we set this value.
84    # FIXME: LLVM_CMAKE_DIR comes from LLVMConfig.cmake.  We should rename
85    # LLVM_CMAKE_PATH to LLVM_CMAKE_DIR throughout the project.
86    set(LLVM_CMAKE_PATH ${LLVM_CMAKE_DIR})
87  endif()
88
89  set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
90  set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
91  set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
92  set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
93  set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
94
95  find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
96    NO_DEFAULT_PATH)
97
98  # They are used as destination of target generators.
99  set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
100  set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
101  if(WIN32 OR CYGWIN)
102    # DLL platform -- put DLLs into bin.
103    set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
104  else()
105    set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
106  endif()
107
108  option(LLVM_INSTALL_TOOLCHAIN_ONLY
109    "Only include toolchain files in the 'install' target." OFF)
110
111  option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
112    "Set to ON to force using an old, unsupported host toolchain." OFF)
113  option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
114  option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
115
116  include(AddLLVM)
117  include(TableGen)
118  include(HandleLLVMOptions)
119  include(VersionFromVCS)
120  include(LLVMDistributionSupport)
121
122  set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
123  set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
124    "Default URL where bug reports are to be submitted.")
125
126  if (NOT DEFINED LLVM_INCLUDE_TESTS)
127    set(LLVM_INCLUDE_TESTS ON)
128  endif()
129
130  include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
131  link_directories("${LLVM_LIBRARY_DIR}")
132
133  set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
134  set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
135  set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
136
137  if(LLVM_INCLUDE_TESTS)
138    find_package(Python3 COMPONENTS Interpreter)
139    if(NOT Python3_Interpreter_FOUND)
140      message(WARNING "Python3 not found, using python2 as a fallback")
141      find_package(Python2 COMPONENTS Interpreter REQUIRED)
142      if(Python2_VERSION VERSION_LESS 2.7)
143        message(SEND_ERROR "Python 2.7 or newer is required")
144      endif()
145
146      # Treat python2 as python3
147      add_executable(Python3::Interpreter IMPORTED)
148      set_target_properties(Python3::Interpreter PROPERTIES
149        IMPORTED_LOCATION ${Python2_EXECUTABLE})
150      set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
151    endif()
152
153    # Check prebuilt llvm/utils.
154    if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
155        AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
156        AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
157      set(LLVM_UTILS_PROVIDED ON)
158    endif()
159
160    if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
161      # Note: path not really used, except for checking if lit was found
162      set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
163      if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
164        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
165      endif()
166      if(NOT LLVM_UTILS_PROVIDED)
167        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
168        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
169        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
170        set(LLVM_UTILS_PROVIDED ON)
171        set(CLANG_TEST_DEPS FileCheck count not)
172      endif()
173      set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
174      if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
175          AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
176          AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
177        add_subdirectory(${UNITTEST_DIR} utils/unittest)
178      endif()
179    else()
180      # Seek installed Lit.
181      find_program(LLVM_LIT
182                   NAMES llvm-lit lit.py lit
183                   PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
184                   DOC "Path to lit.py")
185    endif()
186
187    if(LLVM_LIT)
188      # Define the default arguments to use with 'lit', and an option for the user
189      # to override.
190      set(LIT_ARGS_DEFAULT "-sv")
191      if (MSVC OR XCODE)
192        set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
193      endif()
194      set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
195
196      # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
197      if( WIN32 AND NOT CYGWIN )
198        set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
199      endif()
200    else()
201      set(LLVM_INCLUDE_TESTS OFF)
202    endif()
203  endif()
204
205  set( CLANG_BUILT_STANDALONE 1 )
206  set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
207else()
208  set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
209endif()
210
211# Make sure that our source directory is on the current cmake module path so that
212# we can include cmake files from this directory.
213list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
214
215if(LLVM_ENABLE_LIBXML2)
216  # Don't look for libxml if we're using MSan, since uninstrumented third party
217  # code may call MSan interceptors like strlen, leading to false positives.
218  if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
219    set (LIBXML2_FOUND 0)
220    find_package(LibXml2 2.5.3 QUIET)
221    if (LIBXML2_FOUND)
222      set(CLANG_HAVE_LIBXML 1)
223    endif()
224  endif()
225endif()
226
227include(CheckIncludeFile)
228check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
229
230set(CLANG_RESOURCE_DIR "" CACHE STRING
231  "Relative directory from the Clang binary to its resource files.")
232
233set(C_INCLUDE_DIRS "" CACHE STRING
234  "Colon separated list of directories clang will search for headers.")
235
236set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
237set(DEFAULT_SYSROOT "" CACHE STRING
238  "Default <path> to all compiler invocations for --sysroot=<path>." )
239
240set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
241
242set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
243    "enable x86 relax relocations by default")
244
245set(CLANG_SPAWN_CC1 OFF CACHE BOOL
246    "Whether clang should use a new process for the CC1 invocation")
247
248# TODO: verify the values against LangStandards.def?
249set(CLANG_DEFAULT_STD_C "" CACHE STRING
250  "Default standard to use for C/ObjC code (IDENT from LangStandards.def, empty for platform default)")
251set(CLANG_DEFAULT_STD_CXX "" CACHE STRING
252  "Default standard to use for C++/ObjC++ code (IDENT from LangStandards.def, empty for platform default)")
253
254set(CLANG_DEFAULT_LINKER "" CACHE STRING
255  "Default linker to use (linker name or absolute path, empty for platform default)")
256
257set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
258  "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
259if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
260        CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
261        CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
262  message(WARNING "Resetting default C++ stdlib to use platform default")
263  set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
264    "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
265endif()
266
267set(CLANG_DEFAULT_RTLIB "" CACHE STRING
268  "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
269if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
270        CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
271        CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
272  message(WARNING "Resetting default rtlib to use platform default")
273  set(CLANG_DEFAULT_RTLIB "" CACHE STRING
274    "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
275endif()
276
277set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
278  "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
279if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
280  if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
281    set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
282  elseif (CLANG_DEFAULT_RTLIBS STREQUAL "libunwind")
283    set (CLANG_DEFAULT_UNWINDLIB "none" CACHE STRING "" FORCE)
284  endif()
285endif()
286
287if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
288        CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
289        CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
290        CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
291  message(WARNING "Resetting default unwindlib to use platform default")
292  set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
293    "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty for none)" FORCE)
294endif()
295
296set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
297  "Default objcopy executable to use.")
298
299set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
300  "Default OpenMP runtime used by -fopenmp.")
301
302# OpenMP offloading requires at least sm_35 because we use shuffle instructions
303# to generate efficient code for reductions and the atomicMax instruction on
304# 64-bit integers in the implementation of conditional lastprivate.
305set(CUDA_ARCH_FLAGS "sm_35")
306
307# Try to find the highest Nvidia GPU architecture the system supports
308if (NOT DEFINED CLANG_OPENMP_NVPTX_DEFAULT_ARCH)
309  find_package(CUDA QUIET)
310  if (CUDA_FOUND)
311    cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS)
312  endif()
313else()
314  set(CUDA_ARCH_FLAGS ${CLANG_OPENMP_NVPTX_DEFAULT_ARCH})
315endif()
316
317string(REGEX MATCH "sm_([0-9]+)" CUDA_ARCH_MATCH ${CUDA_ARCH_FLAGS})
318if (NOT DEFINED CUDA_ARCH_MATCH OR "${CMAKE_MATCH_1}" LESS 35)
319  set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH "sm_35" CACHE STRING
320    "Default architecture for OpenMP offloading to Nvidia GPUs." FORCE)
321  message(WARNING "Resetting default architecture for OpenMP offloading to Nvidia GPUs to sm_35")
322else()
323  set(CLANG_OPENMP_NVPTX_DEFAULT_ARCH ${CUDA_ARCH_MATCH} CACHE STRING
324    "Default architecture for OpenMP offloading to Nvidia GPUs.")
325endif()
326
327set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
328
329set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
330  "Vendor-specific text for showing with version information.")
331
332set(CLANG_REPOSITORY_STRING "" CACHE STRING
333  "Vendor-specific text for showing the repository the source is taken from.")
334
335if(CLANG_REPOSITORY_STRING)
336  add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
337endif()
338
339set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
340  "Vendor-specific uti.")
341
342set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
343    "Python versions to install libclang python bindings for")
344
345set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
346    "Link tools against libclang-cpp.so")
347
348if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
349  message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
350                      "LLVM_LINK_LLVM_DYLIB=OFF")
351endif()
352
353# The libdir suffix must exactly match whatever LLVM's configuration used.
354set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
355
356set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
357set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
358
359if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
360  message(FATAL_ERROR "In-source builds are not allowed. "
361"Please create a directory and run cmake "
362"from there, passing the path to this source directory as the last argument. "
363"This process created the file `CMakeCache.txt' and the directory "
364"`CMakeFiles'. Please delete them.")
365endif()
366
367# If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
368if(NOT DEFINED CLANG_VERSION_MAJOR)
369  set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
370endif()
371if(NOT DEFINED CLANG_VERSION_MINOR)
372  set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
373endif()
374if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
375  set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
376endif()
377# Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
378set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
379message(STATUS "Clang version: ${CLANG_VERSION}")
380
381# Configure the Version.inc file.
382configure_file(
383  ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
384  ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
385
386# Add appropriate flags for GCC
387if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
388  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
389  if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
390    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
391  endif ()
392
393  # Enable -pedantic for Clang even if it's not enabled for LLVM.
394  if (NOT LLVM_ENABLE_PEDANTIC)
395    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
396  endif ()
397
398  check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
399  if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
400    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
401  endif()
402endif ()
403
404# Determine HOST_LINK_VERSION on Darwin.
405set(HOST_LINK_VERSION)
406if (APPLE)
407  set(LD_V_OUTPUT)
408  execute_process(
409    COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
410    RESULT_VARIABLE HAD_ERROR
411    OUTPUT_VARIABLE LD_V_OUTPUT
412  )
413  if (HAD_ERROR)
414    message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
415  endif()
416  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
417    string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
418  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
419    string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
420  endif()
421  message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
422endif()
423
424include(CMakeParseArguments)
425include(AddClang)
426
427set(CMAKE_INCLUDE_CURRENT_DIR ON)
428
429include_directories(BEFORE
430  ${CMAKE_CURRENT_BINARY_DIR}/include
431  ${CMAKE_CURRENT_SOURCE_DIR}/include
432  )
433
434if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
435  install(DIRECTORY include/clang include/clang-c
436    DESTINATION include
437    COMPONENT clang-headers
438    FILES_MATCHING
439    PATTERN "*.def"
440    PATTERN "*.h"
441    PATTERN "config.h" EXCLUDE
442    )
443
444  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
445    DESTINATION include
446    COMPONENT clang-headers
447    FILES_MATCHING
448    PATTERN "CMakeFiles" EXCLUDE
449    PATTERN "*.inc"
450    PATTERN "*.h"
451    )
452
453  # Installing the headers needs to depend on generating any public
454  # tablegen'd headers.
455  add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
456  set_target_properties(clang-headers PROPERTIES FOLDER "Misc")
457  if(NOT LLVM_ENABLE_IDE)
458    add_llvm_install_targets(install-clang-headers
459                             DEPENDS clang-headers
460                             COMPONENT clang-headers)
461  endif()
462
463  add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
464  install(PROGRAMS utils/bash-autocomplete.sh
465          DESTINATION share/clang
466          COMPONENT bash-autocomplete)
467  if(NOT LLVM_ENABLE_IDE)
468    add_llvm_install_targets(install-bash-autocomplete
469                             DEPENDS bash-autocomplete
470                             COMPONENT bash-autocomplete)
471  endif()
472endif()
473
474add_definitions( -D_GNU_SOURCE )
475
476option(CLANG_BUILD_TOOLS
477  "Build the Clang tools. If OFF, just generate build targets." ON)
478
479option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
480option(CLANG_ENABLE_STATIC_ANALYZER
481  "Include static analyzer in clang binary." ON)
482
483option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
484
485if(NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
486  message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
487endif()
488
489if(CLANG_ENABLE_ARCMT)
490  set(CLANG_ENABLE_OBJC_REWRITER ON)
491endif()
492
493# Clang version information
494set(CLANG_EXECUTABLE_VERSION
495    "${CLANG_VERSION_MAJOR}" CACHE STRING
496    "Major version number that will be appended to the clang executable name")
497set(LIBCLANG_LIBRARY_VERSION
498    "${CLANG_VERSION_MAJOR}" CACHE STRING
499    "Major version number that will be appended to the libclang library")
500mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
501
502option(CLANG_INCLUDE_TESTS
503       "Generate build targets for the Clang unit tests."
504       ${LLVM_INCLUDE_TESTS})
505
506add_subdirectory(utils/TableGen)
507
508add_subdirectory(include)
509
510# All targets below may depend on all tablegen'd files.
511get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
512add_custom_target(clang-tablegen-targets
513  DEPENDS
514  omp_gen
515  ${CLANG_TABLEGEN_TARGETS})
516set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Misc")
517list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
518
519# Force target to be built as soon as possible. Clang modules builds depend
520# header-wise on it as they ship all headers from the umbrella folders. Building
521# an entire module might include header, which depends on intrinsics_gen.
522if(LLVM_ENABLE_MODULES)
523  list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
524endif()
525
526add_subdirectory(lib)
527add_subdirectory(tools)
528add_subdirectory(runtime)
529
530option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
531add_subdirectory(examples)
532
533if(APPLE)
534  # this line is needed as a cleanup to ensure that any CMakeCaches with the old
535  # default value get updated to the new default.
536  if(CLANG_ORDER_FILE STREQUAL "")
537    unset(CLANG_ORDER_FILE CACHE)
538    unset(CLANG_ORDER_FILE)
539  endif()
540
541
542  set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
543    "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
544
545  if(NOT EXISTS ${CLANG_ORDER_FILE})
546    string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
547    if(PATH_START EQUAL 0)
548      file(WRITE ${CLANG_ORDER_FILE} "\n")
549    else()
550      message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
551    endif()
552  endif()
553endif()
554
555
556if( CLANG_INCLUDE_TESTS )
557  if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
558    add_subdirectory(unittests)
559    list(APPEND CLANG_TEST_DEPS ClangUnitTests)
560    list(APPEND CLANG_TEST_PARAMS
561      clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
562      )
563  endif()
564  add_subdirectory(test)
565  add_subdirectory(bindings/python/tests)
566
567  if(CLANG_BUILT_STANDALONE)
568    # Add a global check rule now that all subdirectories have been traversed
569    # and we know the total set of lit testsuites.
570    get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
571    get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
572    get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
573    get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
574    get_property(LLVM_ADDITIONAL_TEST_TARGETS
575                 GLOBAL PROPERTY LLVM_ADDITIONAL_TEST_TARGETS)
576    add_lit_target(check-all
577      "Running all regression tests"
578      ${LLVM_LIT_TESTSUITES}
579      PARAMS ${LLVM_LIT_PARAMS}
580      DEPENDS ${LLVM_LIT_DEPENDS} ${LLVM_ADDITIONAL_TEST_TARGETS}
581      ARGS ${LLVM_LIT_EXTRA_ARGS}
582      )
583  endif()
584  add_subdirectory(utils/perf-training)
585endif()
586
587option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
588  ${LLVM_INCLUDE_DOCS})
589if( CLANG_INCLUDE_DOCS )
590  add_subdirectory(docs)
591endif()
592
593# Custom target to install all clang libraries.
594add_custom_target(clang-libraries)
595set_target_properties(clang-libraries PROPERTIES FOLDER "Misc")
596
597if(NOT LLVM_ENABLE_IDE)
598  add_llvm_install_targets(install-clang-libraries
599                           DEPENDS clang-libraries
600                           COMPONENT clang-libraries)
601endif()
602
603get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
604if(CLANG_LIBS)
605  list(REMOVE_DUPLICATES CLANG_LIBS)
606  foreach(lib ${CLANG_LIBS})
607    add_dependencies(clang-libraries ${lib})
608    if(NOT LLVM_ENABLE_IDE)
609      add_dependencies(install-clang-libraries install-${lib})
610      add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
611    endif()
612  endforeach()
613endif()
614
615add_subdirectory(cmake/modules)
616
617if(CLANG_STAGE)
618  message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
619endif()
620
621if (CLANG_ENABLE_BOOTSTRAP)
622  include(ExternalProject)
623
624  add_custom_target(clang-bootstrap-deps DEPENDS clang)
625
626  if(NOT CLANG_STAGE)
627    set(CLANG_STAGE stage1)
628  endif()
629
630  string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
631  if(MATCHED_STAGE)
632    if(NOT LLVM_BUILD_INSTRUMENTED)
633      math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
634      set(NEXT_CLANG_STAGE stage${STAGE_NUM})
635    else()
636      set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
637    endif()
638  else()
639    set(NEXT_CLANG_STAGE bootstrap)
640  endif()
641
642  if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
643    set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
644  endif()
645  message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
646
647
648  set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
649  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
650
651  if(BOOTSTRAP_LLVM_ENABLE_LLD)
652    add_dependencies(clang-bootstrap-deps lld)
653  endif()
654
655  # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
656  if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
657    if(APPLE)
658      add_dependencies(clang-bootstrap-deps LTO)
659      # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
660      # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
661      # so that the host object file tools will use the just-built libLTO.
662      # However if System Integrity Protection is enabled the DYLD variables
663      # will be scrubbed from the environment of any base system commands. This
664      # includes /bin/sh, which ninja uses when executing build commands. To
665      # work around the envar being filtered away we pass it in as a CMake
666      # variable, and have LLVM's CMake append the envar to the archiver calls.
667      set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
668        -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
669    elseif(NOT WIN32)
670      add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
671      if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
672        add_dependencies(clang-bootstrap-deps LLVMgold)
673      endif()
674      set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
675      set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
676    endif()
677  endif()
678
679  if(CLANG_BOOTSTRAP_EXTRA_DEPS)
680    add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
681  endif()
682
683  add_custom_target(${NEXT_CLANG_STAGE}-clear
684    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
685    )
686  add_custom_command(
687    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
688    DEPENDS clang-bootstrap-deps
689    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
690    COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
691    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
692    COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
693    COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
694    )
695
696  if(CMAKE_VERBOSE_MAKEFILE)
697    set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
698  endif()
699
700  set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
701    PACKAGE_VERSION
702    PACKAGE_VENDOR
703    LLVM_VERSION_MAJOR
704    LLVM_VERSION_MINOR
705    LLVM_VERSION_PATCH
706    CLANG_VERSION_MAJOR
707    CLANG_VERSION_MINOR
708    CLANG_VERSION_PATCHLEVEL
709    CLANG_VENDOR
710    LLVM_VERSION_SUFFIX
711    LLVM_BINUTILS_INCDIR
712    CLANG_REPOSITORY_STRING
713    CMAKE_C_COMPILER_LAUNCHER
714    CMAKE_CXX_COMPILER_LAUNCHER
715    CMAKE_MAKE_PROGRAM
716    CMAKE_OSX_ARCHITECTURES
717    LLVM_ENABLE_PROJECTS
718    LLVM_ENABLE_RUNTIMES)
719
720  # We don't need to depend on compiler-rt/libcxx if we're building instrumented
721  # because the next stage will use the same compiler used to build this stage.
722  if(NOT LLVM_BUILD_INSTRUMENTED)
723    if(TARGET compiler-rt)
724      add_dependencies(clang-bootstrap-deps compiler-rt)
725    endif()
726    if(TARGET cxx-headers)
727      add_dependencies(clang-bootstrap-deps cxx-headers)
728    endif()
729  endif()
730
731  set(C_COMPILER "clang")
732  set(CXX_COMPILER "clang++")
733  if(WIN32)
734    set(C_COMPILER "clang-cl.exe")
735    set(CXX_COMPILER "clang-cl.exe")
736  endif()
737
738  set(COMPILER_OPTIONS
739    -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
740    -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
741    -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
742    -DCMAKE_ASM_COMPILER_ID=Clang)
743
744  # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
745  # otherwise it defaults the linker to be link.exe.
746  if(BOOTSTRAP_LLVM_ENABLE_LLD)
747    if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
748      set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
749    endif()
750  endif()
751
752  if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
753    set(${CLANG_STAGE}_CONFIG -DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config)
754    set(${CLANG_STAGE}_TABLEGEN
755      -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
756      -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
757    if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
758      if(BOOTSTRAP_LLVM_ENABLE_LLD)
759        set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
760      endif()
761      if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
762        add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
763        set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
764        set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
765      endif()
766      add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
767      set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
768      set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
769    endif()
770  endif()
771
772  if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
773    add_dependencies(clang-bootstrap-deps llvm-profdata)
774    set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
775  endif()
776
777  if(LLVM_BUILD_INSTRUMENTED)
778    add_dependencies(clang-bootstrap-deps generate-profdata)
779    set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
780    # Use the current tools for LTO instead of the instrumented ones
781    list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
782      CMAKE_CXX_COMPILER
783      CMAKE_C_COMPILER
784      CMAKE_ASM_COMPILER
785      CMAKE_AR
786      CMAKE_RANLIB
787      DARWIN_LTO_LIBRARY
788      DYLD_LIBRARY_PATH)
789
790    set(COMPILER_OPTIONS)
791    set(LTO_LIBRARY)
792    set(LTO_AR)
793    set(LTO_RANLIB)
794  endif()
795
796  # Find all variables that start with BOOTSTRAP_ and populate a variable with
797  # them.
798  get_cmake_property(variableNames VARIABLES)
799  foreach(variableName ${variableNames})
800    if(variableName MATCHES "^BOOTSTRAP_")
801      string(SUBSTRING ${variableName} 10 -1 varName)
802      string(REPLACE ";" "|" value "${${variableName}}")
803      list(APPEND PASSTHROUGH_VARIABLES
804        -D${varName}=${value})
805    endif()
806    if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
807      list(APPEND PASSTHROUGH_VARIABLES
808        -D${variableName}=${${variableName}})
809    endif()
810  endforeach()
811
812  # Populate the passthrough variables
813  foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
814    if(DEFINED ${variableName})
815      if("${${variableName}}" STREQUAL "")
816        set(value "")
817      else()
818        string(REPLACE ";" "|" value "${${variableName}}")
819      endif()
820      list(APPEND PASSTHROUGH_VARIABLES
821        -D${variableName}=${value})
822    endif()
823  endforeach()
824
825  ExternalProject_Add(${NEXT_CLANG_STAGE}
826    DEPENDS clang-bootstrap-deps
827    PREFIX ${NEXT_CLANG_STAGE}
828    SOURCE_DIR ${CMAKE_SOURCE_DIR}
829    STAMP_DIR ${STAMP_DIR}
830    BINARY_DIR ${BINARY_DIR}
831    EXCLUDE_FROM_ALL 1
832    CMAKE_ARGS
833                # We shouldn't need to set this here, but INSTALL_DIR doesn't
834                # seem to work, so instead I'm passing this through
835                -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
836                ${PASSTHROUGH_VARIABLES}
837                ${CLANG_BOOTSTRAP_CMAKE_ARGS}
838                 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
839                ${COMPILER_OPTIONS}
840                ${${CLANG_STAGE}_CONFIG}
841                ${${CLANG_STAGE}_TABLEGEN}
842                ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
843                ${${CLANG_STAGE}_LINKER}
844                ${${CLANG_STAGE}_AR}
845                ${${CLANG_STAGE}_RANLIB}
846                ${${CLANG_STAGE}_OBJCOPY}
847                ${${CLANG_STAGE}_STRIP}
848    INSTALL_COMMAND ""
849    STEP_TARGETS configure build
850    USES_TERMINAL_CONFIGURE 1
851    USES_TERMINAL_BUILD 1
852    USES_TERMINAL_INSTALL 1
853    LIST_SEPARATOR |
854    )
855
856  # exclude really-install from main target
857  set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
858  ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
859    COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
860    COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
861    DEPENDEES build
862    USES_TERMINAL 1
863  )
864  ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
865  add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
866
867  if(NOT CLANG_BOOTSTRAP_TARGETS)
868    set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
869  endif()
870  foreach(target ${CLANG_BOOTSTRAP_TARGETS})
871    # exclude from main target
872    set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
873
874    ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
875      COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
876      COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
877      DEPENDEES configure
878      USES_TERMINAL 1
879    )
880
881    if(target MATCHES "^stage[0-9]*")
882      add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
883    endif()
884
885    ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
886  endforeach()
887endif()
888
889if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
890  add_subdirectory(utils/ClangVisualizers)
891endif()
892add_subdirectory(utils/hmaptool)
893
894if(CLANG_BUILT_STANDALONE)
895  llvm_distribution_add_targets()
896  process_llvm_pass_plugins()
897endif()
898
899configure_file(
900  ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
901  ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
902