blob: cfcd2212cfaf81151a5d8ce114d8053d334f050f [file] [log] [blame]
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001cmake_minimum_required(VERSION 3.4.3)
Stephen Hines651f13c2014-04-23 16:59:28 -07002
Oscar Fuentes67410b32011-02-03 20:57:53 +00003# If we are not building as a part of LLVM, build Clang as an
4# standalone project, using LLVM as an external library:
5if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
6 project(Clang)
Oscar Fuentes67410b32011-02-03 20:57:53 +00007
Stephen Hines651f13c2014-04-23 16:59:28 -07008 # Rely on llvm-config.
9 set(CONFIG_OUTPUT)
10 find_program(LLVM_CONFIG "llvm-config")
11 if(LLVM_CONFIG)
12 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
13 set(CONFIG_COMMAND ${LLVM_CONFIG}
14 "--assertion-mode"
15 "--bindir"
16 "--libdir"
17 "--includedir"
18 "--prefix"
19 "--src-root")
20 execute_process(
21 COMMAND ${CONFIG_COMMAND}
22 RESULT_VARIABLE HAD_ERROR
23 OUTPUT_VARIABLE CONFIG_OUTPUT
24 )
25 if(NOT HAD_ERROR)
26 string(REGEX REPLACE
27 "[ \t]*[\r\n]+[ \t]*" ";"
28 CONFIG_OUTPUT ${CONFIG_OUTPUT})
Oscar Fuentes67410b32011-02-03 20:57:53 +000029 else()
Stephen Hines651f13c2014-04-23 16:59:28 -070030 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
31 message(STATUS "${CONFIG_COMMAND_STR}")
32 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
Oscar Fuentes67410b32011-02-03 20:57:53 +000033 endif()
Hans Wennborge90340a2013-08-23 18:05:24 +000034 else()
Stephen Hines651f13c2014-04-23 16:59:28 -070035 message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
Oscar Fuentes67410b32011-02-03 20:57:53 +000036 endif()
37
Stephen Hines651f13c2014-04-23 16:59:28 -070038 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
39 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
40 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
41 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
42 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
43 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
Oscar Fuentes67410b32011-02-03 20:57:53 +000044
Stephen Hines651f13c2014-04-23 16:59:28 -070045 if(NOT MSVC_IDE)
46 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
47 CACHE BOOL "Enable assertions")
48 # Assertions should follow llvm-config's.
49 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
50 endif()
Oscar Fuentes67410b32011-02-03 20:57:53 +000051
Stephen Hines651f13c2014-04-23 16:59:28 -070052 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
53 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
54 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
55 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
56 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
57
58 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
59 NO_DEFAULT_PATH)
60
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070061 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
Stephen Hines651f13c2014-04-23 16:59:28 -070062 set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
63 if(EXISTS ${LLVMCONFIG_FILE})
64 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
65 include(${LLVMCONFIG_FILE})
66 else()
67 message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
68 endif()
69
70 # They are used as destination of target generators.
71 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
Stephen Hines0e2c34f2015-03-23 12:09:02 -070072 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
73 if(WIN32 OR CYGWIN)
74 # DLL platform -- put DLLs into bin.
75 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
76 else()
77 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
78 endif()
Stephen Hines651f13c2014-04-23 16:59:28 -070079
80 option(LLVM_INSTALL_TOOLCHAIN_ONLY
81 "Only include toolchain files in the 'install' target." OFF)
82
83 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
84 "Set to ON to force using an old, unsupported host toolchain." OFF)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080085 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
Hans Wennborg885e36e2013-08-24 00:22:23 +000086
Oscar Fuentes67410b32011-02-03 20:57:53 +000087 include(AddLLVM)
88 include(TableGen)
Oscar Fuentes67410b32011-02-03 20:57:53 +000089 include(HandleLLVMOptions)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070090 include(VersionFromVCS)
Oscar Fuentes67410b32011-02-03 20:57:53 +000091
92 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
93
Stephen Hines651f13c2014-04-23 16:59:28 -070094 if (NOT DEFINED LLVM_INCLUDE_TESTS)
95 set(LLVM_INCLUDE_TESTS ON)
Chandler Carruth9224fb82012-07-02 20:52:55 +000096 endif()
Stephen Hines651f13c2014-04-23 16:59:28 -070097
98 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
99 link_directories("${LLVM_LIBRARY_DIR}")
Chandler Carruth9224fb82012-07-02 20:52:55 +0000100
Oscar Fuentes67410b32011-02-03 20:57:53 +0000101 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700102 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
103 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
Oscar Fuentes67410b32011-02-03 20:57:53 +0000104
Stephen Hines651f13c2014-04-23 16:59:28 -0700105 if(LLVM_INCLUDE_TESTS)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800106 set(Python_ADDITIONAL_VERSIONS 2.7)
107 include(FindPythonInterp)
108 if(NOT PYTHONINTERP_FOUND)
109 message(FATAL_ERROR
110"Unable to find Python interpreter, required for builds and testing.
111
112Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
113 endif()
114
115 if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
116 message(FATAL_ERROR "Python 2.7 or newer is required")
117 endif()
118
Stephen Hines651f13c2014-04-23 16:59:28 -0700119 # 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
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000165 set( CLANG_BUILT_STANDALONE 1 )
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700166 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
167else()
168 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
169endif()
Douglas Gregora34173e2012-12-18 19:39:40 +0000170
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700171# Make sure that our source directory is on the current cmake module path so that
172# we can include cmake files from this directory.
173list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
174
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800175find_package(LibXml2 2.5.3 QUIET)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700176if (LIBXML2_FOUND)
177 set(CLANG_HAVE_LIBXML 1)
Oscar Fuentes67410b32011-02-03 20:57:53 +0000178endif()
Douglas Gregor34d9ffa2009-09-16 21:59:05 +0000179
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000180set(CLANG_RESOURCE_DIR "" CACHE STRING
181 "Relative directory from the Clang binary to its resource files.")
182
183set(C_INCLUDE_DIRS "" CACHE STRING
184 "Colon separated list of directories clang will search for headers.")
185
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000186set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
187set(DEFAULT_SYSROOT "" CACHE PATH
188 "Default <path> to all compiler invocations for --sysroot=<path>." )
189
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700190set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
191
192set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL
193 "enable x86 relax relocations by default")
194
195set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
196 "Default C++ stdlib to use (empty for architecture default, \"libstdc++\" or \"libc++\"")
197if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
198 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
199 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
200 message(WARNING "Resetting default C++ stdlib to use architecture default")
201 set(CLANG_DEFAULT_CXX_STDLIB "")
202endif()
203
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800204set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700205 "Default OpenMP runtime used by -fopenmp.")
206
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700207set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
Oscar Fuentesf752d5b2011-05-20 15:57:59 +0000208 "Vendor-specific text for showing with version information.")
209
210if( CLANG_VENDOR )
211 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
212endif()
213
Michael Gottesmana8c0e4f2013-08-15 19:22:33 +0000214set(CLANG_REPOSITORY_STRING "" CACHE STRING
215 "Vendor-specific text for showing the repository the source is taken from.")
216
217if(CLANG_REPOSITORY_STRING)
218 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
219endif()
220
Michael Gottesmanf3535112013-08-15 19:22:36 +0000221set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
222 "Vendor-specific uti.")
223
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700224# The libdir suffix must exactly match whatever LLVM's configuration used.
225set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
226
Daniel Dunbar91ee77a2009-09-17 00:07:10 +0000227set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
228set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
229
Chandler Carruth63e9c0d2010-04-17 20:12:02 +0000230if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
231 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
232"the makefiles distributed with LLVM. Please create a directory and run cmake "
233"from there, passing the path to this source directory as the last argument. "
234"This process created the file `CMakeCache.txt' and the directory "
235"`CMakeFiles'. Please delete them.")
236endif()
237
238if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
239 file(GLOB_RECURSE
240 tablegenned_files_on_include_dir
241 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
242 if( tablegenned_files_on_include_dir )
243 message(FATAL_ERROR "Apparently there is a previous in-source build, "
244"probably as the result of running `configure' and `make' on "
245"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
246"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
247 endif()
248endif()
249
Daniel Dunbarc4b8e922010-06-25 23:34:47 +0000250# Compute the Clang version from the LLVM version.
Michael J. Spencer5a7f3492010-09-10 21:13:16 +0000251string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
Daniel Dunbarc4b8e922010-06-25 23:34:47 +0000252 ${PACKAGE_VERSION})
Douglas Gregor34d9ffa2009-09-16 21:59:05 +0000253message(STATUS "Clang version: ${CLANG_VERSION}")
Douglas Gregor7f7b7482009-08-23 05:28:29 +0000254
Daniel Dunbara5107672010-06-25 17:33:46 +0000255string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
256 ${CLANG_VERSION})
257string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
258 ${CLANG_VERSION})
259if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
260 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
261 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
262 ${CLANG_VERSION})
263else()
264 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
265endif()
266
267# Configure the Version.inc file.
268configure_file(
269 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
270 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
271
Douglas Gregor9df3faf2009-09-18 14:47:57 +0000272# Add appropriate flags for GCC
Oscar Fuentesc5cd2522011-05-11 13:53:30 +0000273if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800274 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
275 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
276 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
277 endif ()
Richard Smith19258e82013-04-22 14:51:21 +0000278
279 # Enable -pedantic for Clang even if it's not enabled for LLVM.
280 if (NOT LLVM_ENABLE_PEDANTIC)
281 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
282 endif ()
Jordan Rose549a2922013-03-02 00:49:47 +0000283
284 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
285 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
286 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
287 endif()
Douglas Gregor9df3faf2009-09-18 14:47:57 +0000288endif ()
289
Stephen Hines176edba2014-12-01 14:53:08 -0800290# Determine HOST_LINK_VERSION on Darwin.
291set(HOST_LINK_VERSION)
292if (APPLE)
293 set(LD_V_OUTPUT)
294 execute_process(
295 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
296 RESULT_VARIABLE HAD_ERROR
297 OUTPUT_VARIABLE LD_V_OUTPUT
298 )
299 if (NOT HAD_ERROR)
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700300 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
301 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
302 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
303 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
Stephen Hines176edba2014-12-01 14:53:08 -0800304 endif()
305 else()
306 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
307 endif()
308endif()
309
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000310configure_file(
311 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
312 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
313
Stephen Hines651f13c2014-04-23 16:59:28 -0700314include(CMakeParseArguments)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700315include(AddClang)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800316
Stephen Hines651f13c2014-04-23 16:59:28 -0700317set(CMAKE_INCLUDE_CURRENT_DIR ON)
318
Oscar Fuentes617508f2011-03-17 19:03:04 +0000319include_directories(BEFORE
Douglas Gregora393e9e2009-03-16 23:06:59 +0000320 ${CMAKE_CURRENT_BINARY_DIR}/include
Oscar Fuentes617508f2011-03-17 19:03:04 +0000321 ${CMAKE_CURRENT_SOURCE_DIR}/include
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000322 )
323
Hans Wennborg885e36e2013-08-24 00:22:23 +0000324if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Stephen Hines651f13c2014-04-23 16:59:28 -0700325 install(DIRECTORY include/clang include/clang-c
Hans Wennborg885e36e2013-08-24 00:22:23 +0000326 DESTINATION include
327 FILES_MATCHING
328 PATTERN "*.def"
329 PATTERN "*.h"
330 PATTERN "config.h" EXCLUDE
331 PATTERN ".svn" EXCLUDE
332 )
Chris Lattnerbc6ec752008-11-11 18:39:10 +0000333
Stephen Hines651f13c2014-04-23 16:59:28 -0700334 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
Hans Wennborg885e36e2013-08-24 00:22:23 +0000335 DESTINATION include
336 FILES_MATCHING
337 PATTERN "CMakeFiles" EXCLUDE
338 PATTERN "*.inc"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700339 PATTERN "*.h"
Hans Wennborg885e36e2013-08-24 00:22:23 +0000340 )
341endif()
342
Dylan Noblesmithcc8a9452012-02-14 15:54:49 +0000343add_definitions( -D_GNU_SOURCE )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000344
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700345option(CLANG_BUILD_TOOLS
346 "Build the Clang tools. If OFF, just generate build targets." ON)
347
Jordan Rosee6385032013-08-22 15:50:02 +0000348option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
Stephen Hines176edba2014-12-01 14:53:08 -0800349if (CLANG_ENABLE_ARCMT)
350 set(ENABLE_CLANG_ARCMT "1")
351else()
352 set(ENABLE_CLANG_ARCMT "0")
Roman Divackyff62d642013-05-29 21:09:18 +0000353endif()
354
Stephen Hines176edba2014-12-01 14:53:08 -0800355option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
356if (CLANG_ENABLE_STATIC_ANALYZER)
357 set(ENABLE_CLANG_STATIC_ANALYZER "1")
358else()
359 set(ENABLE_CLANG_STATIC_ANALYZER "0")
Roman Divackyff62d642013-05-29 21:09:18 +0000360endif()
361
Jordan Rosee6385032013-08-22 15:50:02 +0000362if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
363 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
364endif()
365
Roman Divackyff62d642013-05-29 21:09:18 +0000366if(CLANG_ENABLE_ARCMT)
367 add_definitions(-DCLANG_ENABLE_ARCMT)
Stephen Hines176edba2014-12-01 14:53:08 -0800368 add_definitions(-DCLANG_ENABLE_OBJC_REWRITER)
Roman Divackyff62d642013-05-29 21:09:18 +0000369endif()
370if(CLANG_ENABLE_STATIC_ANALYZER)
371 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
372endif()
NAKAMURA Takumi68f22af2012-12-13 16:24:59 +0000373
Douglas Gregor8435bf92011-02-25 19:24:02 +0000374# Clang version information
375set(CLANG_EXECUTABLE_VERSION
376 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
377 "Version number that will be placed into the clang executable, in the form XX.YY")
378set(LIBCLANG_LIBRARY_VERSION
379 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
380 "Version number that will be placed into the libclang library , in the form XX.YY")
381mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
382
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800383option(CLANG_INCLUDE_TESTS
384 "Generate build targets for the Clang unit tests."
385 ${LLVM_INCLUDE_TESTS})
386
Peter Collingbourne51d77772011-10-06 13:03:08 +0000387add_subdirectory(utils/TableGen)
Douglas Gregor8435bf92011-02-25 19:24:02 +0000388
Douglas Gregora393e9e2009-03-16 23:06:59 +0000389add_subdirectory(include)
Stephen Hines651f13c2014-04-23 16:59:28 -0700390
391# All targets below may depend on all tablegen'd files.
392get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
393list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS})
394
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000395add_subdirectory(lib)
Daniel Dunbarcbcd98b2009-03-24 02:52:57 +0000396add_subdirectory(tools)
Michael J. Spencer48263ba2010-12-16 03:28:42 +0000397add_subdirectory(runtime)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000398
NAKAMURA Takumie3120ae2012-07-27 06:17:48 +0000399option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
Stephen Hines651f13c2014-04-23 16:59:28 -0700400if (CLANG_BUILD_EXAMPLES)
401 set(ENABLE_CLANG_EXAMPLES "1")
402else()
403 set(ENABLE_CLANG_EXAMPLES "0")
404endif()
NAKAMURA Takumie3120ae2012-07-27 06:17:48 +0000405add_subdirectory(examples)
406
Jordan Rose3092d9d2013-02-08 01:42:37 +0000407if( CLANG_INCLUDE_TESTS )
Stephen Hines651f13c2014-04-23 16:59:28 -0700408 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
409 add_subdirectory(unittests)
410 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
411 list(APPEND CLANG_TEST_PARAMS
412 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
413 )
414 endif()
Michael Gottesman7758bb52013-08-20 07:09:54 +0000415 add_subdirectory(test)
Stephen Hines651f13c2014-04-23 16:59:28 -0700416
417 if(CLANG_BUILT_STANDALONE)
418 # Add a global check rule now that all subdirectories have been traversed
419 # and we know the total set of lit testsuites.
420 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
421 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
422 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
423 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
424 add_lit_target(check-all
425 "Running all regression tests"
426 ${LLVM_LIT_TESTSUITES}
427 PARAMS ${LLVM_LIT_PARAMS}
428 DEPENDS ${LLVM_LIT_DEPENDS}
429 ARGS ${LLVM_LIT_EXTRA_ARGS}
430 )
431 endif()
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800432 add_subdirectory(utils/perf-training)
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000433endif()
NAKAMURA Takumi792f9752011-02-16 03:07:15 +0000434
Michael Gottesman1ab6c6c2013-08-28 20:29:40 +0000435option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
436 ${LLVM_INCLUDE_DOCS})
437if( CLANG_INCLUDE_DOCS )
438 add_subdirectory(docs)
439endif()
440
Stephen Hines176edba2014-12-01 14:53:08 -0800441
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700442if(APPLE)
443 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
444 # default value get updated to the new default.
445 if(CLANG_ORDER_FILE STREQUAL "")
446 unset(CLANG_ORDER_FILE CACHE)
447 unset(CLANG_ORDER_FILE)
448 endif()
Stephen Hines176edba2014-12-01 14:53:08 -0800449
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700450
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700451 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
452 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
Stephen Hines176edba2014-12-01 14:53:08 -0800453
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700454 if(CLANG_ORDER_FILE AND NOT EXISTS ${CLANG_ORDER_FILE})
455 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
456 if(PATH_START EQUAL 0)
457 file(WRITE ${CLANG_ORDER_FILE} "\n")
458 else()
459 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
460 endif()
461 endif()
462endif()
463
464add_subdirectory(cmake/modules)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800465
466if (CLANG_ENABLE_BOOTSTRAP)
467 include(ExternalProject)
468
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800469 if(NOT CLANG_STAGE)
470 set(CLANG_STAGE stage1)
471 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
472 endif()
473
474 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
475 if(MATCHED_STAGE)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700476 if(NOT LLVM_BUILD_INSTRUMENTED)
477 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
478 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
479 else()
480 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
481 endif()
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800482 else()
483 set(NEXT_CLANG_STAGE bootstrap)
484 endif()
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700485
486 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
487 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
488 endif()
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800489 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
490
491
492 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
493 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700494 set(cmake_command ${CMAKE_COMMAND})
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800495
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700496 # If the next stage is LTO we need to depend on LTO and possibly LLVMgold
497 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO)
498 set(LTO_DEP LTO)
499 if(APPLE)
500 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
501 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
502 # so that the host object file tools will use the just-built libLTO.
503 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib)
504 set(cmake_command ${CMAKE_COMMAND} -E env DYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR} ${CMAKE_COMMAND})
505 elseif(NOT WIN32)
506 list(APPEND LTO_DEP LLVMgold llvm-ar llvm-ranlib)
507 set(LTO_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
508 set(LTO_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
509 endif()
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800510 endif()
511
512 add_custom_target(${NEXT_CLANG_STAGE}-clear
513 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
514 )
515 add_custom_command(
516 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
517 DEPENDS clang ${LTO_DEP}
518 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
519 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
520 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
521 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
522 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
523 )
524
525 if(CMAKE_VERBOSE_MAKEFILE)
526 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
527 endif()
528
529 set(BOOTSTRAP_DEFAULT_PASSTHROUGH
530 PACKAGE_VERSION
531 LLVM_VERSION_MAJOR
532 LLVM_VERSION_MINOR
533 LLVM_VERSION_PATCH
534 LLVM_VERSION_SUFFIX
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700535 LLVM_BINUTILS_INCDIR
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800536 CLANG_REPOSITORY_STRING
537 CMAKE_MAKE_PROGRAM)
538
539 if(TARGET compiler-rt)
540 set(RUNTIME_DEP compiler-rt)
541 endif()
542
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700543 set(COMPILER_OPTIONS
544 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
545 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
546 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
547
548 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
549 set(PGO_DEP llvm-profdata)
550 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
551 endif()
552
553 if(LLVM_BUILD_INSTRUMENTED)
554 set(PGO_DEP generate-profdata)
555 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
556 set(COMPILER_OPTIONS
557 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
558 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
559 -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER})
560 set(RUNTIME_DEP) # Don't set runtime dependencies
561 endif()
562
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800563 # Find all variables that start with BOOTSTRAP_ and populate a variable with
564 # them.
565 get_cmake_property(variableNames VARIABLES)
566 foreach(variableName ${variableNames})
567 if(variableName MATCHES "^BOOTSTRAP_")
568 string(SUBSTRING ${variableName} 10 -1 varName)
569 string(REPLACE ";" "\;" value "${${variableName}}")
570 list(APPEND PASSTHROUGH_VARIABLES
571 -D${varName}=${value})
572 endif()
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700573 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
574 list(APPEND PASSTHROUGH_VARIABLES
575 -D${variableName}=${${variableName}})
576 endif()
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800577 endforeach()
578
579 # Populate the passthrough variables
580 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${BOOTSTRAP_DEFAULT_PASSTHROUGH})
581 if(${variableName})
582 string(REPLACE ";" "\;" value ${${variableName}})
583 list(APPEND PASSTHROUGH_VARIABLES
584 -D${variableName}=${value})
585 endif()
586 endforeach()
587
588 ExternalProject_Add(${NEXT_CLANG_STAGE}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700589 DEPENDS clang ${LTO_DEP} ${RUNTIME_DEP} ${PGO_DEP}
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800590 PREFIX ${NEXT_CLANG_STAGE}
591 SOURCE_DIR ${CMAKE_SOURCE_DIR}
592 STAMP_DIR ${STAMP_DIR}
593 BINARY_DIR ${BINARY_DIR}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700594 EXCLUDE_FROM_ALL 1
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800595 CMAKE_ARGS
596 # We shouldn't need to set this here, but INSTALL_DIR doesn't
597 # seem to work, so instead I'm passing this through
598 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
599 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
600 ${PASSTHROUGH_VARIABLES}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700601 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
602 ${COMPILER_OPTIONS}
603 ${LTO_LIBRARY} ${LTO_AR} ${LTO_RANLIB} ${verbose} ${PGO_OPT}
604 CMAKE_COMMAND ${cmake_command}
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800605 INSTALL_COMMAND ""
606 STEP_TARGETS configure build
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700607 USES_TERMINAL_CONFIGURE 1
608 USES_TERMINAL_BUILD 1
609 USES_TERMINAL_INSTALL 1
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800610 )
611
612 # exclude really-install from main target
613 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
614 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700615 COMMAND ${cmake_command} --build <BINARY_DIR> --target install
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800616 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
617 DEPENDEES build
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700618 USES_TERMINAL 1
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800619 )
620 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
621 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
622
623 if(NOT CLANG_BOOTSTRAP_TARGETS)
624 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
625 endif()
626 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
627 # exclude from main target
628 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_${target}_EXCLUDE_FROM_MAIN On)
629
630 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700631 COMMAND ${cmake_command} --build <BINARY_DIR> --target ${target}
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800632 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
633 DEPENDEES configure
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700634 USES_TERMINAL 1
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800635 )
636
637 if(target MATCHES "^stage[0-9]*")
638 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
639 endif()
640
641 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
642 endforeach()
643endif()
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700644
645if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
646 add_subdirectory(utils/ClangVisualizers)
647endif()