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