]> granicus.if.org Git - clang/blob - CMakeLists.txt
[OPENMP 4.1] Initial support for 'simdlen' clause.
[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}/share/llvm/cmake")
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
105   set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
106
107   if (NOT DEFINED LLVM_INCLUDE_TESTS)
108     set(LLVM_INCLUDE_TESTS ON)
109   endif()
110
111   include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
112   link_directories("${LLVM_LIBRARY_DIR}")
113
114   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
115   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
116   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
117
118   if(LLVM_INCLUDE_TESTS)
119     set(Python_ADDITIONAL_VERSIONS 2.7)
120     include(FindPythonInterp)
121     if(NOT PYTHONINTERP_FOUND)
122       message(FATAL_ERROR
123 "Unable to find Python interpreter, required for builds and testing.
124
125 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
126     endif()
127
128     if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
129       message(FATAL_ERROR "Python 2.7 or newer is required")
130     endif()
131
132     # Check prebuilt llvm/utils.
133     if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
134         AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
135         AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
136       set(LLVM_UTILS_PROVIDED ON)
137     endif()
138
139     if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
140       set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
141       if(NOT LLVM_UTILS_PROVIDED)
142         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
143         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
144         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
145         set(LLVM_UTILS_PROVIDED ON)
146         set(CLANG_TEST_DEPS FileCheck count not)
147       endif()
148       set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
149       if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
150           AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
151           AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
152         add_subdirectory(${UNITTEST_DIR} utils/unittest)
153       endif()
154     else()
155       # Seek installed Lit.
156       find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
157         DOC "Path to lit.py")
158     endif()
159
160     if(LLVM_LIT)
161       # Define the default arguments to use with 'lit', and an option for the user
162       # to override.
163       set(LIT_ARGS_DEFAULT "-sv")
164       if (MSVC OR XCODE)
165         set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
166       endif()
167       set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
168
169       # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
170       if( WIN32 AND NOT CYGWIN )
171         set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
172       endif()
173     else()
174       set(LLVM_INCLUDE_TESTS OFF)
175     endif()
176   endif()
177
178   set( CLANG_BUILT_STANDALONE 1 )
179   set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
180 else()
181   set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
182 endif()
183
184 find_package(LibXml2 2.5.3 QUIET)
185 if (LIBXML2_FOUND)
186   set(CLANG_HAVE_LIBXML 1)
187 endif()
188
189 set(CLANG_RESOURCE_DIR "" CACHE STRING
190   "Relative directory from the Clang binary to its resource files.")
191
192 set(C_INCLUDE_DIRS "" CACHE STRING
193   "Colon separated list of directories clang will search for headers.")
194
195 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
196 set(DEFAULT_SYSROOT "" CACHE PATH
197   "Default <path> to all compiler invocations for --sysroot=<path>." )
198
199 set(CLANG_DEFAULT_OPENMP_RUNTIME "libgomp" CACHE STRING
200   "Default OpenMP runtime used by -fopenmp.")
201
202 set(CLANG_VENDOR "" CACHE STRING
203   "Vendor-specific text for showing with version information.")
204
205 if( CLANG_VENDOR )
206   add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
207 endif()
208
209 set(CLANG_REPOSITORY_STRING "" CACHE STRING
210   "Vendor-specific text for showing the repository the source is taken from.")
211
212 if(CLANG_REPOSITORY_STRING)
213   add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
214 endif()
215
216 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
217   "Vendor-specific uti.")
218
219 # The libdir suffix must exactly match whatever LLVM's configuration used.
220 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
221
222 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
223 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
224
225 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
226   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
227 "the makefiles distributed with LLVM. Please create a directory and run cmake "
228 "from there, passing the path to this source directory as the last argument. "
229 "This process created the file `CMakeCache.txt' and the directory "
230 "`CMakeFiles'. Please delete them.")
231 endif()
232
233 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
234   file(GLOB_RECURSE
235     tablegenned_files_on_include_dir
236     "${CLANG_SOURCE_DIR}/include/clang/*.inc")
237   if( tablegenned_files_on_include_dir )
238     message(FATAL_ERROR "Apparently there is a previous in-source build, "
239 "probably as the result of running `configure' and `make' on "
240 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
241 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
242   endif()
243 endif()
244
245 # Compute the Clang version from the LLVM version.
246 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
247   ${PACKAGE_VERSION})
248 message(STATUS "Clang version: ${CLANG_VERSION}")
249
250 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
251   ${CLANG_VERSION})
252 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
253   ${CLANG_VERSION})
254 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
255   set(CLANG_HAS_VERSION_PATCHLEVEL 1)
256   string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
257     ${CLANG_VERSION})
258 else()
259   set(CLANG_HAS_VERSION_PATCHLEVEL 0)
260 endif()
261
262 # Configure the Version.inc file.
263 configure_file(
264   ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
265   ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
266
267 # Add appropriate flags for GCC
268 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
269   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
270   if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
271     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
272   endif ()
273
274   # Enable -pedantic for Clang even if it's not enabled for LLVM.
275   if (NOT LLVM_ENABLE_PEDANTIC)
276     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
277   endif ()
278
279   check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
280   if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
281     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
282   endif()
283 endif ()
284
285 # Determine HOST_LINK_VERSION on Darwin.
286 set(HOST_LINK_VERSION)
287 if (APPLE)
288   set(LD_V_OUTPUT)
289   execute_process(
290     COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
291     RESULT_VARIABLE HAD_ERROR
292     OUTPUT_VARIABLE LD_V_OUTPUT
293   )
294   if (NOT HAD_ERROR)
295     if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
296       string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
297     elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
298       string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
299     endif()
300   else()
301     message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
302   endif()
303 endif()
304
305 configure_file(
306   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
307   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
308
309 include(CMakeParseArguments)
310
311 function(clang_tablegen)
312   # Syntax:
313   # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
314   # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
315   #
316   # Generates a custom command for invoking tblgen as
317   #
318   # tblgen source-file -o=output-file tablegen-arg ...
319   #
320   # and, if cmake-target-name is provided, creates a custom target for
321   # executing the custom command depending on output-file. It is
322   # possible to list more files to depend after DEPENDS.
323
324   cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
325
326   if( NOT CTG_SOURCE )
327     message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
328   endif()
329
330   set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
331   tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS})
332
333   if(CTG_TARGET)
334     add_public_tablegen_target(${CTG_TARGET})
335     set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
336     set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
337   endif()
338 endfunction(clang_tablegen)
339
340 macro(set_clang_windows_version_resource_properties name)
341   if(DEFINED windows_resource_file)
342     set_windows_version_resource_properties(${name} ${windows_resource_file}
343       VERSION_MAJOR ${CLANG_VERSION_MAJOR}
344       VERSION_MINOR ${CLANG_VERSION_MINOR}
345       VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
346       VERSION_STRING "${CLANG_VERSION} (${BACKEND_PACKAGE_STRING})"
347       PRODUCT_NAME "clang")
348   endif()
349 endmacro()
350
351 macro(add_clang_library name)
352   cmake_parse_arguments(ARG
353     ""
354     ""
355     "ADDITIONAL_HEADERS"
356     ${ARGN})
357   set(srcs)
358   if(MSVC_IDE OR XCODE)
359     # Add public headers
360     file(RELATIVE_PATH lib_path
361       ${CLANG_SOURCE_DIR}/lib/
362       ${CMAKE_CURRENT_SOURCE_DIR}
363     )
364     if(NOT lib_path MATCHES "^[.][.]")
365       file( GLOB_RECURSE headers
366         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
367         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
368       )
369       set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
370
371       file( GLOB_RECURSE tds
372         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
373       )
374       source_group("TableGen descriptions" FILES ${tds})
375       set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
376
377       if(headers OR tds)
378         set(srcs ${headers} ${tds})
379       endif()
380     endif()
381   endif(MSVC_IDE OR XCODE)
382   if(srcs OR ARG_ADDITIONAL_HEADERS)
383     set(srcs
384       ADDITIONAL_HEADERS
385       ${srcs}
386       ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
387       )
388   endif()
389   llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
390
391   if(TARGET ${name})
392     target_link_libraries(${name} ${cmake_2_8_12_INTERFACE} ${LLVM_COMMON_LIBS})
393
394     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
395       install(TARGETS ${name}
396         EXPORT ClangTargets
397         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
398         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
399         RUNTIME DESTINATION bin)
400     endif()
401     set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
402   else()
403     # Add empty "phony" target
404     add_custom_target(${name})
405   endif()
406
407   set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
408   set_clang_windows_version_resource_properties(${name})
409 endmacro(add_clang_library)
410
411 macro(add_clang_executable name)
412   add_llvm_executable( ${name} ${ARGN} )
413   set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
414   set_clang_windows_version_resource_properties(${name})
415 endmacro(add_clang_executable)
416
417 set(CMAKE_INCLUDE_CURRENT_DIR ON)
418
419 include_directories(BEFORE
420   ${CMAKE_CURRENT_BINARY_DIR}/include
421   ${CMAKE_CURRENT_SOURCE_DIR}/include
422   )
423
424 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
425   install(DIRECTORY include/clang include/clang-c
426     DESTINATION include
427     FILES_MATCHING
428     PATTERN "*.def"
429     PATTERN "*.h"
430     PATTERN "config.h" EXCLUDE
431     PATTERN ".svn" EXCLUDE
432     )
433
434   install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
435     DESTINATION include
436     FILES_MATCHING
437     PATTERN "CMakeFiles" EXCLUDE
438     PATTERN "*.inc"
439     PATTERN "*.h"
440     )
441 endif()
442
443 install(DIRECTORY include/clang-c
444   DESTINATION include
445   FILES_MATCHING
446   PATTERN "*.h"
447   PATTERN ".svn" EXCLUDE
448   )
449
450 add_definitions( -D_GNU_SOURCE )
451
452 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
453 if (CLANG_ENABLE_ARCMT)
454   set(ENABLE_CLANG_ARCMT "1")
455 else()
456   set(ENABLE_CLANG_ARCMT "0")
457 endif()
458
459 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
460 if (CLANG_ENABLE_STATIC_ANALYZER)
461   set(ENABLE_CLANG_STATIC_ANALYZER "1")
462 else()
463   set(ENABLE_CLANG_STATIC_ANALYZER "0")
464 endif()
465
466 if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
467   message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
468 endif()
469
470 if(CLANG_ENABLE_ARCMT)
471   add_definitions(-DCLANG_ENABLE_ARCMT)
472   add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
473 endif()
474 if(CLANG_ENABLE_STATIC_ANALYZER)
475   add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
476 endif()
477
478 # Clang version information
479 set(CLANG_EXECUTABLE_VERSION
480      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
481     "Version number that will be placed into the clang executable, in the form XX.YY")
482 set(LIBCLANG_LIBRARY_VERSION
483      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
484     "Version number that will be placed into the libclang library , in the form XX.YY")
485 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
486
487 add_subdirectory(utils/TableGen)
488
489 add_subdirectory(include)
490
491 # All targets below may depend on all tablegen'd files.
492 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
493 list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
494
495 add_subdirectory(lib)
496 add_subdirectory(tools)
497 add_subdirectory(runtime)
498
499 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
500 if (CLANG_BUILD_EXAMPLES)
501   set(ENABLE_CLANG_EXAMPLES "1")
502 else()
503   set(ENABLE_CLANG_EXAMPLES "0")
504 endif()
505 add_subdirectory(examples)
506
507 option(CLANG_INCLUDE_TESTS
508        "Generate build targets for the Clang unit tests."
509        ${LLVM_INCLUDE_TESTS})
510
511 if( CLANG_INCLUDE_TESTS )
512   if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
513     add_subdirectory(unittests)
514     list(APPEND CLANG_TEST_DEPS ClangUnitTests)
515     list(APPEND CLANG_TEST_PARAMS
516       clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
517       )
518   endif()
519   add_subdirectory(test)
520
521   if(CLANG_BUILT_STANDALONE)
522     # Add a global check rule now that all subdirectories have been traversed
523     # and we know the total set of lit testsuites.
524     get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
525     get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
526     get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
527     get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
528     add_lit_target(check-all
529       "Running all regression tests"
530       ${LLVM_LIT_TESTSUITES}
531       PARAMS ${LLVM_LIT_PARAMS}
532       DEPENDS ${LLVM_LIT_DEPENDS}
533       ARGS ${LLVM_LIT_EXTRA_ARGS}
534       )
535   endif()
536 endif()
537
538 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
539   ${LLVM_INCLUDE_DOCS})
540 if( CLANG_INCLUDE_DOCS )
541   add_subdirectory(docs)
542 endif()
543
544 set(CLANG_ORDER_FILE "" CACHE FILEPATH
545   "Order file to use when compiling clang in order to improve startup time.")
546
547 if (CLANG_BUILT_STANDALONE)
548   # Generate a list of CMake library targets so that other CMake projects can
549   # link against them. LLVM calls its version of this file LLVMExports.cmake, but
550   # the usual CMake convention seems to be ${Project}Targets.cmake.
551   set(CLANG_INSTALL_PACKAGE_DIR share/clang/cmake)
552   set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
553   get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
554   export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
555
556   # Install a <prefix>/share/clang/cmake/ClangConfig.cmake file so that
557   # find_package(Clang) works. Install the target list with it.
558   install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR})
559
560   install(FILES
561     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
562     DESTINATION share/clang/cmake)
563
564   # Also copy ClangConfig.cmake to the build directory so that dependent projects
565   # can build against a build directory of Clang more easily.
566   configure_file(
567     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
568     ${CLANG_BINARY_DIR}/share/clang/cmake/ClangConfig.cmake
569     COPYONLY)
570 endif ()
571
572 if (CLANG_ENABLE_BOOTSTRAP)
573   include(ExternalProject)
574
575   if(CMAKE_VERSION VERSION_GREATER 3.1.0)
576     set(cmake_3_1_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL 1)
577   endif()
578
579   if(CMAKE_VERSION VERSION_GREATER 3.3.20150708)
580     set(cmake_3_4_USES_TERMINAL_OPTIONS
581       USES_TERMINAL_CONFIGURE 1
582       USES_TERMINAL_BUILD 1
583       USES_TERMINAL_INSTALL 1
584       )
585     set(cmake_3_4_USES_TERMINAL USES_TERMINAL 1)
586   endif()
587   
588   set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-stamps/)
589   set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-bins/)
590
591   add_custom_target(bootstrap-clear
592     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-cleared
593     )
594   add_custom_command(
595     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-cleared
596     DEPENDS clang
597     COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
598     COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
599     COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
600     COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
601     COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-cleared
602     COMMENT "Clobberring bootstrap build and stamp directories"
603     )
604
605   ExternalProject_Add(bootstrap
606     DEPENDS clang
607     PREFIX bootstrap
608     SOURCE_DIR ${CMAKE_SOURCE_DIR}
609     STAMP_DIR ${STAMP_DIR}
610     BINARY_DIR ${BINARY_DIR}
611     ${cmake_3_1_EXCLUDE_FROM_ALL}
612     CMAKE_ARGS
613                 # We shouldn't need to set this here, but INSTALL_DIR doesn't
614                 # seem to work, so instead I'm passing this through
615                 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
616                 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
617                 -DCMAKE_CXX_COMPILER=${CMAKE_BINARY_DIR}/bin/clang++
618                 -DCMAKE_C_COMPILER=${CMAKE_BINARY_DIR}/bin/clang
619     INSTALL_COMMAND ""
620     STEP_TARGETS configure build
621     ${cmake_3_4_USES_TERMINAL_OPTIONS}
622     )
623
624   # exclude really-install from main target
625   set_target_properties(bootstrap PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
626   ExternalProject_Add_Step(bootstrap really-install
627     COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
628     COMMENT "Performing install step for 'bootstrap'"
629     DEPENDEES build
630     ${cmake_3_4_USES_TERMINAL}
631   )
632   ExternalProject_Add_StepTargets(bootstrap really-install)
633   add_custom_target(bootstrap-install DEPENDS bootstrap-really-install)
634
635
636   set(ADDITIONAL_TARGETS_TO_ADD check-llvm check-clang check-all)
637   foreach(target ${ADDITIONAL_TARGETS_TO_ADD})
638     # exclude from main target
639     set_target_properties(bootstrap PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
640
641     ExternalProject_Add_Step(bootstrap ${target}
642       COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
643       COMMENT "Performing ${target} for 'bootstrap'"
644       DEPENDEES configure
645       ${cmake_3_4_USES_TERMINAL}
646     )
647     ExternalProject_Add_StepTargets(bootstrap ${target})
648   endforeach()
649 endif()