• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 2.8.8)
2
3# FIXME: It may be removed when we use 2.8.12.
4if(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)
8else()
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()
15endif()
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:
19if( 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
125Please 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}")
180else()
181  set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
182endif()
183
184find_package(LibXml2 2.5.3 QUIET)
185if (LIBXML2_FOUND)
186  set(CLANG_HAVE_LIBXML 1)
187endif()
188
189set(CLANG_RESOURCE_DIR "" CACHE STRING
190  "Relative directory from the Clang binary to its resource files.")
191
192set(C_INCLUDE_DIRS "" CACHE STRING
193  "Colon separated list of directories clang will search for headers.")
194
195set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
196set(DEFAULT_SYSROOT "" CACHE PATH
197  "Default <path> to all compiler invocations for --sysroot=<path>." )
198
199set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
200  "Default OpenMP runtime used by -fopenmp.")
201
202set(CLANG_VENDOR "" CACHE STRING
203  "Vendor-specific text for showing with version information.")
204
205if( CLANG_VENDOR )
206  add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
207endif()
208
209set(CLANG_REPOSITORY_STRING "" CACHE STRING
210  "Vendor-specific text for showing the repository the source is taken from.")
211
212if(CLANG_REPOSITORY_STRING)
213  add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
214endif()
215
216set(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.
220set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
221
222set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
223set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
224
225if( 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.")
231endif()
232
233if( 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()
243endif()
244
245# Compute the Clang version from the LLVM version.
246string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
247  ${PACKAGE_VERSION})
248message(STATUS "Clang version: ${CLANG_VERSION}")
249
250string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
251  ${CLANG_VERSION})
252string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
253  ${CLANG_VERSION})
254if (${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})
258else()
259  set(CLANG_HAS_VERSION_PATCHLEVEL 0)
260endif()
261
262# Configure the Version.inc file.
263configure_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
268if (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()
283endif ()
284
285# Determine HOST_LINK_VERSION on Darwin.
286set(HOST_LINK_VERSION)
287if (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()
303endif()
304
305configure_file(
306  ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
307  ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
308
309include(CMakeParseArguments)
310
311function(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()
338endfunction(clang_tablegen)
339
340macro(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()
349endmacro()
350
351macro(add_clang_subdirectory name)
352  add_llvm_subdirectory(CLANG TOOL ${name})
353endmacro()
354
355macro(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})
425endmacro(add_clang_library)
426
427macro(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})
431endmacro(add_clang_executable)
432
433macro(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)
437endmacro()
438
439set(CMAKE_INCLUDE_CURRENT_DIR ON)
440
441include_directories(BEFORE
442  ${CMAKE_CURRENT_BINARY_DIR}/include
443  ${CMAKE_CURRENT_SOURCE_DIR}/include
444  )
445
446if (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    )
463endif()
464
465if(INTERNAL_INSTALL_PREFIX)
466  set(LIBCLANG_HEADERS_INSTALL_DESTINATION "${INTERNAL_INSTALL_PREFIX}/include")
467else()
468  set(LIBCLANG_HEADERS_INSTALL_DESTINATION include)
469endif()
470
471install(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
479if (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")
485endif()
486
487add_definitions( -D_GNU_SOURCE )
488
489option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
490if (CLANG_ENABLE_ARCMT)
491  set(ENABLE_CLANG_ARCMT "1")
492else()
493  set(ENABLE_CLANG_ARCMT "0")
494endif()
495
496option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
497if (CLANG_ENABLE_STATIC_ANALYZER)
498  set(ENABLE_CLANG_STATIC_ANALYZER "1")
499else()
500  set(ENABLE_CLANG_STATIC_ANALYZER "0")
501endif()
502
503if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
504  message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
505endif()
506
507if(CLANG_ENABLE_ARCMT)
508  add_definitions(-DCLANG_ENABLE_ARCMT)
509  add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
510endif()
511if(CLANG_ENABLE_STATIC_ANALYZER)
512  add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
513endif()
514
515# Clang version information
516set(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")
519set(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")
522mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
523
524option(CLANG_INCLUDE_TESTS
525       "Generate build targets for the Clang unit tests."
526       ${LLVM_INCLUDE_TESTS})
527
528add_subdirectory(utils/TableGen)
529
530add_subdirectory(include)
531
532# All targets below may depend on all tablegen'd files.
533get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
534list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
535
536add_subdirectory(lib)
537add_subdirectory(tools)
538add_subdirectory(runtime)
539
540option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
541if (CLANG_BUILD_EXAMPLES)
542  set(ENABLE_CLANG_EXAMPLES "1")
543else()
544  set(ENABLE_CLANG_EXAMPLES "0")
545endif()
546add_subdirectory(examples)
547
548if( 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  add_subdirectory(utils/perf-training)
574endif()
575
576option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
577  ${LLVM_INCLUDE_DOCS})
578if( CLANG_INCLUDE_DOCS )
579  add_subdirectory(docs)
580endif()
581
582set(CLANG_ORDER_FILE "" CACHE FILEPATH
583  "Order file to use when compiling clang in order to improve startup time.")
584
585if (CLANG_BUILT_STANDALONE OR CMAKE_VERSION VERSION_EQUAL 3 OR
586    CMAKE_VERSION VERSION_GREATER 3)
587  # Generate a list of CMake library targets so that other CMake projects can
588  # link against them. LLVM calls its version of this file LLVMExports.cmake, but
589  # the usual CMake convention seems to be ${Project}Targets.cmake.
590  set(CLANG_INSTALL_PACKAGE_DIR share/clang/cmake)
591  set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
592  get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
593  export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
594
595  # Install a <prefix>/share/clang/cmake/ClangConfig.cmake file so that
596  # find_package(Clang) works. Install the target list with it.
597  install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR})
598
599  install(FILES
600    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
601    DESTINATION share/clang/cmake)
602
603  # Also copy ClangConfig.cmake to the build directory so that dependent projects
604  # can build against a build directory of Clang more easily.
605  configure_file(
606    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
607    ${CLANG_BINARY_DIR}/share/clang/cmake/ClangConfig.cmake
608    COPYONLY)
609endif ()
610
611if (CLANG_ENABLE_BOOTSTRAP)
612  include(ExternalProject)
613
614  if(CMAKE_VERSION VERSION_GREATER 3.1.0)
615    set(cmake_3_1_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL 1)
616  endif()
617
618  if(CMAKE_VERSION VERSION_GREATER 3.3.20150708)
619    set(cmake_3_4_USES_TERMINAL_OPTIONS
620      USES_TERMINAL_CONFIGURE 1
621      USES_TERMINAL_BUILD 1
622      USES_TERMINAL_INSTALL 1
623      )
624    set(cmake_3_4_USES_TERMINAL USES_TERMINAL 1)
625  endif()
626
627  if(NOT CLANG_STAGE)
628    set(CLANG_STAGE stage1)
629    message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
630  endif()
631
632  string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
633  if(MATCHED_STAGE)
634    math(EXPR STAGE_NUM "${MATCHED_STAGE} + 1")
635    set(NEXT_CLANG_STAGE stage${STAGE_NUM})
636  else()
637    set(NEXT_CLANG_STAGE bootstrap)
638  endif()
639  message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
640
641
642  set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
643  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
644
645  # If on Darwin we need to make bootstrap depend on LTO and pass
646  # DARWIN_LTO_LIBRARY so that -flto will work using the just-built compiler
647  if(APPLE)
648    set(LTO_DEP LTO llvm-ar llvm-ranlib)
649    set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
650    set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
651    set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
652  endif()
653
654  add_custom_target(${NEXT_CLANG_STAGE}-clear
655    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
656    )
657  add_custom_command(
658    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
659    DEPENDS clang ${LTO_DEP}
660    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
661    COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
662    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
663    COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
664    COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
665    )
666
667  if(CMAKE_VERBOSE_MAKEFILE)
668    set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
669  endif()
670
671  set(BOOTSTRAP_DEFAULT_PASSTHROUGH
672    PACKAGE_VERSION
673    LLVM_VERSION_MAJOR
674    LLVM_VERSION_MINOR
675    LLVM_VERSION_PATCH
676    LLVM_VERSION_SUFFIX
677    CLANG_REPOSITORY_STRING
678    CMAKE_MAKE_PROGRAM)
679
680  if(TARGET compiler-rt)
681    set(RUNTIME_DEP compiler-rt)
682  endif()
683
684  # Find all variables that start with BOOTSTRAP_ and populate a variable with
685  # them.
686  get_cmake_property(variableNames VARIABLES)
687  foreach(variableName ${variableNames})
688    if(variableName MATCHES "^BOOTSTRAP_")
689      string(SUBSTRING ${variableName} 10 -1 varName)
690      string(REPLACE ";" "\;" value "${${variableName}}")
691      list(APPEND PASSTHROUGH_VARIABLES
692        -D${varName}=${value})
693    endif()
694  endforeach()
695
696  # Populate the passthrough variables
697  foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${BOOTSTRAP_DEFAULT_PASSTHROUGH})
698    if(${variableName})
699      string(REPLACE ";" "\;" value ${${variableName}})
700      list(APPEND PASSTHROUGH_VARIABLES
701        -D${variableName}=${value})
702    endif()
703  endforeach()
704
705  ExternalProject_Add(${NEXT_CLANG_STAGE}
706    DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP}
707    PREFIX ${NEXT_CLANG_STAGE}
708    SOURCE_DIR ${CMAKE_SOURCE_DIR}
709    STAMP_DIR ${STAMP_DIR}
710    BINARY_DIR ${BINARY_DIR}
711    ${cmake_3_1_EXCLUDE_FROM_ALL}
712    CMAKE_ARGS
713                # We shouldn't need to set this here, but INSTALL_DIR doesn't
714                # seem to work, so instead I'm passing this through
715                -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
716                ${CLANG_BOOTSTRAP_CMAKE_ARGS}
717                ${PASSTHROUGH_VARIABLES}
718                -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
719                -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
720                -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
721                -DCLANG_STAGE=${NEXT_CLANG_STAGE}
722                ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose}
723    INSTALL_COMMAND ""
724    STEP_TARGETS configure build
725    ${cmake_3_4_USES_TERMINAL_OPTIONS}
726    )
727
728  # exclude really-install from main target
729  set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
730  ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
731    COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
732    COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
733    DEPENDEES build
734    ${cmake_3_4_USES_TERMINAL}
735  )
736  ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
737  add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
738
739  if(NOT CLANG_BOOTSTRAP_TARGETS)
740    set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
741  endif()
742  foreach(target ${CLANG_BOOTSTRAP_TARGETS})
743    # exclude from main target
744    set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
745
746    ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
747      COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
748      COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
749      DEPENDEES configure
750      ${cmake_3_4_USES_TERMINAL}
751    )
752
753    if(target MATCHES "^stage[0-9]*")
754      add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
755    endif()
756
757    ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
758  endforeach()
759endif()
760