]> granicus.if.org Git - clang/blob - CMakeLists.txt
[CMake] Cleaning up CMake version checks in ExternalProject calls
[clang] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.4.3)
2
3 # If we are not building as a part of LLVM, build Clang as an
4 # standalone project, using LLVM as an external library:
5 if( 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
112 Please 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}")
167 else()
168   set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
169 endif()
170
171 find_package(LibXml2 2.5.3 QUIET)
172 if (LIBXML2_FOUND)
173   set(CLANG_HAVE_LIBXML 1)
174 endif()
175
176 set(CLANG_RESOURCE_DIR "" CACHE STRING
177   "Relative directory from the Clang binary to its resource files.")
178
179 set(C_INCLUDE_DIRS "" CACHE STRING
180   "Colon separated list of directories clang will search for headers.")
181
182 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
183 set(DEFAULT_SYSROOT "" CACHE PATH
184   "Default <path> to all compiler invocations for --sysroot=<path>." )
185
186 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
187
188 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
189   "Default C++ stdlib to use (empty for architecture default, \"libstdc++\" or \"libc++\"")
190 if (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 "")
195 endif()
196
197 set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
198   "Default OpenMP runtime used by -fopenmp.")
199
200 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
201   "Vendor-specific text for showing with version information.")
202
203 if( CLANG_VENDOR )
204   add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
205 endif()
206
207 set(CLANG_REPOSITORY_STRING "" CACHE STRING
208   "Vendor-specific text for showing the repository the source is taken from.")
209
210 if(CLANG_REPOSITORY_STRING)
211   add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
212 endif()
213
214 option(CLANG_APPEND_VC_REV
215   "Append the version control system revision id to clang version spew" OFF)
216 if(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()
225 endif()
226
227 set(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.
231 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
232
233 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
234 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
235
236 if( 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.")
242 endif()
243
244 if( 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()
254 endif()
255
256 # Compute the Clang version from the LLVM version.
257 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
258   ${PACKAGE_VERSION})
259 message(STATUS "Clang version: ${CLANG_VERSION}")
260
261 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
262   ${CLANG_VERSION})
263 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
264   ${CLANG_VERSION})
265 if (${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})
269 else()
270   set(CLANG_HAS_VERSION_PATCHLEVEL 0)
271 endif()
272
273 # Configure the Version.inc file.
274 configure_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
279 if (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()
294 endif ()
295
296 # Determine HOST_LINK_VERSION on Darwin.
297 set(HOST_LINK_VERSION)
298 if (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()
314 endif()
315
316 configure_file(
317   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
318   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
319
320 include(CMakeParseArguments)
321
322 function(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()
349 endfunction(clang_tablegen)
350
351 macro(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()
360 endmacro()
361
362 macro(add_clang_subdirectory name)
363   add_llvm_subdirectory(CLANG TOOL ${name})
364 endmacro()
365
366 macro(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})
410     target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
411
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})
436 endmacro(add_clang_library)
437
438 macro(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})
442 endmacro(add_clang_executable)
443
444 macro(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()
457 endmacro()
458
459 macro(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)
463 endmacro()
464
465 set(CMAKE_INCLUDE_CURRENT_DIR ON)
466
467 include_directories(BEFORE
468   ${CMAKE_CURRENT_BINARY_DIR}/include
469   ${CMAKE_CURRENT_SOURCE_DIR}/include
470   )
471
472 if (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     )
489 endif()
490
491 add_definitions( -D_GNU_SOURCE )
492
493 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
494 if (CLANG_ENABLE_ARCMT)
495   set(ENABLE_CLANG_ARCMT "1")
496 else()
497   set(ENABLE_CLANG_ARCMT "0")
498 endif()
499
500 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
501 if (CLANG_ENABLE_STATIC_ANALYZER)
502   set(ENABLE_CLANG_STATIC_ANALYZER "1")
503 else()
504   set(ENABLE_CLANG_STATIC_ANALYZER "0")
505 endif()
506
507 if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
508   message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
509 endif()
510
511 if(CLANG_ENABLE_ARCMT)
512   add_definitions(-DCLANG_ENABLE_ARCMT)
513   add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
514 endif()
515 if(CLANG_ENABLE_STATIC_ANALYZER)
516   add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
517 endif()
518
519 # Clang version information
520 set(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")
523 set(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")
526 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
527
528 option(CLANG_INCLUDE_TESTS
529        "Generate build targets for the Clang unit tests."
530        ${LLVM_INCLUDE_TESTS})
531
532 add_subdirectory(utils/TableGen)
533
534 add_subdirectory(include)
535
536 # All targets below may depend on all tablegen'd files.
537 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
538 list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
539
540 add_subdirectory(lib)
541 add_subdirectory(tools)
542 add_subdirectory(runtime)
543
544 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
545 if (CLANG_BUILD_EXAMPLES)
546   set(ENABLE_CLANG_EXAMPLES "1")
547 else()
548   set(ENABLE_CLANG_EXAMPLES "0")
549 endif()
550 add_subdirectory(examples)
551
552 if( 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)
578 endif()
579
580 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
581   ${LLVM_INCLUDE_DOCS})
582 if( CLANG_INCLUDE_DOCS )
583   add_subdirectory(docs)
584 endif()
585
586
587 if(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()
594
595
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()
607 endif()
608
609 # 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.
612 set(CLANG_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/clang)
613 set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
614 get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
615 export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
616
617 # Install a <prefix>/lib/cmake/clang/ClangConfig.cmake file so that
618 # find_package(Clang) works. Install the target list with it.
619 install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR})
620
621 install(FILES
622   ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
623   DESTINATION lib${LLVM_LIBDIR_SUFFIX}/cmake/clang)
624
625 # Also copy ClangConfig.cmake to the build directory so that dependent projects
626 # can build against a build directory of Clang more easily.
627 configure_file(
628   ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
629   ${CLANG_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang/ClangConfig.cmake
630   COPYONLY)
631
632 if (CLANG_ENABLE_BOOTSTRAP)
633   include(ExternalProject)
634
635   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/)
660   set(cmake_command ${CMAKE_COMMAND})
661
662   # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
663   if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
664     set(LTO_DEP LTO)
665     if(APPLE)
666       # 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.
669       set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
670       set(cmake_command ${CMAKE_COMMAND} -E env DYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR} ${CMAKE_COMMAND})
671     elseif(NOT WIN32)
672       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)
675     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}
760     EXCLUDE_FROM_ALL 1
761     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}
770     CMAKE_COMMAND ${cmake_command}
771     INSTALL_COMMAND ""
772     STEP_TARGETS configure build
773     USES_TERMINAL_CONFIGURE 1
774     USES_TERMINAL_BUILD 1
775     USES_TERMINAL_INSTALL 1
776     )
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
781     COMMAND ${cmake_command} --build <BINARY_DIR> --target install
782     COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
783     DEPENDEES build
784     USES_TERMINAL 1
785   )
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}
797       COMMAND ${cmake_command} --build <BINARY_DIR> --target ${target}
798       COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
799       DEPENDEES configure
800       USES_TERMINAL 1
801     )
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()
809 endif()
810
811 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
812   add_subdirectory(utils/ClangVisualizers)
813 endif()