blob: 4601c5523f88220e8d00c0b698cef376a129a3ff [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
171find_package(LibXml2 2.5.3 QUIET)
172if (LIBXML2_FOUND)
173 set(CLANG_HAVE_LIBXML 1)
174endif()
175
176set(CLANG_RESOURCE_DIR "" CACHE STRING
177 "Relative directory from the Clang binary to its resource files.")
178
179set(C_INCLUDE_DIRS "" CACHE STRING
180 "Colon separated list of directories clang will search for headers.")
181
182set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
183set(DEFAULT_SYSROOT "" CACHE PATH
184 "Default <path> to all compiler invocations for --sysroot=<path>." )
185
Rafael Espindola5ed89d42016-06-03 17:26:16 +0000186set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
187
Rafael Espindola557679f2016-06-20 23:54:44 +0000188set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
189 "enable x86 relax relocations by default")
190
Mike Spertuse9f15b42016-03-28 18:24:22 +0000191set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
192 "Default C++ stdlib to use (empty for architecture default, \"libstdc++\" or \"libc++\"")
193if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
194 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
195 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
196 message(WARNING "Resetting default C++ stdlib to use architecture default")
197 set(CLANG_DEFAULT_CXX_STDLIB "")
198endif()
199
200set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
201 "Default OpenMP runtime used by -fopenmp.")
202
203set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
204 "Vendor-specific text for showing with version information.")
205
206if( CLANG_VENDOR )
207 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
208endif()
209
210set(CLANG_REPOSITORY_STRING "" CACHE STRING
211 "Vendor-specific text for showing the repository the source is taken from.")
212
213if(CLANG_REPOSITORY_STRING)
214 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
215endif()
216
Mike Spertuse9f15b42016-03-28 18:24:22 +0000217set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
218 "Vendor-specific uti.")
219
220# The libdir suffix must exactly match whatever LLVM's configuration used.
221set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
222
223set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
224set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
225
226if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
227 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
228"the makefiles distributed with LLVM. Please create a directory and run cmake "
229"from there, passing the path to this source directory as the last argument. "
230"This process created the file `CMakeCache.txt' and the directory "
231"`CMakeFiles'. Please delete them.")
232endif()
233
234if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
235 file(GLOB_RECURSE
236 tablegenned_files_on_include_dir
237 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
238 if( tablegenned_files_on_include_dir )
239 message(FATAL_ERROR "Apparently there is a previous in-source build, "
240"probably as the result of running `configure' and `make' on "
241"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
242"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
243 endif()
244endif()
245
246# Compute the Clang version from the LLVM version.
247string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
248 ${PACKAGE_VERSION})
249message(STATUS "Clang version: ${CLANG_VERSION}")
250
251string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
252 ${CLANG_VERSION})
253string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
254 ${CLANG_VERSION})
255if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
256 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
257 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
258 ${CLANG_VERSION})
259else()
260 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
261endif()
262
263# Configure the Version.inc file.
264configure_file(
265 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
266 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
267
268# Add appropriate flags for GCC
269if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
270 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
271 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
272 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
273 endif ()
274
275 # Enable -pedantic for Clang even if it's not enabled for LLVM.
276 if (NOT LLVM_ENABLE_PEDANTIC)
277 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
278 endif ()
279
280 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
281 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
282 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
283 endif()
284endif ()
285
286# Determine HOST_LINK_VERSION on Darwin.
287set(HOST_LINK_VERSION)
288if (APPLE)
289 set(LD_V_OUTPUT)
290 execute_process(
291 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
292 RESULT_VARIABLE HAD_ERROR
293 OUTPUT_VARIABLE LD_V_OUTPUT
294 )
295 if (NOT HAD_ERROR)
296 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
297 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
298 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
299 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
300 endif()
301 else()
302 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
303 endif()
304endif()
305
306configure_file(
307 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
308 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
309
310include(CMakeParseArguments)
311
312function(clang_tablegen)
313 # Syntax:
314 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
315 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
316 #
317 # Generates a custom command for invoking tblgen as
318 #
319 # tblgen source-file -o=output-file tablegen-arg ...
320 #
321 # and, if cmake-target-name is provided, creates a custom target for
322 # executing the custom command depending on output-file. It is
323 # possible to list more files to depend after DEPENDS.
324
325 cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
326
327 if( NOT CTG_SOURCE )
328 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
329 endif()
330
331 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
332 tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS})
333
334 if(CTG_TARGET)
335 add_public_tablegen_target(${CTG_TARGET})
336 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
337 set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
338 endif()
339endfunction(clang_tablegen)
340
341macro(set_clang_windows_version_resource_properties name)
342 if(DEFINED windows_resource_file)
343 set_windows_version_resource_properties(${name} ${windows_resource_file}
344 VERSION_MAJOR ${CLANG_VERSION_MAJOR}
345 VERSION_MINOR ${CLANG_VERSION_MINOR}
346 VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
347 VERSION_STRING "${CLANG_VERSION} (${BACKEND_PACKAGE_STRING})"
348 PRODUCT_NAME "clang")
349 endif()
350endmacro()
351
352macro(add_clang_subdirectory name)
353 add_llvm_subdirectory(CLANG TOOL ${name})
354endmacro()
355
356macro(add_clang_library name)
357 cmake_parse_arguments(ARG
358 "SHARED"
359 ""
360 "ADDITIONAL_HEADERS"
361 ${ARGN})
362 set(srcs)
363 if(MSVC_IDE OR XCODE)
364 # Add public headers
365 file(RELATIVE_PATH lib_path
366 ${CLANG_SOURCE_DIR}/lib/
367 ${CMAKE_CURRENT_SOURCE_DIR}
368 )
369 if(NOT lib_path MATCHES "^[.][.]")
370 file( GLOB_RECURSE headers
371 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
372 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
373 )
374 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
375
376 file( GLOB_RECURSE tds
377 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
378 )
379 source_group("TableGen descriptions" FILES ${tds})
380 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
381
382 if(headers OR tds)
383 set(srcs ${headers} ${tds})
384 endif()
385 endif()
386 endif(MSVC_IDE OR XCODE)
387 if(srcs OR ARG_ADDITIONAL_HEADERS)
388 set(srcs
389 ADDITIONAL_HEADERS
390 ${srcs}
391 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
392 )
393 endif()
394 if(ARG_SHARED)
395 set(ARG_ENABLE_SHARED SHARED)
396 endif()
397 llvm_add_library(${name} ${ARG_ENABLE_SHARED} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
398
399 if(TARGET ${name})
Chris Bieneman28cdb672016-06-09 21:29:55 +0000400 target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000401
402 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
403 install(TARGETS ${name}
404 COMPONENT ${name}
405 EXPORT ClangTargets
406 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
407 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
408 RUNTIME DESTINATION bin)
409
410 if (${ARG_SHARED} AND NOT CMAKE_CONFIGURATION_TYPES)
411 add_custom_target(install-${name}
412 DEPENDS ${name}
413 COMMAND "${CMAKE_COMMAND}"
414 -DCMAKE_INSTALL_COMPONENT=${name}
415 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
416 endif()
417 endif()
418 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
419 else()
420 # Add empty "phony" target
421 add_custom_target(${name})
422 endif()
423
424 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
425 set_clang_windows_version_resource_properties(${name})
426endmacro(add_clang_library)
427
428macro(add_clang_executable name)
429 add_llvm_executable( ${name} ${ARGN} )
430 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
431 set_clang_windows_version_resource_properties(${name})
432endmacro(add_clang_executable)
433
434macro(add_clang_tool name)
435 add_clang_executable(${name} ${ARGN})
436 install(TARGETS ${name}
437 RUNTIME DESTINATION bin
438 COMPONENT ${name})
439
440 if(NOT CMAKE_CONFIGURATION_TYPES)
441 add_custom_target(install-${name}
442 DEPENDS ${name}
443 COMMAND "${CMAKE_COMMAND}"
444 -DCMAKE_INSTALL_COMPONENT=${name}
445 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
446 endif()
447endmacro()
448
449macro(add_clang_symlink name dest)
450 add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
451 # Always generate install targets
452 llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
453endmacro()
454
455set(CMAKE_INCLUDE_CURRENT_DIR ON)
456
457include_directories(BEFORE
458 ${CMAKE_CURRENT_BINARY_DIR}/include
459 ${CMAKE_CURRENT_SOURCE_DIR}/include
460 )
461
462if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
463 install(DIRECTORY include/clang include/clang-c
464 DESTINATION include
465 FILES_MATCHING
466 PATTERN "*.def"
467 PATTERN "*.h"
468 PATTERN "config.h" EXCLUDE
469 PATTERN ".svn" EXCLUDE
470 )
471
472 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
473 DESTINATION include
474 FILES_MATCHING
475 PATTERN "CMakeFiles" EXCLUDE
476 PATTERN "*.inc"
477 PATTERN "*.h"
478 )
479endif()
480
481add_definitions( -D_GNU_SOURCE )
482
483option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
484if (CLANG_ENABLE_ARCMT)
485 set(ENABLE_CLANG_ARCMT "1")
486else()
487 set(ENABLE_CLANG_ARCMT "0")
488endif()
489
490option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
491if (CLANG_ENABLE_STATIC_ANALYZER)
492 set(ENABLE_CLANG_STATIC_ANALYZER "1")
493else()
494 set(ENABLE_CLANG_STATIC_ANALYZER "0")
495endif()
496
497if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
498 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
499endif()
500
501if(CLANG_ENABLE_ARCMT)
502 add_definitions(-DCLANG_ENABLE_ARCMT)
503 add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
504endif()
505if(CLANG_ENABLE_STATIC_ANALYZER)
506 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
507endif()
508
509# Clang version information
510set(CLANG_EXECUTABLE_VERSION
511 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
512 "Version number that will be placed into the clang executable, in the form XX.YY")
513set(LIBCLANG_LIBRARY_VERSION
514 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
515 "Version number that will be placed into the libclang library , in the form XX.YY")
516mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
517
518option(CLANG_INCLUDE_TESTS
519 "Generate build targets for the Clang unit tests."
520 ${LLVM_INCLUDE_TESTS})
521
522add_subdirectory(utils/TableGen)
523
524add_subdirectory(include)
525
526# All targets below may depend on all tablegen'd files.
527get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
528list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
529
530add_subdirectory(lib)
531add_subdirectory(tools)
532add_subdirectory(runtime)
533
534option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
535if (CLANG_BUILD_EXAMPLES)
536 set(ENABLE_CLANG_EXAMPLES "1")
537else()
538 set(ENABLE_CLANG_EXAMPLES "0")
539endif()
540add_subdirectory(examples)
541
542if( CLANG_INCLUDE_TESTS )
543 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
544 add_subdirectory(unittests)
545 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
546 list(APPEND CLANG_TEST_PARAMS
547 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
548 )
549 endif()
550 add_subdirectory(test)
551
552 if(CLANG_BUILT_STANDALONE)
553 # Add a global check rule now that all subdirectories have been traversed
554 # and we know the total set of lit testsuites.
555 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
556 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
557 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
558 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
559 add_lit_target(check-all
560 "Running all regression tests"
561 ${LLVM_LIT_TESTSUITES}
562 PARAMS ${LLVM_LIT_PARAMS}
563 DEPENDS ${LLVM_LIT_DEPENDS}
564 ARGS ${LLVM_LIT_EXTRA_ARGS}
565 )
566 endif()
567 add_subdirectory(utils/perf-training)
568endif()
569
570option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
571 ${LLVM_INCLUDE_DOCS})
572if( CLANG_INCLUDE_DOCS )
573 add_subdirectory(docs)
574endif()
575
Mike Spertuse9f15b42016-03-28 18:24:22 +0000576
Chris Bieneman834a40b2016-04-08 22:48:18 +0000577if(APPLE)
578 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
579 # default value get updated to the new default.
580 if(CLANG_ORDER_FILE STREQUAL "")
581 unset(CLANG_ORDER_FILE CACHE)
582 unset(CLANG_ORDER_FILE)
583 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000584
Chris Bieneman834a40b2016-04-08 22:48:18 +0000585
586 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
587 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
588
589 if(CLANG_ORDER_FILE AND NOT EXISTS ${CLANG_ORDER_FILE})
590 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
591 if(PATH_START EQUAL 0)
592 file(WRITE ${CLANG_ORDER_FILE} "\n")
593 else()
594 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
595 endif()
596 endif()
597endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000598
Michael Gottesmanfe9d2d82016-06-29 20:22:44 +0000599add_subdirectory(cmake/modules)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000600
601if (CLANG_ENABLE_BOOTSTRAP)
602 include(ExternalProject)
603
Mike Spertuse9f15b42016-03-28 18:24:22 +0000604 if(NOT CLANG_STAGE)
605 set(CLANG_STAGE stage1)
606 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
607 endif()
608
609 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
610 if(MATCHED_STAGE)
611 if(NOT LLVM_BUILD_INSTRUMENTED)
612 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
613 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
614 else()
615 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
616 endif()
617 else()
618 set(NEXT_CLANG_STAGE bootstrap)
619 endif()
620
621 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
622 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
623 endif()
624 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
625
626
627 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
628 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
Chris Bieneman31046052016-04-27 18:52:48 +0000629 set(cmake_command ${CMAKE_COMMAND})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000630
Chris Bieneman31046052016-04-27 18:52:48 +0000631 # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
Mike Spertuse9f15b42016-03-28 18:24:22 +0000632 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
Chris Bieneman31046052016-04-27 18:52:48 +0000633 set(LTO_DEP LTO)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000634 if(APPLE)
Chris Bieneman31046052016-04-27 18:52:48 +0000635 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
636 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
637 # so that the host object file tools will use the just-built libLTO.
Mike Spertuse9f15b42016-03-28 18:24:22 +0000638 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
Chris Bieneman31046052016-04-27 18:52:48 +0000639 set(cmake_command ${CMAKE_COMMAND} -E env DYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR} ${CMAKE_COMMAND})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000640 elseif(NOT WIN32)
Chris Bieneman31046052016-04-27 18:52:48 +0000641 list(APPEND LTO_DEP LLVMgold llvm-ar llvm-ranlib)
642 set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
643 set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000644 endif()
645 endif()
646
647 add_custom_target(${NEXT_CLANG_STAGE}-clear
648 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
649 )
650 add_custom_command(
651 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
652 DEPENDS clang ${LTO_DEP}
653 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
654 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
655 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
656 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
657 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
658 )
659
660 if(CMAKE_VERBOSE_MAKEFILE)
661 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
662 endif()
663
664 set(BOOTSTRAP_DEFAULT_PASSTHROUGH
665 PACKAGE_VERSION
666 LLVM_VERSION_MAJOR
667 LLVM_VERSION_MINOR
668 LLVM_VERSION_PATCH
669 LLVM_VERSION_SUFFIX
670 LLVM_BINUTILS_INCDIR
671 CLANG_REPOSITORY_STRING
672 CMAKE_MAKE_PROGRAM)
673
674 if(TARGET compiler-rt)
675 set(RUNTIME_DEP compiler-rt)
676 endif()
677
678 set(COMPILER_OPTIONS
679 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
680 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
681 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
682
683 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
684 set(PGO_DEP llvm-profdata)
685 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
686 endif()
687
688 if(LLVM_BUILD_INSTRUMENTED)
689 set(PGO_DEP generate-profdata)
690 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
691 set(COMPILER_OPTIONS
692 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
693 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
694 -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER})
695 set(RUNTIME_DEP) # Don't set runtime dependencies
696 endif()
697
698 # Find all variables that start with BOOTSTRAP_ and populate a variable with
699 # them.
700 get_cmake_property(variableNames VARIABLES)
701 foreach(variableName ${variableNames})
702 if(variableName MATCHES "^BOOTSTRAP_")
703 string(SUBSTRING ${variableName} 10 -1 varName)
704 string(REPLACE ";" "\;" value "${${variableName}}")
705 list(APPEND PASSTHROUGH_VARIABLES
706 -D${varName}=${value})
707 endif()
708 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
709 list(APPEND PASSTHROUGH_VARIABLES
710 -D${variableName}=${${variableName}})
711 endif()
712 endforeach()
713
714 # Populate the passthrough variables
715 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${BOOTSTRAP_DEFAULT_PASSTHROUGH})
716 if(${variableName})
717 string(REPLACE ";" "\;" value ${${variableName}})
718 list(APPEND PASSTHROUGH_VARIABLES
719 -D${variableName}=${value})
720 endif()
721 endforeach()
722
723 ExternalProject_Add(${NEXT_CLANG_STAGE}
724 DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP} ${PGO_DEP}
725 PREFIX ${NEXT_CLANG_STAGE}
726 SOURCE_DIR ${CMAKE_SOURCE_DIR}
727 STAMP_DIR ${STAMP_DIR}
728 BINARY_DIR ${BINARY_DIR}
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000729 EXCLUDE_FROM_ALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000730 CMAKE_ARGS
731 # We shouldn't need to set this here, but INSTALL_DIR doesn't
732 # seem to work, so instead I'm passing this through
733 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
734 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
735 ${PASSTHROUGH_VARIABLES}
736 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
737 ${COMPILER_OPTIONS}
738 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
Chris Bieneman31046052016-04-27 18:52:48 +0000739 CMAKE_COMMAND ${cmake_command}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000740 INSTALL_COMMAND ""
741 STEP_TARGETS configure build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000742 USES_TERMINAL_CONFIGURE 1
743 USES_TERMINAL_BUILD 1
744 USES_TERMINAL_INSTALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000745 )
746
747 # exclude really-install from main target
748 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
749 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
Chris Bieneman31046052016-04-27 18:52:48 +0000750 COMMAND ${cmake_command} --build <BINARY_DIR> --target install
Mike Spertuse9f15b42016-03-28 18:24:22 +0000751 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
752 DEPENDEES build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000753 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000754 )
755 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
756 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
757
758 if(NOT CLANG_BOOTSTRAP_TARGETS)
759 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
760 endif()
761 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
762 # exclude from main target
763 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
764
765 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
Chris Bieneman31046052016-04-27 18:52:48 +0000766 COMMAND ${cmake_command} --build <BINARY_DIR> --target ${target}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000767 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
768 DEPENDEES configure
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000769 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000770 )
771
772 if(target MATCHES "^stage[0-9]*")
773 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
774 endif()
775
776 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
777 endforeach()
778endif()
779
780if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
781 add_subdirectory(utils/ClangVisualizers)
782endif()