blob: aee0d0a2426cbc6fcff902b9f58d70a2a4e531c2 [file] [log] [blame]
Chris Bienemanf8ebfc42016-05-31 20:21:38 +00001cmake_minimum_required(VERSION 3.4.3)
Mike Spertuse9f15b42016-03-28 18:24:22 +00002
Mike Spertuse9f15b42016-03-28 18:24:22 +00003# 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
Michael Gottesmanca589cc2016-07-09 21:58:40 +0000171# 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
Mike Spertuse9f15b42016-03-28 18:24:22 +0000175find_package(LibXml2 2.5.3 QUIET)
176if (LIBXML2_FOUND)
177 set(CLANG_HAVE_LIBXML 1)
178endif()
179
Chris Bienemana6b39ab2016-08-23 20:07:07 +0000180include(CheckIncludeFile)
181check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
182
Mike Spertuse9f15b42016-03-28 18:24:22 +0000183set(CLANG_RESOURCE_DIR "" CACHE STRING
184 "Relative directory from the Clang binary to its resource files.")
185
186set(C_INCLUDE_DIRS "" CACHE STRING
187 "Colon separated list of directories clang will search for headers.")
188
189set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
190set(DEFAULT_SYSROOT "" CACHE PATH
191 "Default <path> to all compiler invocations for --sysroot=<path>." )
192
Rafael Espindola5ed89d42016-06-03 17:26:16 +0000193set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
194
Rafael Espindola557679f2016-06-20 23:54:44 +0000195set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
196 "enable x86 relax relocations by default")
197
Mike Spertuse9f15b42016-03-28 18:24:22 +0000198set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000199 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000200if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
201 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
202 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
Jonas Hahnfeldebf86622016-07-25 08:04:26 +0000203 message(WARNING "Resetting default C++ stdlib to use platform default")
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000204 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
205 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
206endif()
207
208set(CLANG_DEFAULT_RTLIB "" CACHE STRING
209 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
210if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
211 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
212 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
213 message(WARNING "Resetting default rtlib to use platform default")
214 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
215 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000216endif()
217
218set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
219 "Default OpenMP runtime used by -fopenmp.")
220
221set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
222 "Vendor-specific text for showing with version information.")
223
224if( CLANG_VENDOR )
225 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
226endif()
227
228set(CLANG_REPOSITORY_STRING "" CACHE STRING
229 "Vendor-specific text for showing the repository the source is taken from.")
230
231if(CLANG_REPOSITORY_STRING)
232 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
233endif()
234
Mike Spertuse9f15b42016-03-28 18:24:22 +0000235set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
236 "Vendor-specific uti.")
237
238# The libdir suffix must exactly match whatever LLVM's configuration used.
239set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
240
241set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
242set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
243
244if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
245 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
246"the makefiles distributed with LLVM. Please create a directory and run cmake "
247"from there, passing the path to this source directory as the last argument. "
248"This process created the file `CMakeCache.txt' and the directory "
249"`CMakeFiles'. Please delete them.")
250endif()
251
252if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
253 file(GLOB_RECURSE
254 tablegenned_files_on_include_dir
255 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
256 if( tablegenned_files_on_include_dir )
257 message(FATAL_ERROR "Apparently there is a previous in-source build, "
258"probably as the result of running `configure' and `make' on "
259"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
260"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
261 endif()
262endif()
263
264# Compute the Clang version from the LLVM version.
265string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
266 ${PACKAGE_VERSION})
267message(STATUS "Clang version: ${CLANG_VERSION}")
268
269string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
270 ${CLANG_VERSION})
271string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
272 ${CLANG_VERSION})
273if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
274 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
275 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
276 ${CLANG_VERSION})
277else()
278 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
279endif()
280
281# Configure the Version.inc file.
282configure_file(
283 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
284 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
285
286# Add appropriate flags for GCC
287if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
288 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
289 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
290 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
291 endif ()
292
293 # Enable -pedantic for Clang even if it's not enabled for LLVM.
294 if (NOT LLVM_ENABLE_PEDANTIC)
295 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
296 endif ()
297
298 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
299 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
300 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
301 endif()
302endif ()
303
304# Determine HOST_LINK_VERSION on Darwin.
305set(HOST_LINK_VERSION)
306if (APPLE)
307 set(LD_V_OUTPUT)
308 execute_process(
309 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
310 RESULT_VARIABLE HAD_ERROR
311 OUTPUT_VARIABLE LD_V_OUTPUT
312 )
313 if (NOT HAD_ERROR)
314 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
315 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
316 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
317 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
318 endif()
319 else()
320 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
321 endif()
322endif()
323
324configure_file(
325 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
326 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
327
328include(CMakeParseArguments)
Michael Gottesmanca589cc2016-07-09 21:58:40 +0000329include(AddClang)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000330
331set(CMAKE_INCLUDE_CURRENT_DIR ON)
332
333include_directories(BEFORE
334 ${CMAKE_CURRENT_BINARY_DIR}/include
335 ${CMAKE_CURRENT_SOURCE_DIR}/include
336 )
337
338if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
339 install(DIRECTORY include/clang include/clang-c
340 DESTINATION include
341 FILES_MATCHING
342 PATTERN "*.def"
343 PATTERN "*.h"
344 PATTERN "config.h" EXCLUDE
345 PATTERN ".svn" EXCLUDE
346 )
347
348 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
349 DESTINATION include
350 FILES_MATCHING
351 PATTERN "CMakeFiles" EXCLUDE
352 PATTERN "*.inc"
353 PATTERN "*.h"
354 )
355endif()
356
357add_definitions( -D_GNU_SOURCE )
358
Michael Gottesmaneb396a62016-07-10 01:44:00 +0000359option(CLANG_BUILD_TOOLS
360 "Build the Clang tools. If OFF, just generate build targets." ON)
361
Mike Spertuse9f15b42016-03-28 18:24:22 +0000362option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
363if (CLANG_ENABLE_ARCMT)
364 set(ENABLE_CLANG_ARCMT "1")
365else()
366 set(ENABLE_CLANG_ARCMT "0")
367endif()
368
369option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
370if (CLANG_ENABLE_STATIC_ANALYZER)
371 set(ENABLE_CLANG_STATIC_ANALYZER "1")
372else()
373 set(ENABLE_CLANG_STATIC_ANALYZER "0")
374endif()
375
376if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
377 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
378endif()
379
380if(CLANG_ENABLE_ARCMT)
381 add_definitions(-DCLANG_ENABLE_ARCMT)
382 add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
383endif()
384if(CLANG_ENABLE_STATIC_ANALYZER)
385 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
386endif()
387
388# Clang version information
389set(CLANG_EXECUTABLE_VERSION
390 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
391 "Version number that will be placed into the clang executable, in the form XX.YY")
392set(LIBCLANG_LIBRARY_VERSION
393 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
394 "Version number that will be placed into the libclang library , in the form XX.YY")
395mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
396
397option(CLANG_INCLUDE_TESTS
398 "Generate build targets for the Clang unit tests."
399 ${LLVM_INCLUDE_TESTS})
400
401add_subdirectory(utils/TableGen)
402
403add_subdirectory(include)
404
405# All targets below may depend on all tablegen'd files.
406get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
407list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
408
409add_subdirectory(lib)
410add_subdirectory(tools)
411add_subdirectory(runtime)
412
413option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
414if (CLANG_BUILD_EXAMPLES)
415 set(ENABLE_CLANG_EXAMPLES "1")
416else()
417 set(ENABLE_CLANG_EXAMPLES "0")
418endif()
419add_subdirectory(examples)
420
421if( CLANG_INCLUDE_TESTS )
422 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
423 add_subdirectory(unittests)
424 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
425 list(APPEND CLANG_TEST_PARAMS
426 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
427 )
428 endif()
429 add_subdirectory(test)
430
431 if(CLANG_BUILT_STANDALONE)
432 # Add a global check rule now that all subdirectories have been traversed
433 # and we know the total set of lit testsuites.
434 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
435 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
436 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
437 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
438 add_lit_target(check-all
439 "Running all regression tests"
440 ${LLVM_LIT_TESTSUITES}
441 PARAMS ${LLVM_LIT_PARAMS}
442 DEPENDS ${LLVM_LIT_DEPENDS}
443 ARGS ${LLVM_LIT_EXTRA_ARGS}
444 )
445 endif()
446 add_subdirectory(utils/perf-training)
447endif()
448
449option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
450 ${LLVM_INCLUDE_DOCS})
451if( CLANG_INCLUDE_DOCS )
452 add_subdirectory(docs)
453endif()
454
Mike Spertuse9f15b42016-03-28 18:24:22 +0000455
Chris Bieneman834a40b2016-04-08 22:48:18 +0000456if(APPLE)
457 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
458 # default value get updated to the new default.
459 if(CLANG_ORDER_FILE STREQUAL "")
460 unset(CLANG_ORDER_FILE CACHE)
461 unset(CLANG_ORDER_FILE)
462 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000463
Chris Bieneman834a40b2016-04-08 22:48:18 +0000464
465 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
466 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
467
468 if(CLANG_ORDER_FILE AND NOT EXISTS ${CLANG_ORDER_FILE})
469 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
470 if(PATH_START EQUAL 0)
471 file(WRITE ${CLANG_ORDER_FILE} "\n")
472 else()
473 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
474 endif()
475 endif()
476endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000477
Michael Gottesmanfe9d2d82016-06-29 20:22:44 +0000478add_subdirectory(cmake/modules)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000479
Chris Bienemanc4865412016-07-25 18:54:30 +0000480if(CLANG_STAGE)
481 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
482endif()
483
Mike Spertuse9f15b42016-03-28 18:24:22 +0000484if (CLANG_ENABLE_BOOTSTRAP)
485 include(ExternalProject)
486
Mike Spertuse9f15b42016-03-28 18:24:22 +0000487 if(NOT CLANG_STAGE)
488 set(CLANG_STAGE stage1)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000489 endif()
490
491 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
492 if(MATCHED_STAGE)
493 if(NOT LLVM_BUILD_INSTRUMENTED)
494 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
495 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
496 else()
497 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
498 endif()
499 else()
500 set(NEXT_CLANG_STAGE bootstrap)
501 endif()
502
503 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
504 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
505 endif()
506 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
507
508
509 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
510 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
511
Chris Bieneman31046052016-04-27 18:52:48 +0000512 # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
Mike Spertuse9f15b42016-03-28 18:24:22 +0000513 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
Chris Bieneman31046052016-04-27 18:52:48 +0000514 set(LTO_DEP LTO)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000515 if(APPLE)
Chris Bieneman31046052016-04-27 18:52:48 +0000516 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
517 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
518 # so that the host object file tools will use the just-built libLTO.
Chris Bienemand052efc2016-07-25 23:48:14 +0000519 # However if System Integrity Protection is enabled the DYLD variables
520 # will be scrubbed from the environment of any base system commands. This
521 # includes /bin/sh, which ninja uses when executing build commands. To
522 # work around the envar being filtered away we pass it in as a CMake
523 # variable, and have LLVM's CMake append the envar to the archiver calls.
524 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
525 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000526 elseif(NOT WIN32)
Chris Bieneman31046052016-04-27 18:52:48 +0000527 list(APPEND LTO_DEP LLVMgold llvm-ar llvm-ranlib)
528 set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
529 set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000530 endif()
531 endif()
532
533 add_custom_target(${NEXT_CLANG_STAGE}-clear
534 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
535 )
536 add_custom_command(
537 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
538 DEPENDS clang ${LTO_DEP}
539 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
540 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
541 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
542 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
543 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
544 )
545
546 if(CMAKE_VERBOSE_MAKEFILE)
547 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
548 endif()
549
Chris Bienemanc4865412016-07-25 18:54:30 +0000550 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
Mike Spertuse9f15b42016-03-28 18:24:22 +0000551 PACKAGE_VERSION
552 LLVM_VERSION_MAJOR
553 LLVM_VERSION_MINOR
554 LLVM_VERSION_PATCH
555 LLVM_VERSION_SUFFIX
556 LLVM_BINUTILS_INCDIR
557 CLANG_REPOSITORY_STRING
558 CMAKE_MAKE_PROGRAM)
559
560 if(TARGET compiler-rt)
561 set(RUNTIME_DEP compiler-rt)
562 endif()
563
564 set(COMPILER_OPTIONS
565 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
566 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
567 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
568
569 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
570 set(PGO_DEP llvm-profdata)
571 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
572 endif()
573
574 if(LLVM_BUILD_INSTRUMENTED)
575 set(PGO_DEP generate-profdata)
576 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
Chris Bienemana1aa4062016-08-16 22:16:29 +0000577 # Use the current tools for LTO instead of the instrumented ones
578 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
579 CMAKE_CXX_COMPILER
580 CMAKE_C_COMPILER
581 CMAKE_ASM_COMPILER
582 CMAKE_AR
583 CMAKE_RANLIB
584 DARWIN_LTO_LIBRARY
585 DYLD_LIBRARY_PATH)
586
587 set(COMPILER_OPTIONS)
588 set(LTO_LIBRARY)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000589 set(RUNTIME_DEP) # Don't set runtime dependencies
Chris Bienemana1aa4062016-08-16 22:16:29 +0000590 set(LTO_DEP) # Don't need to depend on LTO
591 set(LTO_AR)
592 set(LTO_RANLIB)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000593 endif()
594
595 # Find all variables that start with BOOTSTRAP_ and populate a variable with
596 # them.
597 get_cmake_property(variableNames VARIABLES)
598 foreach(variableName ${variableNames})
599 if(variableName MATCHES "^BOOTSTRAP_")
600 string(SUBSTRING ${variableName} 10 -1 varName)
601 string(REPLACE ";" "\;" value "${${variableName}}")
602 list(APPEND PASSTHROUGH_VARIABLES
603 -D${varName}=${value})
604 endif()
605 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
606 list(APPEND PASSTHROUGH_VARIABLES
607 -D${variableName}=${${variableName}})
608 endif()
609 endforeach()
610
611 # Populate the passthrough variables
Chris Bienemanc4865412016-07-25 18:54:30 +0000612 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000613 if(${variableName})
614 string(REPLACE ";" "\;" value ${${variableName}})
615 list(APPEND PASSTHROUGH_VARIABLES
616 -D${variableName}=${value})
617 endif()
618 endforeach()
619
620 ExternalProject_Add(${NEXT_CLANG_STAGE}
621 DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP} ${PGO_DEP}
622 PREFIX ${NEXT_CLANG_STAGE}
623 SOURCE_DIR ${CMAKE_SOURCE_DIR}
624 STAMP_DIR ${STAMP_DIR}
625 BINARY_DIR ${BINARY_DIR}
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000626 EXCLUDE_FROM_ALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000627 CMAKE_ARGS
628 # We shouldn't need to set this here, but INSTALL_DIR doesn't
629 # seem to work, so instead I'm passing this through
630 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
631 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
632 ${PASSTHROUGH_VARIABLES}
633 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
634 ${COMPILER_OPTIONS}
635 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
636 INSTALL_COMMAND ""
637 STEP_TARGETS configure build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000638 USES_TERMINAL_CONFIGURE 1
639 USES_TERMINAL_BUILD 1
640 USES_TERMINAL_INSTALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000641 )
642
643 # exclude really-install from main target
644 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
645 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
Chris Bienemand052efc2016-07-25 23:48:14 +0000646 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
Mike Spertuse9f15b42016-03-28 18:24:22 +0000647 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
648 DEPENDEES build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000649 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000650 )
651 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
652 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
653
654 if(NOT CLANG_BOOTSTRAP_TARGETS)
655 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
656 endif()
657 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
658 # exclude from main target
659 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
660
661 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
Chris Bienemand052efc2016-07-25 23:48:14 +0000662 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000663 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
664 DEPENDEES configure
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000665 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000666 )
667
668 if(target MATCHES "^stage[0-9]*")
669 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
670 endif()
671
672 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
673 endforeach()
674endif()
675
676if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
677 add_subdirectory(utils/ClangVisualizers)
678endif()