blob: ca696b1ce79fc45ead6b2907139aabedc471afd0 [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"
Michal Gorny8c5c7ff2017-01-09 23:06:39 +000019 "--src-root"
20 "--cmakedir")
Mike Spertuse9f15b42016-03-28 18:24:22 +000021 execute_process(
22 COMMAND ${CONFIG_COMMAND}
23 RESULT_VARIABLE HAD_ERROR
24 OUTPUT_VARIABLE CONFIG_OUTPUT
25 )
26 if(NOT HAD_ERROR)
27 string(REGEX REPLACE
28 "[ \t]*[\r\n]+[ \t]*" ";"
29 CONFIG_OUTPUT ${CONFIG_OUTPUT})
30 else()
31 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
32 message(STATUS "${CONFIG_COMMAND_STR}")
33 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
34 endif()
35 else()
36 message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
37 endif()
38
39 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
40 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
41 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
42 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
43 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
44 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
NAKAMURA Takumia1c0f602017-02-13 14:59:53 +000045 list(GET CONFIG_OUTPUT 6 LLVM_CONFIG_CMAKE_PATH)
Mike Spertuse9f15b42016-03-28 18:24:22 +000046
47 if(NOT MSVC_IDE)
48 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
49 CACHE BOOL "Enable assertions")
50 # Assertions should follow llvm-config's.
51 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
52 endif()
53
54 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
55 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
56 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
57 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
58 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
59
NAKAMURA Takumia1c0f602017-02-13 14:59:53 +000060 # Normalize LLVM_CMAKE_PATH. --cmakedir might contain backslashes.
61 # CMake assumes slashes as PATH.
NAKAMURA Takumi3cc0d4e2017-02-19 03:17:31 +000062 file(TO_CMAKE_PATH ${LLVM_CONFIG_CMAKE_PATH} LLVM_CMAKE_PATH)
NAKAMURA Takumia1c0f602017-02-13 14:59:53 +000063
Mike Spertuse9f15b42016-03-28 18:24:22 +000064 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
65 NO_DEFAULT_PATH)
66
Mike Spertuse9f15b42016-03-28 18:24:22 +000067 set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
68 if(EXISTS ${LLVMCONFIG_FILE})
69 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
70 include(${LLVMCONFIG_FILE})
71 else()
72 message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
73 endif()
74
75 # They are used as destination of target generators.
76 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
77 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
78 if(WIN32 OR CYGWIN)
79 # DLL platform -- put DLLs into bin.
80 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
81 else()
82 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
83 endif()
84
Eric Fiselierc14f3fb2017-03-07 00:15:18 +000085 option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
Mike Spertuse9f15b42016-03-28 18:24:22 +000086 option(LLVM_INSTALL_TOOLCHAIN_ONLY
87 "Only include toolchain files in the 'install' target." OFF)
88
89 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
90 "Set to ON to force using an old, unsupported host toolchain." OFF)
91 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
92
93 include(AddLLVM)
94 include(TableGen)
95 include(HandleLLVMOptions)
96 include(VersionFromVCS)
97
98 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
99
100 if (NOT DEFINED LLVM_INCLUDE_TESTS)
101 set(LLVM_INCLUDE_TESTS ON)
102 endif()
103
104 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
105 link_directories("${LLVM_LIBRARY_DIR}")
106
107 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
108 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
109 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
110
111 if(LLVM_INCLUDE_TESTS)
112 set(Python_ADDITIONAL_VERSIONS 2.7)
113 include(FindPythonInterp)
114 if(NOT PYTHONINTERP_FOUND)
115 message(FATAL_ERROR
116"Unable to find Python interpreter, required for builds and testing.
117
118Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
119 endif()
120
121 if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
122 message(FATAL_ERROR "Python 2.7 or newer is required")
123 endif()
124
125 # Check prebuilt llvm/utils.
126 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
127 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
128 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
129 set(LLVM_UTILS_PROVIDED ON)
130 endif()
131
132 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
Michal Gornyda6d2b82016-10-18 17:07:30 +0000133 # Note: path not really used, except for checking if lit was found
Mike Spertuse9f15b42016-03-28 18:24:22 +0000134 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
135 if(NOT LLVM_UTILS_PROVIDED)
136 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
137 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
138 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
139 set(LLVM_UTILS_PROVIDED ON)
140 set(CLANG_TEST_DEPS FileCheck count not)
141 endif()
142 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
143 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
144 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
145 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
146 add_subdirectory(${UNITTEST_DIR} utils/unittest)
147 endif()
148 else()
149 # Seek installed Lit.
Michal Gornyda6d2b82016-10-18 17:07:30 +0000150 find_program(LLVM_LIT
151 NAMES llvm-lit lit.py lit
152 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
153 DOC "Path to lit.py")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000154 endif()
155
156 if(LLVM_LIT)
157 # Define the default arguments to use with 'lit', and an option for the user
158 # to override.
159 set(LIT_ARGS_DEFAULT "-sv")
160 if (MSVC OR XCODE)
161 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
162 endif()
163 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
164
165 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
166 if( WIN32 AND NOT CYGWIN )
167 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
168 endif()
169 else()
170 set(LLVM_INCLUDE_TESTS OFF)
171 endif()
172 endif()
173
174 set( CLANG_BUILT_STANDALONE 1 )
175 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
176else()
177 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
178endif()
179
Michael Gottesmanca589cc2016-07-09 21:58:40 +0000180# Make sure that our source directory is on the current cmake module path so that
181# we can include cmake files from this directory.
182list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
183
Mike Spertuse9f15b42016-03-28 18:24:22 +0000184find_package(LibXml2 2.5.3 QUIET)
185if (LIBXML2_FOUND)
186 set(CLANG_HAVE_LIBXML 1)
187endif()
188
Dominic Chen08f943c2017-04-04 19:52:25 +0000189find_package(Z3 4.5)
190
Chris Bienemana6b39ab2016-08-23 20:07:07 +0000191include(CheckIncludeFile)
192check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
193
Mike Spertuse9f15b42016-03-28 18:24:22 +0000194set(CLANG_RESOURCE_DIR "" CACHE STRING
195 "Relative directory from the Clang binary to its resource files.")
196
197set(C_INCLUDE_DIRS "" CACHE STRING
198 "Colon separated list of directories clang will search for headers.")
199
200set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
201set(DEFAULT_SYSROOT "" CACHE PATH
202 "Default <path> to all compiler invocations for --sysroot=<path>." )
203
Rafael Espindola5ed89d42016-06-03 17:26:16 +0000204set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
205
Rafael Espindola557679f2016-06-20 23:54:44 +0000206set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
207 "enable x86 relax relocations by default")
208
Petr Hosekfe2c2b02016-12-14 16:46:50 +0000209set(CLANG_DEFAULT_LINKER "" CACHE STRING
210 "Default linker to use (linker name or absolute path, empty for platform default)")
211
Mike Spertuse9f15b42016-03-28 18:24:22 +0000212set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000213 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000214if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
215 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
216 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
Jonas Hahnfeldebf86622016-07-25 08:04:26 +0000217 message(WARNING "Resetting default C++ stdlib to use platform default")
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000218 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
219 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
220endif()
221
222set(CLANG_DEFAULT_RTLIB "" CACHE STRING
223 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
224if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
225 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
226 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
227 message(WARNING "Resetting default rtlib to use platform default")
228 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
229 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000230endif()
231
232set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
233 "Default OpenMP runtime used by -fopenmp.")
234
235set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
236 "Vendor-specific text for showing with version information.")
237
238if( CLANG_VENDOR )
239 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
240endif()
241
242set(CLANG_REPOSITORY_STRING "" CACHE STRING
243 "Vendor-specific text for showing the repository the source is taken from.")
244
245if(CLANG_REPOSITORY_STRING)
246 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
247endif()
248
Mike Spertuse9f15b42016-03-28 18:24:22 +0000249set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
250 "Vendor-specific uti.")
251
252# The libdir suffix must exactly match whatever LLVM's configuration used.
253set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
254
255set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
256set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
257
258if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
259 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
260"the makefiles distributed with LLVM. Please create a directory and run cmake "
261"from there, passing the path to this source directory as the last argument. "
262"This process created the file `CMakeCache.txt' and the directory "
263"`CMakeFiles'. Please delete them.")
264endif()
265
266if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
267 file(GLOB_RECURSE
268 tablegenned_files_on_include_dir
269 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
270 if( tablegenned_files_on_include_dir )
271 message(FATAL_ERROR "Apparently there is a previous in-source build, "
272"probably as the result of running `configure' and `make' on "
273"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
274"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
275 endif()
276endif()
277
Chris Bienemanebfc6802016-09-20 19:09:21 +0000278# If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
279if(NOT DEFINED CLANG_VERSION_MAJOR)
280 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
281endif()
282if(NOT DEFINED CLANG_VERSION_MINOR)
283 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
284endif()
285if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
286 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
287endif()
David L. Jones2f754522016-09-15 22:12:26 +0000288# Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
289set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000290message(STATUS "Clang version: ${CLANG_VERSION}")
291
Mike Spertuse9f15b42016-03-28 18:24:22 +0000292# Configure the Version.inc file.
293configure_file(
294 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
295 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
296
297# Add appropriate flags for GCC
298if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
299 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
300 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
301 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
302 endif ()
303
304 # Enable -pedantic for Clang even if it's not enabled for LLVM.
305 if (NOT LLVM_ENABLE_PEDANTIC)
306 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
307 endif ()
308
309 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
310 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
311 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
312 endif()
313endif ()
314
315# Determine HOST_LINK_VERSION on Darwin.
316set(HOST_LINK_VERSION)
317if (APPLE)
318 set(LD_V_OUTPUT)
319 execute_process(
320 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
321 RESULT_VARIABLE HAD_ERROR
322 OUTPUT_VARIABLE LD_V_OUTPUT
323 )
324 if (NOT HAD_ERROR)
325 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
326 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
327 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
328 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
329 endif()
330 else()
331 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
332 endif()
333endif()
334
Mike Spertuse9f15b42016-03-28 18:24:22 +0000335include(CMakeParseArguments)
Michael Gottesmanca589cc2016-07-09 21:58:40 +0000336include(AddClang)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000337
338set(CMAKE_INCLUDE_CURRENT_DIR ON)
339
340include_directories(BEFORE
341 ${CMAKE_CURRENT_BINARY_DIR}/include
342 ${CMAKE_CURRENT_SOURCE_DIR}/include
343 )
344
345if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
346 install(DIRECTORY include/clang include/clang-c
347 DESTINATION include
348 FILES_MATCHING
349 PATTERN "*.def"
350 PATTERN "*.h"
351 PATTERN "config.h" EXCLUDE
352 PATTERN ".svn" EXCLUDE
353 )
354
355 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
356 DESTINATION include
357 FILES_MATCHING
358 PATTERN "CMakeFiles" EXCLUDE
359 PATTERN "*.inc"
360 PATTERN "*.h"
361 )
362endif()
363
364add_definitions( -D_GNU_SOURCE )
365
Michael Gottesmaneb396a62016-07-10 01:44:00 +0000366option(CLANG_BUILD_TOOLS
367 "Build the Clang tools. If OFF, just generate build targets." ON)
368
Mike Spertuse9f15b42016-03-28 18:24:22 +0000369option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000370option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000371
Dominic Chen08f943c2017-04-04 19:52:25 +0000372option(CLANG_ANALYZER_BUILD_Z3
373 "Build the static analyzer with the Z3 constraint manager." OFF)
374
375if(NOT CLANG_ENABLE_STATIC_ANALYZER AND (CLANG_ENABLE_ARCMT OR CLANG_ANALYZER_BUILD_Z3))
376 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT or Z3")
377endif()
378
379if(CLANG_ANALYZER_BUILD_Z3)
380 if(Z3_FOUND)
381 set(CLANG_ANALYZER_WITH_Z3 1)
382 else()
383 message(FATAL_ERROR "Cannot find Z3 header file or shared library")
384 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000385endif()
386
387if(CLANG_ENABLE_ARCMT)
388 add_definitions(-DCLANG_ENABLE_ARCMT)
389 add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
390endif()
391if(CLANG_ENABLE_STATIC_ANALYZER)
392 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
393endif()
394
395# Clang version information
396set(CLANG_EXECUTABLE_VERSION
397 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
398 "Version number that will be placed into the clang executable, in the form XX.YY")
399set(LIBCLANG_LIBRARY_VERSION
400 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
401 "Version number that will be placed into the libclang library , in the form XX.YY")
402mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
403
404option(CLANG_INCLUDE_TESTS
405 "Generate build targets for the Clang unit tests."
406 ${LLVM_INCLUDE_TESTS})
407
408add_subdirectory(utils/TableGen)
409
410add_subdirectory(include)
411
412# All targets below may depend on all tablegen'd files.
413get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
414list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
415
416add_subdirectory(lib)
417add_subdirectory(tools)
418add_subdirectory(runtime)
419
420option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000421add_subdirectory(examples)
422
Alexander Shaposhnikovfd7afa72016-12-31 05:25:52 +0000423if(APPLE)
424 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
425 # default value get updated to the new default.
426 if(CLANG_ORDER_FILE STREQUAL "")
427 unset(CLANG_ORDER_FILE CACHE)
428 unset(CLANG_ORDER_FILE)
429 endif()
430
431
432 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
433 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
434
435 if(NOT EXISTS ${CLANG_ORDER_FILE})
436 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
437 if(PATH_START EQUAL 0)
438 file(WRITE ${CLANG_ORDER_FILE} "\n")
439 else()
440 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
441 endif()
442 endif()
443endif()
444
445
Mike Spertuse9f15b42016-03-28 18:24:22 +0000446if( CLANG_INCLUDE_TESTS )
447 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
448 add_subdirectory(unittests)
449 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
450 list(APPEND CLANG_TEST_PARAMS
451 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
452 )
453 endif()
454 add_subdirectory(test)
455
456 if(CLANG_BUILT_STANDALONE)
457 # Add a global check rule now that all subdirectories have been traversed
458 # and we know the total set of lit testsuites.
459 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
460 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
461 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
462 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
463 add_lit_target(check-all
464 "Running all regression tests"
465 ${LLVM_LIT_TESTSUITES}
466 PARAMS ${LLVM_LIT_PARAMS}
467 DEPENDS ${LLVM_LIT_DEPENDS}
468 ARGS ${LLVM_LIT_EXTRA_ARGS}
469 )
470 endif()
471 add_subdirectory(utils/perf-training)
472endif()
473
474option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
475 ${LLVM_INCLUDE_DOCS})
476if( CLANG_INCLUDE_DOCS )
477 add_subdirectory(docs)
478endif()
479
Michael Gottesmanfe9d2d82016-06-29 20:22:44 +0000480add_subdirectory(cmake/modules)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000481
Chris Bienemanc4865412016-07-25 18:54:30 +0000482if(CLANG_STAGE)
483 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
484endif()
485
Mike Spertuse9f15b42016-03-28 18:24:22 +0000486if (CLANG_ENABLE_BOOTSTRAP)
487 include(ExternalProject)
488
Chris Bieneman76a2e602016-10-19 21:18:48 +0000489 add_custom_target(clang-bootstrap-deps DEPENDS clang)
490
Mike Spertuse9f15b42016-03-28 18:24:22 +0000491 if(NOT CLANG_STAGE)
492 set(CLANG_STAGE stage1)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000493 endif()
494
495 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
496 if(MATCHED_STAGE)
497 if(NOT LLVM_BUILD_INSTRUMENTED)
498 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
499 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
500 else()
501 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
502 endif()
503 else()
504 set(NEXT_CLANG_STAGE bootstrap)
505 endif()
506
507 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
508 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
509 endif()
510 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
511
512
513 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
514 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
515
Petr Hosek03203142017-01-18 05:41:17 +0000516 if(BOOTSTRAP_LLVM_ENABLE_LLD)
517 add_dependencies(clang-bootstrap-deps lld)
518 endif()
519
Petr Hosek8e07a882016-11-16 23:59:06 +0000520 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
Chris Bieneman76a2e602016-10-19 21:18:48 +0000521 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000522 if(APPLE)
Petr Hosek8e07a882016-11-16 23:59:06 +0000523 add_dependencies(clang-bootstrap-deps LTO)
Chris Bieneman31046052016-04-27 18:52:48 +0000524 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
525 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
526 # so that the host object file tools will use the just-built libLTO.
Chris Bienemand052efc2016-07-25 23:48:14 +0000527 # However if System Integrity Protection is enabled the DYLD variables
528 # will be scrubbed from the environment of any base system commands. This
529 # includes /bin/sh, which ninja uses when executing build commands. To
530 # work around the envar being filtered away we pass it in as a CMake
531 # variable, and have LLVM's CMake append the envar to the archiver calls.
532 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
533 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000534 elseif(NOT WIN32)
Petr Hosek8e07a882016-11-16 23:59:06 +0000535 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
Petr Hosek03203142017-01-18 05:41:17 +0000536 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
Petr Hosek8e07a882016-11-16 23:59:06 +0000537 add_dependencies(clang-bootstrap-deps LLVMgold)
538 endif()
Chris Bieneman31046052016-04-27 18:52:48 +0000539 set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
540 set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000541 endif()
542 endif()
543
544 add_custom_target(${NEXT_CLANG_STAGE}-clear
545 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
546 )
547 add_custom_command(
548 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
Chris Bieneman76a2e602016-10-19 21:18:48 +0000549 DEPENDS clang-bootstrap-deps
Mike Spertuse9f15b42016-03-28 18:24:22 +0000550 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
551 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
552 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
553 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
554 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
555 )
556
557 if(CMAKE_VERBOSE_MAKEFILE)
558 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
559 endif()
560
Chris Bienemanc4865412016-07-25 18:54:30 +0000561 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
Mike Spertuse9f15b42016-03-28 18:24:22 +0000562 PACKAGE_VERSION
Chris Bienemanb1370532016-10-18 00:50:20 +0000563 PACKAGE_VENDOR
Mike Spertuse9f15b42016-03-28 18:24:22 +0000564 LLVM_VERSION_MAJOR
565 LLVM_VERSION_MINOR
566 LLVM_VERSION_PATCH
Chris Bieneman2c4d54f2016-09-21 20:43:43 +0000567 CLANG_VERSION_MAJOR
568 CLANG_VERSION_MINOR
569 CLANG_VERSION_PATCHLEVEL
Mike Spertuse9f15b42016-03-28 18:24:22 +0000570 LLVM_VERSION_SUFFIX
571 LLVM_BINUTILS_INCDIR
572 CLANG_REPOSITORY_STRING
Chris Bienemanb1370532016-10-18 00:50:20 +0000573 CMAKE_MAKE_PROGRAM
574 CMAKE_OSX_ARCHITECTURES)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000575
Chris Bieneman76a2e602016-10-19 21:18:48 +0000576 # We don't need to depend on compiler-rt if we're building instrumented
577 # because the next stage will use the same compiler used to build this stage.
578 if(TARGET compiler-rt AND NOT LLVM_BUILD_INSTRUMENTED)
579 add_dependencies(clang-bootstrap-deps compiler-rt)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000580 endif()
581
582 set(COMPILER_OPTIONS
583 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
584 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
585 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
586
587 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
Chris Bieneman76a2e602016-10-19 21:18:48 +0000588 add_dependencies(clang-bootstrap-deps llvm-profdata)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000589 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
590 endif()
591
592 if(LLVM_BUILD_INSTRUMENTED)
Chris Bieneman76a2e602016-10-19 21:18:48 +0000593 add_dependencies(clang-bootstrap-deps generate-profdata)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000594 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
Chris Bienemana1aa4062016-08-16 22:16:29 +0000595 # Use the current tools for LTO instead of the instrumented ones
596 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
597 CMAKE_CXX_COMPILER
598 CMAKE_C_COMPILER
599 CMAKE_ASM_COMPILER
600 CMAKE_AR
601 CMAKE_RANLIB
602 DARWIN_LTO_LIBRARY
603 DYLD_LIBRARY_PATH)
604
605 set(COMPILER_OPTIONS)
606 set(LTO_LIBRARY)
Chris Bienemana1aa4062016-08-16 22:16:29 +0000607 set(LTO_AR)
608 set(LTO_RANLIB)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000609 endif()
610
611 # Find all variables that start with BOOTSTRAP_ and populate a variable with
612 # them.
613 get_cmake_property(variableNames VARIABLES)
614 foreach(variableName ${variableNames})
615 if(variableName MATCHES "^BOOTSTRAP_")
616 string(SUBSTRING ${variableName} 10 -1 varName)
617 string(REPLACE ";" "\;" value "${${variableName}}")
618 list(APPEND PASSTHROUGH_VARIABLES
619 -D${varName}=${value})
620 endif()
621 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
622 list(APPEND PASSTHROUGH_VARIABLES
623 -D${variableName}=${${variableName}})
624 endif()
625 endforeach()
626
627 # Populate the passthrough variables
Chris Bienemanc4865412016-07-25 18:54:30 +0000628 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
Chris Bieneman43a601d2016-09-21 23:24:15 +0000629 if(DEFINED ${variableName})
Chris Bieneman725acc42016-09-22 00:18:12 +0000630 if("${${variableName}}" STREQUAL "")
631 set(value "")
632 else()
633 string(REPLACE ";" "\;" value ${${variableName}})
634 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000635 list(APPEND PASSTHROUGH_VARIABLES
636 -D${variableName}=${value})
637 endif()
638 endforeach()
639
640 ExternalProject_Add(${NEXT_CLANG_STAGE}
Chris Bieneman76a2e602016-10-19 21:18:48 +0000641 DEPENDS clang-bootstrap-deps
Mike Spertuse9f15b42016-03-28 18:24:22 +0000642 PREFIX ${NEXT_CLANG_STAGE}
643 SOURCE_DIR ${CMAKE_SOURCE_DIR}
644 STAMP_DIR ${STAMP_DIR}
645 BINARY_DIR ${BINARY_DIR}
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000646 EXCLUDE_FROM_ALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000647 CMAKE_ARGS
648 # We shouldn't need to set this here, but INSTALL_DIR doesn't
649 # seem to work, so instead I'm passing this through
650 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
651 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
652 ${PASSTHROUGH_VARIABLES}
653 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
654 ${COMPILER_OPTIONS}
655 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
656 INSTALL_COMMAND ""
657 STEP_TARGETS configure build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000658 USES_TERMINAL_CONFIGURE 1
659 USES_TERMINAL_BUILD 1
660 USES_TERMINAL_INSTALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000661 )
662
663 # exclude really-install from main target
664 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
665 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
Chris Bienemand052efc2016-07-25 23:48:14 +0000666 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
Mike Spertuse9f15b42016-03-28 18:24:22 +0000667 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
668 DEPENDEES build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000669 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000670 )
671 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
672 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
673
674 if(NOT CLANG_BOOTSTRAP_TARGETS)
675 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
676 endif()
677 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
678 # exclude from main target
679 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
680
681 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
Chris Bienemand052efc2016-07-25 23:48:14 +0000682 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000683 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
684 DEPENDEES configure
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000685 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000686 )
687
688 if(target MATCHES "^stage[0-9]*")
689 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
690 endif()
691
692 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
693 endforeach()
694endif()
695
696if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
697 add_subdirectory(utils/ClangVisualizers)
698endif()
Dominic Chen08f943c2017-04-04 19:52:25 +0000699
700configure_file(
701 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
702 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)