]> granicus.if.org Git - clang/blob - CMakeLists.txt
Fix ASTMatcher reference newlines and make the generator script windows-proof.
[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_subdirectory name)
352   add_llvm_subdirectory(CLANG TOOL ${name})
353 endmacro()
354
355 macro(add_clang_library name)
356   cmake_parse_arguments(ARG
357     "SHARED"
358     ""
359     "ADDITIONAL_HEADERS"
360     ${ARGN})
361   set(srcs)
362   if(MSVC_IDE OR XCODE)
363     # Add public headers
364     file(RELATIVE_PATH lib_path
365       ${CLANG_SOURCE_DIR}/lib/
366       ${CMAKE_CURRENT_SOURCE_DIR}
367     )
368     if(NOT lib_path MATCHES "^[.][.]")
369       file( GLOB_RECURSE headers
370         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
371         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
372       )
373       set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
374
375       file( GLOB_RECURSE tds
376         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
377       )
378       source_group("TableGen descriptions" FILES ${tds})
379       set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
380
381       if(headers OR tds)
382         set(srcs ${headers} ${tds})
383       endif()
384     endif()
385   endif(MSVC_IDE OR XCODE)
386   if(srcs OR ARG_ADDITIONAL_HEADERS)
387     set(srcs
388       ADDITIONAL_HEADERS
389       ${srcs}
390       ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
391       )
392   endif()
393   if(ARG_SHARED)
394     set(ARG_ENABLE_SHARED SHARED)
395   endif()
396   llvm_add_library(${name} ${ARG_ENABLE_SHARED} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
397
398   if(TARGET ${name})
399     target_link_libraries(${name} ${cmake_2_8_12_INTERFACE} ${LLVM_COMMON_LIBS})
400
401     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
402       install(TARGETS ${name}
403         COMPONENT ${name}
404         EXPORT ClangTargets
405         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
406         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
407         RUNTIME DESTINATION bin)
408
409       if (${ARG_SHARED} AND NOT CMAKE_CONFIGURATION_TYPES)
410         add_custom_target(install-${name}
411                           DEPENDS ${name}
412                           COMMAND "${CMAKE_COMMAND}"
413                                   -DCMAKE_INSTALL_COMPONENT=${name}
414                                   -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
415       endif()
416     endif()
417     set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
418   else()
419     # Add empty "phony" target
420     add_custom_target(${name})
421   endif()
422
423   set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
424   set_clang_windows_version_resource_properties(${name})
425 endmacro(add_clang_library)
426
427 macro(add_clang_executable name)
428   add_llvm_executable( ${name} ${ARGN} )
429   set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
430   set_clang_windows_version_resource_properties(${name})
431 endmacro(add_clang_executable)
432
433 macro(add_clang_symlink name dest)
434   add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
435   # Always generate install targets
436   llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
437 endmacro()
438
439 set(CMAKE_INCLUDE_CURRENT_DIR ON)
440
441 include_directories(BEFORE
442   ${CMAKE_CURRENT_BINARY_DIR}/include
443   ${CMAKE_CURRENT_SOURCE_DIR}/include
444   )
445
446 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
447   install(DIRECTORY include/clang include/clang-c
448     DESTINATION include
449     FILES_MATCHING
450     PATTERN "*.def"
451     PATTERN "*.h"
452     PATTERN "config.h" EXCLUDE
453     PATTERN ".svn" EXCLUDE
454     )
455
456   install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
457     DESTINATION include
458     FILES_MATCHING
459     PATTERN "CMakeFiles" EXCLUDE
460     PATTERN "*.inc"
461     PATTERN "*.h"
462     )
463 endif()
464
465 if(INTERNAL_INSTALL_PREFIX)
466   set(LIBCLANG_HEADERS_INSTALL_DESTINATION "${INTERNAL_INSTALL_PREFIX}/include")
467 else()
468   set(LIBCLANG_HEADERS_INSTALL_DESTINATION include)
469 endif()
470
471 install(DIRECTORY include/clang-c
472   COMPONENT libclang-headers
473   DESTINATION "${LIBCLANG_HEADERS_INSTALL_DESTINATION}"
474   FILES_MATCHING
475   PATTERN "*.h"
476   PATTERN ".svn" EXCLUDE
477   )
478
479 if (NOT CMAKE_CONFIGURATION_TYPES) # don't add this for IDE's.
480   add_custom_target(install-libclang-headers
481     DEPENDS
482     COMMAND "${CMAKE_COMMAND}"
483             -DCMAKE_INSTALL_COMPONENT=libclang-headers
484             -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
485 endif()
486
487 add_definitions( -D_GNU_SOURCE )
488
489 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
490 if (CLANG_ENABLE_ARCMT)
491   set(ENABLE_CLANG_ARCMT "1")
492 else()
493   set(ENABLE_CLANG_ARCMT "0")
494 endif()
495
496 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
497 if (CLANG_ENABLE_STATIC_ANALYZER)
498   set(ENABLE_CLANG_STATIC_ANALYZER "1")
499 else()
500   set(ENABLE_CLANG_STATIC_ANALYZER "0")
501 endif()
502
503 if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
504   message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
505 endif()
506
507 if(CLANG_ENABLE_ARCMT)
508   add_definitions(-DCLANG_ENABLE_ARCMT)
509   add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
510 endif()
511 if(CLANG_ENABLE_STATIC_ANALYZER)
512   add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
513 endif()
514
515 # Clang version information
516 set(CLANG_EXECUTABLE_VERSION
517      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
518     "Version number that will be placed into the clang executable, in the form XX.YY")
519 set(LIBCLANG_LIBRARY_VERSION
520      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
521     "Version number that will be placed into the libclang library , in the form XX.YY")
522 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
523
524 option(CLANG_INCLUDE_TESTS
525        "Generate build targets for the Clang unit tests."
526        ${LLVM_INCLUDE_TESTS})
527
528 add_subdirectory(utils/TableGen)
529
530 add_subdirectory(include)
531
532 # All targets below may depend on all tablegen'd files.
533 get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
534 list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
535
536 add_subdirectory(lib)
537 add_subdirectory(tools)
538 add_subdirectory(runtime)
539
540 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
541 if (CLANG_BUILD_EXAMPLES)
542   set(ENABLE_CLANG_EXAMPLES "1")
543 else()
544   set(ENABLE_CLANG_EXAMPLES "0")
545 endif()
546 add_subdirectory(examples)
547
548 if( CLANG_INCLUDE_TESTS )
549   if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
550     add_subdirectory(unittests)
551     list(APPEND CLANG_TEST_DEPS ClangUnitTests)
552     list(APPEND CLANG_TEST_PARAMS
553       clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
554       )
555   endif()
556   add_subdirectory(test)
557
558   if(CLANG_BUILT_STANDALONE)
559     # Add a global check rule now that all subdirectories have been traversed
560     # and we know the total set of lit testsuites.
561     get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
562     get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
563     get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
564     get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
565     add_lit_target(check-all
566       "Running all regression tests"
567       ${LLVM_LIT_TESTSUITES}
568       PARAMS ${LLVM_LIT_PARAMS}
569       DEPENDS ${LLVM_LIT_DEPENDS}
570       ARGS ${LLVM_LIT_EXTRA_ARGS}
571       )
572   endif()
573 endif()
574
575 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
576   ${LLVM_INCLUDE_DOCS})
577 if( CLANG_INCLUDE_DOCS )
578   add_subdirectory(docs)
579 endif()
580
581 set(CLANG_ORDER_FILE "" CACHE FILEPATH
582   "Order file to use when compiling clang in order to improve startup time.")
583
584 if (CLANG_BUILT_STANDALONE OR CMAKE_VERSION VERSION_EQUAL 3 OR
585     CMAKE_VERSION VERSION_GREATER 3)
586   # Generate a list of CMake library targets so that other CMake projects can
587   # link against them. LLVM calls its version of this file LLVMExports.cmake, but
588   # the usual CMake convention seems to be ${Project}Targets.cmake.
589   set(CLANG_INSTALL_PACKAGE_DIR share/clang/cmake)
590   set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
591   get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
592   export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
593
594   # Install a <prefix>/share/clang/cmake/ClangConfig.cmake file so that
595   # find_package(Clang) works. Install the target list with it.
596   install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR})
597
598   install(FILES
599     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
600     DESTINATION share/clang/cmake)
601
602   # Also copy ClangConfig.cmake to the build directory so that dependent projects
603   # can build against a build directory of Clang more easily.
604   configure_file(
605     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
606     ${CLANG_BINARY_DIR}/share/clang/cmake/ClangConfig.cmake
607     COPYONLY)
608 endif ()
609
610 if (CLANG_ENABLE_BOOTSTRAP)
611   include(ExternalProject)
612
613   if(CMAKE_VERSION VERSION_GREATER 3.1.0)
614     set(cmake_3_1_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL 1)
615   endif()
616
617   if(CMAKE_VERSION VERSION_GREATER 3.3.20150708)
618     set(cmake_3_4_USES_TERMINAL_OPTIONS
619       USES_TERMINAL_CONFIGURE 1
620       USES_TERMINAL_BUILD 1
621       USES_TERMINAL_INSTALL 1
622       )
623     set(cmake_3_4_USES_TERMINAL USES_TERMINAL 1)
624   endif()
625   
626   set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-stamps/)
627   set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-bins/)
628
629   # If on Darwin we need to make bootstrap depend on LTO and pass
630   # DARWIN_LTO_LIBRARY so that -flto will work using the just-built compiler
631   if(APPLE)
632     set(LTO_DEP LTO llvm-ar llvm-ranlib)
633     set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
634     set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
635     set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
636   endif()
637
638   add_custom_target(bootstrap-clear
639     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-cleared
640     )
641   add_custom_command(
642     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bootstrap-cleared
643     DEPENDS clang ${LTO_DEP}
644     COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
645     COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
646     COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
647     COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
648     COMMENT "Clobberring bootstrap build and stamp directories"
649     )
650
651   if(CMAKE_VERBOSE_MAKEFILE)
652     set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
653   endif()
654
655   ExternalProject_Add(bootstrap
656     DEPENDS clang ${LTO_DEP}
657     PREFIX bootstrap
658     SOURCE_DIR ${CMAKE_SOURCE_DIR}
659     STAMP_DIR ${STAMP_DIR}
660     BINARY_DIR ${BINARY_DIR}
661     ${cmake_3_1_EXCLUDE_FROM_ALL}
662     CMAKE_ARGS
663                 # We shouldn't need to set this here, but INSTALL_DIR doesn't
664                 # seem to work, so instead I'm passing this through
665                 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
666                 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
667                 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
668                 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
669                 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
670                 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose}
671     INSTALL_COMMAND ""
672     STEP_TARGETS configure build
673     ${cmake_3_4_USES_TERMINAL_OPTIONS}
674     )
675
676   # exclude really-install from main target
677   set_target_properties(bootstrap PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
678   ExternalProject_Add_Step(bootstrap really-install
679     COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
680     COMMENT "Performing install step for 'bootstrap'"
681     DEPENDEES build
682     ${cmake_3_4_USES_TERMINAL}
683   )
684   ExternalProject_Add_StepTargets(bootstrap really-install)
685   add_custom_target(bootstrap-install DEPENDS bootstrap-really-install)
686
687
688   set(ADDITIONAL_TARGETS_TO_ADD check-llvm check-clang check-all)
689   foreach(target ${ADDITIONAL_TARGETS_TO_ADD})
690     # exclude from main target
691     set_target_properties(bootstrap PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
692
693     ExternalProject_Add_Step(bootstrap ${target}
694       COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
695       COMMENT "Performing ${target} for 'bootstrap'"
696       DEPENDEES configure
697       ${cmake_3_4_USES_TERMINAL}
698     )
699     ExternalProject_Add_StepTargets(bootstrap ${target})
700   endforeach()
701 endif()