blob: c3f30e2a8e9c0655198ce4606b2e3945192c3cbd [file] [log] [blame]
Mark de Wevercbaa3592023-05-24 18:12:32 +02001cmake_minimum_required(VERSION 3.20.0)
Michael Krusef2a385c2024-05-25 17:16:39 +02002set(LLVM_SUBPROJECT_TITLE "Clang")
Mike Spertuse9f15b42016-03-28 18:24:22 +00003
Michał Górny9dd01a52022-10-24 06:31:37 +02004if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6endif()
7include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
8 NO_POLICY_SCOPE)
9
Mike Spertuse9f15b42016-03-28 18:24:22 +000010# If we are not building as a part of LLVM, build Clang as an
11# standalone project, using LLVM as an external library:
John Ericsona3ab2c92022-01-02 06:29:26 +000012if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
Mike Spertuse9f15b42016-03-28 18:24:22 +000013 project(Clang)
John Ericson10d0d8c2022-01-16 06:14:24 +000014 set(CLANG_BUILT_STANDALONE TRUE)
15endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +000016
Michał Górnye14c6fa2024-03-23 20:26:20 +010017# Make sure that our source directory is on the current cmake module path so that
18# we can include cmake files from this directory.
19list(INSERT CMAKE_MODULE_PATH 0
20 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
21 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
22 )
23
John Ericson10d0d8c2022-01-16 06:14:24 +000024# Must go below project(..)
25include(GNUInstallDirs)
Usama Hameed3bc71c22024-03-22 15:29:36 -070026include(GetDarwinLinkerVersion)
John Ericson10d0d8c2022-01-16 06:14:24 +000027
28if(CLANG_BUILT_STANDALONE)
Tobias Hietab1356502022-08-05 21:45:55 +020029 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
Saleem Abdulrasoolaa7a5ad2020-07-10 09:33:54 -070030 set(CMAKE_CXX_STANDARD_REQUIRED YES)
31 set(CMAKE_CXX_EXTENSIONS NO)
32
Mike Spertuse9f15b42016-03-28 18:24:22 +000033 if(NOT MSVC_IDE)
34 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
35 CACHE BOOL "Enable assertions")
36 # Assertions should follow llvm-config's.
37 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
38 endif()
39
Alfonso Gregorya2c319f2021-09-16 18:27:53 +020040 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
John Ericsone0eeae92022-02-02 15:37:13 +000041 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
Tom Stellarde4faa5c2018-11-13 03:42:46 +000042
Tom Stellardd2b158e2022-08-06 09:22:05 -040043 # Turn into CACHE PATHs for overwritting
44 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
45 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
46 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
47 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
48 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
Mike Spertuse9f15b42016-03-28 18:24:22 +000049
50 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
51 NO_DEFAULT_PATH)
52
Mike Spertuse9f15b42016-03-28 18:24:22 +000053 # They are used as destination of target generators.
54 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
John Ericsone941b032022-08-18 22:44:46 -040055 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
Mike Spertuse9f15b42016-03-28 18:24:22 +000056 if(WIN32 OR CYGWIN)
57 # DLL platform -- put DLLs into bin.
58 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
59 else()
60 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
61 endif()
62
63 option(LLVM_INSTALL_TOOLCHAIN_ONLY
64 "Only include toolchain files in the 'install' target." OFF)
65
Jonas Hahnfeld0b3c48d2023-05-17 10:28:58 +020066 option(LLVM_FORCE_USE_OLD_TOOLCHAIN
Mike Spertuse9f15b42016-03-28 18:24:22 +000067 "Set to ON to force using an old, unsupported host toolchain." OFF)
68 option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
David Callahan63c77fd2018-11-29 14:57:14 +000069 option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
Mike Spertuse9f15b42016-03-28 18:24:22 +000070
71 include(AddLLVM)
72 include(TableGen)
73 include(HandleLLVMOptions)
74 include(VersionFromVCS)
Sam James20132d82022-10-19 20:12:10 +010075 include(CheckAtomic)
Markus Böckaf2796c2021-03-15 20:56:08 +010076 include(GetErrcMessages)
Michal Gornycdbeaf52019-10-07 18:14:56 +000077 include(LLVMDistributionSupport)
Mike Spertuse9f15b42016-03-28 18:24:22 +000078
79 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
Michał Górny21c165d2020-07-31 00:11:40 +020080 set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
81 "Default URL where bug reports are to be submitted.")
Mike Spertuse9f15b42016-03-28 18:24:22 +000082
83 if (NOT DEFINED LLVM_INCLUDE_TESTS)
84 set(LLVM_INCLUDE_TESTS ON)
85 endif()
86
John Ericsoncc56a502022-07-26 07:17:30 +000087 include_directories(${LLVM_INCLUDE_DIRS})
Mike Spertuse9f15b42016-03-28 18:24:22 +000088 link_directories("${LLVM_LIBRARY_DIR}")
89
90 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
John Ericsone941b032022-08-18 22:44:46 -040091 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
92 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
Mike Spertuse9f15b42016-03-28 18:24:22 +000093
Petr Hosek96962d52023-06-09 07:51:40 +000094 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
95 COMPONENTS Interpreter)
Mike Spertuse9f15b42016-03-28 18:24:22 +000096
Petr Hosek96962d52023-06-09 07:51:40 +000097 if(LLVM_INCLUDE_TESTS)
Mike Spertuse9f15b42016-03-28 18:24:22 +000098 # Check prebuilt llvm/utils.
99 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
100 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
101 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
102 set(LLVM_UTILS_PROVIDED ON)
103 endif()
104
Tom Stellardc9a95932022-11-03 15:12:50 -0700105 # Seek installed Lit.
106 find_program(LLVM_LIT
107 NAMES llvm-lit lit.py lit
108 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
109 DOC "Path to lit.py")
110
Mike Spertuse9f15b42016-03-28 18:24:22 +0000111 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
Michal Gornyda6d2b82016-10-18 17:07:30 +0000112 # Note: path not really used, except for checking if lit was found
Michal Gornyba996ab2017-11-17 22:21:23 +0000113 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
114 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
115 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000116 if(NOT LLVM_UTILS_PROVIDED)
117 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
118 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
119 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
120 set(LLVM_UTILS_PROVIDED ON)
121 set(CLANG_TEST_DEPS FileCheck count not)
122 endif()
Tom Stellard82169102023-03-10 18:00:06 -0800123 endif()
124
125 if (NOT TARGET llvm_gtest)
126 message(FATAL_ERROR "llvm-gtest not found. Please install llvm-gtest or disable tests with -DLLVM_INCLUDE_TESTS=OFF")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000127 endif()
128
129 if(LLVM_LIT)
130 # Define the default arguments to use with 'lit', and an option for the user
131 # to override.
132 set(LIT_ARGS_DEFAULT "-sv")
133 if (MSVC OR XCODE)
134 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
135 endif()
136 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
137
Markus Böckaf2796c2021-03-15 20:56:08 +0100138 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
139
Mike Spertuse9f15b42016-03-28 18:24:22 +0000140 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
141 if( WIN32 AND NOT CYGWIN )
142 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
143 endif()
144 else()
145 set(LLVM_INCLUDE_TESTS OFF)
146 endif()
Sam McCall7cc83772022-03-16 19:46:28 +0100147
148 umbrella_lit_testsuite_begin(check-all)
149 endif() # LLVM_INCLUDE_TESTS
John Ericsona3ab2c92022-01-02 06:29:26 +0000150endif() # standalone
Mike Spertuse9f15b42016-03-28 18:24:22 +0000151
Nico Weberdf239a62022-10-25 13:22:23 -0400152# This allows disabling clang's XML dependency even if LLVM finds libxml2.
153# By default, clang depends on libxml2 if LLVM does.
154option(CLANG_ENABLE_LIBXML2 "Whether libclang may depend on libxml2"
155 ${LLVM_ENABLE_LIBXML2})
156
157if(CLANG_ENABLE_LIBXML2)
David Callahan63c77fd2018-11-29 14:57:14 +0000158 # Don't look for libxml if we're using MSan, since uninstrumented third party
159 # code may call MSan interceptors like strlen, leading to false positives.
160 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
161 set (LIBXML2_FOUND 0)
162 find_package(LibXml2 2.5.3 QUIET)
163 if (LIBXML2_FOUND)
164 set(CLANG_HAVE_LIBXML 1)
165 endif()
Vitaly Buka3d8e5092017-09-02 03:53:42 +0000166 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000167endif()
168
Nathan Lanza44de2bb2024-04-11 16:56:31 -0400169if(CLANG_ENABLE_CIR)
Nathan Lanza10661ba2024-04-24 22:26:40 -0400170 if (CLANG_BUILT_STANDALONE)
171 message(FATAL_ERROR
172 "ClangIR is not yet supported in the standalone build.")
173 endif()
Nathan Lanza44de2bb2024-04-11 16:56:31 -0400174 if (NOT "${LLVM_ENABLE_PROJECTS}" MATCHES "MLIR|mlir")
175 message(FATAL_ERROR
176 "Cannot build ClangIR without MLIR in LLVM_ENABLE_PROJECTS")
177 endif()
178endif()
179
Chris Bienemana6b39ab2016-08-23 20:07:07 +0000180include(CheckIncludeFile)
181check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
182
Abhina Sreed430c142023-12-22 08:12:19 -0500183# This check requires _GNU_SOURCE on linux
184check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
185if( CLANG_HAVE_DLFCN_H )
186 include(CheckLibraryExists)
187 include(CheckSymbolExists)
188 check_library_exists(dl dlopen "" HAVE_LIBDL)
189 if( HAVE_LIBDL )
190 list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
191 endif()
192 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
193 check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
194 list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
195 if( HAVE_LIBDL )
196 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
197 endif()
198endif()
199
Mike Spertuse9f15b42016-03-28 18:24:22 +0000200set(CLANG_RESOURCE_DIR "" CACHE STRING
201 "Relative directory from the Clang binary to its resource files.")
202
203set(C_INCLUDE_DIRS "" CACHE STRING
204 "Colon separated list of directories clang will search for headers.")
205
Fangrui Songd59730d2024-03-20 22:45:38 -0700206set(USE_DEPRECATED_GCC_INSTALL_PREFIX OFF CACHE BOOL "Temporary workaround before GCC_INSTALL_PREFIX is completely removed")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000207set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
Sam Clegg07313722020-03-19 15:20:49 -0700208set(DEFAULT_SYSROOT "" CACHE STRING
Mike Spertuse9f15b42016-03-28 18:24:22 +0000209 "Default <path> to all compiler invocations for --sysroot=<path>." )
Fangrui Songd59730d2024-03-20 22:45:38 -0700210if(GCC_INSTALL_PREFIX AND NOT USE_DEPRECATED_GCC_INSTALL_PREFIX)
211 message(FATAL_ERROR "GCC_INSTALL_PREFIX is deprecated and will be removed. Use "
Fangrui Song3358c772024-01-10 11:01:55 -0800212 "configuration files (https://clang.llvm.org/docs/UsersManual.html#configuration-files)"
213 "to specify the default --gcc-install-dir= or --gcc-triple=. --gcc-toolchain= is discouraged. "
214 "See https://github.com/llvm/llvm-project/pull/77537 for detail.")
215endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000216
Rafael Espindola5ed89d42016-06-03 17:26:16 +0000217set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
218
Fangrui Songc41a18c2020-08-02 23:05:50 -0700219set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
Rafael Espindola557679f2016-06-20 23:54:44 +0000220 "enable x86 relax relocations by default")
221
Qiu Chaofanb797d5e2022-01-27 00:49:17 +0800222set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
223 "Enable IEEE binary128 as default long double format on PowerPC Linux.")
224
Alexandre Ganeab4a99a02020-01-13 10:40:04 -0500225set(CLANG_SPAWN_CC1 OFF CACHE BOOL
226 "Whether clang should use a new process for the CC1 invocation")
227
Fangrui Songca680382022-04-08 23:40:18 -0700228option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
Fangrui Song1042de92021-12-14 10:08:59 -0800229
Petr Hosekfe2c2b02016-12-14 16:46:50 +0000230set(CLANG_DEFAULT_LINKER "" CACHE STRING
231 "Default linker to use (linker name or absolute path, empty for platform default)")
232
Mike Spertuse9f15b42016-03-28 18:24:22 +0000233set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000234 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000235if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
236 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
237 CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
Jonas Hahnfeldebf86622016-07-25 08:04:26 +0000238 message(WARNING "Resetting default C++ stdlib to use platform default")
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000239 set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
240 "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
241endif()
242
243set(CLANG_DEFAULT_RTLIB "" CACHE STRING
244 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
245if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
246 CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
247 CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
248 message(WARNING "Resetting default rtlib to use platform default")
249 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
250 "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000251endif()
252
Sterling Augustine62716062019-03-19 20:01:59 +0000253set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
254 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
255if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
256 if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
257 set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
Sterling Augustine62716062019-03-19 20:01:59 +0000258 endif()
259endif()
260
Evandro Menezes7e8476d2019-03-25 16:38:48 +0000261if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
262 CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
Sterling Augustine62716062019-03-19 20:01:59 +0000263 CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
264 CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
265 message(WARNING "Resetting default unwindlib to use platform default")
266 set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
Martin Storsjöe81d8132021-03-07 14:41:04 +0200267 "Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
Sterling Augustine62716062019-03-19 20:01:59 +0000268endif()
269
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000270set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
271 "Default objcopy executable to use.")
272
Mike Spertuse9f15b42016-03-28 18:24:22 +0000273set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
274 "Default OpenMP runtime used by -fopenmp.")
275
Nico Weberc506adc2020-03-30 14:11:35 -0400276set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
Ulrich Weigand9c9d88d2020-03-30 14:20:48 +0200277
Mike Spertuse9f15b42016-03-28 18:24:22 +0000278set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
279 "Vendor-specific text for showing with version information.")
280
Mike Spertuse9f15b42016-03-28 18:24:22 +0000281set(CLANG_REPOSITORY_STRING "" CACHE STRING
282 "Vendor-specific text for showing the repository the source is taken from.")
283
284if(CLANG_REPOSITORY_STRING)
285 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
286endif()
287
Mike Spertuse9f15b42016-03-28 18:24:22 +0000288set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
289 "Vendor-specific uti.")
290
Saleem Abdulrasoolf02e4f92018-08-20 22:50:18 +0000291set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
292 "Python versions to install libclang python bindings for")
293
Tom Stellard2e97d2a2019-07-03 22:45:55 +0000294set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
Sylvestre Ledru76b26552019-07-11 21:42:55 +0000295 "Link tools against libclang-cpp.so")
Tom Stellard2e97d2a2019-07-03 22:45:55 +0000296
297if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
298 message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
299 "LLVM_LINK_LLVM_DYLIB=OFF")
300endif()
301
John Ericsone941b032022-08-18 22:44:46 -0400302# The libdir suffix must exactly match whatever LLVM's configuration used.
303set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
304
John Ericson07b74982022-06-11 06:11:59 +0000305set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
306 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
307mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
308
Mike Spertuse9f15b42016-03-28 18:24:22 +0000309set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
310set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
311
312if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
Stephen Kellya21cc1f2018-08-30 23:41:03 +0000313 message(FATAL_ERROR "In-source builds are not allowed. "
314"Please create a directory and run cmake "
Mike Spertuse9f15b42016-03-28 18:24:22 +0000315"from there, passing the path to this source directory as the last argument. "
316"This process created the file `CMakeCache.txt' and the directory "
317"`CMakeFiles'. Please delete them.")
318endif()
319
Chris Bienemanebfc6802016-09-20 19:09:21 +0000320# If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
321if(NOT DEFINED CLANG_VERSION_MAJOR)
322 set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
323endif()
324if(NOT DEFINED CLANG_VERSION_MINOR)
325 set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
326endif()
327if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
328 set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
329endif()
James Y Knight31aebdd2023-12-05 12:20:12 -0500330if(NOT DEFINED CLANG_VERSION_SUFFIX)
331 set(CLANG_VERSION_SUFFIX ${LLVM_VERSION_SUFFIX})
332endif()
333set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}${CLANG_VERSION_SUFFIX}")
Akira Hatanaka60927402025-01-28 18:59:05 -0800334set(MAX_CLANG_ABI_COMPAT_VERSION "${LLVM_VERSION_MAJOR}")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000335message(STATUS "Clang version: ${CLANG_VERSION}")
336
Mike Spertuse9f15b42016-03-28 18:24:22 +0000337# Configure the Version.inc file.
338configure_file(
339 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
340 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
341
342# Add appropriate flags for GCC
343if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
344 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
345 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
346 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
347 endif ()
348
349 # Enable -pedantic for Clang even if it's not enabled for LLVM.
350 if (NOT LLVM_ENABLE_PEDANTIC)
351 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
352 endif ()
353
Martin Storsjö41c650e2024-06-11 09:03:41 +0300354 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
355 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
356 endif ()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000357endif ()
358
359# Determine HOST_LINK_VERSION on Darwin.
360set(HOST_LINK_VERSION)
Petr Hosekc624cc22022-03-21 18:32:03 -0700361if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
Usama Hameed3bc71c22024-03-22 15:29:36 -0700362 get_darwin_linker_version(HOST_LINK_VERSION)
Petr Hosek633e3da2020-08-05 14:03:12 -0700363 message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000364endif()
365
Michael Gottesmanca589cc2016-07-09 21:58:40 +0000366include(AddClang)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000367
368set(CMAKE_INCLUDE_CURRENT_DIR ON)
369
370include_directories(BEFORE
371 ${CMAKE_CURRENT_BINARY_DIR}/include
372 ${CMAKE_CURRENT_SOURCE_DIR}/include
373 )
374
375if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
376 install(DIRECTORY include/clang include/clang-c
John Ericson10d0d8c2022-01-16 06:14:24 +0000377 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Shoaib Meenai20e7c0c2019-03-11 18:53:57 +0000378 COMPONENT clang-headers
Mike Spertuse9f15b42016-03-28 18:24:22 +0000379 FILES_MATCHING
380 PATTERN "*.def"
381 PATTERN "*.h"
382 PATTERN "config.h" EXCLUDE
Mike Spertuse9f15b42016-03-28 18:24:22 +0000383 )
384
385 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
John Ericson10d0d8c2022-01-16 06:14:24 +0000386 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Shoaib Meenai20e7c0c2019-03-11 18:53:57 +0000387 COMPONENT clang-headers
Mike Spertuse9f15b42016-03-28 18:24:22 +0000388 FILES_MATCHING
389 PATTERN "CMakeFiles" EXCLUDE
390 PATTERN "*.inc"
391 PATTERN "*.h"
392 )
Yuka Takahashic8068db2017-05-23 18:39:08 +0000393
Shoaib Meenai20e7c0c2019-03-11 18:53:57 +0000394 # Installing the headers needs to depend on generating any public
395 # tablegen'd headers.
396 add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
Michael Krusef2a385c2024-05-25 17:16:39 +0200397 set_target_properties(clang-headers PROPERTIES FOLDER "Clang/Resources")
Shoaib Meenai20e7c0c2019-03-11 18:53:57 +0000398 if(NOT LLVM_ENABLE_IDE)
399 add_llvm_install_targets(install-clang-headers
400 DEPENDS clang-headers
401 COMPONENT clang-headers)
402 endif()
403
Michal Gorny5caeb4a2019-10-04 05:43:20 +0000404 add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
Michael Krusef2a385c2024-05-25 17:16:39 +0200405 set_target_properties(bash-autocomplete PROPERTIES FOLDER "Clang/Misc")
Sinan Lin735931452022-09-05 22:00:00 +0800406 install(FILES utils/bash-autocomplete.sh
John Ericson10d0d8c2022-01-16 06:14:24 +0000407 DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
Michal Gorny5caeb4a2019-10-04 05:43:20 +0000408 COMPONENT bash-autocomplete)
409 if(NOT LLVM_ENABLE_IDE)
410 add_llvm_install_targets(install-bash-autocomplete
411 DEPENDS bash-autocomplete
412 COMPONENT bash-autocomplete)
413 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000414endif()
415
Michael Gottesmaneb396a62016-07-10 01:44:00 +0000416option(CLANG_BUILD_TOOLS
417 "Build the Clang tools. If OFF, just generate build targets." ON)
418
Jameson Nash9d59cfc2022-02-11 16:42:37 -0500419if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
420 set(HAVE_CLANG_PLUGIN_SUPPORT ON)
421else()
422 set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
423endif()
Jameson Nash76cad512022-02-07 18:46:33 -0500424CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
425 "Build clang with plugin support" ON
Jameson Nash9d59cfc2022-02-11 16:42:37 -0500426 "HAVE_CLANG_PLUGIN_SUPPORT" OFF)
Jameson Nash76cad512022-02-07 18:46:33 -0500427
Sunho Kima8f2e242022-07-31 05:42:16 +0900428# If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
Amir Ayupov3dab7fe2022-09-23 10:08:58 +0200429# itself, which is not possible in many platforms because of current limitations in
Sunho Kima8f2e242022-07-31 05:42:16 +0900430# JIT stack. (more platforms need to be supported by JITLink)
431if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
432 set(HAVE_CLANG_REPL_SUPPORT ON)
433endif()
434
Sirraidec4a01972025-01-30 05:32:25 +0100435option(CLANG_ENABLE_OBJC_REWRITER "Build the Objective-C rewriter tool" OFF)
436
Nico Weber33c9dbb2020-09-03 19:37:29 -0400437option(CLANG_ENABLE_STATIC_ANALYZER
438 "Include static analyzer in clang binary." ON)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000439
Matt Morehousef051f5d2017-08-08 20:15:04 +0000440option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
441
Sirraidec4a01972025-01-30 05:32:25 +0100442if (DEFINED CLANG_ENABLE_ARCMT)
443 set(CLANG_ENABLE_OBJC_REWRITER ${CLANG_ENABLE_ARCMT})
444 message(DEPRECATION "'CLANG_ENABLE_ARCMT' is deprecated as ARCMigrate has been removed from Clang. Please use 'CLANG_ENABLE_OBJC_REWRITER' instead to enable or disable the Objective-C rewriter.")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000445endif()
446
H. Vetinari0f28d4852022-08-25 08:35:46 +0200447# This option is a stop-gap, we should commit to removing this as
448# soon as possible. See discussion:
449# https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
450option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
451 "Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
452
Mike Spertuse9f15b42016-03-28 18:24:22 +0000453# Clang version information
454set(CLANG_EXECUTABLE_VERSION
Sylvestre Ledrua8b717f2018-03-29 10:05:46 +0000455 "${CLANG_VERSION_MAJOR}" CACHE STRING
456 "Major version number that will be appended to the clang executable name")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000457set(LIBCLANG_LIBRARY_VERSION
Sylvestre Ledrua8b717f2018-03-29 10:05:46 +0000458 "${CLANG_VERSION_MAJOR}" CACHE STRING
459 "Major version number that will be appended to the libclang library")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000460mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
461
462option(CLANG_INCLUDE_TESTS
463 "Generate build targets for the Clang unit tests."
464 ${LLVM_INCLUDE_TESTS})
465
Chris Bienemane4321082022-09-26 21:44:13 -0500466option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
467# While HLSL support is experimental this should stay hidden.
468mark_as_advanced(CLANG_ENABLE_HLSL)
469
Mike Spertuse9f15b42016-03-28 18:24:22 +0000470add_subdirectory(utils/TableGen)
471
Nikita Popovc04eab82022-08-08 12:40:49 +0200472# Export CLANG_TABLEGEN_EXE for use by flang docs.
473set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
474
Mike Spertuse9f15b42016-03-28 18:24:22 +0000475add_subdirectory(include)
476
477# All targets below may depend on all tablegen'd files.
478get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
Simon Tatham9e6f19f2020-07-02 09:16:13 +0100479add_custom_target(clang-tablegen-targets
480 DEPENDS
481 omp_gen
Jon Roelofs2fb1c1082023-05-10 11:48:11 -0700482 ClangDriverOptions
Simon Tatham9e6f19f2020-07-02 09:16:13 +0100483 ${CLANG_TABLEGEN_TARGETS})
Michael Krusef2a385c2024-05-25 17:16:39 +0200484set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Clang/Tablegenning/Targets")
Chris Bieneman6b5851b2017-07-28 15:33:47 +0000485list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000486
NAKAMURA Takumi7ae667d2017-07-23 05:09:44 +0000487# Force target to be built as soon as possible. Clang modules builds depend
488# header-wise on it as they ship all headers from the umbrella folders. Building
489# an entire module might include header, which depends on intrinsics_gen.
Michele Scandale53880b82020-07-17 16:43:05 -0700490if(LLVM_ENABLE_MODULES)
NAKAMURA Takumi7ae667d2017-07-23 05:09:44 +0000491 list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
492endif()
493
Mike Spertuse9f15b42016-03-28 18:24:22 +0000494add_subdirectory(lib)
495add_subdirectory(tools)
496add_subdirectory(runtime)
497
498option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000499add_subdirectory(examples)
500
Alexander Shaposhnikovfd7afa72016-12-31 05:25:52 +0000501if(APPLE)
502 # this line is needed as a cleanup to ensure that any CMakeCaches with the old
503 # default value get updated to the new default.
504 if(CLANG_ORDER_FILE STREQUAL "")
505 unset(CLANG_ORDER_FILE CACHE)
506 unset(CLANG_ORDER_FILE)
507 endif()
508
509
510 set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
511 "Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
512
513 if(NOT EXISTS ${CLANG_ORDER_FILE})
514 string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
515 if(PATH_START EQUAL 0)
516 file(WRITE ${CLANG_ORDER_FILE} "\n")
517 else()
518 message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
519 endif()
520 endif()
521endif()
522
523
Mike Spertuse9f15b42016-03-28 18:24:22 +0000524if( CLANG_INCLUDE_TESTS )
Matthias Braun2868e26d2024-05-08 07:35:47 -0700525 find_package(Perl)
526
Tom Stellard82169102023-03-10 18:00:06 -0800527 add_subdirectory(unittests)
528 list(APPEND CLANG_TEST_DEPS ClangUnitTests)
529 list(APPEND CLANG_TEST_PARAMS
530 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
531 )
Mike Spertuse9f15b42016-03-28 18:24:22 +0000532 add_subdirectory(test)
Michal Gorny61adf8a2018-10-11 16:32:54 +0000533 add_subdirectory(bindings/python/tests)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000534
535 if(CLANG_BUILT_STANDALONE)
Sam McCall7cc83772022-03-16 19:46:28 +0100536 umbrella_lit_testsuite_end(check-all)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000537 endif()
538 add_subdirectory(utils/perf-training)
539endif()
540
541option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
542 ${LLVM_INCLUDE_DOCS})
543if( CLANG_INCLUDE_DOCS )
544 add_subdirectory(docs)
545endif()
546
Shoaib Meenaid7058642019-02-15 15:59:04 +0000547# Custom target to install all clang libraries.
548add_custom_target(clang-libraries)
Michael Krusef2a385c2024-05-25 17:16:39 +0200549set_target_properties(clang-libraries PROPERTIES FOLDER "Clang/Install")
Shoaib Meenaid7058642019-02-15 15:59:04 +0000550
Shoaib Meenaidefb5a32019-02-20 23:08:43 +0000551if(NOT LLVM_ENABLE_IDE)
Shoaib Meenaid7058642019-02-15 15:59:04 +0000552 add_llvm_install_targets(install-clang-libraries
553 DEPENDS clang-libraries
554 COMPONENT clang-libraries)
555endif()
556
557get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
558if(CLANG_LIBS)
559 list(REMOVE_DUPLICATES CLANG_LIBS)
560 foreach(lib ${CLANG_LIBS})
561 add_dependencies(clang-libraries ${lib})
Shoaib Meenaidefb5a32019-02-20 23:08:43 +0000562 if(NOT LLVM_ENABLE_IDE)
Shoaib Meenaid7058642019-02-15 15:59:04 +0000563 add_dependencies(install-clang-libraries install-${lib})
Shoaib Meenaia2991782020-03-20 18:42:09 -0700564 add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
Shoaib Meenaid7058642019-02-15 15:59:04 +0000565 endif()
566 endforeach()
567endif()
568
Michael Gottesmanfe9d2d82016-06-29 20:22:44 +0000569add_subdirectory(cmake/modules)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000570
Chris Bienemanc4865412016-07-25 18:54:30 +0000571if(CLANG_STAGE)
572 message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
573endif()
574
Mike Spertuse9f15b42016-03-28 18:24:22 +0000575if (CLANG_ENABLE_BOOTSTRAP)
576 include(ExternalProject)
577
Chris Bieneman76a2e602016-10-19 21:18:48 +0000578 add_custom_target(clang-bootstrap-deps DEPENDS clang)
579
Mike Spertuse9f15b42016-03-28 18:24:22 +0000580 if(NOT CLANG_STAGE)
581 set(CLANG_STAGE stage1)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000582 endif()
583
584 string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
585 if(MATCHED_STAGE)
586 if(NOT LLVM_BUILD_INSTRUMENTED)
587 math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
588 set(NEXT_CLANG_STAGE stage${STAGE_NUM})
589 else()
590 set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
591 endif()
592 else()
593 set(NEXT_CLANG_STAGE bootstrap)
594 endif()
595
596 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
597 set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
598 endif()
599 message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000600
601
Mike Spertuse9f15b42016-03-28 18:24:22 +0000602 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
603 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
604
Petr Hosek03203142017-01-18 05:41:17 +0000605 if(BOOTSTRAP_LLVM_ENABLE_LLD)
Timm Bäderd9ef6bc2021-03-09 11:51:09 +0100606 # adding lld to clang-bootstrap-deps without having it enabled in
607 # LLVM_ENABLE_PROJECTS just generates a cryptic error message.
608 if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
Frederic Cambusf0ffff432021-10-06 22:37:31 +0530609 message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
Timm Bäderd9ef6bc2021-03-09 11:51:09 +0100610 endif()
Petr Hosek03203142017-01-18 05:41:17 +0000611 add_dependencies(clang-bootstrap-deps lld)
612 endif()
613
Haowei Wudc1c8912023-01-30 15:43:09 -0800614 if (WIN32)
615 # Build llvm-rc and llvm-mt which are needed by the Windows build.
616 add_dependencies(clang-bootstrap-deps llvm-rc)
617 if(LLVM_ENABLE_LIBXML2)
618 add_dependencies(clang-bootstrap-deps llvm-mt)
619 endif()
620 endif()
621
Petr Hosek8e07a882016-11-16 23:59:06 +0000622 # If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
Chris Bieneman76a2e602016-10-19 21:18:48 +0000623 if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000624 if(APPLE)
Petr Hosek8e07a882016-11-16 23:59:06 +0000625 add_dependencies(clang-bootstrap-deps LTO)
Chris Bieneman31046052016-04-27 18:52:48 +0000626 # on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
627 # using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
628 # so that the host object file tools will use the just-built libLTO.
Chris Bienemand052efc2016-07-25 23:48:14 +0000629 # However if System Integrity Protection is enabled the DYLD variables
630 # will be scrubbed from the environment of any base system commands. This
631 # includes /bin/sh, which ninja uses when executing build commands. To
632 # work around the envar being filtered away we pass it in as a CMake
633 # variable, and have LLVM's CMake append the envar to the archiver calls.
634 set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
635 -DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
Igor Kudrin43c307f2023-04-20 17:02:17 -0700636 elseif(MSVC)
637 add_dependencies(clang-bootstrap-deps llvm-lib)
638 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000639 elseif(NOT WIN32)
Petr Hosek8e07a882016-11-16 23:59:06 +0000640 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
Petr Hosek03203142017-01-18 05:41:17 +0000641 if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
Petr Hosek8e07a882016-11-16 23:59:06 +0000642 add_dependencies(clang-bootstrap-deps LLVMgold)
643 endif()
Petr Hosekf8e27b32018-11-16 04:46:48 +0000644 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
645 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000646 endif()
647 endif()
648
Petr Hosek534a1042018-06-11 20:59:31 +0000649 if(CLANG_BOOTSTRAP_EXTRA_DEPS)
650 add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
651 endif()
652
Mike Spertuse9f15b42016-03-28 18:24:22 +0000653 add_custom_target(${NEXT_CLANG_STAGE}-clear
654 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
655 )
656 add_custom_command(
657 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
Chris Bieneman76a2e602016-10-19 21:18:48 +0000658 DEPENDS clang-bootstrap-deps
Mike Spertuse9f15b42016-03-28 18:24:22 +0000659 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
660 COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
661 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
662 COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
663 COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
664 )
665
666 if(CMAKE_VERBOSE_MAKEFILE)
667 set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
668 endif()
669
Chris Bienemanc4865412016-07-25 18:54:30 +0000670 set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
Mike Spertuse9f15b42016-03-28 18:24:22 +0000671 PACKAGE_VERSION
Chris Bienemanb13705312016-10-18 00:50:20 +0000672 PACKAGE_VENDOR
Mike Spertuse9f15b42016-03-28 18:24:22 +0000673 LLVM_VERSION_MAJOR
674 LLVM_VERSION_MINOR
675 LLVM_VERSION_PATCH
Chris Bieneman2c4d54f2016-09-21 20:43:43 +0000676 CLANG_VERSION_MAJOR
677 CLANG_VERSION_MINOR
678 CLANG_VERSION_PATCHLEVEL
James Y Knight31aebdd2023-12-05 12:20:12 -0500679 CLANG_VERSION_SUFFIX
Sylvestre Ledruca467542020-05-29 09:13:08 +0200680 CLANG_VENDOR
Mike Spertuse9f15b42016-03-28 18:24:22 +0000681 LLVM_VERSION_SUFFIX
682 LLVM_BINUTILS_INCDIR
683 CLANG_REPOSITORY_STRING
Chris Bienemanb13705312016-10-18 00:50:20 +0000684 CMAKE_MAKE_PROGRAM
Petr Hosekd32e51e2017-11-29 00:34:46 +0000685 CMAKE_OSX_ARCHITECTURES
Sylvestre Ledrueccd4772021-09-21 10:44:08 +0200686 CMAKE_BUILD_TYPE
Petr Hosekd32e51e2017-11-29 00:34:46 +0000687 LLVM_ENABLE_PROJECTS
688 LLVM_ENABLE_RUNTIMES)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000689
Ahmed Bougacha8b597882018-06-28 18:35:25 +0000690 # We don't need to depend on compiler-rt/libcxx if we're building instrumented
Chris Bieneman76a2e602016-10-19 21:18:48 +0000691 # because the next stage will use the same compiler used to build this stage.
Ahmed Bougacha8b597882018-06-28 18:35:25 +0000692 if(NOT LLVM_BUILD_INSTRUMENTED)
693 if(TARGET compiler-rt)
694 add_dependencies(clang-bootstrap-deps compiler-rt)
695 endif()
696 if(TARGET cxx-headers)
697 add_dependencies(clang-bootstrap-deps cxx-headers)
698 endif()
Mike Spertuse9f15b42016-03-28 18:24:22 +0000699 endif()
700
NAKAMURA Takumi25f1a6e2017-05-11 13:19:24 +0000701 set(C_COMPILER "clang")
702 set(CXX_COMPILER "clang++")
703 if(WIN32)
704 set(C_COMPILER "clang-cl.exe")
705 set(CXX_COMPILER "clang-cl.exe")
706 endif()
707
Mike Spertuse9f15b42016-03-28 18:24:22 +0000708 set(COMPILER_OPTIONS
NAKAMURA Takumi25f1a6e2017-05-11 13:19:24 +0000709 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
710 -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
Don Hinton976f0512018-01-19 18:31:12 +0000711 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
712 -DCMAKE_ASM_COMPILER_ID=Clang)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000713
Kristina Bessonovaad4ab812020-05-29 13:14:51 +0200714 # cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
715 # otherwise it defaults the linker to be link.exe.
716 if(BOOTSTRAP_LLVM_ENABLE_LLD)
717 if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
718 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
719 endif()
720 endif()
721
Petr Hosekf8e27b32018-11-16 04:46:48 +0000722 if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
Petr Hosekf8e27b32018-11-16 04:46:48 +0000723 set(${CLANG_STAGE}_TABLEGEN
724 -DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
725 -DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
726 if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
727 if(BOOTSTRAP_LLVM_ENABLE_LLD)
728 set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
729 endif()
730 if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
731 add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
732 set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
733 set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
734 endif()
735 add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
736 set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
737 set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
Petr Hosekd8936922021-09-24 17:56:00 -0700738 set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
Petr Hosekf8e27b32018-11-16 04:46:48 +0000739 endif()
740 endif()
741
Mike Spertuse9f15b42016-03-28 18:24:22 +0000742 if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
Chris Bieneman76a2e602016-10-19 21:18:48 +0000743 add_dependencies(clang-bootstrap-deps llvm-profdata)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000744 set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
745 endif()
746
747 if(LLVM_BUILD_INSTRUMENTED)
Chris Bieneman76a2e602016-10-19 21:18:48 +0000748 add_dependencies(clang-bootstrap-deps generate-profdata)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000749 set(PGO_OPT -DLLVM_PROFDATA_FILE=${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata)
Chris Bienemana1aa4062016-08-16 22:16:29 +0000750 # Use the current tools for LTO instead of the instrumented ones
751 list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
752 CMAKE_CXX_COMPILER
753 CMAKE_C_COMPILER
754 CMAKE_ASM_COMPILER
755 CMAKE_AR
756 CMAKE_RANLIB
757 DARWIN_LTO_LIBRARY
758 DYLD_LIBRARY_PATH)
759
760 set(COMPILER_OPTIONS)
761 set(LTO_LIBRARY)
Chris Bienemana1aa4062016-08-16 22:16:29 +0000762 set(LTO_AR)
763 set(LTO_RANLIB)
Mike Spertuse9f15b42016-03-28 18:24:22 +0000764 endif()
765
Carlos Alberto Enciso177cbb12022-08-17 08:16:10 +0100766 # Populate the passthrough variables
767 foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
768 if(DEFINED ${variableName})
769 if("${${variableName}}" STREQUAL "")
770 set(value "")
771 else()
772 string(REPLACE ";" "|" value "${${variableName}}")
773 endif()
774 list(APPEND PASSTHROUGH_VARIABLES
775 -D${variableName}=${value})
776 endif()
777 endforeach()
778
Mike Spertuse9f15b42016-03-28 18:24:22 +0000779 # Find all variables that start with BOOTSTRAP_ and populate a variable with
780 # them.
781 get_cmake_property(variableNames VARIABLES)
782 foreach(variableName ${variableNames})
783 if(variableName MATCHES "^BOOTSTRAP_")
784 string(SUBSTRING ${variableName} 10 -1 varName)
Petr Hosek5a34c342017-12-05 00:15:20 +0000785 string(REPLACE ";" "|" value "${${variableName}}")
Mike Spertuse9f15b42016-03-28 18:24:22 +0000786 list(APPEND PASSTHROUGH_VARIABLES
787 -D${varName}=${value})
788 endif()
789 if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
790 list(APPEND PASSTHROUGH_VARIABLES
791 -D${variableName}=${${variableName}})
792 endif()
793 endforeach()
794
Carlos Alberto Enciso6c6c4f62022-08-23 05:45:25 +0100795 # Build arguments for native tool used in CMake.
796 set(build_configuration "$<CONFIG>")
797 set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
798 if(NOT build_tool_args STREQUAL "")
799 string(PREPEND build_tool_args "-- ")
800 separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
801 endif()
802
Mike Spertuse9f15b42016-03-28 18:24:22 +0000803 ExternalProject_Add(${NEXT_CLANG_STAGE}
Chris Bieneman76a2e602016-10-19 21:18:48 +0000804 DEPENDS clang-bootstrap-deps
Mike Spertuse9f15b42016-03-28 18:24:22 +0000805 PREFIX ${NEXT_CLANG_STAGE}
806 SOURCE_DIR ${CMAKE_SOURCE_DIR}
807 STAMP_DIR ${STAMP_DIR}
808 BINARY_DIR ${BINARY_DIR}
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000809 EXCLUDE_FROM_ALL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000810 CMAKE_ARGS
811 # We shouldn't need to set this here, but INSTALL_DIR doesn't
812 # seem to work, so instead I'm passing this through
813 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000814 ${PASSTHROUGH_VARIABLES}
Xin-Xin Wang61c8ee62019-12-13 19:04:36 -0800815 ${CLANG_BOOTSTRAP_CMAKE_ARGS}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000816 -DCLANG_STAGE=${NEXT_CLANG_STAGE}
817 ${COMPILER_OPTIONS}
Petr Hosekf8e27b32018-11-16 04:46:48 +0000818 ${${CLANG_STAGE}_TABLEGEN}
819 ${LTO_LIBRARY} ${verbose} ${PGO_OPT}
820 ${${CLANG_STAGE}_LINKER}
821 ${${CLANG_STAGE}_AR}
822 ${${CLANG_STAGE}_RANLIB}
823 ${${CLANG_STAGE}_OBJCOPY}
824 ${${CLANG_STAGE}_STRIP}
Carlos Alberto Enciso6c6c4f62022-08-23 05:45:25 +0100825 BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
826 --config ${build_configuration}
827 ${build_tool_args}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000828 INSTALL_COMMAND ""
829 STEP_TARGETS configure build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000830 USES_TERMINAL_CONFIGURE 1
831 USES_TERMINAL_BUILD 1
832 USES_TERMINAL_INSTALL 1
Petr Hosek5a34c342017-12-05 00:15:20 +0000833 LIST_SEPARATOR |
Mike Spertuse9f15b42016-03-28 18:24:22 +0000834 )
835
836 # exclude really-install from main target
837 set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
838 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
Chris Bienemand052efc2016-07-25 23:48:14 +0000839 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
Mike Spertuse9f15b42016-03-28 18:24:22 +0000840 COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
841 DEPENDEES build
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000842 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000843 )
844 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
845 add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
846
847 if(NOT CLANG_BOOTSTRAP_TARGETS)
848 set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
849 endif()
850 foreach(target ${CLANG_BOOTSTRAP_TARGETS})
Shoaib Meenai0135bc92021-03-25 00:16:47 -0700851
Mike Spertuse9f15b42016-03-28 18:24:22 +0000852 ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
Chris Bienemand052efc2016-07-25 23:48:14 +0000853 COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
Mike Spertuse9f15b42016-03-28 18:24:22 +0000854 COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
855 DEPENDEES configure
Tom Stellardbe067612024-06-10 13:01:58 -0700856 # We need to set ALWAYS to ON here, otherwise these targets won't be
857 # built on a second invocation of ninja. The targets have their own
858 # logic to determine if they should build or not so setting ALWAYS ON
859 # here does not mean the targets will always rebuild it just means that
860 # they will check their dependenices and see if they need to be built.
861 ALWAYS ON
Shoaib Meenai33930a02021-03-25 00:20:01 -0700862 EXCLUDE_FROM_MAIN ON
Chris Bienemanf325b8c2016-06-09 22:38:42 +0000863 USES_TERMINAL 1
Mike Spertuse9f15b42016-03-28 18:24:22 +0000864 )
865
866 if(target MATCHES "^stage[0-9]*")
867 add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
868 endif()
869
870 ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
871 endforeach()
872endif()
873
Mike Spertuse9f15b42016-03-28 18:24:22 +0000874if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
875 add_subdirectory(utils/ClangVisualizers)
876endif()
Bruno Cardoso Lopesdc3f88a2018-06-21 21:45:24 +0000877add_subdirectory(utils/hmaptool)
Dominic Chen08f943c2017-04-04 19:52:25 +0000878
Michal Gornycdbeaf52019-10-07 18:14:56 +0000879if(CLANG_BUILT_STANDALONE)
880 llvm_distribution_add_targets()
serge-sans-paille3a0f6e62020-02-16 09:31:16 +0100881 process_llvm_pass_plugins()
Michal Gornycdbeaf52019-10-07 18:14:56 +0000882endif()
883
John Ericson3adda392022-08-20 11:20:12 -0400884set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
885
Dominic Chen08f943c2017-04-04 19:52:25 +0000886configure_file(
887 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
888 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)