blob: fce82295508bb4f7ea0ba9bd706fbdedbc6899bc [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
3# FIXME: It may be removed when we use 2.8.12.
4if(CMAKE_VERSION VERSION_LESS 2.8.12)
5 # Invalidate a couple of keywords.
6 set(cmake_2_8_12_INTERFACE)
7 set(cmake_2_8_12_PRIVATE)
8else()
9 # Use ${cmake_2_8_12_KEYWORD} intead of KEYWORD in target_link_libraries().
10 set(cmake_2_8_12_INTERFACE INTERFACE)
11 set(cmake_2_8_12_PRIVATE PRIVATE)
12 if(POLICY CMP0022)
13 cmake_policy(SET CMP0022 NEW) # automatic when 2.8.12 is required
14 endif()
15endif()
16
17# If we are not building as a part of LLVM, build Clang as an
18# standalone project, using LLVM as an external library:
19if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
20 project(Clang)
21
22 # Rely on llvm-config.
23 set(CONFIG_OUTPUT)
24 find_program(LLVM_CONFIG "llvm-config")
25 if(LLVM_CONFIG)
26 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
27 set(CONFIG_COMMAND ${LLVM_CONFIG}
28 "--assertion-mode"
29 "--bindir"
30 "--libdir"
31 "--includedir"
32 "--prefix"
33 "--src-root")
34 execute_process(
35 COMMAND ${CONFIG_COMMAND}
36 RESULT_VARIABLE HAD_ERROR
37 OUTPUT_VARIABLE CONFIG_OUTPUT
38 )
39 if(NOT HAD_ERROR)
40 string(REGEX REPLACE
41 "[ \t]*[\r\n]+[ \t]*" ";"
42 CONFIG_OUTPUT ${CONFIG_OUTPUT})
43 else()
44 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
45 message(STATUS "${CONFIG_COMMAND_STR}")
46 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
47 endif()
48 else()
49 message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
50 endif()
51
52 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
53 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
54 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
55 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
56 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
57 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
58
59 if(NOT MSVC_IDE)
60 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
61 CACHE BOOL "Enable assertions")
62 # Assertions should follow llvm-config's.
63 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
64 endif()
65
66 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
67 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
68 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
69 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
70 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
71
72 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
73 NO_DEFAULT_PATH)
74
75 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
76 set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
77 if(EXISTS ${LLVMCONFIG_FILE})
78 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
79 include(${LLVMCONFIG_FILE})
80 else()
81 message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
82 endif()
83
84 # They are used as destination of target generators.
85 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
86 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
87 if(WIN32 OR CYGWIN)
88 # DLL platform -- put DLLs into bin.
89 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
90 else()
91 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
92 endif()
93
94 option(LLVM_INSTALL_TOOLCHAIN_ONLY
95 "Only include toolchain files in the 'install' target." OFF)
96
97 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
98 "Set to ON to force using an old, unsupported host toolchain." OFF)
99 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
100
101 include(AddLLVM)
102 include(TableGen)
103 include(HandleLLVMOptions)
104 include(VersionFromVCS)
105
106 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
107
108 if (NOT DEFINED LLVM_INCLUDE_TESTS)
109 set(LLVM_INCLUDE_TESTS ON)
110 endif()
111
112 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
113 link_directories("${LLVM_LIBRARY_DIR}")
114
115 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
116 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
117 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
118
119 if(LLVM_INCLUDE_TESTS)
120 set(Python_ADDITIONAL_VERSIONS 2.7)
121 include(FindPythonInterp)
122 if(NOT PYTHONINTERP_FOUND)
123 message(FATAL_ERROR
124"Unable to find Python interpreter, required for builds and testing.
125
126Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
127 endif()
128
129 if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
130 message(FATAL_ERROR "Python 2.7 or newer is required")
131 endif()
132
133 # Check prebuilt llvm/utils.
134 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
135 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
136 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
137 set(LLVM_UTILS_PROVIDED ON)
138 endif()
139
140 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
141 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
142 if(NOT LLVM_UTILS_PROVIDED)
143 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
144 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
145 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
146 set(LLVM_UTILS_PROVIDED ON)
147 set(CLANG_TEST_DEPS FileCheck count not)
148 endif()
149 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
150 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
151 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
152 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
153 add_subdirectory(${UNITTEST_DIR} utils/unittest)
154 endif()
155 else()
156 # Seek installed Lit.
157 find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
158 DOC "Path to lit.py")
159 endif()
160
161 if(LLVM_LIT)
162 # Define the default arguments to use with 'lit', and an option for the user
163 # to override.
164 set(LIT_ARGS_DEFAULT "-sv")
165 if (MSVC OR XCODE)
166 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
167 endif()
168 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
169
170 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
171 if( WIN32 AND NOT CYGWIN )
172 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
173 endif()
174 else()
175 set(LLVM_INCLUDE_TESTS OFF)
176 endif()
177 endif()
178
179 set( CLANG_BUILT_STANDALONE 1 )
180 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
181else()
182 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
183endif()
184
185find_package(LibXml2 2.5.3 QUIET)
186if (LIBXML2_FOUND)
187 set(CLANG_HAVE_LIBXML 1)
188endif()
189
190set(CLANG_RESOURCE_DIR "" CACHE STRING
191 "Relative directory from the Clang binary to its resource files.")
192
193set(C_INCLUDE_DIRS "" CACHE STRING
194 "Colon separated list of directories clang will search for headers.")
195
196set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
197set(DEFAULT_SYSROOT "" CACHE PATH
198 "Default <path> to all compiler invocations for --sysroot=<path>." )
199
Rafael Espindola5ed89d42016-06-03 17:26:16 +0000200set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
201
Mike Spertuse9f15b42016-03-28 18:24:22 +0000202set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
203 "Default C++ stdlib to use (empty for architecture default, \"libstdc++\" or \"libc++\"")
204if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
205 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
206 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
207 message(WARNING "Resetting default C++ stdlib to use architecture default")
208 set(CLANG_DEFAULT_CXX_STDLIB "")
209endif()
210
211set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
212 "Default OpenMP runtime used by -fopenmp.")
213
214set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
215 "Vendor-specific text for showing with version information.")
216
217if( CLANG_VENDOR )
218 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
219endif()
220
221set(CLANG_REPOSITORY_STRING "" CACHE STRING
222 "Vendor-specific text for showing the repository the source is taken from.")
223
224if(CLANG_REPOSITORY_STRING)
225 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
226endif()
227
228option(CLANG_APPEND_VC_REV
229 "Append the version control system revision id to clang version spew" OFF)
230if(CLANG_APPEND_VC_REV)
231 if(NOT SVN_REVISION)
232 # This macro will set SVN_REVISION in the parent scope
233 add_version_info_from_vcs(VERSION_VAR)
234 endif()
235
236 if(SVN_REVISION)
237 add_definitions(-DSVN_REVISION="${SVN_REVISION}")
238 endif()
239endif()
240
241set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
242 "Vendor-specific uti.")
243
244# The libdir suffix must exactly match whatever LLVM's configuration used.
245set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
246
247set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
248set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
249
250if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
251 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
252"the makefiles distributed with LLVM. Please create a directory and run cmake "
253"from there, passing the path to this source directory as the last argument. "
254"This process created the file `CMakeCache.txt' and the directory "
255"`CMakeFiles'. Please delete them.")
256endif()
257
258if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
259 file(GLOB_RECURSE
260 tablegenned_files_on_include_dir
261 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
262 if( tablegenned_files_on_include_dir )
263 message(FATAL_ERROR "Apparently there is a previous in-source build, "
264"probably as the result of running `configure' and `make' on "
265"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
266"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
267 endif()
268endif()
269
270# Compute the Clang version from the LLVM version.
271string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
272 ${PACKAGE_VERSION})
273message(STATUS "Clang version: ${CLANG_VERSION}")
274
275string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
276 ${CLANG_VERSION})
277string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
278 ${CLANG_VERSION})
279if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
280 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
281 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
282 ${CLANG_VERSION})
283else()
284 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
285endif()
286
287# Configure the Version.inc file.
288configure_file(
289 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
290 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
291
292# Add appropriate flags for GCC
293if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
294 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
295 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
296 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
297 endif ()
298
299 # Enable -pedantic for Clang even if it's not enabled for LLVM.
300 if (NOT LLVM_ENABLE_PEDANTIC)
301 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
302 endif ()
303
304 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
305 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
306 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
307 endif()
308endif ()
309
310# Determine HOST_LINK_VERSION on Darwin.
311set(HOST_LINK_VERSION)
312if (APPLE)
313 set(LD_V_OUTPUT)
314 execute_process(
315 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
316 RESULT_VARIABLE HAD_ERROR
317 OUTPUT_VARIABLE LD_V_OUTPUT
318 )
319 if (NOT HAD_ERROR)
320 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
321 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
322 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
323 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
324 endif()
325 else()
326 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
327 endif()
328endif()
329
330configure_file(
331 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
332 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
333
334include(CMakeParseArguments)
335
336function(clang_tablegen)
337 # Syntax:
338 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
339 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
340 #
341 # Generates a custom command for invoking tblgen as
342 #
343 # tblgen source-file -o=output-file tablegen-arg ...
344 #
345 # and, if cmake-target-name is provided, creates a custom target for
346 # executing the custom command depending on output-file. It is
347 # possible to list more files to depend after DEPENDS.
348
349 cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
350
351 if( NOT CTG_SOURCE )
352 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
353 endif()
354
355 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
356 tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS})
357
358 if(CTG_TARGET)
359 add_public_tablegen_target(${CTG_TARGET})
360 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
361 set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
362 endif()
363endfunction(clang_tablegen)
364
365macro(set_clang_windows_version_resource_properties name)
366 if(DEFINED windows_resource_file)
367 set_windows_version_resource_properties(${name} ${windows_resource_file}
368 VERSION_MAJOR ${CLANG_VERSION_MAJOR}
369 VERSION_MINOR ${CLANG_VERSION_MINOR}
370 VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
371 VERSION_STRING "${CLANG_VERSION} (${BACKEND_PACKAGE_STRING})"
372 PRODUCT_NAME "clang")
373 endif()
374endmacro()
375
376macro(add_clang_subdirectory name)
377 add_llvm_subdirectory(CLANG TOOL ${name})
378endmacro()
379
380macro(add_clang_library name)
381 cmake_parse_arguments(ARG
382 "SHARED"
383 ""
384 "ADDITIONAL_HEADERS"
385 ${ARGN})
386 set(srcs)
387 if(MSVC_IDE OR XCODE)
388 # Add public headers
389 file(RELATIVE_PATH lib_path
390 ${CLANG_SOURCE_DIR}/lib/
391 ${CMAKE_CURRENT_SOURCE_DIR}
392 )
393 if(NOT lib_path MATCHES "^[.][.]")
394 file( GLOB_RECURSE headers
395 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
396 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
397 )
398 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
399
400 file( GLOB_RECURSE tds
401 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
402 )
403 source_group("TableGen descriptions" FILES ${tds})
404 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
405
406 if(headers OR tds)
407 set(srcs ${headers} ${tds})
408 endif()
409 endif()
410 endif(MSVC_IDE OR XCODE)
411 if(srcs OR ARG_ADDITIONAL_HEADERS)
412 set(srcs
413 ADDITIONAL_HEADERS
414 ${srcs}
415 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
416 )
417 endif()
418 if(ARG_SHARED)
419 set(ARG_ENABLE_SHARED SHARED)
420 endif()
421 llvm_add_library(${name} ${ARG_ENABLE_SHARED} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
422
423 if(TARGET ${name})
424 target_link_libraries(${name} ${cmake_2_8_12_INTERFACE} ${LLVM_COMMON_LIBS})
425
426 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
427 install(TARGETS ${name}
428 COMPONENT ${name}
429 EXPORT ClangTargets
430 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
431 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
432 RUNTIME DESTINATION bin)
433
434 if (${ARG_SHARED} AND NOT CMAKE_CONFIGURATION_TYPES)
435 add_custom_target(install-${name}
436 DEPENDS ${name}
437 COMMAND "${CMAKE_COMMAND}"
438 -DCMAKE_INSTALL_COMPONENT=${name}
439 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
440 endif()
441 endif()
442 set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
443 else()
444 # Add empty "phony" target
445 add_custom_target(${name})
446 endif()
447
448 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
449 set_clang_windows_version_resource_properties(${name})
450endmacro(add_clang_library)
451
452macro(add_clang_executable name)
453 add_llvm_executable( ${name} ${ARGN} )
454 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
455 set_clang_windows_version_resource_properties(${name})
456endmacro(add_clang_executable)
457
458macro(add_clang_tool name)
459 add_clang_executable(${name} ${ARGN})
460 install(TARGETS ${name}
461 RUNTIME DESTINATION bin
462 COMPONENT ${name})
463
464 if(NOT CMAKE_CONFIGURATION_TYPES)
465 add_custom_target(install-${name}
466 DEPENDS ${name}
467 COMMAND "${CMAKE_COMMAND}"
468 -DCMAKE_INSTALL_COMPONENT=${name}
469 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
470 endif()
471endmacro()
472
473macro(add_clang_symlink name dest)
474 add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
475 # Always generate install targets
476 llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
477endmacro()
478
479set(CMAKE_INCLUDE_CURRENT_DIR ON)
480
481include_directories(BEFORE
482 ${CMAKE_CURRENT_BINARY_DIR}/include
483 ${CMAKE_CURRENT_SOURCE_DIR}/include
484 )
485
486if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
487 install(DIRECTORY include/clang include/clang-c
488 DESTINATION include
489 FILES_MATCHING
490 PATTERN "*.def"
491 PATTERN "*.h"
492 PATTERN "config.h" EXCLUDE
493 PATTERN ".svn" EXCLUDE
494 )
495
496 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
497 DESTINATION include
498 FILES_MATCHING
499 PATTERN "CMakeFiles" EXCLUDE
500 PATTERN "*.inc"
501 PATTERN "*.h"
502 )
503endif()
504
505add_definitions( -D_GNU_SOURCE )
506
507option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
508if (CLANG_ENABLE_ARCMT)
509 set(ENABLE_CLANG_ARCMT "1")
510else()
511 set(ENABLE_CLANG_ARCMT "0")
512endif()
513
514option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
515if (CLANG_ENABLE_STATIC_ANALYZER)
516 set(ENABLE_CLANG_STATIC_ANALYZER "1")
517else()
518 set(ENABLE_CLANG_STATIC_ANALYZER "0")
519endif()
520
521if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
522 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
523endif()
524
525if(CLANG_ENABLE_ARCMT)
526 add_definitions(-DCLANG_ENABLE_ARCMT)
527 add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
528endif()
529if(CLANG_ENABLE_STATIC_ANALYZER)
530 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
531endif()
532
533# Clang version information
534set(CLANG_EXECUTABLE_VERSION
535 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
536 "Version number that will be placed into the clang executable, in the form XX.YY")
537set(LIBCLANG_LIBRARY_VERSION
538 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
539 "Version number that will be placed into the libclang library , in the form XX.YY")
540mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
541
542option(CLANG_INCLUDE_TESTS
543 "Generate build targets for the Clang unit tests."
544 ${LLVM_INCLUDE_TESTS})
545
546add_subdirectory(utils/TableGen)
547
548add_subdirectory(include)
549
550# All targets below may depend on all tablegen'd files.
551get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
552list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
553
554add_subdirectory(lib)
555add_subdirectory(tools)
556add_subdirectory(runtime)
557
558option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
559if (CLANG_BUILD_EXAMPLES)
560 set(ENABLE_CLANG_EXAMPLES "1")
561else()
562 set(ENABLE_CLANG_EXAMPLES "0")
563endif()
564add_subdirectory(examples)
565
566if( CLANG_INCLUDE_TESTS )
567 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
568 add_subdirectory(unittests)
569 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
570 list(APPEND CLANG_TEST_PARAMS
571 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
572 )
573 endif()
574 add_subdirectory(test)
575
576 if(CLANG_BUILT_STANDALONE)
577 # Add a global check rule now that all subdirectories have been traversed
578 # and we know the total set of lit testsuites.
579 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
580 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
581 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
582 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
583 add_lit_target(check-all
584 "Running all regression tests"
585 ${LLVM_LIT_TESTSUITES}
586 PARAMS ${LLVM_LIT_PARAMS}
587 DEPENDS ${LLVM_LIT_DEPENDS}
588 ARGS ${LLVM_LIT_EXTRA_ARGS}
589 )
590 endif()
591 add_subdirectory(utils/perf-training)
592endif()
593
594option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
595 ${LLVM_INCLUDE_DOCS})
596if( CLANG_INCLUDE_DOCS )
597 add_subdirectory(docs)
598endif()
599
Mike Spertuse9f15b42016-03-28 18:24:22 +0000600
Chris Bieneman834a40b2016-04-08 22:48:18 +0000601if(APPLE)
602 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
603 # default value get updated to the new default.
604 if(CLANG_ORDER_FILE STREQUAL "")
605 unset(CLANG_ORDER_FILE CACHE)
606 unset(CLANG_ORDER_FILE)
607 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000608
Chris Bieneman834a40b2016-04-08 22:48:18 +0000609
610 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
611 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
612
613 if(CLANG_ORDER_FILE AND NOT EXISTS ${CLANG_ORDER_FILE})
614 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
615 if(PATH_START EQUAL 0)
616 file(WRITE ${CLANG_ORDER_FILE} "\n")
617 else()
618 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
619 endif()
620 endif()
621endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000622
623if (CLANG_BUILT_STANDALONE OR CMAKE_VERSION VERSION_EQUAL 3 OR
624 CMAKE_VERSION VERSION_GREATER 3)
625 # Generate a list of CMake library targets so that other CMake projects can
626 # link against them. LLVM calls its version of this file LLVMExports.cmake, but
627 # the usual CMake convention seems to be ${Project}Targets.cmake.
628 set(CLANG_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/clang)
629 set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
630 get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
631 export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
632
633 # Install a <prefix>/lib/cmake/clang/ClangConfig.cmake file so that
634 # find_package(Clang) works. Install the target list with it.
635 install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR})
636
637 install(FILES
638 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
639 DESTINATION lib${LLVM_LIBDIR_SUFFIX}/cmake/clang)
640
641 # Also copy ClangConfig.cmake to the build directory so that dependent projects
642 # can build against a build directory of Clang more easily.
643 configure_file(
644 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
645 ${CLANG_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang/ClangConfig.cmake
646 COPYONLY)
647endif ()
648
649if (CLANG_ENABLE_BOOTSTRAP)
650 include(ExternalProject)
651
652 if(CMAKE_VERSION VERSION_GREATER 3.1.0)
653 set(cmake_3_1_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL 1)
654 endif()
655
656 if(CMAKE_VERSION VERSION_GREATER 3.3.20150708)
657 set(cmake_3_4_USES_TERMINAL_OPTIONS
658 USES_TERMINAL_CONFIGURE 1
659 USES_TERMINAL_BUILD 1
660 USES_TERMINAL_INSTALL 1
661 )
662 set(cmake_3_4_USES_TERMINAL USES_TERMINAL 1)
663 endif()
664
665 if(NOT CLANG_STAGE)
666 set(CLANG_STAGE stage1)
667 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
668 endif()
669
670 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
671 if(MATCHED_STAGE)
672 if(NOT LLVM_BUILD_INSTRUMENTED)
673 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
674 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
675 else()
676 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
677 endif()
678 else()
679 set(NEXT_CLANG_STAGE bootstrap)
680 endif()
681
682 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
683 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
684 endif()
685 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
686
687
688 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
689 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
Chris Bieneman31046052016-04-27 18:52:48 +0000690 set(cmake_command ${CMAKE_COMMAND})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000691
Chris Bieneman31046052016-04-27 18:52:48 +0000692 # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
Mike Spertuse9f15b42016-03-28 18:24:22 +0000693 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
Chris Bieneman31046052016-04-27 18:52:48 +0000694 set(LTO_DEP LTO)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000695 if(APPLE)
Chris Bieneman31046052016-04-27 18:52:48 +0000696 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
697 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
698 # so that the host object file tools will use the just-built libLTO.
Mike Spertuse9f15b42016-03-28 18:24:22 +0000699 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
Chris Bieneman31046052016-04-27 18:52:48 +0000700 set(cmake_command ${CMAKE_COMMAND} -E env DYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR} ${CMAKE_COMMAND})
Mike Spertuse9f15b42016-03-28 18:24:22 +0000701 elseif(NOT WIN32)
Chris Bieneman31046052016-04-27 18:52:48 +0000702 list(APPEND LTO_DEP LLVMgold llvm-ar llvm-ranlib)
703 set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
704 set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000705 endif()
706 endif()
707
708 add_custom_target(${NEXT_CLANG_STAGE}-clear
709 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
710 )
711 add_custom_command(
712 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
713 DEPENDS clang ${LTO_DEP}
714 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
715 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
716 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
717 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
718 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
719 )
720
721 if(CMAKE_VERBOSE_MAKEFILE)
722 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
723 endif()
724
725 set(BOOTSTRAP_DEFAULT_PASSTHROUGH
726 PACKAGE_VERSION
727 LLVM_VERSION_MAJOR
728 LLVM_VERSION_MINOR
729 LLVM_VERSION_PATCH
730 LLVM_VERSION_SUFFIX
731 LLVM_BINUTILS_INCDIR
732 CLANG_REPOSITORY_STRING
733 CMAKE_MAKE_PROGRAM)
734
735 if(TARGET compiler-rt)
736 set(RUNTIME_DEP compiler-rt)
737 endif()
738
739 set(COMPILER_OPTIONS
740 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
741 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
742 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
743
744 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
745 set(PGO_DEP llvm-profdata)
746 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
747 endif()
748
749 if(LLVM_BUILD_INSTRUMENTED)
750 set(PGO_DEP generate-profdata)
751 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
752 set(COMPILER_OPTIONS
753 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
754 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
755 -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER})
756 set(RUNTIME_DEP) # Don't set runtime dependencies
757 endif()
758
759 # Find all variables that start with BOOTSTRAP_ and populate a variable with
760 # them.
761 get_cmake_property(variableNames VARIABLES)
762 foreach(variableName ${variableNames})
763 if(variableName MATCHES "^BOOTSTRAP_")
764 string(SUBSTRING ${variableName} 10 -1 varName)
765 string(REPLACE ";" "\;" value "${${variableName}}")
766 list(APPEND PASSTHROUGH_VARIABLES
767 -D${varName}=${value})
768 endif()
769 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
770 list(APPEND PASSTHROUGH_VARIABLES
771 -D${variableName}=${${variableName}})
772 endif()
773 endforeach()
774
775 # Populate the passthrough variables
776 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${BOOTSTRAP_DEFAULT_PASSTHROUGH})
777 if(${variableName})
778 string(REPLACE ";" "\;" value ${${variableName}})
779 list(APPEND PASSTHROUGH_VARIABLES
780 -D${variableName}=${value})
781 endif()
782 endforeach()
783
784 ExternalProject_Add(${NEXT_CLANG_STAGE}
785 DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP} ${PGO_DEP}
786 PREFIX ${NEXT_CLANG_STAGE}
787 SOURCE_DIR ${CMAKE_SOURCE_DIR}
788 STAMP_DIR ${STAMP_DIR}
789 BINARY_DIR ${BINARY_DIR}
790 ${cmake_3_1_EXCLUDE_FROM_ALL}
791 CMAKE_ARGS
792 # We shouldn't need to set this here, but INSTALL_DIR doesn't
793 # seem to work, so instead I'm passing this through
794 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
795 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
796 ${PASSTHROUGH_VARIABLES}
797 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
798 ${COMPILER_OPTIONS}
799 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
Chris Bieneman31046052016-04-27 18:52:48 +0000800 CMAKE_COMMAND ${cmake_command}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000801 INSTALL_COMMAND ""
802 STEP_TARGETS configure build
803 ${cmake_3_4_USES_TERMINAL_OPTIONS}
804 )
805
806 # exclude really-install from main target
807 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
808 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
Chris Bieneman31046052016-04-27 18:52:48 +0000809 COMMAND ${cmake_command} --build <BINARY_DIR> --target install
Mike Spertuse9f15b42016-03-28 18:24:22 +0000810 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
811 DEPENDEES build
812 ${cmake_3_4_USES_TERMINAL}
813 )
814 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
815 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
816
817 if(NOT CLANG_BOOTSTRAP_TARGETS)
818 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
819 endif()
820 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
821 # exclude from main target
822 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
823
824 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
Chris Bieneman31046052016-04-27 18:52:48 +0000825 COMMAND ${cmake_command} --build <BINARY_DIR> --target ${target}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000826 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
827 DEPENDEES configure
828 ${cmake_3_4_USES_TERMINAL}
829 )
830
831 if(target MATCHES "^stage[0-9]*")
832 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
833 endif()
834
835 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
836 endforeach()
837endif()
838
839if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
840 add_subdirectory(utils/ClangVisualizers)
841endif()