diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index c450ee5a3d72e..d52bd84814aec 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -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) @@ -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 @@ -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 diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index 80e59a4df2433..f26dd7aa549c5 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -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 () @@ -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) @@ -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. @@ -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") @@ -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}. @@ -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}) @@ -1500,8 +1506,8 @@ macro(llvm_add_tool project name) RUNTIME DESTINATION ${${project}_TOOLS_INSTALL_DIR} COMPONENT ${name}) if (LLVM_ENABLE_PDB) - install(FILES $ - DESTINATION "${${project}_TOOLS_INSTALL_DIR}" COMPONENT ${name} + install(FILES $ + DESTINATION "${${project}_TOOLS_INSTALL_DIR}" COMPONENT ${name} OPTIONAL) endif() @@ -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 $ - DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}" COMPONENT ${name} + install(FILES $ + DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}" COMPONENT ${name} OPTIONAL) endif() endif() @@ -1574,8 +1580,8 @@ macro(add_llvm_utility name) RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR} COMPONENT ${name}) if (LLVM_ENABLE_PDB) - install(FILES $ - DESTINATION "${LLVM_UTILS_INSTALL_DIR}" COMPONENT ${name} + install(FILES $ + DESTINATION "${LLVM_UTILS_INSTALL_DIR}" COMPONENT ${name} OPTIONAL) endif() @@ -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" diff --git a/llvm/cmake/modules/LLVMCacheSnapshot.cmake b/llvm/cmake/modules/LLVMCacheSnapshot.cmake new file mode 100644 index 0000000000000..89d592a0a4165 --- /dev/null +++ b/llvm/cmake/modules/LLVMCacheSnapshot.cmake @@ -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)") + 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() diff --git a/llvm/cmake/modules/LLVMProcessSources.cmake b/llvm/cmake/modules/LLVMProcessSources.cmake index 0670d60bf2afd..0bcce4c6c78ad 100644 --- a/llvm/cmake/modules/LLVMProcessSources.cmake +++ b/llvm/cmake/modules/LLVMProcessSources.cmake @@ -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: + 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) diff --git a/third-party/benchmark/cmake/AddCXXCompilerFlag.cmake b/third-party/benchmark/cmake/AddCXXCompilerFlag.cmake index 858589e9775c6..1154311851f5d 100644 --- a/third-party/benchmark/cmake/AddCXXCompilerFlag.cmake +++ b/third-party/benchmark/cmake/AddCXXCompilerFlag.cmake @@ -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()