• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.4.3)
2
3# If we are not building as a part of LLVM, build Clang as an
4# standalone project, using LLVM as an external library:
5if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
6  project(Clang)
7
8  # Rely on llvm-config.
9  set(CONFIG_OUTPUT)
10  find_program(LLVM_CONFIG "llvm-config")
11  if(LLVM_CONFIG)
12    message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
13    set(CONFIG_COMMAND ${LLVM_CONFIG}
14      "--assertion-mode"
15      "--bindir"
16      "--libdir"
17      "--includedir"
18      "--prefix"
19      "--src-root")
20    execute_process(
21      COMMAND ${CONFIG_COMMAND}
22      RESULT_VARIABLE HAD_ERROR
23      OUTPUT_VARIABLE CONFIG_OUTPUT
24    )
25    if(NOT HAD_ERROR)
26      string(REGEX REPLACE
27        "[ \t]*[\r\n]+[ \t]*" ";"
28        CONFIG_OUTPUT ${CONFIG_OUTPUT})
29    else()
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}")
33    endif()
34  else()
35    message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
36  endif()
37
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)
44
45  if(NOT MSVC_IDE)
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)
50  endif()
51
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")
57
58  find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
59    NO_DEFAULT_PATH)
60
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})
66  else()
67    message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
68  endif()
69
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})
73  if(WIN32 OR CYGWIN)
74    # DLL platform -- put DLLs into bin.
75    set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
76  else()
77    set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
78  endif()
79
80  option(LLVM_INSTALL_TOOLCHAIN_ONLY
81    "Only include toolchain files in the 'install' target." OFF)
82
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)
86
87  include(AddLLVM)
88  include(TableGen)
89  include(HandleLLVMOptions)
90  include(VersionFromVCS)
91
92  set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
93
94  if (NOT DEFINED LLVM_INCLUDE_TESTS)
95    set(LLVM_INCLUDE_TESTS ON)
96  endif()
97
98  include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
99  link_directories("${LLVM_LIBRARY_DIR}")
100
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} )
104
105  if(LLVM_INCLUDE_TESTS)
106    set(Python_ADDITIONAL_VERSIONS 2.7)
107    include(FindPythonInterp)
108    if(NOT PYTHONINTERP_FOUND)
109      message(FATAL_ERROR
110"Unable to find Python interpreter, required for builds and testing.
111
112Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
113    endif()
114
115    if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
116      message(FATAL_ERROR "Python 2.7 or newer is required")
117    endif()
118
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)
124    endif()
125
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)
134      endif()
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)
140      endif()
141    else()
142      # Seek installed Lit.
143      find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
144        DOC "Path to lit.py")
145    endif()
146
147    if(LLVM_LIT)
148      # Define the default arguments to use with 'lit', and an option for the user
149      # to override.
150      set(LIT_ARGS_DEFAULT "-sv")
151      if (MSVC OR XCODE)
152        set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
153      endif()
154      set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
155
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")
159      endif()
160    else()
161      set(LLVM_INCLUDE_TESTS OFF)
162    endif()
163  endif()
164
165  set( CLANG_BUILT_STANDALONE 1 )
166  set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
167else()
168  set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
169endif()
170
171# Make sure that our source directory is on the current cmake module path so that
172# we can include cmake files from this directory.
173list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
174
175find_package(LibXml2 2.5.3 QUIET)
176if (LIBXML2_FOUND)
177  set(CLANG_HAVE_LIBXML 1)
178endif()
179
180set(CLANG_RESOURCE_DIR "" CACHE STRING
181  "Relative directory from the Clang binary to its resource files.")
182
183set(C_INCLUDE_DIRS "" CACHE STRING
184  "Colon separated list of directories clang will search for headers.")
185
186set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
187set(DEFAULT_SYSROOT "" CACHE PATH
188  "Default <path> to all compiler invocations for --sysroot=<path>." )
189
190set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
191
192set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
193    "enable x86 relax relocations by default")
194
195set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
196  "Default C++ stdlib to use (empty for architecture default, \"libstdc++\" or \"libc++\"")
197if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
198        CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
199        CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
200  message(WARNING "Resetting default C++ stdlib to use architecture default")
201  set(CLANG_DEFAULT_CXX_STDLIB "")
202endif()
203
204set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
205  "Default OpenMP runtime used by -fopenmp.")
206
207set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
208  "Vendor-specific text for showing with version information.")
209
210if( CLANG_VENDOR )
211  add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
212endif()
213
214set(CLANG_REPOSITORY_STRING "" CACHE STRING
215  "Vendor-specific text for showing the repository the source is taken from.")
216
217if(CLANG_REPOSITORY_STRING)
218  add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
219endif()
220
221set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
222  "Vendor-specific uti.")
223
224# The libdir suffix must exactly match whatever LLVM's configuration used.
225set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
226
227set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
228set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
229
230if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
231  message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
232"the makefiles distributed with LLVM. Please create a directory and run cmake "
233"from there, passing the path to this source directory as the last argument. "
234"This process created the file `CMakeCache.txt' and the directory "
235"`CMakeFiles'. Please delete them.")
236endif()
237
238if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
239  file(GLOB_RECURSE
240    tablegenned_files_on_include_dir
241    "${CLANG_SOURCE_DIR}/include/clang/*.inc")
242  if( tablegenned_files_on_include_dir )
243    message(FATAL_ERROR "Apparently there is a previous in-source build, "
244"probably as the result of running `configure' and `make' on "
245"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
246"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
247  endif()
248endif()
249
250# Compute the Clang version from the LLVM version.
251string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
252  ${PACKAGE_VERSION})
253message(STATUS "Clang version: ${CLANG_VERSION}")
254
255string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
256  ${CLANG_VERSION})
257string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
258  ${CLANG_VERSION})
259if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
260  set(CLANG_HAS_VERSION_PATCHLEVEL 1)
261  string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
262    ${CLANG_VERSION})
263else()
264  set(CLANG_HAS_VERSION_PATCHLEVEL 0)
265endif()
266
267# Configure the Version.inc file.
268configure_file(
269  ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
270  ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
271
272# Add appropriate flags for GCC
273if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
274  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
275  if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
276    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
277  endif ()
278
279  # Enable -pedantic for Clang even if it's not enabled for LLVM.
280  if (NOT LLVM_ENABLE_PEDANTIC)
281    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
282  endif ()
283
284  check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
285  if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
286    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
287  endif()
288endif ()
289
290# Determine HOST_LINK_VERSION on Darwin.
291set(HOST_LINK_VERSION)
292if (APPLE)
293  set(LD_V_OUTPUT)
294  execute_process(
295    COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
296    RESULT_VARIABLE HAD_ERROR
297    OUTPUT_VARIABLE LD_V_OUTPUT
298  )
299  if (NOT HAD_ERROR)
300    if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
301      string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
302    elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
303      string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
304    endif()
305  else()
306    message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
307  endif()
308endif()
309
310configure_file(
311  ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
312  ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
313
314include(CMakeParseArguments)
315include(AddClang)
316
317set(CMAKE_INCLUDE_CURRENT_DIR ON)
318
319include_directories(BEFORE
320  ${CMAKE_CURRENT_BINARY_DIR}/include
321  ${CMAKE_CURRENT_SOURCE_DIR}/include
322  )
323
324if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
325  install(DIRECTORY include/clang include/clang-c
326    DESTINATION include
327    FILES_MATCHING
328    PATTERN "*.def"
329    PATTERN "*.h"
330    PATTERN "config.h" EXCLUDE
331    PATTERN ".svn" EXCLUDE
332    )
333
334  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
335    DESTINATION include
336    FILES_MATCHING
337    PATTERN "CMakeFiles" EXCLUDE
338    PATTERN "*.inc"
339    PATTERN "*.h"
340    )
341endif()
342
343add_definitions( -D_GNU_SOURCE )
344
345option(CLANG_BUILD_TOOLS
346  "Build the Clang tools. If OFF, just generate build targets." ON)
347
348option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
349if (CLANG_ENABLE_ARCMT)
350  set(ENABLE_CLANG_ARCMT "1")
351else()
352  set(ENABLE_CLANG_ARCMT "0")
353endif()
354
355option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
356if (CLANG_ENABLE_STATIC_ANALYZER)
357  set(ENABLE_CLANG_STATIC_ANALYZER "1")
358else()
359  set(ENABLE_CLANG_STATIC_ANALYZER "0")
360endif()
361
362if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
363  message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
364endif()
365
366if(CLANG_ENABLE_ARCMT)
367  add_definitions(-DCLANG_ENABLE_ARCMT)
368  add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
369endif()
370if(CLANG_ENABLE_STATIC_ANALYZER)
371  add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
372endif()
373
374# Clang version information
375set(CLANG_EXECUTABLE_VERSION
376     "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
377    "Version number that will be placed into the clang executable, in the form XX.YY")
378set(LIBCLANG_LIBRARY_VERSION
379     "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
380    "Version number that will be placed into the libclang library , in the form XX.YY")
381mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
382
383option(CLANG_INCLUDE_TESTS
384       "Generate build targets for the Clang unit tests."
385       ${LLVM_INCLUDE_TESTS})
386
387add_subdirectory(utils/TableGen)
388
389add_subdirectory(include)
390
391# All targets below may depend on all tablegen'd files.
392get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
393list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
394
395add_subdirectory(lib)
396add_subdirectory(tools)
397add_subdirectory(runtime)
398
399option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
400if (CLANG_BUILD_EXAMPLES)
401  set(ENABLE_CLANG_EXAMPLES "1")
402else()
403  set(ENABLE_CLANG_EXAMPLES "0")
404endif()
405add_subdirectory(examples)
406
407if( CLANG_INCLUDE_TESTS )
408  if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
409    add_subdirectory(unittests)
410    list(APPEND CLANG_TEST_DEPS ClangUnitTests)
411    list(APPEND CLANG_TEST_PARAMS
412      clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
413      )
414  endif()
415  add_subdirectory(test)
416
417  if(CLANG_BUILT_STANDALONE)
418    # Add a global check rule now that all subdirectories have been traversed
419    # and we know the total set of lit testsuites.
420    get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
421    get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
422    get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
423    get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
424    add_lit_target(check-all
425      "Running all regression tests"
426      ${LLVM_LIT_TESTSUITES}
427      PARAMS ${LLVM_LIT_PARAMS}
428      DEPENDS ${LLVM_LIT_DEPENDS}
429      ARGS ${LLVM_LIT_EXTRA_ARGS}
430      )
431  endif()
432  add_subdirectory(utils/perf-training)
433endif()
434
435option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
436  ${LLVM_INCLUDE_DOCS})
437if( CLANG_INCLUDE_DOCS )
438  add_subdirectory(docs)
439endif()
440
441
442if(APPLE)
443  # this line is needed as a cleanup to ensure that any CMakeCaches with the old
444  # default value get updated to the new default.
445  if(CLANG_ORDER_FILE STREQUAL "")
446    unset(CLANG_ORDER_FILE CACHE)
447    unset(CLANG_ORDER_FILE)
448  endif()
449
450
451  set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
452    "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
453
454  if(CLANG_ORDER_FILE AND NOT EXISTS ${CLANG_ORDER_FILE})
455    string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
456    if(PATH_START EQUAL 0)
457      file(WRITE ${CLANG_ORDER_FILE} "\n")
458    else()
459      message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
460    endif()
461  endif()
462endif()
463
464add_subdirectory(cmake/modules)
465
466if (CLANG_ENABLE_BOOTSTRAP)
467  include(ExternalProject)
468
469  if(NOT CLANG_STAGE)
470    set(CLANG_STAGE stage1)
471    message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
472  endif()
473
474  string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
475  if(MATCHED_STAGE)
476    if(NOT LLVM_BUILD_INSTRUMENTED)
477      math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
478      set(NEXT_CLANG_STAGE stage${STAGE_NUM})
479    else()
480      set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
481    endif()
482  else()
483    set(NEXT_CLANG_STAGE bootstrap)
484  endif()
485
486  if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
487    set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
488  endif()
489  message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
490
491
492  set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
493  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
494  set(cmake_command ${CMAKE_COMMAND})
495
496  # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
497  if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
498    set(LTO_DEP LTO)
499    if(APPLE)
500      # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
501      # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
502      # so that the host object file tools will use the just-built libLTO.
503      set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
504      set(cmake_command ${CMAKE_COMMAND} -E env DYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR} ${CMAKE_COMMAND})
505    elseif(NOT WIN32)
506      list(APPEND LTO_DEP LLVMgold llvm-ar llvm-ranlib)
507      set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
508      set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
509    endif()
510  endif()
511
512  add_custom_target(${NEXT_CLANG_STAGE}-clear
513    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
514    )
515  add_custom_command(
516    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
517    DEPENDS clang ${LTO_DEP}
518    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
519    COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
520    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
521    COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
522    COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
523    )
524
525  if(CMAKE_VERBOSE_MAKEFILE)
526    set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
527  endif()
528
529  set(BOOTSTRAP_DEFAULT_PASSTHROUGH
530    PACKAGE_VERSION
531    LLVM_VERSION_MAJOR
532    LLVM_VERSION_MINOR
533    LLVM_VERSION_PATCH
534    LLVM_VERSION_SUFFIX
535    LLVM_BINUTILS_INCDIR
536    CLANG_REPOSITORY_STRING
537    CMAKE_MAKE_PROGRAM)
538
539  if(TARGET compiler-rt)
540    set(RUNTIME_DEP compiler-rt)
541  endif()
542
543  set(COMPILER_OPTIONS
544    -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
545    -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
546    -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
547
548  if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
549    set(PGO_DEP llvm-profdata)
550    set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
551  endif()
552
553  if(LLVM_BUILD_INSTRUMENTED)
554    set(PGO_DEP generate-profdata)
555    set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
556    set(COMPILER_OPTIONS
557      -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
558      -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
559      -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER})
560    set(RUNTIME_DEP) # Don't set runtime dependencies
561  endif()
562
563  # Find all variables that start with BOOTSTRAP_ and populate a variable with
564  # them.
565  get_cmake_property(variableNames VARIABLES)
566  foreach(variableName ${variableNames})
567    if(variableName MATCHES "^BOOTSTRAP_")
568      string(SUBSTRING ${variableName} 10 -1 varName)
569      string(REPLACE ";" "\;" value "${${variableName}}")
570      list(APPEND PASSTHROUGH_VARIABLES
571        -D${varName}=${value})
572    endif()
573    if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
574      list(APPEND PASSTHROUGH_VARIABLES
575        -D${variableName}=${${variableName}})
576    endif()
577  endforeach()
578
579  # Populate the passthrough variables
580  foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${BOOTSTRAP_DEFAULT_PASSTHROUGH})
581    if(${variableName})
582      string(REPLACE ";" "\;" value ${${variableName}})
583      list(APPEND PASSTHROUGH_VARIABLES
584        -D${variableName}=${value})
585    endif()
586  endforeach()
587
588  ExternalProject_Add(${NEXT_CLANG_STAGE}
589    DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP} ${PGO_DEP}
590    PREFIX ${NEXT_CLANG_STAGE}
591    SOURCE_DIR ${CMAKE_SOURCE_DIR}
592    STAMP_DIR ${STAMP_DIR}
593    BINARY_DIR ${BINARY_DIR}
594    EXCLUDE_FROM_ALL 1
595    CMAKE_ARGS
596                # We shouldn't need to set this here, but INSTALL_DIR doesn't
597                # seem to work, so instead I'm passing this through
598                -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
599                ${CLANG_BOOTSTRAP_CMAKE_ARGS}
600                ${PASSTHROUGH_VARIABLES}
601                 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
602                ${COMPILER_OPTIONS}
603                ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
604    CMAKE_COMMAND ${cmake_command}
605    INSTALL_COMMAND ""
606    STEP_TARGETS configure build
607    USES_TERMINAL_CONFIGURE 1
608    USES_TERMINAL_BUILD 1
609    USES_TERMINAL_INSTALL 1
610    )
611
612  # exclude really-install from main target
613  set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
614  ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
615    COMMAND ${cmake_command} --build <BINARY_DIR> --target install
616    COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
617    DEPENDEES build
618    USES_TERMINAL 1
619  )
620  ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
621  add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
622
623  if(NOT CLANG_BOOTSTRAP_TARGETS)
624    set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
625  endif()
626  foreach(target ${CLANG_BOOTSTRAP_TARGETS})
627    # exclude from main target
628    set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
629
630    ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
631      COMMAND ${cmake_command} --build <BINARY_DIR> --target ${target}
632      COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
633      DEPENDEES configure
634      USES_TERMINAL 1
635    )
636
637    if(target MATCHES "^stage[0-9]*")
638      add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
639    endif()
640
641    ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
642  endforeach()
643endif()
644
645if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
646  add_subdirectory(utils/ClangVisualizers)
647endif()
648