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