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