xref: /minix3/external/bsd/llvm/dist/clang/CMakeLists.txt (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuccmake_minimum_required(VERSION 2.8.8)
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc# FIXME: It may be removed when we use 2.8.12.
4*0a6a1f1dSLionel Sambucif(CMAKE_VERSION VERSION_LESS 2.8.12)
5*0a6a1f1dSLionel Sambuc  # Invalidate a couple of keywords.
6*0a6a1f1dSLionel Sambuc  set(cmake_2_8_12_INTERFACE)
7*0a6a1f1dSLionel Sambuc  set(cmake_2_8_12_PRIVATE)
8*0a6a1f1dSLionel Sambucelse()
9*0a6a1f1dSLionel Sambuc  # Use ${cmake_2_8_12_KEYWORD} intead of KEYWORD in target_link_libraries().
10*0a6a1f1dSLionel Sambuc  set(cmake_2_8_12_INTERFACE INTERFACE)
11*0a6a1f1dSLionel Sambuc  set(cmake_2_8_12_PRIVATE PRIVATE)
12*0a6a1f1dSLionel Sambuc  if(POLICY CMP0022)
13*0a6a1f1dSLionel Sambuc    cmake_policy(SET CMP0022 NEW) # automatic when 2.8.12 is required
14*0a6a1f1dSLionel Sambuc  endif()
15*0a6a1f1dSLionel Sambucendif()
16*0a6a1f1dSLionel Sambuc
17f4a2713aSLionel Sambuc# If we are not building as a part of LLVM, build Clang as an
18f4a2713aSLionel Sambuc# standalone project, using LLVM as an external library:
19f4a2713aSLionel Sambucif( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
20f4a2713aSLionel Sambuc  project(Clang)
21f4a2713aSLionel Sambuc
22*0a6a1f1dSLionel Sambuc  # Rely on llvm-config.
23*0a6a1f1dSLionel Sambuc  set(CONFIG_OUTPUT)
24*0a6a1f1dSLionel Sambuc  find_program(LLVM_CONFIG "llvm-config")
25*0a6a1f1dSLionel Sambuc  if(LLVM_CONFIG)
26*0a6a1f1dSLionel Sambuc    message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
27*0a6a1f1dSLionel Sambuc    set(CONFIG_COMMAND ${LLVM_CONFIG}
28*0a6a1f1dSLionel Sambuc      "--assertion-mode"
29*0a6a1f1dSLionel Sambuc      "--bindir"
30*0a6a1f1dSLionel Sambuc      "--libdir"
31*0a6a1f1dSLionel Sambuc      "--includedir"
32*0a6a1f1dSLionel Sambuc      "--prefix"
33*0a6a1f1dSLionel Sambuc      "--src-root")
34*0a6a1f1dSLionel Sambuc    execute_process(
35*0a6a1f1dSLionel Sambuc      COMMAND ${CONFIG_COMMAND}
36*0a6a1f1dSLionel Sambuc      RESULT_VARIABLE HAD_ERROR
37*0a6a1f1dSLionel Sambuc      OUTPUT_VARIABLE CONFIG_OUTPUT
38*0a6a1f1dSLionel Sambuc    )
39*0a6a1f1dSLionel Sambuc    if(NOT HAD_ERROR)
40*0a6a1f1dSLionel Sambuc      string(REGEX REPLACE
41*0a6a1f1dSLionel Sambuc        "[ \t]*[\r\n]+[ \t]*" ";"
42*0a6a1f1dSLionel Sambuc        CONFIG_OUTPUT ${CONFIG_OUTPUT})
43f4a2713aSLionel Sambuc    else()
44*0a6a1f1dSLionel Sambuc      string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
45*0a6a1f1dSLionel Sambuc      message(STATUS "${CONFIG_COMMAND_STR}")
46*0a6a1f1dSLionel Sambuc      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
47f4a2713aSLionel Sambuc    endif()
48f4a2713aSLionel Sambuc  else()
49*0a6a1f1dSLionel Sambuc    message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
50f4a2713aSLionel Sambuc  endif()
51f4a2713aSLionel Sambuc
52*0a6a1f1dSLionel Sambuc  list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
53*0a6a1f1dSLionel Sambuc  list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
54*0a6a1f1dSLionel Sambuc  list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
55*0a6a1f1dSLionel Sambuc  list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
56*0a6a1f1dSLionel Sambuc  list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
57*0a6a1f1dSLionel Sambuc  list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
58f4a2713aSLionel Sambuc
59*0a6a1f1dSLionel Sambuc  if(NOT MSVC_IDE)
60*0a6a1f1dSLionel Sambuc    set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
61*0a6a1f1dSLionel Sambuc      CACHE BOOL "Enable assertions")
62*0a6a1f1dSLionel Sambuc    # Assertions should follow llvm-config's.
63*0a6a1f1dSLionel Sambuc    mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
64*0a6a1f1dSLionel Sambuc  endif()
65f4a2713aSLionel Sambuc
66*0a6a1f1dSLionel Sambuc  set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
67*0a6a1f1dSLionel Sambuc  set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
68*0a6a1f1dSLionel Sambuc  set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
69*0a6a1f1dSLionel Sambuc  set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
70*0a6a1f1dSLionel Sambuc  set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc  find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
73*0a6a1f1dSLionel Sambuc    NO_DEFAULT_PATH)
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc  set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/share/llvm/cmake")
76*0a6a1f1dSLionel Sambuc  set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
77*0a6a1f1dSLionel Sambuc  if(EXISTS ${LLVMCONFIG_FILE})
78*0a6a1f1dSLionel Sambuc    list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
79*0a6a1f1dSLionel Sambuc    include(${LLVMCONFIG_FILE})
80*0a6a1f1dSLionel Sambuc  else()
81*0a6a1f1dSLionel Sambuc    message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
82*0a6a1f1dSLionel Sambuc  endif()
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc  # They are used as destination of target generators.
85*0a6a1f1dSLionel Sambuc  set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
86*0a6a1f1dSLionel Sambuc  set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
87*0a6a1f1dSLionel Sambuc  if(WIN32 OR CYGWIN)
88*0a6a1f1dSLionel Sambuc    # DLL platform -- put DLLs into bin.
89*0a6a1f1dSLionel Sambuc    set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
90*0a6a1f1dSLionel Sambuc  else()
91*0a6a1f1dSLionel Sambuc    set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
92*0a6a1f1dSLionel Sambuc  endif()
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc  option(LLVM_INSTALL_TOOLCHAIN_ONLY
95*0a6a1f1dSLionel Sambuc    "Only include toolchain files in the 'install' target." OFF)
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc  option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
98*0a6a1f1dSLionel Sambuc    "Set to ON to force using an old, unsupported host toolchain." OFF)
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc  include(AddLLVM)
101f4a2713aSLionel Sambuc  include(TableGen)
102f4a2713aSLionel Sambuc  include(HandleLLVMOptions)
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc  set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
105f4a2713aSLionel Sambuc
106*0a6a1f1dSLionel Sambuc  if (NOT DEFINED LLVM_INCLUDE_TESTS)
107*0a6a1f1dSLionel Sambuc    set(LLVM_INCLUDE_TESTS ON)
108*0a6a1f1dSLionel Sambuc  endif()
109f4a2713aSLionel Sambuc
110*0a6a1f1dSLionel Sambuc  include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
111*0a6a1f1dSLionel Sambuc  link_directories("${LLVM_LIBRARY_DIR}")
112f4a2713aSLionel Sambuc
113*0a6a1f1dSLionel Sambuc  set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
114*0a6a1f1dSLionel Sambuc  set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
115*0a6a1f1dSLionel Sambuc  set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
116f4a2713aSLionel Sambuc
117*0a6a1f1dSLionel Sambuc  if(LLVM_INCLUDE_TESTS)
118*0a6a1f1dSLionel Sambuc    # Check prebuilt llvm/utils.
119*0a6a1f1dSLionel Sambuc    if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
120*0a6a1f1dSLionel Sambuc        AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
121*0a6a1f1dSLionel Sambuc        AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
122*0a6a1f1dSLionel Sambuc      set(LLVM_UTILS_PROVIDED ON)
123*0a6a1f1dSLionel Sambuc    endif()
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc    if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
126*0a6a1f1dSLionel Sambuc      set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
127*0a6a1f1dSLionel Sambuc      if(NOT LLVM_UTILS_PROVIDED)
128*0a6a1f1dSLionel Sambuc        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
129*0a6a1f1dSLionel Sambuc        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
130*0a6a1f1dSLionel Sambuc        add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
131*0a6a1f1dSLionel Sambuc        set(LLVM_UTILS_PROVIDED ON)
132*0a6a1f1dSLionel Sambuc        set(CLANG_TEST_DEPS FileCheck count not)
133*0a6a1f1dSLionel Sambuc      endif()
134*0a6a1f1dSLionel Sambuc      set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
135*0a6a1f1dSLionel Sambuc      if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
136*0a6a1f1dSLionel Sambuc          AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
137*0a6a1f1dSLionel Sambuc          AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
138*0a6a1f1dSLionel Sambuc        add_subdirectory(${UNITTEST_DIR} utils/unittest)
139*0a6a1f1dSLionel Sambuc      endif()
140*0a6a1f1dSLionel Sambuc    else()
141*0a6a1f1dSLionel Sambuc      # Seek installed Lit.
142*0a6a1f1dSLionel Sambuc      find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
143*0a6a1f1dSLionel Sambuc        DOC "Path to lit.py")
144*0a6a1f1dSLionel Sambuc    endif()
145*0a6a1f1dSLionel Sambuc
146*0a6a1f1dSLionel Sambuc    if(LLVM_LIT)
147f4a2713aSLionel Sambuc      # Define the default arguments to use with 'lit', and an option for the user
148f4a2713aSLionel Sambuc      # to override.
149f4a2713aSLionel Sambuc      set(LIT_ARGS_DEFAULT "-sv")
150f4a2713aSLionel Sambuc      if (MSVC OR XCODE)
151f4a2713aSLionel Sambuc        set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
152f4a2713aSLionel Sambuc      endif()
153f4a2713aSLionel Sambuc      set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
154f4a2713aSLionel Sambuc
155*0a6a1f1dSLionel Sambuc      # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
156*0a6a1f1dSLionel Sambuc      if( WIN32 AND NOT CYGWIN )
157*0a6a1f1dSLionel Sambuc        set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
158*0a6a1f1dSLionel Sambuc      endif()
159*0a6a1f1dSLionel Sambuc    else()
160*0a6a1f1dSLionel Sambuc      set(LLVM_INCLUDE_TESTS OFF)
161*0a6a1f1dSLionel Sambuc    endif()
162*0a6a1f1dSLionel Sambuc  endif()
163f4a2713aSLionel Sambuc
164f4a2713aSLionel Sambuc  set( CLANG_BUILT_STANDALONE 1 )
165*0a6a1f1dSLionel Sambuc  set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
166*0a6a1f1dSLionel Sambucelse()
167*0a6a1f1dSLionel Sambuc  set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
168*0a6a1f1dSLionel Sambucendif()
169f4a2713aSLionel Sambuc
170f4a2713aSLionel Sambucfind_package(LibXml2)
171f4a2713aSLionel Sambucif (LIBXML2_FOUND)
172f4a2713aSLionel Sambuc  set(CLANG_HAVE_LIBXML 1)
173f4a2713aSLionel Sambucendif()
174f4a2713aSLionel Sambuc
175f4a2713aSLionel Sambucset(CLANG_RESOURCE_DIR "" CACHE STRING
176f4a2713aSLionel Sambuc  "Relative directory from the Clang binary to its resource files.")
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambucset(C_INCLUDE_DIRS "" CACHE STRING
179f4a2713aSLionel Sambuc  "Colon separated list of directories clang will search for headers.")
180f4a2713aSLionel Sambuc
181f4a2713aSLionel Sambucset(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
182f4a2713aSLionel Sambucset(DEFAULT_SYSROOT "" CACHE PATH
183f4a2713aSLionel Sambuc  "Default <path> to all compiler invocations for --sysroot=<path>." )
184f4a2713aSLionel Sambuc
185f4a2713aSLionel Sambucset(CLANG_VENDOR "" CACHE STRING
186f4a2713aSLionel Sambuc  "Vendor-specific text for showing with version information.")
187f4a2713aSLionel Sambuc
188f4a2713aSLionel Sambucif( CLANG_VENDOR )
189f4a2713aSLionel Sambuc  add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
190f4a2713aSLionel Sambucendif()
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambucset(CLANG_REPOSITORY_STRING "" CACHE STRING
193f4a2713aSLionel Sambuc  "Vendor-specific text for showing the repository the source is taken from.")
194f4a2713aSLionel Sambuc
195f4a2713aSLionel Sambucif(CLANG_REPOSITORY_STRING)
196f4a2713aSLionel Sambuc  add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
197f4a2713aSLionel Sambucendif()
198f4a2713aSLionel Sambuc
199f4a2713aSLionel Sambucset(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
200f4a2713aSLionel Sambuc  "Vendor-specific uti.")
201f4a2713aSLionel Sambuc
202*0a6a1f1dSLionel Sambuc# The libdir suffix must exactly match whatever LLVM's configuration used.
203*0a6a1f1dSLionel Sambucset(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
204*0a6a1f1dSLionel Sambuc
205f4a2713aSLionel Sambucset(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
206f4a2713aSLionel Sambucset(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambucif( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
209f4a2713aSLionel Sambuc  message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
210f4a2713aSLionel Sambuc"the makefiles distributed with LLVM. Please create a directory and run cmake "
211f4a2713aSLionel Sambuc"from there, passing the path to this source directory as the last argument. "
212f4a2713aSLionel Sambuc"This process created the file `CMakeCache.txt' and the directory "
213f4a2713aSLionel Sambuc"`CMakeFiles'. Please delete them.")
214f4a2713aSLionel Sambucendif()
215f4a2713aSLionel Sambuc
216f4a2713aSLionel Sambucif( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
217f4a2713aSLionel Sambuc  file(GLOB_RECURSE
218f4a2713aSLionel Sambuc    tablegenned_files_on_include_dir
219f4a2713aSLionel Sambuc    "${CLANG_SOURCE_DIR}/include/clang/*.inc")
220f4a2713aSLionel Sambuc  if( tablegenned_files_on_include_dir )
221f4a2713aSLionel Sambuc    message(FATAL_ERROR "Apparently there is a previous in-source build, "
222f4a2713aSLionel Sambuc"probably as the result of running `configure' and `make' on "
223f4a2713aSLionel Sambuc"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
224f4a2713aSLionel Sambuc"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
225f4a2713aSLionel Sambuc  endif()
226f4a2713aSLionel Sambucendif()
227f4a2713aSLionel Sambuc
228f4a2713aSLionel Sambuc# Compute the Clang version from the LLVM version.
229f4a2713aSLionel Sambucstring(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
230f4a2713aSLionel Sambuc  ${PACKAGE_VERSION})
231f4a2713aSLionel Sambucmessage(STATUS "Clang version: ${CLANG_VERSION}")
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambucstring(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
234f4a2713aSLionel Sambuc  ${CLANG_VERSION})
235f4a2713aSLionel Sambucstring(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
236f4a2713aSLionel Sambuc  ${CLANG_VERSION})
237f4a2713aSLionel Sambucif (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
238f4a2713aSLionel Sambuc  set(CLANG_HAS_VERSION_PATCHLEVEL 1)
239f4a2713aSLionel Sambuc  string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
240f4a2713aSLionel Sambuc    ${CLANG_VERSION})
241f4a2713aSLionel Sambucelse()
242f4a2713aSLionel Sambuc  set(CLANG_HAS_VERSION_PATCHLEVEL 0)
243f4a2713aSLionel Sambucendif()
244f4a2713aSLionel Sambuc
245f4a2713aSLionel Sambuc# Configure the Version.inc file.
246f4a2713aSLionel Sambucconfigure_file(
247f4a2713aSLionel Sambuc  ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
248f4a2713aSLionel Sambuc  ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
249f4a2713aSLionel Sambuc
250f4a2713aSLionel Sambuc# Add appropriate flags for GCC
251f4a2713aSLionel Sambucif (LLVM_COMPILER_IS_GCC_COMPATIBLE)
252*0a6a1f1dSLionel Sambuc  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -fno-strict-aliasing")
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc  # Enable -pedantic for Clang even if it's not enabled for LLVM.
255f4a2713aSLionel Sambuc  if (NOT LLVM_ENABLE_PEDANTIC)
256f4a2713aSLionel Sambuc    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
257f4a2713aSLionel Sambuc  endif ()
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc  check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
260f4a2713aSLionel Sambuc  if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
261f4a2713aSLionel Sambuc    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
262f4a2713aSLionel Sambuc  endif()
263f4a2713aSLionel Sambucendif ()
264f4a2713aSLionel Sambuc
265*0a6a1f1dSLionel Sambuc# Determine HOST_LINK_VERSION on Darwin.
266*0a6a1f1dSLionel Sambucset(HOST_LINK_VERSION)
267f4a2713aSLionel Sambucif (APPLE)
268*0a6a1f1dSLionel Sambuc  set(LD_V_OUTPUT)
269*0a6a1f1dSLionel Sambuc  execute_process(
270*0a6a1f1dSLionel Sambuc    COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
271*0a6a1f1dSLionel Sambuc    RESULT_VARIABLE HAD_ERROR
272*0a6a1f1dSLionel Sambuc    OUTPUT_VARIABLE LD_V_OUTPUT
273*0a6a1f1dSLionel Sambuc  )
274*0a6a1f1dSLionel Sambuc  if (NOT HAD_ERROR)
275*0a6a1f1dSLionel Sambuc    if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
276*0a6a1f1dSLionel Sambuc      string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
277*0a6a1f1dSLionel Sambuc    elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
278*0a6a1f1dSLionel Sambuc      string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
279*0a6a1f1dSLionel Sambuc    endif()
280*0a6a1f1dSLionel Sambuc  else()
281*0a6a1f1dSLionel Sambuc    message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
282*0a6a1f1dSLionel Sambuc  endif()
283f4a2713aSLionel Sambucendif()
284f4a2713aSLionel Sambuc
285f4a2713aSLionel Sambucconfigure_file(
286f4a2713aSLionel Sambuc  ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
287f4a2713aSLionel Sambuc  ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
288f4a2713aSLionel Sambuc
289*0a6a1f1dSLionel Sambucinclude(CMakeParseArguments)
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambucfunction(clang_tablegen)
292f4a2713aSLionel Sambuc  # Syntax:
293f4a2713aSLionel Sambuc  # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
294f4a2713aSLionel Sambuc  # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
295f4a2713aSLionel Sambuc  #
296f4a2713aSLionel Sambuc  # Generates a custom command for invoking tblgen as
297f4a2713aSLionel Sambuc  #
298f4a2713aSLionel Sambuc  # tblgen source-file -o=output-file tablegen-arg ...
299f4a2713aSLionel Sambuc  #
300f4a2713aSLionel Sambuc  # and, if cmake-target-name is provided, creates a custom target for
301f4a2713aSLionel Sambuc  # executing the custom command depending on output-file. It is
302f4a2713aSLionel Sambuc  # possible to list more files to depend after DEPENDS.
303f4a2713aSLionel Sambuc
304*0a6a1f1dSLionel Sambuc  cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
305f4a2713aSLionel Sambuc
306f4a2713aSLionel Sambuc  if( NOT CTG_SOURCE )
307f4a2713aSLionel Sambuc    message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
308f4a2713aSLionel Sambuc  endif()
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc  set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
311*0a6a1f1dSLionel Sambuc  tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS})
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc  if(CTG_TARGET)
314*0a6a1f1dSLionel Sambuc    add_public_tablegen_target(${CTG_TARGET})
315f4a2713aSLionel Sambuc    set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
316*0a6a1f1dSLionel Sambuc    set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
317f4a2713aSLionel Sambuc  endif()
318f4a2713aSLionel Sambucendfunction(clang_tablegen)
319f4a2713aSLionel Sambuc
320f4a2713aSLionel Sambucmacro(add_clang_library name)
321*0a6a1f1dSLionel Sambuc  cmake_parse_arguments(ARG
322*0a6a1f1dSLionel Sambuc    ""
323*0a6a1f1dSLionel Sambuc    ""
324*0a6a1f1dSLionel Sambuc    "ADDITIONAL_HEADERS"
325*0a6a1f1dSLionel Sambuc    ${ARGN})
326*0a6a1f1dSLionel Sambuc  set(srcs)
327f4a2713aSLionel Sambuc  if(MSVC_IDE OR XCODE)
328f4a2713aSLionel Sambuc    # Add public headers
329f4a2713aSLionel Sambuc    file(RELATIVE_PATH lib_path
330f4a2713aSLionel Sambuc      ${CLANG_SOURCE_DIR}/lib/
331f4a2713aSLionel Sambuc      ${CMAKE_CURRENT_SOURCE_DIR}
332f4a2713aSLionel Sambuc    )
333f4a2713aSLionel Sambuc    if(NOT lib_path MATCHES "^[.][.]")
334f4a2713aSLionel Sambuc      file( GLOB_RECURSE headers
335f4a2713aSLionel Sambuc        ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
336f4a2713aSLionel Sambuc        ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
337f4a2713aSLionel Sambuc      )
338f4a2713aSLionel Sambuc      set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
339f4a2713aSLionel Sambuc
340f4a2713aSLionel Sambuc      file( GLOB_RECURSE tds
341f4a2713aSLionel Sambuc        ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
342f4a2713aSLionel Sambuc      )
343f4a2713aSLionel Sambuc      source_group("TableGen descriptions" FILES ${tds})
344f4a2713aSLionel Sambuc      set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
345f4a2713aSLionel Sambuc
346*0a6a1f1dSLionel Sambuc      if(headers OR tds)
347*0a6a1f1dSLionel Sambuc	set(srcs ${headers} ${tds})
348*0a6a1f1dSLionel Sambuc      endif()
349f4a2713aSLionel Sambuc    endif()
350f4a2713aSLionel Sambuc  endif(MSVC_IDE OR XCODE)
351*0a6a1f1dSLionel Sambuc  if(srcs OR ARG_ADDITIONAL_HEADERS)
352*0a6a1f1dSLionel Sambuc    set(srcs
353*0a6a1f1dSLionel Sambuc      ADDITIONAL_HEADERS
354*0a6a1f1dSLionel Sambuc      ${srcs}
355*0a6a1f1dSLionel Sambuc      ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
356*0a6a1f1dSLionel Sambuc      )
357f4a2713aSLionel Sambuc  endif()
358*0a6a1f1dSLionel Sambuc  llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
359f4a2713aSLionel Sambuc
360*0a6a1f1dSLionel Sambuc  if(TARGET ${name})
361*0a6a1f1dSLionel Sambuc    target_link_libraries(${name} ${cmake_2_8_12_INTERFACE} ${LLVM_COMMON_LIBS})
362f4a2713aSLionel Sambuc
363f4a2713aSLionel Sambuc    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
364f4a2713aSLionel Sambuc      install(TARGETS ${name}
365f4a2713aSLionel Sambuc        LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
366f4a2713aSLionel Sambuc        ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
367f4a2713aSLionel Sambuc        RUNTIME DESTINATION bin)
368f4a2713aSLionel Sambuc    endif()
369*0a6a1f1dSLionel Sambuc    set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
370*0a6a1f1dSLionel Sambuc  else()
371*0a6a1f1dSLionel Sambuc    # Add empty "phony" target
372*0a6a1f1dSLionel Sambuc    add_custom_target(${name})
373*0a6a1f1dSLionel Sambuc  endif()
374f4a2713aSLionel Sambuc
375f4a2713aSLionel Sambuc  set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
376f4a2713aSLionel Sambucendmacro(add_clang_library)
377f4a2713aSLionel Sambuc
378f4a2713aSLionel Sambucmacro(add_clang_executable name)
379f4a2713aSLionel Sambuc  add_llvm_executable( ${name} ${ARGN} )
380f4a2713aSLionel Sambuc  set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
381f4a2713aSLionel Sambucendmacro(add_clang_executable)
382f4a2713aSLionel Sambuc
383*0a6a1f1dSLionel Sambucset(CMAKE_INCLUDE_CURRENT_DIR ON)
384*0a6a1f1dSLionel Sambuc
385f4a2713aSLionel Sambucinclude_directories(BEFORE
386f4a2713aSLionel Sambuc  ${CMAKE_CURRENT_BINARY_DIR}/include
387f4a2713aSLionel Sambuc  ${CMAKE_CURRENT_SOURCE_DIR}/include
388f4a2713aSLionel Sambuc  )
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambucif (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
391*0a6a1f1dSLionel Sambuc  install(DIRECTORY include/clang include/clang-c
392f4a2713aSLionel Sambuc    DESTINATION include
393f4a2713aSLionel Sambuc    FILES_MATCHING
394f4a2713aSLionel Sambuc    PATTERN "*.def"
395f4a2713aSLionel Sambuc    PATTERN "*.h"
396f4a2713aSLionel Sambuc    PATTERN "config.h" EXCLUDE
397f4a2713aSLionel Sambuc    PATTERN ".svn" EXCLUDE
398f4a2713aSLionel Sambuc    )
399f4a2713aSLionel Sambuc
400*0a6a1f1dSLionel Sambuc  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
401f4a2713aSLionel Sambuc    DESTINATION include
402f4a2713aSLionel Sambuc    FILES_MATCHING
403f4a2713aSLionel Sambuc    PATTERN "CMakeFiles" EXCLUDE
404f4a2713aSLionel Sambuc    PATTERN "*.inc"
405f4a2713aSLionel Sambuc    )
406f4a2713aSLionel Sambucendif()
407f4a2713aSLionel Sambuc
408f4a2713aSLionel Sambucinstall(DIRECTORY include/clang-c
409f4a2713aSLionel Sambuc  DESTINATION include
410f4a2713aSLionel Sambuc  FILES_MATCHING
411f4a2713aSLionel Sambuc  PATTERN "*.h"
412f4a2713aSLionel Sambuc  PATTERN ".svn" EXCLUDE
413f4a2713aSLionel Sambuc  )
414f4a2713aSLionel Sambuc
415f4a2713aSLionel Sambucadd_definitions( -D_GNU_SOURCE )
416f4a2713aSLionel Sambuc
417f4a2713aSLionel Sambucoption(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
418*0a6a1f1dSLionel Sambucif (CLANG_ENABLE_ARCMT)
419*0a6a1f1dSLionel Sambuc  set(ENABLE_CLANG_ARCMT "1")
420*0a6a1f1dSLionel Sambucelse()
421*0a6a1f1dSLionel Sambuc  set(ENABLE_CLANG_ARCMT "0")
422f4a2713aSLionel Sambucendif()
423f4a2713aSLionel Sambuc
424*0a6a1f1dSLionel Sambucoption(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
425*0a6a1f1dSLionel Sambucif (CLANG_ENABLE_STATIC_ANALYZER)
426*0a6a1f1dSLionel Sambuc  set(ENABLE_CLANG_STATIC_ANALYZER "1")
427*0a6a1f1dSLionel Sambucelse()
428*0a6a1f1dSLionel Sambuc  set(ENABLE_CLANG_STATIC_ANALYZER "0")
429f4a2713aSLionel Sambucendif()
430f4a2713aSLionel Sambuc
431f4a2713aSLionel Sambucif (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
432f4a2713aSLionel Sambuc  message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
433f4a2713aSLionel Sambucendif()
434f4a2713aSLionel Sambuc
435f4a2713aSLionel Sambucif(CLANG_ENABLE_ARCMT)
436f4a2713aSLionel Sambuc  add_definitions(-DCLANG_ENABLE_ARCMT)
437*0a6a1f1dSLionel Sambuc  add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
438f4a2713aSLionel Sambucendif()
439f4a2713aSLionel Sambucif(CLANG_ENABLE_STATIC_ANALYZER)
440f4a2713aSLionel Sambuc  add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
441f4a2713aSLionel Sambucendif()
442f4a2713aSLionel Sambuc
443f4a2713aSLionel Sambuc# Clang version information
444f4a2713aSLionel Sambucset(CLANG_EXECUTABLE_VERSION
445f4a2713aSLionel Sambuc     "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
446f4a2713aSLionel Sambuc    "Version number that will be placed into the clang executable, in the form XX.YY")
447f4a2713aSLionel Sambucset(LIBCLANG_LIBRARY_VERSION
448f4a2713aSLionel Sambuc     "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
449f4a2713aSLionel Sambuc    "Version number that will be placed into the libclang library , in the form XX.YY")
450f4a2713aSLionel Sambucmark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambucadd_subdirectory(utils/TableGen)
453f4a2713aSLionel Sambuc
454f4a2713aSLionel Sambucadd_subdirectory(include)
455*0a6a1f1dSLionel Sambuc
456*0a6a1f1dSLionel Sambuc# All targets below may depend on all tablegen'd files.
457*0a6a1f1dSLionel Sambucget_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
458*0a6a1f1dSLionel Sambuclist(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
459*0a6a1f1dSLionel Sambuc
460f4a2713aSLionel Sambucadd_subdirectory(lib)
461f4a2713aSLionel Sambucadd_subdirectory(tools)
462f4a2713aSLionel Sambucadd_subdirectory(runtime)
463f4a2713aSLionel Sambuc
464f4a2713aSLionel Sambucoption(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
465*0a6a1f1dSLionel Sambucif (CLANG_BUILD_EXAMPLES)
466*0a6a1f1dSLionel Sambuc  set(ENABLE_CLANG_EXAMPLES "1")
467*0a6a1f1dSLionel Sambucelse()
468*0a6a1f1dSLionel Sambuc  set(ENABLE_CLANG_EXAMPLES "0")
469*0a6a1f1dSLionel Sambucendif()
470f4a2713aSLionel Sambucadd_subdirectory(examples)
471f4a2713aSLionel Sambuc
472f4a2713aSLionel Sambucoption(CLANG_INCLUDE_TESTS
473f4a2713aSLionel Sambuc       "Generate build targets for the Clang unit tests."
474f4a2713aSLionel Sambuc       ${LLVM_INCLUDE_TESTS})
475f4a2713aSLionel Sambuc
476f4a2713aSLionel Sambucif( CLANG_INCLUDE_TESTS )
477*0a6a1f1dSLionel Sambuc  if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
478f4a2713aSLionel Sambuc    add_subdirectory(unittests)
479*0a6a1f1dSLionel Sambuc    list(APPEND CLANG_TEST_DEPS ClangUnitTests)
480*0a6a1f1dSLionel Sambuc    list(APPEND CLANG_TEST_PARAMS
481*0a6a1f1dSLionel Sambuc      clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
482*0a6a1f1dSLionel Sambuc      )
483*0a6a1f1dSLionel Sambuc  endif()
484*0a6a1f1dSLionel Sambuc  add_subdirectory(test)
485*0a6a1f1dSLionel Sambuc
486*0a6a1f1dSLionel Sambuc  if(CLANG_BUILT_STANDALONE)
487*0a6a1f1dSLionel Sambuc    # Add a global check rule now that all subdirectories have been traversed
488*0a6a1f1dSLionel Sambuc    # and we know the total set of lit testsuites.
489*0a6a1f1dSLionel Sambuc    get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
490*0a6a1f1dSLionel Sambuc    get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
491*0a6a1f1dSLionel Sambuc    get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
492*0a6a1f1dSLionel Sambuc    get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
493*0a6a1f1dSLionel Sambuc    add_lit_target(check-all
494*0a6a1f1dSLionel Sambuc      "Running all regression tests"
495*0a6a1f1dSLionel Sambuc      ${LLVM_LIT_TESTSUITES}
496*0a6a1f1dSLionel Sambuc      PARAMS ${LLVM_LIT_PARAMS}
497*0a6a1f1dSLionel Sambuc      DEPENDS ${LLVM_LIT_DEPENDS}
498*0a6a1f1dSLionel Sambuc      ARGS ${LLVM_LIT_EXTRA_ARGS}
499*0a6a1f1dSLionel Sambuc      )
500*0a6a1f1dSLionel Sambuc  endif()
501f4a2713aSLionel Sambucendif()
502f4a2713aSLionel Sambuc
503f4a2713aSLionel Sambucoption(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
504f4a2713aSLionel Sambuc  ${LLVM_INCLUDE_DOCS})
505f4a2713aSLionel Sambucif( CLANG_INCLUDE_DOCS )
506f4a2713aSLionel Sambuc  add_subdirectory(docs)
507f4a2713aSLionel Sambucendif()
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambucset(CLANG_ORDER_FILE "" CACHE FILEPATH
510f4a2713aSLionel Sambuc  "Order file to use when compiling clang in order to improve startup time.")
511*0a6a1f1dSLionel Sambuc
512*0a6a1f1dSLionel Sambucif (CLANG_BUILT_STANDALONE)
513*0a6a1f1dSLionel Sambuc  # Generate a list of CMake library targets so that other CMake projects can
514*0a6a1f1dSLionel Sambuc  # link against them. LLVM calls its version of this file LLVMExports.cmake, but
515*0a6a1f1dSLionel Sambuc  # the usual CMake convention seems to be ${Project}Targets.cmake.
516*0a6a1f1dSLionel Sambuc  set(CLANG_INSTALL_PACKAGE_DIR share/clang/cmake)
517*0a6a1f1dSLionel Sambuc  set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
518*0a6a1f1dSLionel Sambuc  get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
519*0a6a1f1dSLionel Sambuc  export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
520*0a6a1f1dSLionel Sambuc
521*0a6a1f1dSLionel Sambuc  # Install a <prefix>/share/clang/cmake/ClangConfig.cmake file so that
522*0a6a1f1dSLionel Sambuc  # find_package(Clang) works. Install the target list with it.
523*0a6a1f1dSLionel Sambuc  install(FILES
524*0a6a1f1dSLionel Sambuc    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
525*0a6a1f1dSLionel Sambuc    ${CLANG_BINARY_DIR}/share/clang/cmake/ClangTargets.cmake
526*0a6a1f1dSLionel Sambuc    DESTINATION share/clang/cmake)
527*0a6a1f1dSLionel Sambuc
528*0a6a1f1dSLionel Sambuc  # Also copy ClangConfig.cmake to the build directory so that dependent projects
529*0a6a1f1dSLionel Sambuc  # can build against a build directory of Clang more easily.
530*0a6a1f1dSLionel Sambuc  configure_file(
531*0a6a1f1dSLionel Sambuc    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
532*0a6a1f1dSLionel Sambuc    ${CLANG_BINARY_DIR}/share/clang/cmake/ClangConfig.cmake
533*0a6a1f1dSLionel Sambuc    COPYONLY)
534*0a6a1f1dSLionel Sambucendif ()
535