Reland "[CMake] Bumps minimum version to 3.20.0.
This reverts commit d763c6e5e2d0a6b34097aa7dabca31e9aff9b0b6.
Adds the patch by @hans from
https://github.com/llvm/llvm-project/issues/62719
This patch fixes the Windows build.
d763c6e5e2d0a6b34097aa7dabca31e9aff9b0b6 reverted the reviews
D144509 [CMake] Bumps minimum version to 3.20.0.
This partly undoes D137724.
This change has been discussed on discourse
https://discourse.llvm.org/t/rfc-upgrading-llvms-minimum-required-cmake-version/66193
Note this does not remove work-arounds for older CMake versions, that
will be done in followup patches.
D150532 [OpenMP] Compile assembly files as ASM, not C
Since CMake 3.20, CMake explicitly passes "-x c" (or equivalent)
when compiling a file which has been set as having the language
C. This behaviour change only takes place if "cmake_minimum_required"
is set to 3.20 or newer, or if the policy CMP0119 is set to new.
Attempting to compile assembly files with "-x c" fails, however
this is workarounded in many cases, as OpenMP overrides this with
"-x assembler-with-cpp", however this is only added for non-Windows
targets.
Thus, after increasing cmake_minimum_required to 3.20, this breaks
compiling the GNU assembly for Windows targets; the GNU assembly is
used for ARM and AArch64 Windows targets when building with Clang.
This patch unbreaks that.
D150688 [cmake] Set CMP0091 to fix Windows builds after the cmake_minimum_required bump
The build uses other mechanism to select the runtime.
Fixes #62719
Reviewed By: #libc, Mordante
Differential Revision: https://reviews.llvm.org/D151344
2023-05-24 08:12:32 -08:00
cmake_minimum_required(VERSION 3.20.0)
2026-06-29 10:16:27 -08:00
if("${CMAKE_VERSION}" VERSION_LESS "3.31.0")
message(WARNING
"Your CMake version is ${CMAKE_VERSION}. Starting with LLVM 24, the "
"minimum version of CMake required to build LLVM will become 3.31.0, and "
"using an older CMake will become an error. Please upgrade your CMake to "
"at least 3.31.0 now to avoid issues in the future.")
endif()
2024-05-25 07:16:39 -08:00
set(LLVM_SUBPROJECT_TITLE "Clang")
2016-03-28 10:24:22 -08:00
2022-10-23 20:31:37 -08:00
if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
endif()
include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
NO_POLICY_SCOPE)
2016-03-28 10:24:22 -08:00
# If we are not building as a part of LLVM, build Clang as an
# standalone project, using LLVM as an external library:
2022-01-01 21:29:26 -09:00
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
2016-03-28 10:24:22 -08:00
project(Clang)
2022-01-15 21:14:24 -09:00
set(CLANG_BUILT_STANDALONE TRUE)
endif()
2024-03-23 11:26:20 -08:00
# Make sure that our source directory is on the current cmake module path so that
# we can include cmake files from this directory.
list(INSERT CMAKE_MODULE_PATH 0
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
"${LLVM_COMMON_CMAKE_UTILS}/Modules"
)
2022-01-15 21:14:24 -09:00
# Must go below project(..)
include(GNUInstallDirs)
2024-03-22 14:29:36 -08:00
include(GetDarwinLinkerVersion)
2016-03-28 10:24:22 -08:00
2022-01-15 21:14:24 -09:00
if(CLANG_BUILT_STANDALONE)
2022-08-05 11:45:55 -08:00
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
2020-07-10 08:33:54 -08:00
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
2016-03-28 10:24:22 -08:00
if(NOT MSVC_IDE)
set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
CACHE BOOL "Enable assertions")
# Assertions should follow llvm-config's.
mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
endif()
2021-09-16 08:27:53 -08:00
find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
2022-02-02 06:37:13 -09:00
list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
CMake: Deprecate using llvm-config to detect llvm installation
Summary:
clang currently uses llvm-config to determine the installation paths
for llvm's headers and binaries. clang is also using LLVM's cmake
files to determine other information about the LLVM build, like
LLVM_LIBDIR_SUFFIX, LLVM_VERSION_*, etc. Since the installation
paths are also available via the cmake files, we can simplify the code
by only relying on information from cmake about the LLVM install and
dropping the use of llvm-config altogether.
In addition to simplifying the code, the cmake files have more
accurate information about the llvm installation paths. llvm-config
assumes that the lib, bin, and cmake directories are always located
in the same place relative to the path of the llvm-config executable.
This can be wrong if a user decides to install headers, binaries
or libraries to a non-standard location: e.g. static libraries
installed to /usr/lib/llvm6.0/
This patch takes the first step towards dropping llvm-config by
removing the automatic detection of llvm-config (users can still
manually supply a path to llvm-config by passing
-DLLVM_CONFIG=/usr/bin/llvm-config to cmake) and adding a
deprecation warning when users try to use this option.
Reviewers: chandlerc, beanz, mgorny, chapuni
Subscribers: mehdi_amini, dexonsmith, cfe-commits
Differential Revision: https://reviews.llvm.org/D51714
llvm-svn: 346732
2018-11-12 18:42:46 -09:00
2022-08-06 05:22:05 -08:00
# Turn into CACHE PATHs for overwritting
set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
2016-03-28 10:24:22 -08:00
find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH)
# They are used as destination of target generators.
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
2022-08-18 18:44:46 -08:00
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
2016-03-28 10:24:22 -08:00
if(WIN32 OR CYGWIN)
# DLL platform -- put DLLs into bin.
set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
else()
set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
endif()
option(LLVM_INSTALL_TOOLCHAIN_ONLY
"Only include toolchain files in the 'install' target." OFF)
2023-05-17 00:28:58 -08:00
option(LLVM_FORCE_USE_OLD_TOOLCHAIN
2016-03-28 10:24:22 -08:00
"Set to ON to force using an old, unsupported host toolchain." OFF)
option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
2018-11-29 05:57:14 -09:00
option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
2016-03-28 10:24:22 -08:00
2025-05-21 21:14:13 -08:00
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
list(APPEND CMAKE_REQUIRED_DEFINITIONS ${LLVM_DEFINITIONS_LIST})
2016-03-28 10:24:22 -08:00
include(AddLLVM)
include(TableGen)
include(HandleLLVMOptions)
include(VersionFromVCS)
2022-10-19 11:12:10 -08:00
include(CheckAtomic)
2021-03-15 11:56:08 -08:00
include(GetErrcMessages)
2019-10-07 10:14:56 -08:00
include(LLVMDistributionSupport)
2016-03-28 10:24:22 -08:00
[clang] Support building native tools when cross-compiling standalone clang (#160605)
When cross-compiling the LLVM project as a whole (from llvm/), if it
cannot find presupplied tools it will create a native build environment
to build the tools it needs.
However, when doing a standalone build of clang (that is, from clang/
and linking against an existing libLLVM) this doesn't work. Instead a
_target_ binary is built which predictably then fails.
The conventional workaround for this is to build the native tools in a
separate native compile phase and pass the paths to the cross build, for
example see OpenEmbedded[1] or Nix[2]. But we can do better!
The first problem is that LLVM_USE_HOST_TOOLS is only set in the llvm/
CMakeLists.txt, so setup_host_tool() will never consider building a
native binary. This can be solved by setting LLVM_USE_HOST_TOOLS based
on CMAKE_CROSSCOMPILING in clang/CMakeLists.txt in the standalone case.
Now setup_host_tool() will try to build a native tool, but it needs
build_native_tool() from CrossCompile.cmake, so that also needs to be
included.
Finally, the native binary then fails because there's no provider for
the dependency "CONFIGURE_Clang_NATIVE", so use llvm_create_cross_target
to create the native environment.
These few lines mirror what the lldb CMakeLists.txt does in the
standalone case, so there is prior art for this.
[1]
https://git.openembedded.org/openembedded-core/tree/meta/recipes-devtools/clang/clang_git.bb?id=e18d697e92b55e57124e80234369d46575226386#n212
[2]
https://github.com/NixOS/nixpkgs/blob/3354d448f2a26117a74638957b0131ce3da9c8c4/pkgs/development/compilers/llvm/common/tblgen.nix#L54
2025-09-26 02:48:00 -08:00
if(CMAKE_CROSSCOMPILING)
set(LLVM_USE_HOST_TOOLS ON)
include(CrossCompile)
llvm_create_cross_target(Clang NATIVE "" Release)
endif()
2016-03-28 10:24:22 -08:00
set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
2020-07-30 14:11:40 -08:00
set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
"Default URL where bug reports are to be submitted.")
2016-03-28 10:24:22 -08:00
if (NOT DEFINED LLVM_INCLUDE_TESTS)
set(LLVM_INCLUDE_TESTS ON)
endif()
2022-07-25 23:17:30 -08:00
include_directories(${LLVM_INCLUDE_DIRS})
2016-03-28 10:24:22 -08:00
link_directories("${LLVM_LIBRARY_DIR}")
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
2022-08-18 18:44:46 -08:00
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
2016-03-28 10:24:22 -08:00
2023-06-08 23:51:40 -08:00
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
COMPONENTS Interpreter)
2016-03-28 10:24:22 -08:00
2023-06-08 23:51:40 -08:00
if(LLVM_INCLUDE_TESTS)
2016-03-28 10:24:22 -08:00
# Check prebuilt llvm/utils.
if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
set(LLVM_UTILS_PROVIDED ON)
endif()
2022-11-03 14:12:50 -08:00
# Seek installed Lit.
find_program(LLVM_LIT
NAMES llvm-lit lit.py lit
PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
DOC "Path to lit.py")
2016-03-28 10:24:22 -08:00
if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
2016-10-18 09:07:30 -08:00
# Note: path not really used, except for checking if lit was found
2017-11-17 13:21:23 -09:00
if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
endif()
2016-03-28 10:24:22 -08:00
if(NOT LLVM_UTILS_PROVIDED)
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
set(LLVM_UTILS_PROVIDED ON)
set(CLANG_TEST_DEPS FileCheck count not)
endif()
2023-03-10 17:00:06 -09:00
endif()
if (NOT TARGET llvm_gtest)
message(FATAL_ERROR "llvm-gtest not found. Please install llvm-gtest or disable tests with -DLLVM_INCLUDE_TESTS=OFF")
2016-03-28 10:24:22 -08:00
endif()
if(LLVM_LIT)
# Define the default arguments to use with 'lit', and an option for the user
# to override.
set(LIT_ARGS_DEFAULT "-sv")
if (MSVC OR XCODE)
set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
endif()
set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
2021-03-15 11:56:08 -08:00
get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
2016-03-28 10:24:22 -08:00
# On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
if( WIN32 AND NOT CYGWIN )
set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
endif()
else()
set(LLVM_INCLUDE_TESTS OFF)
endif()
2022-03-16 10:46:28 -08:00
umbrella_lit_testsuite_begin(check-all)
2025-06-27 23:10:49 -08:00
# If LLVM was built with Z3, check if we still have the headers around.
# They are used by a few tests.
if (LLVM_WITH_Z3)
find_package(Z3 4.8.9)
endif()
2022-03-16 10:46:28 -08:00
endif() # LLVM_INCLUDE_TESTS
2022-01-01 21:29:26 -09:00
endif() # standalone
2016-03-28 10:24:22 -08:00
2022-10-25 09:22:23 -08:00
# This allows disabling clang's XML dependency even if LLVM finds libxml2.
# By default, clang depends on libxml2 if LLVM does.
option(CLANG_ENABLE_LIBXML2 "Whether libclang may depend on libxml2"
${LLVM_ENABLE_LIBXML2})
if(CLANG_ENABLE_LIBXML2)
2018-11-29 05:57:14 -09:00
# Don't look for libxml if we're using MSan, since uninstrumented third party
# code may call MSan interceptors like strlen, leading to false positives.
if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
set (LIBXML2_FOUND 0)
find_package(LibXml2 2.5.3 QUIET)
if (LIBXML2_FOUND)
set(CLANG_HAVE_LIBXML 1)
endif()
2017-09-01 19:53:42 -08:00
endif()
2016-03-28 10:24:22 -08:00
endif()
2024-04-11 12:56:31 -08:00
if(CLANG_ENABLE_CIR)
2024-04-24 18:26:40 -08:00
if (CLANG_BUILT_STANDALONE)
message(FATAL_ERROR
"ClangIR is not yet supported in the standalone build.")
endif()
2024-04-11 12:56:31 -08:00
if (NOT "${LLVM_ENABLE_PROJECTS}" MATCHES "MLIR|mlir")
message(FATAL_ERROR
"Cannot build ClangIR without MLIR in LLVM_ENABLE_PROJECTS")
endif()
endif()
2016-08-23 12:07:07 -08:00
include(CheckIncludeFile)
check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)
2023-12-22 04:12:19 -09:00
# This check requires _GNU_SOURCE on linux
check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
if( CLANG_HAVE_DLFCN_H )
2025-05-07 11:17:54 -08:00
include(CMakePushCheckState)
2023-12-22 04:12:19 -09:00
include(CheckLibraryExists)
include(CheckSymbolExists)
check_library_exists(dl dlopen "" HAVE_LIBDL)
2025-05-07 11:17:54 -08:00
cmake_push_check_state()
2023-12-22 04:12:19 -09:00
if( HAVE_LIBDL )
list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
endif()
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
2025-05-07 11:17:54 -08:00
cmake_pop_check_state()
2023-12-22 04:12:19 -09:00
endif()
2016-03-28 10:24:22 -08:00
set(CLANG_RESOURCE_DIR "" CACHE STRING
"Relative directory from the Clang binary to its resource files.")
set(C_INCLUDE_DIRS "" CACHE STRING
"Colon separated list of directories clang will search for headers.")
2024-03-20 21:45:38 -08:00
set(USE_DEPRECATED_GCC_INSTALL_PREFIX OFF CACHE BOOL "Temporary workaround before GCC_INSTALL_PREFIX is completely removed")
2016-03-28 10:24:22 -08:00
set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
2020-03-19 14:20:49 -08:00
set(DEFAULT_SYSROOT "" CACHE STRING
2016-03-28 10:24:22 -08:00
"Default <path> to all compiler invocations for --sysroot=<path>." )
2024-03-20 21:45:38 -08:00
if(GCC_INSTALL_PREFIX AND NOT USE_DEPRECATED_GCC_INSTALL_PREFIX)
message(FATAL_ERROR "GCC_INSTALL_PREFIX is deprecated and will be removed. Use "
2024-01-10 10:01:55 -09:00
"configuration files (https://clang.llvm.org/docs/UsersManual.html#configuration-files)"
"to specify the default --gcc-install-dir= or --gcc-triple=. --gcc-toolchain= is discouraged. "
"See https://github.com/llvm/llvm-project/pull/77537 for detail.")
endif()
2016-03-28 10:24:22 -08:00
2026-03-14 14:00:56 -08:00
cmake_dependent_option(CLANG_USE_XCSELECT "Use libxcselect to find the macOS SDK." OFF "APPLE" OFF)
if(CLANG_USE_XCSELECT)
if(DEFAULT_SYSROOT)
message(FATAL_ERROR "Setting DEFAULT_SYSROOT is incompatible with CLANG_USE_XCSELECT.")
endif()
check_include_file(xcselect.h CLANG_HAVE_XCSELECT_H)
if(NOT CLANG_HAVE_XCSELECT_H)
message(FATAL_ERROR "CLANG_USE_XCSELECT is enabled but xcselect.h was not found.")
endif()
include(CheckSymbolExists)
list(APPEND CMAKE_REQUIRED_LIBRARIES xcselect)
check_symbol_exists(xcselect_host_sdk_path xcselect.h CLANG_HAVE_XCSELECT_HOST_SDK_PATH)
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES xcselect)
if(NOT CLANG_HAVE_XCSELECT_HOST_SDK_PATH)
message(FATAL_ERROR "CLANG_USE_XCSELECT is enabled but either libxcselect is not available "
"or it is missing xcselect_host_sdk_path.")
endif()
set(XCSELECT_VALID_POLICIES LATEST MATCHING_ONLY MATCHING_PREFERRED)
set(CLANG_XCSELECT_HOST_SDK_POLICY "LATEST" CACHE STRING
"Policy to use for xcselect. One of: ${XCSELECT_VALID_POLICIES}")
set_property(CACHE CLANG_XCSELECT_HOST_SDK_POLICY PROPERTY STRINGS ${XCSELECT_VALID_POLICIES})
string(TOUPPER ${CLANG_XCSELECT_HOST_SDK_POLICY} CLANG_XCSELECT_HOST_SDK_POLICY)
list(JOIN XCSELECT_VALID_POLICIES "|" XCSELECT_POLICY_REGEX)
if(NOT CLANG_XCSELECT_HOST_SDK_POLICY MATCHES "^XCSELECT_HOST_SDK_POLICY_(${XCSELECT_POLICY_REGEX})$")
if(NOT CLANG_XCSELECT_HOST_SDK_POLICY IN_LIST XCSELECT_VALID_POLICIES)
message(FATAL_ERROR
"CLANG_XCSELECT_HOST_SDK_POLICY (${CLANG_XCSELECT_HOST_SDK_POLICY}) must be one of: ${XCSELECT_VALID_POLICIES}")
endif()
set(CLANG_XCSELECT_HOST_SDK_POLICY "XCSELECT_HOST_SDK_POLICY_${CLANG_XCSELECT_HOST_SDK_POLICY}")
endif()
endif()
2016-06-03 09:26:16 -08:00
set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
2020-08-02 22:05:50 -08:00
set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
2016-06-20 15:54:44 -08:00
"enable x86 relax relocations by default")
2022-01-26 07:49:17 -09:00
set(PPC_LINUX_DEFAULT_IEEELONGDOUBLE OFF CACHE BOOL
"Enable IEEE binary128 as default long double format on PowerPC Linux.")
[Clang][Driver] Re-use the calling process instead of creating a new process for the cc1 invocation
With this patch, the clang tool will now call the -cc1 invocation directly inside the same process. Previously, the -cc1 invocation was creating, and waiting for, a new process.
This patch therefore reduces the number of created processes during a build, thus it reduces build times on platforms where process creation can be costly (Windows) and/or impacted by a antivirus.
It also makes debugging a bit easier, as there's no need to attach to the secondary -cc1 process anymore, breakpoints will be hit inside the same process.
Crashes or signaling inside the -cc1 invocation will have the same side-effect as before, and will be reported through the same means.
This behavior can be controlled at compile-time through the CLANG_SPAWN_CC1 cmake flag, which defaults to OFF. Setting it to ON will revert to the previous behavior, where any -cc1 invocation will create/fork a secondary process.
At run-time, it is also possible to tweak the CLANG_SPAWN_CC1 environment variable. Setting it and will override the compile-time setting. A value of 0 calls -cc1 inside the calling process; a value of 1 will create a secondary process, as before.
Differential Revision: https://reviews.llvm.org/D69825
2020-01-13 06:40:04 -09:00
set(CLANG_SPAWN_CC1 OFF CACHE BOOL
"Whether clang should use a new process for the CC1 invocation")
2022-04-08 22:40:18 -08:00
option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on linux-gnu" ON)
2021-12-14 09:08:59 -09:00
2016-12-14 07:46:50 -09:00
set(CLANG_DEFAULT_LINKER "" CACHE STRING
"Default linker to use (linker name or absolute path, empty for platform default)")
2016-03-28 10:24:22 -08:00
set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
2016-07-27 00:15:54 -08:00
"Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default")
2016-03-28 10:24:22 -08:00
if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
CLANG_DEFAULT_CXX_STDLIB STREQUAL "libstdc++" OR
CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++"))
2016-07-25 00:04:26 -08:00
message(WARNING "Resetting default C++ stdlib to use platform default")
2016-07-27 00:15:54 -08:00
set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING
"Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
endif()
set(CLANG_DEFAULT_RTLIB "" CACHE STRING
"Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
CLANG_DEFAULT_RTLIB STREQUAL "libgcc" OR
CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt"))
message(WARNING "Resetting default rtlib to use platform default")
set(CLANG_DEFAULT_RTLIB "" CACHE STRING
"Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)" FORCE)
2016-03-28 10:24:22 -08:00
endif()
2019-03-19 12:01:59 -08:00
set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
"Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)")
if (CLANG_DEFAULT_UNWINDLIB STREQUAL "")
if (CLANG_DEFAULT_RTLIB STREQUAL "libgcc")
set (CLANG_DEFAULT_UNWINDLIB "libgcc" CACHE STRING "" FORCE)
endif()
endif()
2019-03-25 08:38:48 -08:00
if (NOT(CLANG_DEFAULT_UNWINDLIB STREQUAL "" OR
CLANG_DEFAULT_UNWINDLIB STREQUAL "none" OR
2019-03-19 12:01:59 -08:00
CLANG_DEFAULT_UNWINDLIB STREQUAL "libgcc" OR
CLANG_DEFAULT_UNWINDLIB STREQUAL "libunwind"))
message(WARNING "Resetting default unwindlib to use platform default")
set(CLANG_DEFAULT_UNWINDLIB "" CACHE STRING
2021-03-07 03:41:04 -09:00
"Default unwind library to use (\"none\" \"libgcc\" or \"libunwind\", empty to match runtime library.)" FORCE)
2019-03-19 12:01:59 -08:00
endif()
2017-11-10 16:15:41 -09:00
set(CLANG_DEFAULT_OBJCOPY "objcopy" CACHE STRING
"Default objcopy executable to use.")
2016-03-28 10:24:22 -08:00
set(CLANG_DEFAULT_OPENMP_RUNTIME "libomp" CACHE STRING
"Default OpenMP runtime used by -fopenmp.")
2020-03-30 10:11:35 -08:00
set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
2020-03-30 04:20:48 -08:00
2016-03-28 10:24:22 -08:00
set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
"Vendor-specific text for showing with version information.")
set(CLANG_REPOSITORY_STRING "" CACHE STRING
"Vendor-specific text for showing the repository the source is taken from.")
if(CLANG_REPOSITORY_STRING)
add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
endif()
set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
"Vendor-specific uti.")
2018-08-20 14:50:18 -08:00
set(CLANG_PYTHON_BINDINGS_VERSIONS "" CACHE STRING
"Python versions to install libclang python bindings for")
2019-07-03 14:45:55 -08:00
set(CLANG_LINK_CLANG_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
2019-07-11 13:42:55 -08:00
"Link tools against libclang-cpp.so")
2019-07-03 14:45:55 -08:00
if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
message(FATAL_ERROR "Cannot set CLANG_LINK_CLANG_DYLIB=ON when "
"LLVM_LINK_LLVM_DYLIB=OFF")
endif()
2022-08-18 18:44:46 -08:00
# The libdir suffix must exactly match whatever LLVM's configuration used.
set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
[cmake] Don't export `LLVM_TOOLS_INSTALL_DIR` anymore
First of all, `LLVM_TOOLS_INSTALL_DIR` put there breaks our NixOS
builds, because `LLVM_TOOLS_INSTALL_DIR` defined the same as
`CMAKE_INSTALL_BINDIR` becomes an *absolute* path, and then when
downstream projects try to install there too this breaks because our
builds always install to fresh directories for isolation's sake.
Second of all, note that `LLVM_TOOLS_INSTALL_DIR` stands out against the
other specially crafted `LLVM_CONFIG_*` variables substituted in
`llvm/cmake/modules/LLVMConfig.cmake.in`.
@beanz added it in d0e1c2a550ef348aae036d0fe78cab6f038c420c to fix a
dangling reference in `AddLLVM`, but I am suspicious of how this
variable doesn't follow the pattern.
Those other ones are carefully made to be build-time vs install-time
variables depending on which `LLVMConfig.cmake` is being generated, are
carefully made relative as appropriate, etc. etc. For my NixOS use-case
they are also fine because they are never used as downstream install
variables, only for reading not writing.
To avoid the problems I face, and restore symmetry, I deleted the
exported and arranged to have many `${project}_TOOLS_INSTALL_DIR`s.
`AddLLVM` now instead expects each project to define its own, and they
do so based on `CMAKE_INSTALL_BINDIR`. `LLVMConfig` still exports
`LLVM_TOOLS_BINARY_DIR` which is the location for the tools defined in
the usual way, matching the other remaining exported variables.
For the `AddLLVM` changes, I tried to copy the existing pattern of
internal vs non-internal or for LLVM vs for downstream function/macro
names, but it would good to confirm I did that correctly.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D117977
2022-06-10 22:11:59 -08:00
set(CLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
"Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
mark_as_advanced(CLANG_TOOLS_INSTALL_DIR)
2016-03-28 10:24:22 -08:00
set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
2018-08-30 15:41:03 -08:00
message(FATAL_ERROR "In-source builds are not allowed. "
"Please create a directory and run cmake "
2016-03-28 10:24:22 -08:00
"from there, passing the path to this source directory as the last argument. "
"This process created the file `CMakeCache.txt' and the directory "
"`CMakeFiles'. Please delete them.")
endif()
2016-09-20 11:09:21 -08:00
# If CLANG_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
if(NOT DEFINED CLANG_VERSION_MAJOR)
set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
endif()
if(NOT DEFINED CLANG_VERSION_MINOR)
set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
endif()
if(NOT DEFINED CLANG_VERSION_PATCHLEVEL)
set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
endif()
2023-12-05 08:20:12 -09:00
if(NOT DEFINED CLANG_VERSION_SUFFIX)
set(CLANG_VERSION_SUFFIX ${LLVM_VERSION_SUFFIX})
endif()
set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}${CLANG_VERSION_SUFFIX}")
2025-01-28 17:59:05 -09:00
set(MAX_CLANG_ABI_COMPAT_VERSION "${LLVM_VERSION_MAJOR}")
2016-03-28 10:24:22 -08:00
message(STATUS "Clang version: ${CLANG_VERSION}")
# Configure the Version.inc file.
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
# Add appropriate flags for GCC
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
# Enable -pedantic for Clang even if it's not enabled for LLVM.
if (NOT LLVM_ENABLE_PEDANTIC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
endif ()
2024-06-10 22:03:41 -08:00
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
endif ()
2016-03-28 10:24:22 -08:00
endif ()
# Determine HOST_LINK_VERSION on Darwin.
2026-06-15 15:06:37 -08:00
set(HOST_LINK_VERSION "" CACHE STRING "Host linker version on Darwin")
if (APPLE AND NOT HOST_LINK_VERSION AND NOT CMAKE_LINKER MATCHES ".*lld.*")
2024-03-22 14:29:36 -08:00
get_darwin_linker_version(HOST_LINK_VERSION)
2026-06-15 15:06:37 -08:00
set(HOST_LINK_VERSION "${HOST_LINK_VERSION}" CACHE STRING "Host linker version on Darwin" FORCE)
endif()
if (HOST_LINK_VERSION)
2020-08-05 13:03:12 -08:00
message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
2016-03-28 10:24:22 -08:00
endif()
2016-07-09 13:58:40 -08:00
include(AddClang)
2016-03-28 10:24:22 -08:00
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories(BEFORE
${CMAKE_CURRENT_BINARY_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include
)
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
install(DIRECTORY include/clang include/clang-c
2022-01-15 21:14:24 -09:00
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
2019-03-11 10:53:57 -08:00
COMPONENT clang-headers
2016-03-28 10:24:22 -08:00
FILES_MATCHING
PATTERN "*.def"
PATTERN "*.h"
PATTERN "config.h" EXCLUDE
)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang
2022-01-15 21:14:24 -09:00
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
2019-03-11 10:53:57 -08:00
COMPONENT clang-headers
2016-03-28 10:24:22 -08:00
FILES_MATCHING
PATTERN "CMakeFiles" EXCLUDE
PATTERN "*.inc"
PATTERN "*.h"
)
2017-05-23 10:39:08 -08:00
2019-03-11 10:53:57 -08:00
# Installing the headers needs to depend on generating any public
# tablegen'd headers.
add_custom_target(clang-headers DEPENDS clang-tablegen-targets)
2024-05-25 07:16:39 -08:00
set_target_properties(clang-headers PROPERTIES FOLDER "Clang/Resources")
2019-03-11 10:53:57 -08:00
if(NOT LLVM_ENABLE_IDE)
add_llvm_install_targets(install-clang-headers
DEPENDS clang-headers
COMPONENT clang-headers)
endif()
2019-10-03 21:43:20 -08:00
add_custom_target(bash-autocomplete DEPENDS utils/bash-autocomplete.sh)
2024-05-25 07:16:39 -08:00
set_target_properties(bash-autocomplete PROPERTIES FOLDER "Clang/Misc")
2022-09-05 06:00:00 -08:00
install(FILES utils/bash-autocomplete.sh
2022-01-15 21:14:24 -09:00
DESTINATION "${CMAKE_INSTALL_DATADIR}/clang"
2019-10-03 21:43:20 -08:00
COMPONENT bash-autocomplete)
if(NOT LLVM_ENABLE_IDE)
add_llvm_install_targets(install-bash-autocomplete
DEPENDS bash-autocomplete
COMPONENT bash-autocomplete)
endif()
2016-03-28 10:24:22 -08:00
endif()
2016-07-09 17:44:00 -08:00
option(CLANG_BUILD_TOOLS
"Build the Clang tools. If OFF, just generate build targets." ON)
2022-02-11 12:42:37 -09:00
if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
set(HAVE_CLANG_PLUGIN_SUPPORT ON)
else()
set(HAVE_CLANG_PLUGIN_SUPPORT OFF)
endif()
2022-02-07 14:46:33 -09:00
CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT
"Build clang with plugin support" ON
2022-02-11 12:42:37 -09:00
"HAVE_CLANG_PLUGIN_SUPPORT" OFF)
2022-02-07 14:46:33 -09:00
2022-07-30 12:42:16 -08:00
# If libstdc++ is statically linked, clang-repl needs to statically link libstdc++
2022-09-23 00:08:58 -08:00
# itself, which is not possible in many platforms because of current limitations in
2022-07-30 12:42:16 -08:00
# JIT stack. (more platforms need to be supported by JITLink)
if(NOT LLVM_STATIC_LINK_CXX_STDLIB)
set(HAVE_CLANG_REPL_SUPPORT ON)
endif()
2025-01-29 19:32:25 -09:00
option(CLANG_ENABLE_OBJC_REWRITER "Build the Objective-C rewriter tool" OFF)
2020-09-03 15:37:29 -08:00
option(CLANG_ENABLE_STATIC_ANALYZER
"Include static analyzer in clang binary." ON)
2016-03-28 10:24:22 -08:00
[analyzer] Improved cmake configuration for Z3
Summary:
Enhanced support for Z3 in the cmake configuration of clang; now it is possible to specify any arbitrary Z3 install prefix (CLANG_ANALYZER_Z3_PREFIX) to cmake with lib (or bin) and include folders. Before the patch only in cmake default locations
were searched (https://cmake.org/cmake/help/v3.4/command/find_path.html).
Specifying any CLANG_ANALYZER_Z3_PREFIX will force also CLANG_ANALYZER_BUILD_Z3 to ON.
Removed also Z3 4.5 version requirement since it was not checked, and now Clang works with Z3 4.7
Reviewers: NoQ, george.karpenkov, mikhail.ramalho
Reviewed By: george.karpenkov
Subscribers: rnkovacs, NoQ, esteffin, george.karpenkov, delcypher, ddcc, mgorny, xazax.hun, szepet, a.sidorin, Szelethus
Tags: #clang
Differential Revision: https://reviews.llvm.org/D50818
llvm-svn: 344464
2018-10-13 11:45:48 -08:00
option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
2026-05-29 20:07:14 -08:00
option(CLANG_USE_EXPERIMENTAL_CONST_INTERP
"Use the new experimental constant interpreter for compile-time evaluation." OFF)
2025-01-29 19:32:25 -09:00
if (DEFINED CLANG_ENABLE_ARCMT)
set(CLANG_ENABLE_OBJC_REWRITER ${CLANG_ENABLE_ARCMT})
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.")
2016-03-28 10:24:22 -08:00
endif()
2022-08-24 22:35:46 -08:00
# This option is a stop-gap, we should commit to removing this as
# soon as possible. See discussion:
# https://discourse.llvm.org/t/rationale-for-removing-versioned-libclang-middle-ground-to-keep-it-behind-option/
option(CLANG_FORCE_MATCHING_LIBCLANG_SOVERSION
"Force the SOVERSION of libclang to be equal to CLANG_MAJOR" ON)
2016-03-28 10:24:22 -08:00
# Clang version information
set(CLANG_EXECUTABLE_VERSION
Rename clang link from clang-X.Y to clang-X
Summary:
As we are only doing X.0.Z releases (not using the minor version), there is no need to keep -X.Y in the version.
So, instead, I propose the following:
Instead of having clang-7.0 in bin/, we will have clang-7
Since also matches was gcc is doing.
Reviewers: tstellar, dlj, dim, hans
Reviewed By: dim, hans
Subscribers: dim, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D41808
llvm-svn: 328769
2018-03-29 02:05:46 -08:00
"${CLANG_VERSION_MAJOR}" CACHE STRING
"Major version number that will be appended to the clang executable name")
2016-03-28 10:24:22 -08:00
set(LIBCLANG_LIBRARY_VERSION
Rename clang link from clang-X.Y to clang-X
Summary:
As we are only doing X.0.Z releases (not using the minor version), there is no need to keep -X.Y in the version.
So, instead, I propose the following:
Instead of having clang-7.0 in bin/, we will have clang-7
Since also matches was gcc is doing.
Reviewers: tstellar, dlj, dim, hans
Reviewed By: dim, hans
Subscribers: dim, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D41808
llvm-svn: 328769
2018-03-29 02:05:46 -08:00
"${CLANG_VERSION_MAJOR}" CACHE STRING
"Major version number that will be appended to the libclang library")
2016-03-28 10:24:22 -08:00
mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
option(CLANG_INCLUDE_TESTS
"Generate build targets for the Clang unit tests."
${LLVM_INCLUDE_TESTS})
2022-09-26 18:44:13 -08:00
option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
# While HLSL support is experimental this should stay hidden.
mark_as_advanced(CLANG_ENABLE_HLSL)
2016-03-28 10:24:22 -08:00
add_subdirectory(utils/TableGen)
2022-08-08 02:40:49 -08:00
# Export CLANG_TABLEGEN_EXE for use by flang docs.
set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
2016-03-28 10:24:22 -08:00
add_subdirectory(include)
# All targets below may depend on all tablegen'd files.
get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
Fix missing build dependency on omp_gen.
Summary:
`include/llvm/Frontend/OpenMP/CMakeLists.txt` creates a new target
called `omp_gen` which builds the generated include file `OMP.h.inc`.
This target must therefore be a dependency of every compilation step
whose transitive #include dependencies contain `OMP.h.inc`, or else
it's possible for builds to fail if Ninja (or make or whatever)
schedules that compilation step before building `OMP.h.inc` at all.
A few of those dependencies are currently missing, which leads to
intermittent build failures, depending on the order that Ninja (or
whatever) happens to schedule its commands. As far as I can see,
compiles in `clang/lib/CodeGen`, `clang/lib/Frontend`, and
`clang/examples` all depend transitivily on `OMP.h.inc` (usually via
`clang/AST/AST.h`), but don't have the formal dependency in the ninja
graph.
Adding `omp_gen` to the dependencies of `clang-tablegen-targets` seems
to be the way to get the missing dependency into the `clang/examples`
subdirectory. This also fixes the other two clang subdirectories, as
far as I can see.
Reviewers: clementval, thakis, chandlerc, jdoerfert
Reviewed By: clementval
Subscribers: cfe-commits, jdenny, mgorny, sstefan1, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D82659
2020-07-02 00:16:13 -08:00
add_custom_target(clang-tablegen-targets
DEPENDS
omp_gen
2023-05-10 10:48:11 -08:00
ClangDriverOptions
Fix missing build dependency on omp_gen.
Summary:
`include/llvm/Frontend/OpenMP/CMakeLists.txt` creates a new target
called `omp_gen` which builds the generated include file `OMP.h.inc`.
This target must therefore be a dependency of every compilation step
whose transitive #include dependencies contain `OMP.h.inc`, or else
it's possible for builds to fail if Ninja (or make or whatever)
schedules that compilation step before building `OMP.h.inc` at all.
A few of those dependencies are currently missing, which leads to
intermittent build failures, depending on the order that Ninja (or
whatever) happens to schedule its commands. As far as I can see,
compiles in `clang/lib/CodeGen`, `clang/lib/Frontend`, and
`clang/examples` all depend transitivily on `OMP.h.inc` (usually via
`clang/AST/AST.h`), but don't have the formal dependency in the ninja
graph.
Adding `omp_gen` to the dependencies of `clang-tablegen-targets` seems
to be the way to get the missing dependency into the `clang/examples`
subdirectory. This also fixes the other two clang subdirectories, as
far as I can see.
Reviewers: clementval, thakis, chandlerc, jdoerfert
Reviewed By: clementval
Subscribers: cfe-commits, jdenny, mgorny, sstefan1, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D82659
2020-07-02 00:16:13 -08:00
${CLANG_TABLEGEN_TARGETS})
2024-05-25 07:16:39 -08:00
set_target_properties(clang-tablegen-targets PROPERTIES FOLDER "Clang/Tablegenning/Targets")
2017-07-28 07:33:47 -08:00
list(APPEND LLVM_COMMON_DEPENDS clang-tablegen-targets)
2016-03-28 10:24:22 -08:00
2017-07-22 21:09:44 -08:00
# Force target to be built as soon as possible. Clang modules builds depend
# header-wise on it as they ship all headers from the umbrella folders. Building
# an entire module might include header, which depends on intrinsics_gen.
2020-07-17 15:43:05 -08:00
if(LLVM_ENABLE_MODULES)
2017-07-22 21:09:44 -08:00
list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
endif()
2016-03-28 10:24:22 -08:00
add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(runtime)
option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
2026-07-10 18:27:34 -08:00
option(CLANG_INCLUDE_EXAMPLES
"Generate build targets for the Clang examples."
${LLVM_INCLUDE_EXAMPLES})
if (CLANG_INCLUDE_EXAMPLES)
add_subdirectory(examples)
endif()
2016-03-28 10:24:22 -08:00
2016-12-30 20:25:52 -09:00
if(APPLE)
# this line is needed as a cleanup to ensure that any CMakeCaches with the old
# default value get updated to the new default.
if(CLANG_ORDER_FILE STREQUAL "")
unset(CLANG_ORDER_FILE CACHE)
unset(CLANG_ORDER_FILE)
endif()
set(CLANG_ORDER_FILE ${CMAKE_CURRENT_BINARY_DIR}/clang.order CACHE FILEPATH
"Order file to use when compiling clang in order to improve startup time (Darwin Only - requires ld64).")
if(NOT EXISTS ${CLANG_ORDER_FILE})
string(FIND "${CLANG_ORDER_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" PATH_START)
if(PATH_START EQUAL 0)
file(WRITE ${CLANG_ORDER_FILE} "\n")
else()
message(FATAL_ERROR "Specified order file '${CLANG_ORDER_FILE}' does not exist.")
endif()
endif()
endif()
2016-03-28 10:24:22 -08:00
if( CLANG_INCLUDE_TESTS )
2024-05-08 06:35:47 -08:00
find_package(Perl)
2023-03-10 17:00:06 -09:00
add_subdirectory(unittests)
list(APPEND CLANG_TEST_DEPS ClangUnitTests)
list(APPEND CLANG_TEST_PARAMS
clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
)
2016-03-28 10:24:22 -08:00
add_subdirectory(test)
2025-07-15 20:32:08 -08:00
add_subdirectory(bindings/python/tests)
2016-03-28 10:24:22 -08:00
if(CLANG_BUILT_STANDALONE)
2022-03-16 10:46:28 -08:00
umbrella_lit_testsuite_end(check-all)
2016-03-28 10:24:22 -08:00
endif()
endif()
2026-06-02 00:59:53 -08:00
add_subdirectory(utils/perf-training)
2016-03-28 10:24:22 -08:00
option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
${LLVM_INCLUDE_DOCS})
if( CLANG_INCLUDE_DOCS )
add_subdirectory(docs)
endif()
2019-02-15 06:59:04 -09:00
# Custom target to install all clang libraries.
add_custom_target(clang-libraries)
2024-05-25 07:16:39 -08:00
set_target_properties(clang-libraries PROPERTIES FOLDER "Clang/Install")
2019-02-15 06:59:04 -09:00
2019-02-20 14:08:43 -09:00
if(NOT LLVM_ENABLE_IDE)
2019-02-15 06:59:04 -09:00
add_llvm_install_targets(install-clang-libraries
DEPENDS clang-libraries
COMPONENT clang-libraries)
endif()
get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_LIBS)
if(CLANG_LIBS)
list(REMOVE_DUPLICATES CLANG_LIBS)
foreach(lib ${CLANG_LIBS})
add_dependencies(clang-libraries ${lib})
2019-02-20 14:08:43 -09:00
if(NOT LLVM_ENABLE_IDE)
2019-02-15 06:59:04 -09:00
add_dependencies(install-clang-libraries install-${lib})
2020-03-20 17:42:09 -08:00
add_dependencies(install-clang-libraries-stripped install-${lib}-stripped)
2019-02-15 06:59:04 -09:00
endif()
endforeach()
endif()
2016-06-29 12:22:44 -08:00
add_subdirectory(cmake/modules)
2016-03-28 10:24:22 -08:00
2016-07-25 10:54:30 -08:00
if(CLANG_STAGE)
message(STATUS "Setting current clang stage to: ${CLANG_STAGE}")
endif()
2016-03-28 10:24:22 -08:00
if (CLANG_ENABLE_BOOTSTRAP)
include(ExternalProject)
2016-10-19 13:18:48 -08:00
add_custom_target(clang-bootstrap-deps DEPENDS clang)
2016-03-28 10:24:22 -08:00
if(NOT CLANG_STAGE)
set(CLANG_STAGE stage1)
endif()
string(REGEX MATCH "stage([0-9]*)" MATCHED_STAGE "${CLANG_STAGE}")
if(MATCHED_STAGE)
if(NOT LLVM_BUILD_INSTRUMENTED)
math(EXPR STAGE_NUM "${CMAKE_MATCH_1} + 1")
set(NEXT_CLANG_STAGE stage${STAGE_NUM})
else()
set(NEXT_CLANG_STAGE stage${CMAKE_MATCH_1})
endif()
else()
set(NEXT_CLANG_STAGE bootstrap)
endif()
if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
set(NEXT_CLANG_STAGE ${NEXT_CLANG_STAGE}-instrumented)
endif()
message(STATUS "Setting next clang stage to: ${NEXT_CLANG_STAGE}")
2017-11-10 16:15:41 -09:00
2016-03-28 10:24:22 -08:00
set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-stamps/)
set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-bins/)
2017-01-17 20:41:17 -09:00
if(BOOTSTRAP_LLVM_ENABLE_LLD)
2021-03-09 01:51:09 -09:00
# adding lld to clang-bootstrap-deps without having it enabled in
# LLVM_ENABLE_PROJECTS just generates a cryptic error message.
if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
2021-10-06 09:07:31 -08:00
message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
2021-03-09 01:51:09 -09:00
endif()
2017-01-17 20:41:17 -09:00
add_dependencies(clang-bootstrap-deps lld)
endif()
2023-01-30 14:43:09 -09:00
if (WIN32)
# Build llvm-rc and llvm-mt which are needed by the Windows build.
add_dependencies(clang-bootstrap-deps llvm-rc)
if(LLVM_ENABLE_LIBXML2)
add_dependencies(clang-bootstrap-deps llvm-mt)
endif()
endif()
2016-11-16 14:59:06 -09:00
# If the next stage is LTO we need to depend on LTO and possibly lld or LLVMgold
2016-10-19 13:18:48 -08:00
if(BOOTSTRAP_LLVM_ENABLE_LTO OR LLVM_ENABLE_LTO AND NOT LLVM_BUILD_INSTRUMENTED)
2016-03-28 10:24:22 -08:00
if(APPLE)
2016-11-16 14:59:06 -09:00
add_dependencies(clang-bootstrap-deps LTO)
2016-04-27 10:52:48 -08:00
# on Darwin we need to set DARWIN_LTO_LIBRARY so that -flto will work
# using the just-built compiler, and we need to override DYLD_LIBRARY_PATH
# so that the host object file tools will use the just-built libLTO.
2016-07-25 15:48:14 -08:00
# However if System Integrity Protection is enabled the DYLD variables
# will be scrubbed from the environment of any base system commands. This
# includes /bin/sh, which ninja uses when executing build commands. To
# work around the envar being filtered away we pass it in as a CMake
# variable, and have LLVM's CMake append the envar to the archiver calls.
set(LTO_LIBRARY -DDARWIN_LTO_LIBRARY=${LLVM_SHLIB_OUTPUT_INTDIR}/libLTO.dylib
-DDYLD_LIBRARY_PATH=${LLVM_LIBRARY_OUTPUT_INTDIR})
2026-01-21 21:50:09 -09:00
set(${CLANG_STAGE}_LIBTOOL -DCMAKE_LIBTOOL=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-libtool-darwin)
2023-04-20 16:02:17 -08:00
elseif(MSVC)
add_dependencies(clang-bootstrap-deps llvm-lib)
set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
2016-03-28 10:24:22 -08:00
elseif(NOT WIN32)
2016-11-16 14:59:06 -09:00
add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
2017-01-17 20:41:17 -09:00
if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
2016-11-16 14:59:06 -09:00
add_dependencies(clang-bootstrap-deps LLVMgold)
endif()
2018-11-15 19:46:48 -09:00
set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
2016-03-28 10:24:22 -08:00
endif()
endif()
2018-06-11 12:59:31 -08:00
if(CLANG_BOOTSTRAP_EXTRA_DEPS)
add_dependencies(clang-bootstrap-deps ${CLANG_BOOTSTRAP_EXTRA_DEPS})
endif()
2016-03-28 10:24:22 -08:00
add_custom_target(${NEXT_CLANG_STAGE}-clear
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NEXT_CLANG_STAGE}-cleared
2016-10-19 13:18:48 -08:00
DEPENDS clang-bootstrap-deps
2016-03-28 10:24:22 -08:00
COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR}
COMMENT "Clobberring ${NEXT_CLANG_STAGE} build and stamp directories"
)
if(CMAKE_VERBOSE_MAKEFILE)
set(verbose -DCMAKE_VERBOSE_MAKEFILE=On)
endif()
2016-07-25 10:54:30 -08:00
set(_BOOTSTRAP_DEFAULT_PASSTHROUGH
2016-03-28 10:24:22 -08:00
PACKAGE_VERSION
2016-10-17 16:50:20 -08:00
PACKAGE_VENDOR
2016-03-28 10:24:22 -08:00
LLVM_VERSION_MAJOR
LLVM_VERSION_MINOR
LLVM_VERSION_PATCH
2026-04-02 17:35:50 -08:00
LLVM_VERSIONED_DYLIB_NAME_ON_DARWIN
LLVM_UNVERSIONED_LIBCLANG_ON_DARWIN
LLVM_UNVERSIONED_LIBLTO_ON_DARWIN
2016-09-21 12:43:43 -08:00
CLANG_VERSION_MAJOR
CLANG_VERSION_MINOR
CLANG_VERSION_PATCHLEVEL
2023-12-05 08:20:12 -09:00
CLANG_VERSION_SUFFIX
2020-05-28 23:13:08 -08:00
CLANG_VENDOR
2016-03-28 10:24:22 -08:00
LLVM_VERSION_SUFFIX
LLVM_BINUTILS_INCDIR
CLANG_REPOSITORY_STRING
2016-10-17 16:50:20 -08:00
CMAKE_MAKE_PROGRAM
2017-11-28 15:34:46 -09:00
CMAKE_OSX_ARCHITECTURES
2021-09-21 00:44:08 -08:00
CMAKE_BUILD_TYPE
2017-11-28 15:34:46 -09:00
LLVM_ENABLE_PROJECTS
LLVM_ENABLE_RUNTIMES)
2016-03-28 10:24:22 -08:00
2018-06-28 10:35:25 -08:00
# We don't need to depend on compiler-rt/libcxx if we're building instrumented
2016-10-19 13:18:48 -08:00
# because the next stage will use the same compiler used to build this stage.
2018-06-28 10:35:25 -08:00
if(NOT LLVM_BUILD_INSTRUMENTED)
if(TARGET compiler-rt)
add_dependencies(clang-bootstrap-deps compiler-rt)
endif()
if(TARGET cxx-headers)
add_dependencies(clang-bootstrap-deps cxx-headers)
endif()
2016-03-28 10:24:22 -08:00
endif()
2017-05-11 05:19:24 -08:00
set(C_COMPILER "clang")
set(CXX_COMPILER "clang++")
if(WIN32)
set(C_COMPILER "clang-cl.exe")
set(CXX_COMPILER "clang-cl.exe")
endif()
2016-03-28 10:24:22 -08:00
set(COMPILER_OPTIONS
2017-05-11 05:19:24 -08:00
-DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER}
-DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
2018-01-19 09:31:12 -09:00
-DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/${C_COMPILER}
-DCMAKE_ASM_COMPILER_ID=Clang)
2016-03-28 10:24:22 -08:00
2020-05-29 03:14:51 -08:00
# cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
# otherwise it defaults the linker to be link.exe.
if(BOOTSTRAP_LLVM_ENABLE_LLD)
if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
endif()
endif()
2018-11-15 19:46:48 -09:00
if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
set(${CLANG_STAGE}_TABLEGEN
-DLLVM_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-tblgen
-DCLANG_TABLEGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-tblgen)
if(BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(BOOTSTRAP_LLVM_ENABLE_LLD)
set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
endif()
if(NOT BOOTSTRAP_LLVM_ENABLE_LTO)
add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
endif()
add_dependencies(clang-bootstrap-deps llvm-objcopy llvm-strip)
set(${CLANG_STAGE}_OBJCOPY -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
set(${CLANG_STAGE}_STRIP -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
2021-09-24 16:56:00 -08:00
set(${CLANG_STAGE}_READELF -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf)
2018-11-15 19:46:48 -09:00
endif()
endif()
2016-03-28 10:24:22 -08:00
if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
2016-10-19 13:18:48 -08:00
add_dependencies(clang-bootstrap-deps llvm-profdata)
2016-03-28 10:24:22 -08:00
set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
[Clang][CMake] Add CSSPGO support to LLVM_BUILD_INSTRUMENTED (#79942)
Build on Clang-BOLT infrastructure to collect sample profile for CSSPGO.
Add CSSPGO.cmake and BOLT-CSSPGO.cmake to automate CSSPGO/+BOLT
Clang builds.
Note that `CLANG_PGO_TRAINING_DATA_SOURCE_DIR` is required as built-in
training set is inadequate for collecting sampled profile.
Hardware compatibility: CSSPGO requires synchronized (0-skid) call
and branch stacks, which is only available with Intel PEBS (Sandy
Bridge+),
AMD Zen3 with BRS, Zen4 with LBRv2+LBR_PMC_FREEZE, and Zen5 with LBRv2.
This patch adds support for Intel `br_inst_retired.near_taken:uppp`
event.
Test Plan:
Added BOLT-CSSPGO.cmake with same use as BOLT-PGO.cmake,
e.g. for bootstrapped ThinLTO+CSSPGO+BOLT, with CSSPGO profile collected
from LLVM build, and BOLT profile collected from Hello World
(instrumentation):
```
cmake -B clang-csspgo-bolt -S /path/to/llvm-project/llvm \
-DLLVM_ENABLE_LLD=ON -DBOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DBOOTSTRAP_BOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DPGO_INSTRUMENT_LTO=Thin \
-DBOOTSTRAP_CLANG_PGO_TRAINING_DATA_SOURCE_DIR=/path/to/llvm-project/llvm \
-GNinja -C /path/to/llvm-project/clang/cmake/caches/BOLT-CSSPGO.cmake
ninja stage2-clang-bolt
...
warning: Sample PGO is estimated to optimize better with 19.5x more samples. Please consider increasing sampling rate or profiling for longer duration to get more samples.
...
[2800/2801] Optimizing Clang with BOLT
BOLT-INFO: 8189 out of 106942 functions in the binary (7.7%) have non-empty execution profile
13776393 : taken branches (-42.1%)
```
Performance testing with Clang:
- Setup: Clang-BOLT testing harness
https://github.com/aaupov/llvm-devmtg-2022/commit/9f2b46f67a1930a51c58a0e4894637a8c64c570e
- CSSPGO training: building LLVM,
- InstrPGO training: building Hello World,
- BOLT training: building Hello World, instrumentation,
- benchmark: building small LLVM tool (not),
- 2S Intel SKX Xeon 6138 with 40C/80T and 256GB RAM, using 20C/40T for
build,
- Results, wall time, lower is better
- Baseline (bootstrapped build): 10.36s,
- InstrPGO + ThinLTO: 9.34s,
- CSSPGO + ThinLTO: 8.85s.
- BOLT results, for reference:
- Baseline: 9.09s,
- InstrPGO + ThinLTO: 9.09s,
- CSSPGO + ThinLTO: 8.58s.
---------
Co-authored-by: Matthias Braun <matze@braunis.de>
2025-10-01 14:25:34 -08:00
string(TOUPPER "${BOOTSTRAP_LLVM_BUILD_INSTRUMENTED}" BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
if (BOOTSTRAP_LLVM_BUILD_INSTRUMENTED STREQUAL "CSSPGO")
add_dependencies(clang-bootstrap-deps llvm-profgen)
list(APPEND PGO_OPT -DLLVM_PROFGEN=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profgen)
endif()
2016-03-28 10:24:22 -08:00
endif()
if(LLVM_BUILD_INSTRUMENTED)
[Clang][CMake] Add CSSPGO support to LLVM_BUILD_INSTRUMENTED (#79942)
Build on Clang-BOLT infrastructure to collect sample profile for CSSPGO.
Add CSSPGO.cmake and BOLT-CSSPGO.cmake to automate CSSPGO/+BOLT
Clang builds.
Note that `CLANG_PGO_TRAINING_DATA_SOURCE_DIR` is required as built-in
training set is inadequate for collecting sampled profile.
Hardware compatibility: CSSPGO requires synchronized (0-skid) call
and branch stacks, which is only available with Intel PEBS (Sandy
Bridge+),
AMD Zen3 with BRS, Zen4 with LBRv2+LBR_PMC_FREEZE, and Zen5 with LBRv2.
This patch adds support for Intel `br_inst_retired.near_taken:uppp`
event.
Test Plan:
Added BOLT-CSSPGO.cmake with same use as BOLT-PGO.cmake,
e.g. for bootstrapped ThinLTO+CSSPGO+BOLT, with CSSPGO profile collected
from LLVM build, and BOLT profile collected from Hello World
(instrumentation):
```
cmake -B clang-csspgo-bolt -S /path/to/llvm-project/llvm \
-DLLVM_ENABLE_LLD=ON -DBOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DBOOTSTRAP_BOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DPGO_INSTRUMENT_LTO=Thin \
-DBOOTSTRAP_CLANG_PGO_TRAINING_DATA_SOURCE_DIR=/path/to/llvm-project/llvm \
-GNinja -C /path/to/llvm-project/clang/cmake/caches/BOLT-CSSPGO.cmake
ninja stage2-clang-bolt
...
warning: Sample PGO is estimated to optimize better with 19.5x more samples. Please consider increasing sampling rate or profiling for longer duration to get more samples.
...
[2800/2801] Optimizing Clang with BOLT
BOLT-INFO: 8189 out of 106942 functions in the binary (7.7%) have non-empty execution profile
13776393 : taken branches (-42.1%)
```
Performance testing with Clang:
- Setup: Clang-BOLT testing harness
https://github.com/aaupov/llvm-devmtg-2022/commit/9f2b46f67a1930a51c58a0e4894637a8c64c570e
- CSSPGO training: building LLVM,
- InstrPGO training: building Hello World,
- BOLT training: building Hello World, instrumentation,
- benchmark: building small LLVM tool (not),
- 2S Intel SKX Xeon 6138 with 40C/80T and 256GB RAM, using 20C/40T for
build,
- Results, wall time, lower is better
- Baseline (bootstrapped build): 10.36s,
- InstrPGO + ThinLTO: 9.34s,
- CSSPGO + ThinLTO: 8.85s.
- BOLT results, for reference:
- Baseline: 9.09s,
- InstrPGO + ThinLTO: 9.09s,
- CSSPGO + ThinLTO: 8.58s.
---------
Co-authored-by: Matthias Braun <matze@braunis.de>
2025-10-01 14:25:34 -08:00
string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" LLVM_BUILD_INSTRUMENTED)
if (LLVM_BUILD_INSTRUMENTED STREQUAL "CSSPGO")
2026-06-24 09:01:07 -08:00
set(PGO_OPT_SPROFDATA "${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.sprofdata" CACHE STRING "")
set(PGO_OPT_SPROFDATA_PROVIDER generate-sprofdata CACHE STRING "")
add_dependencies(clang-bootstrap-deps ${PGO_OPT_SPROFDATA_PROVIDER})
set(PGO_OPT -DLLVM_SPROFDATA_FILE=${PGO_OPT_SPROFDATA})
[Clang][CMake] Add CSSPGO support to LLVM_BUILD_INSTRUMENTED (#79942)
Build on Clang-BOLT infrastructure to collect sample profile for CSSPGO.
Add CSSPGO.cmake and BOLT-CSSPGO.cmake to automate CSSPGO/+BOLT
Clang builds.
Note that `CLANG_PGO_TRAINING_DATA_SOURCE_DIR` is required as built-in
training set is inadequate for collecting sampled profile.
Hardware compatibility: CSSPGO requires synchronized (0-skid) call
and branch stacks, which is only available with Intel PEBS (Sandy
Bridge+),
AMD Zen3 with BRS, Zen4 with LBRv2+LBR_PMC_FREEZE, and Zen5 with LBRv2.
This patch adds support for Intel `br_inst_retired.near_taken:uppp`
event.
Test Plan:
Added BOLT-CSSPGO.cmake with same use as BOLT-PGO.cmake,
e.g. for bootstrapped ThinLTO+CSSPGO+BOLT, with CSSPGO profile collected
from LLVM build, and BOLT profile collected from Hello World
(instrumentation):
```
cmake -B clang-csspgo-bolt -S /path/to/llvm-project/llvm \
-DLLVM_ENABLE_LLD=ON -DBOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DBOOTSTRAP_BOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DPGO_INSTRUMENT_LTO=Thin \
-DBOOTSTRAP_CLANG_PGO_TRAINING_DATA_SOURCE_DIR=/path/to/llvm-project/llvm \
-GNinja -C /path/to/llvm-project/clang/cmake/caches/BOLT-CSSPGO.cmake
ninja stage2-clang-bolt
...
warning: Sample PGO is estimated to optimize better with 19.5x more samples. Please consider increasing sampling rate or profiling for longer duration to get more samples.
...
[2800/2801] Optimizing Clang with BOLT
BOLT-INFO: 8189 out of 106942 functions in the binary (7.7%) have non-empty execution profile
13776393 : taken branches (-42.1%)
```
Performance testing with Clang:
- Setup: Clang-BOLT testing harness
https://github.com/aaupov/llvm-devmtg-2022/commit/9f2b46f67a1930a51c58a0e4894637a8c64c570e
- CSSPGO training: building LLVM,
- InstrPGO training: building Hello World,
- BOLT training: building Hello World, instrumentation,
- benchmark: building small LLVM tool (not),
- 2S Intel SKX Xeon 6138 with 40C/80T and 256GB RAM, using 20C/40T for
build,
- Results, wall time, lower is better
- Baseline (bootstrapped build): 10.36s,
- InstrPGO + ThinLTO: 9.34s,
- CSSPGO + ThinLTO: 8.85s.
- BOLT results, for reference:
- Baseline: 9.09s,
- InstrPGO + ThinLTO: 9.09s,
- CSSPGO + ThinLTO: 8.58s.
---------
Co-authored-by: Matthias Braun <matze@braunis.de>
2025-10-01 14:25:34 -08:00
else()
2026-06-24 09:01:07 -08:00
set(PGO_OPT_PROFDATA "${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/clang.profdata" CACHE STRING "")
set(PGO_OPT_PROFDATA_PROVIDER generate-profdata CACHE STRING "")
add_dependencies(clang-bootstrap-deps ${PGO_OPT_PROFDATA_PROVIDER})
set(PGO_OPT -DLLVM_PROFDATA_FILE=${PGO_OPT_PROFDATA})
[Clang][CMake] Add CSSPGO support to LLVM_BUILD_INSTRUMENTED (#79942)
Build on Clang-BOLT infrastructure to collect sample profile for CSSPGO.
Add CSSPGO.cmake and BOLT-CSSPGO.cmake to automate CSSPGO/+BOLT
Clang builds.
Note that `CLANG_PGO_TRAINING_DATA_SOURCE_DIR` is required as built-in
training set is inadequate for collecting sampled profile.
Hardware compatibility: CSSPGO requires synchronized (0-skid) call
and branch stacks, which is only available with Intel PEBS (Sandy
Bridge+),
AMD Zen3 with BRS, Zen4 with LBRv2+LBR_PMC_FREEZE, and Zen5 with LBRv2.
This patch adds support for Intel `br_inst_retired.near_taken:uppp`
event.
Test Plan:
Added BOLT-CSSPGO.cmake with same use as BOLT-PGO.cmake,
e.g. for bootstrapped ThinLTO+CSSPGO+BOLT, with CSSPGO profile collected
from LLVM build, and BOLT profile collected from Hello World
(instrumentation):
```
cmake -B clang-csspgo-bolt -S /path/to/llvm-project/llvm \
-DLLVM_ENABLE_LLD=ON -DBOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DBOOTSTRAP_BOOTSTRAP_LLVM_ENABLE_LLD=ON \
-DPGO_INSTRUMENT_LTO=Thin \
-DBOOTSTRAP_CLANG_PGO_TRAINING_DATA_SOURCE_DIR=/path/to/llvm-project/llvm \
-GNinja -C /path/to/llvm-project/clang/cmake/caches/BOLT-CSSPGO.cmake
ninja stage2-clang-bolt
...
warning: Sample PGO is estimated to optimize better with 19.5x more samples. Please consider increasing sampling rate or profiling for longer duration to get more samples.
...
[2800/2801] Optimizing Clang with BOLT
BOLT-INFO: 8189 out of 106942 functions in the binary (7.7%) have non-empty execution profile
13776393 : taken branches (-42.1%)
```
Performance testing with Clang:
- Setup: Clang-BOLT testing harness
https://github.com/aaupov/llvm-devmtg-2022/commit/9f2b46f67a1930a51c58a0e4894637a8c64c570e
- CSSPGO training: building LLVM,
- InstrPGO training: building Hello World,
- BOLT training: building Hello World, instrumentation,
- benchmark: building small LLVM tool (not),
- 2S Intel SKX Xeon 6138 with 40C/80T and 256GB RAM, using 20C/40T for
build,
- Results, wall time, lower is better
- Baseline (bootstrapped build): 10.36s,
- InstrPGO + ThinLTO: 9.34s,
- CSSPGO + ThinLTO: 8.85s.
- BOLT results, for reference:
- Baseline: 9.09s,
- InstrPGO + ThinLTO: 9.09s,
- CSSPGO + ThinLTO: 8.58s.
---------
Co-authored-by: Matthias Braun <matze@braunis.de>
2025-10-01 14:25:34 -08:00
endif()
2016-08-16 14:16:29 -08:00
# Use the current tools for LTO instead of the instrumented ones
list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
CMAKE_CXX_COMPILER
CMAKE_C_COMPILER
CMAKE_ASM_COMPILER
CMAKE_AR
CMAKE_RANLIB
DARWIN_LTO_LIBRARY
DYLD_LIBRARY_PATH)
set(COMPILER_OPTIONS)
set(LTO_LIBRARY)
set(LTO_AR)
set(LTO_RANLIB)
2016-03-28 10:24:22 -08:00
endif()
2022-08-16 23:16:10 -08:00
# Populate the passthrough variables
foreach(variableName ${CLANG_BOOTSTRAP_PASSTHROUGH} ${_BOOTSTRAP_DEFAULT_PASSTHROUGH})
if(DEFINED ${variableName})
if("${${variableName}}" STREQUAL "")
set(value "")
else()
string(REPLACE ";" "|" value "${${variableName}}")
endif()
list(APPEND PASSTHROUGH_VARIABLES
-D${variableName}=${value})
endif()
endforeach()
2016-03-28 10:24:22 -08:00
# Find all variables that start with BOOTSTRAP_ and populate a variable with
# them.
get_cmake_property(variableNames VARIABLES)
foreach(variableName ${variableNames})
if(variableName MATCHES "^BOOTSTRAP_")
string(SUBSTRING ${variableName} 10 -1 varName)
2017-12-04 15:15:20 -09:00
string(REPLACE ";" "|" value "${${variableName}}")
2016-03-28 10:24:22 -08:00
list(APPEND PASSTHROUGH_VARIABLES
-D${varName}=${value})
endif()
if(${variableName} AND variableName MATCHES "LLVM_EXTERNAL_.*_SOURCE_DIR")
list(APPEND PASSTHROUGH_VARIABLES
-D${variableName}=${${variableName}})
endif()
endforeach()
2022-08-22 20:45:25 -08:00
# Build arguments for native tool used in CMake.
set(build_configuration "$<CONFIG>")
set(build_tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
if(NOT build_tool_args STREQUAL "")
string(PREPEND build_tool_args "-- ")
separate_arguments(build_tool_args UNIX_COMMAND "${build_tool_args}")
endif()
2016-03-28 10:24:22 -08:00
ExternalProject_Add(${NEXT_CLANG_STAGE}
2016-10-19 13:18:48 -08:00
DEPENDS clang-bootstrap-deps
2016-03-28 10:24:22 -08:00
PREFIX ${NEXT_CLANG_STAGE}
SOURCE_DIR ${CMAKE_SOURCE_DIR}
STAMP_DIR ${STAMP_DIR}
BINARY_DIR ${BINARY_DIR}
2016-06-09 14:38:42 -08:00
EXCLUDE_FROM_ALL 1
2016-03-28 10:24:22 -08:00
CMAKE_ARGS
# We shouldn't need to set this here, but INSTALL_DIR doesn't
# seem to work, so instead I'm passing this through
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
${PASSTHROUGH_VARIABLES}
2019-12-13 18:04:36 -09:00
${CLANG_BOOTSTRAP_CMAKE_ARGS}
2016-03-28 10:24:22 -08:00
-DCLANG_STAGE=${NEXT_CLANG_STAGE}
${COMPILER_OPTIONS}
2018-11-15 19:46:48 -09:00
${${CLANG_STAGE}_TABLEGEN}
${LTO_LIBRARY} ${verbose} ${PGO_OPT}
${${CLANG_STAGE}_LINKER}
${${CLANG_STAGE}_AR}
${${CLANG_STAGE}_RANLIB}
${${CLANG_STAGE}_OBJCOPY}
${${CLANG_STAGE}_STRIP}
2026-01-21 21:50:09 -09:00
${${CLANG_STAGE}_LIBTOOL}
2022-08-22 20:45:25 -08:00
BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR}
--config ${build_configuration}
${build_tool_args}
2016-03-28 10:24:22 -08:00
INSTALL_COMMAND ""
STEP_TARGETS configure build
2016-06-09 14:38:42 -08:00
USES_TERMINAL_CONFIGURE 1
USES_TERMINAL_BUILD 1
USES_TERMINAL_INSTALL 1
2017-12-04 15:15:20 -09:00
LIST_SEPARATOR |
2016-03-28 10:24:22 -08:00
)
# exclude really-install from main target
set_target_properties(${NEXT_CLANG_STAGE} PROPERTIES _EP_really-install_EXCLUDE_FROM_MAIN On)
ExternalProject_Add_Step(${NEXT_CLANG_STAGE} really-install
2016-07-25 15:48:14 -08:00
COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
2016-03-28 10:24:22 -08:00
COMMENT "Performing install step for '${NEXT_CLANG_STAGE}'"
DEPENDEES build
2016-06-09 14:38:42 -08:00
USES_TERMINAL 1
2016-03-28 10:24:22 -08:00
)
ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} really-install)
add_custom_target(${NEXT_CLANG_STAGE}-install DEPENDS ${NEXT_CLANG_STAGE}-really-install)
if(NOT CLANG_BOOTSTRAP_TARGETS)
set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
endif()
foreach(target ${CLANG_BOOTSTRAP_TARGETS})
2021-03-24 23:16:47 -08:00
2016-03-28 10:24:22 -08:00
ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
2016-07-25 15:48:14 -08:00
COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
2016-03-28 10:24:22 -08:00
COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
DEPENDEES configure
2024-06-10 12:01:58 -08:00
# We need to set ALWAYS to ON here, otherwise these targets won't be
# built on a second invocation of ninja. The targets have their own
# logic to determine if they should build or not so setting ALWAYS ON
# here does not mean the targets will always rebuild it just means that
# they will check their dependenices and see if they need to be built.
ALWAYS ON
2021-03-24 23:20:01 -08:00
EXCLUDE_FROM_MAIN ON
2016-06-09 14:38:42 -08:00
USES_TERMINAL 1
2016-03-28 10:24:22 -08:00
)
if(target MATCHES "^stage[0-9]*")
add_custom_target(${target} DEPENDS ${NEXT_CLANG_STAGE}-${target})
endif()
ExternalProject_Add_StepTargets(${NEXT_CLANG_STAGE} ${target})
endforeach()
endif()
if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
add_subdirectory(utils/ClangVisualizers)
endif()
2018-06-21 13:45:24 -08:00
add_subdirectory(utils/hmaptool)
2017-04-04 11:52:25 -08:00
2019-10-07 10:14:56 -08:00
if(CLANG_BUILT_STANDALONE)
llvm_distribution_add_targets()
2020-02-15 23:31:16 -09:00
process_llvm_pass_plugins()
2019-10-07 10:14:56 -08:00
endif()
2022-08-20 07:20:12 -08:00
set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
2017-04-04 11:52:25 -08:00
configure_file(
${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
${CLANG_BINARY_DIR}/include/clang/Config/config.h)