Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,11 @@ option(LLVM_INSTALL_GTEST
"Install the llvm gtest library. This should be on if you want to do
stand-alone builds of the other projects and run their unit tests." OFF)

option(LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS
"Enable use of expensive CMake checks for unused source files" ON)
option(LLVM_ENABLE_LIT_CONVENIENCE_TARGETS
"Enable use of convenience targets for all subdirectories of a LIT test suite" ON)

option(LLVM_BUILD_BENCHMARKS "Add LLVM benchmark targets to the list of default
targets. If OFF, benchmarks still could be built using Benchmarks target." OFF)
option(LLVM_INCLUDE_BENCHMARKS "Generate benchmark targets. If OFF, benchmarks can't be built." ON)
Expand Down Expand Up @@ -1027,9 +1032,18 @@ endif()
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
COMPONENTS Interpreter)

set(LLVM_TOOLCHAIN_CHECK_CACHE "" CACHE PATH
"Path to where a generated *.cmake cache file will be saved.")

include(LLVMCacheSnapshot)
# All options referred to from HandleLLVMOptions have to be specified
# BEFORE this include, otherwise options will not be correctly set on
# first cmake run
# first cmake run.
if(LLVM_TOOLCHAIN_CHECK_CACHE AND EXISTS "${LLVM_TOOLCHAIN_CHECK_CACHE}")
include("${LLVM_TOOLCHAIN_CHECK_CACHE}")
elseif(LLVM_TOOLCHAIN_CHECK_CACHE)
llvm_get_list_of_existing_cache_variables(cache_before)
endif()
include(config-ix)

# By default, we target the host, but this can be overridden at CMake
Expand Down Expand Up @@ -1081,6 +1095,12 @@ endif()

include(HandleLLVMOptions)

if(LLVM_TOOLCHAIN_CHECK_CACHE AND NOT EXISTS "${LLVM_TOOLCHAIN_CHECK_CACHE}")
llvm_list_of_new_cache_variables_and_values(cache_before cache_new_pairs)
list(JOIN cache_new_pairs "\n" cache_new_pairs_joined)
file(WRITE "${LLVM_TOOLCHAIN_CHECK_CACHE}" "${cache_new_pairs_joined}")
endif()

######

# Configure all of the various header file fragments LLVM uses which depend on
Expand Down
51 changes: 30 additions & 21 deletions llvm/cmake/modules/AddLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ include(LLVMDistributionSupport)
include(LLVMProcessSources)
include(LLVM-Config)
include(DetermineGCCCompatible)
include(CheckLinkerFlag)

# get_subproject_title(titlevar)
# Set ${outvar} to the title of the current LLVM subproject (Clang, MLIR ...)
#
#
# The title is set in the subproject's top-level using the variable
# LLVM_SUBPROJECT_TITLE. If it does not exist, it is assumed it is LLVM itself.
# The title is not semantically significant, but use to create folders in
# CMake-generated IDE projects (Visual Studio/XCode).
function(get_subproject_title outvar)
if (LLVM_SUBPROJECT_TITLE)
set(${outvar} "${LLVM_SUBPROJECT_TITLE}" PARENT_SCOPE)
set(${outvar} "${LLVM_SUBPROJECT_TITLE}" PARENT_SCOPE)
else ()
set(${outvar} "LLVM" PARENT_SCOPE)
endif ()
Expand Down Expand Up @@ -269,7 +270,6 @@ if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
endif()

if("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
include(CheckLinkerFlag)
# Linkers that support Darwin allow a setting to internalize all symbol exports,
# aiding in reducing binary size and often is applicable for executables.
check_linker_flag(C "-Wl,-no_exported_symbols" LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
Expand All @@ -289,8 +289,23 @@ if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
endif()
endif()

if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
if(NOT LLVM_NO_DEAD_STRIP)
if("${CMAKE_SYSTEM_NAME}" MATCHES "SunOS" AND LLVM_LINKER_IS_SOLARISLD)
# Support for ld -z discard-unused=sections was only added in
# Solaris 11.4. GNU ld ignores it, but warns every time.
check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
endif()
endif()
endif()

# Check for existence of symbolic functions flag. Not supported
# by the older BFD linker (such as on some OpenBSD archs), the
# MinGW driver for LLD, and the Solaris native linker.
check_linker_flag(CXX "-Wl,-Bsymbolic-functions"
LLVM_LINKER_SUPPORTS_B_SYMBOLIC_FUNCTIONS)

function(add_link_opts target_name)
include(CheckLinkerFlag)
get_llvm_distribution(${target_name} in_distribution in_distribution_var)
if(NOT in_distribution)
# Don't LTO optimize targets that aren't part of any distribution.
Expand Down Expand Up @@ -320,9 +335,6 @@ function(add_link_opts target_name)
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-dead_strip")
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "SunOS" AND LLVM_LINKER_IS_SOLARISLD)
# Support for ld -z discard-unused=sections was only added in
# Solaris 11.4. GNU ld ignores it, but warns every time.
check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
if (LINKER_SUPPORTS_Z_DISCARD_UNUSED)
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-z,discard-unused=sections")
Expand All @@ -349,12 +361,6 @@ function(add_link_opts target_name)
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-brtl")
endif()

# Check for existence of symbolic functions flag. Not supported
# by the older BFD linker (such as on some OpenBSD archs), the
# MinGW driver for LLD, and the Solaris native linker.
check_linker_flag(CXX "-Wl,-Bsymbolic-functions"
LLVM_LINKER_SUPPORTS_B_SYMBOLIC_FUNCTIONS)
endfunction(add_link_opts)

# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
Expand Down Expand Up @@ -645,11 +651,11 @@ function(llvm_add_library name)
endif()
set_target_properties(${name} PROPERTIES FOLDER "${subproject_title}/Libraries")

## If were compiling with clang-cl use /Zc:dllexportInlines- to exclude inline
## If were compiling with clang-cl use /Zc:dllexportInlines- to exclude inline
## class members from being dllexport'ed to reduce compile time.
## This will also keep us below the 64k exported symbol limit
## https://blog.llvm.org/2018/11/30-faster-windows-builds-with-clang-cl_14.html
if(LLVM_BUILD_LLVM_DYLIB AND NOT LLVM_DYLIB_EXPORT_INLINES AND
if(LLVM_BUILD_LLVM_DYLIB AND NOT LLVM_DYLIB_EXPORT_INLINES AND
MSVC AND CMAKE_CXX_COMPILER_ID MATCHES Clang)
target_compile_options(${name} PUBLIC /Zc:dllexportInlines-)
if(TARGET ${obj_name})
Expand Down Expand Up @@ -1500,8 +1506,8 @@ macro(llvm_add_tool project name)
RUNTIME DESTINATION ${${project}_TOOLS_INSTALL_DIR}
COMPONENT ${name})
if (LLVM_ENABLE_PDB)
install(FILES $<TARGET_PDB_FILE:${name}>
DESTINATION "${${project}_TOOLS_INSTALL_DIR}" COMPONENT ${name}
install(FILES $<TARGET_PDB_FILE:${name}>
DESTINATION "${${project}_TOOLS_INSTALL_DIR}" COMPONENT ${name}
OPTIONAL)
endif()

Expand Down Expand Up @@ -1535,8 +1541,8 @@ macro(add_llvm_example name)
if( LLVM_BUILD_EXAMPLES )
install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
if (LLVM_ENABLE_PDB)
install(FILES $<TARGET_PDB_FILE:${name}>
DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}" COMPONENT ${name}
install(FILES $<TARGET_PDB_FILE:${name}>
DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}" COMPONENT ${name}
OPTIONAL)
endif()
endif()
Expand Down Expand Up @@ -1574,8 +1580,8 @@ macro(add_llvm_utility name)
RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
COMPONENT ${name})
if (LLVM_ENABLE_PDB)
install(FILES $<TARGET_PDB_FILE:${name}>
DESTINATION "${LLVM_UTILS_INSTALL_DIR}" COMPONENT ${name}
install(FILES $<TARGET_PDB_FILE:${name}>
DESTINATION "${LLVM_UTILS_INSTALL_DIR}" COMPONENT ${name}
OPTIONAL)
endif()

Expand Down Expand Up @@ -2192,6 +2198,9 @@ function(add_lit_testsuite target comment)
endfunction()

function(add_lit_testsuites project directory)
if(NOT LLVM_ENABLE_LIT_CONVENIENCE_TARGETS)
return()
endif()
if (NOT LLVM_ENABLE_IDE)
cmake_parse_arguments(ARG
"EXCLUDE_FROM_CHECK_ALL"
Expand Down
51 changes: 51 additions & 0 deletions llvm/cmake/modules/LLVMCacheSnapshot.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Example usage
# llvm_get_cache_vars(before)
# include(SomeModule)
# llvm_diff_cache_vars("${before}" new_vars new_pairs)

# message(STATUS "New cache variables: ${new_vars}")
# message(STATUS "New cache vars and values:\n${new_pairs}")

# get_list_of_existing_cache_variables(existing)
function(llvm_get_list_of_existing_cache_variables out_var)
get_cmake_property(_all CACHE_VARIABLES)
if(NOT _all)
set(_all "")
endif()
set(${out_var} "${_all}" PARENT_SCOPE)
endfunction()

# list_of_new_cache_variables_and_values(existing new_vars_and_values)
# - `existing` is the name of the var returned by the first helper
# - `new_vars_and_values` will be a list like: NAME=VALUE (TYPE=...);NAME2=VALUE2 (TYPE=...)
function(llvm_list_of_new_cache_variables_and_values existing_list_var out_var)
# Existing (pre-include) snapshot
set(_before "${${existing_list_var}}")

# Current (post-include) snapshot
get_cmake_property(_after CACHE_VARIABLES)

# Compute new names
set(_new "${_after}")
if(_before)
list(REMOVE_ITEM _new ${_before})
endif()

# Pack "NAME=VALUE (TYPE=...)" for each new cache entry
set(_pairs "")
foreach(_k IN LISTS _new)
if(NOT "${_k}" MATCHES "^((C|CXX)_SUPPORTS|HAVE_|GLIBCXX_USE|SUPPORTS_FVISI)")
Copy link

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern SUPPORTS_FVISI appears to be incomplete or a typo. This likely should be SUPPORTS_FVISIBILITY or similar. The incomplete pattern may not match intended cache variables.

Suggested change
if(NOT "${_k}" MATCHES "^((C|CXX)_SUPPORTS|HAVE_|GLIBCXX_USE|SUPPORTS_FVISI)")
if(NOT "${_k}" MATCHES "^((C|CXX)_SUPPORTS|HAVE_|GLIBCXX_USE|SUPPORTS_FVISIBILITY)")

Copilot uses AI. Check for mistakes.

continue()
endif()
# Cache VALUE: dereference is fine here because cache entries read like normal vars
set(_val "${${_k}}")
# Cache TYPE (e.g., STRING, BOOL, PATH, FILEPATH, INTERNAL, UNINITIALIZED)
get_property(_type CACHE "${_k}" PROPERTY TYPE)
if(NOT _type)
set(_type "UNINITIALIZED")
endif()
list(APPEND _pairs "set(${_k} \"${_val}\" CACHE ${_type} \"\")")
endforeach()

set(${out_var} "${_pairs}" PARENT_SCOPE)
endfunction()
24 changes: 16 additions & 8 deletions llvm/cmake/modules/LLVMProcessSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,25 @@ endfunction(find_all_header_files)
function(llvm_process_sources OUT_VAR)
cmake_parse_arguments(ARG "PARTIAL_SOURCES_INTENDED" "" "ADDITIONAL_HEADERS;ADDITIONAL_HEADER_DIRS" ${ARGN})
set(sources ${ARG_UNPARSED_ARGUMENTS})
llvm_check_source_file_list(${sources})

# This adds .td and .h files to the Visual Studio solution:
add_td_sources(sources)
find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")
if (hdrs)
set_source_files_properties(${hdrs} PROPERTIES HEADER_FILE_ONLY ON)
if(LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS)
llvm_check_source_file_list(${sources})
endif()

if(ARG_ADDITIONAL_HEADERS)
set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
list(APPEND sources ${ARG_ADDITIONAL_HEADERS})
endif()
set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
list(APPEND sources ${ARG_ADDITIONAL_HEADERS} ${hdrs})

# This adds .td and .h files to the Visual Studio solution:
Copy link

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition logic has changed from always executing to conditional execution based on MSVC OR LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS. This could break builds on non-MSVC platforms where headers are needed but LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS is OFF. Consider documenting this behavioral change or adding a comment explaining when this optimization is safe.

Suggested change
# This adds .td and .h files to the Visual Studio solution:
# This adds .td and .h files to the Visual Studio solution.
# NOTE: The following conditional logic only adds these files for MSVC or when
# LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS is enabled. On non-MSVC platforms, if
# LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS is OFF, these files will NOT be added.
# This optimization is intended to reduce solution clutter for non-MSVC builds,
# but may break IDE integration or developer workflows on platforms that expect
# these files to be present. If you encounter issues with missing headers or
# .td files in your IDE or build system, consider enabling
# LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS or revisiting this logic.

Copilot uses AI. Check for mistakes.

if(MSVC OR LLVM_ENABLE_EXPENSIVE_CMAKE_CHECKS)
add_td_sources(sources)
find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")
if (hdrs)
set_source_files_properties(${hdrs} PROPERTIES HEADER_FILE_ONLY ON)
endif()
list(APPEND sources ${hdrs})
endif()
set( ${OUT_VAR} ${sources} PARENT_SCOPE )
endfunction(llvm_process_sources)

Expand Down
39 changes: 32 additions & 7 deletions third-party/benchmark/cmake/AddCXXCompilerFlag.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,45 @@ function(mangle_compiler_flag FLAG OUTPUT)
set(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE)
endfunction(mangle_compiler_flag)

set(BENCHMARK_GCC_COMPATIBLE_FLAGS
-fno-exceptions
-fstrict-aliasing
-pedantic
-pedantic-errors
-Wall
-Werror
-Wextra
-Wfloat-equal
-Wno-deprecated
-Wno-deprecated-declarations
-Wold-style-cast
-Wshadow
-Wstrict-aliasing
-Wsuggest-override
)

macro(_benchmark_populate_cxx_compiler_flag)
if(ARGC GREATER 1)
set(VARIANT ${ARGV1})
string(TOUPPER "_${VARIANT}" VARIANT)
else()
set(VARIANT "")
endif()
set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
endmacro()

function(add_cxx_compiler_flag FLAG)
mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
if(LLVM_COMPILER_IS_GCC_COMPATIBLE AND "${FLAG}" IN_LIST BENCHMARK_GCC_COMPATIBLE_FLAGS)
_benchmark_populate_cxx_compiler_flag(${FLAG} ${VARIANT})
return()
endif()
set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
if(${MANGLED_FLAG})
if(ARGC GREATER 1)
set(VARIANT ${ARGV1})
string(TOUPPER "_${VARIANT}" VARIANT)
else()
set(VARIANT "")
endif()
set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
_benchmark_populate_cxx_compiler_flag(${FLAG} ${VARIANT})
endif()
endfunction()

Expand Down