From 9da31a4d7a7195d8dcefd19eca8ed70198fd4bbf Mon Sep 17 00:00:00 2001 From: nicole mazzuca Date: Sun, 18 Jul 2021 20:34:10 -0700 Subject: [PATCH] [rollup:2021-07-16 1/7] PR #18201 (@JackBoosY) [vcpkg-cmake] Add check for unused cmake variables --- .../vcpkg-cmake/vcpkg_cmake_configure.md | 7 ++ docs/maintainers/vcpkg_configure_cmake.md | 4 ++ ports/vcpkg-cmake/vcpkg.json | 2 +- ports/vcpkg-cmake/vcpkg_cmake_configure.cmake | 66 +++++++++++++++++-- scripts/cmake/vcpkg_configure_cmake.cmake | 64 +++++++++++++++++- .../cmake/vcpkg_internal_get_cmake_vars.cmake | 1 + versions/baseline.json | 2 +- versions/v-/vcpkg-cmake.json | 5 ++ 8 files changed, 143 insertions(+), 8 deletions(-) diff --git a/docs/maintainers/ports/vcpkg-cmake/vcpkg_cmake_configure.md b/docs/maintainers/ports/vcpkg-cmake/vcpkg_cmake_configure.md index 95830313df41ba..3745df08058fec 100644 --- a/docs/maintainers/ports/vcpkg-cmake/vcpkg_cmake_configure.md +++ b/docs/maintainers/ports/vcpkg-cmake/vcpkg_cmake_configure.md @@ -18,6 +18,8 @@ vcpkg_cmake_configure( ...] [OPTIONS_DEBUG ...] + [MAYBE_UNUSED_VARIABLES + ...] ) ``` @@ -56,6 +58,11 @@ By default, this function adds flags to `CMAKE_C_FLAGS` and `CMAKE_CXX_FLAGS` which set the default character set to utf-8 for MSVC. If the library sets its own code page, pass the `NO_CHARSET_FLAG` option. +This function makes certain that all options passed in are used by the +underlying CMake build system. If there are options that might be unused, +perhaps on certain platforms, pass those variable names to +`MAYBE_UNUSED_VARIABLES`. + `LOGFILE_BASE` is used to set the base of the logfile names; by default, this is `config`, and thus the logfiles end up being something like `config-x86-windows-dbg.log`. You can set it to anything you like; diff --git a/docs/maintainers/vcpkg_configure_cmake.md b/docs/maintainers/vcpkg_configure_cmake.md index 195017b3c93e80..fd6ce13187bb7a 100644 --- a/docs/maintainers/vcpkg_configure_cmake.md +++ b/docs/maintainers/vcpkg_configure_cmake.md @@ -17,6 +17,7 @@ vcpkg_configure_cmake( [OPTIONS <-DUSE_THIS_IN_ALL_BUILDS=1>...] [OPTIONS_RELEASE <-DOPTIMIZE=1>...] [OPTIONS_DEBUG <-DDEBUGGABLE=1>...] + [MAYBE_UNUSED_VARIABLES ...] ) ``` @@ -55,6 +56,9 @@ Additional options passed to CMake during the Release configuration. These are i ### OPTIONS_DEBUG Additional options passed to CMake during the Debug configuration. These are in addition to `OPTIONS`. +### MAYBE_UNUSED_VARIABLES +Any CMake variables which are explicitly passed in, but which may not be used on all platforms. + ### LOGNAME Name of the log to write the output of the configure call to. diff --git a/ports/vcpkg-cmake/vcpkg.json b/ports/vcpkg-cmake/vcpkg.json index ffda714e3fe066..88ee459ff35111 100644 --- a/ports/vcpkg-cmake/vcpkg.json +++ b/ports/vcpkg-cmake/vcpkg.json @@ -1,5 +1,5 @@ { "name": "vcpkg-cmake", "version-date": "2021-06-25", - "port-version": 4 + "port-version": 5 } diff --git a/ports/vcpkg-cmake/vcpkg_cmake_configure.cmake b/ports/vcpkg-cmake/vcpkg_cmake_configure.cmake index beffd32b0519ba..aa99a81ce282d1 100644 --- a/ports/vcpkg-cmake/vcpkg_cmake_configure.cmake +++ b/ports/vcpkg-cmake/vcpkg_cmake_configure.cmake @@ -17,6 +17,8 @@ vcpkg_cmake_configure( ...] [OPTIONS_DEBUG ...] + [MAYBE_UNUSED_VARIABLES + ...] ) ``` @@ -55,6 +57,11 @@ By default, this function adds flags to `CMAKE_C_FLAGS` and `CMAKE_CXX_FLAGS` which set the default character set to utf-8 for MSVC. If the library sets its own code page, pass the `NO_CHARSET_FLAG` option. +This function makes certain that all options passed in are used by the +underlying CMake build system. If there are options that might be unused, +perhaps on certain platforms, pass those variable names to +`MAYBE_UNUSED_VARIABLES`. + `LOGFILE_BASE` is used to set the base of the logfile names; by default, this is `config`, and thus the logfiles end up being something like `config-x86-windows-dbg.log`. You can set it to anything you like; @@ -88,7 +95,7 @@ function(vcpkg_cmake_configure) cmake_parse_arguments(PARSE_ARGV 0 "arg" "PREFER_NINJA;DISABLE_PARALLEL_CONFIGURE;WINDOWS_USE_MSBUILD;NO_CHARSET_FLAG" "SOURCE_PATH;GENERATOR;LOGFILE_BASE" - "OPTIONS;OPTIONS_DEBUG;OPTIONS_RELEASE" + "OPTIONS;OPTIONS_DEBUG;OPTIONS_RELEASE;MAYBE_UNUSED_VARIABLES" ) if(DEFINED CACHE{Z_VCPKG_CMAKE_GENERATOR}) @@ -102,9 +109,19 @@ function(vcpkg_cmake_configure) message(FATAL_ERROR "SOURCE_PATH must be set") endif() if(NOT DEFINED arg_LOGFILE_BASE) - set(arg_LOGFILE_BASE "config") + set(arg_LOGFILE_BASE "config-${TARGET_TRIPLET}") endif() + set(manually_specified_variables "") + foreach(option IN LISTS arg_OPTIONS arg_OPTIONS_RELEASE arg_OPTIONS_DEBUG) + if(option MATCHES "^-D([^:=]*)[:=]") + list(APPEND manually_specified_variables "${CMAKE_MATCH_1}") + endif() + endforeach() + list(REMOVE_DUPLICATES manually_specified_variables) + list(REMOVE_ITEM manually_specified_variables ${arg_MAYBE_UNUSED_VARIABLES}) + debug_message("manually specified variables: ${manually_specified_variables}") + if(CMAKE_HOST_WIN32) if(DEFINED ENV{PROCESSOR_ARCHITEW6432}) set(host_architecture "$ENV{PROCESSOR_ARCHITEW6432}") @@ -364,8 +381,11 @@ function(vcpkg_cmake_configure) vcpkg_execute_required_process( COMMAND ninja -v WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel/vcpkg-parallel-configure" - LOGNAME "${arg_LOGFILE_BASE}-${TARGET_TRIPLET}" + LOGNAME "${arg_LOGFILE_BASE}" ) + list(APPEND config_logs + "${CURRENT_BUILDTREES_DIR}/${arg_LOGFILE_BASE}-out.log" + "${CURRENT_BUILDTREES_DIR}/${arg_LOGFILE_BASE}-err.log") else() if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug") message(STATUS "Configuring ${TARGET_TRIPLET}-dbg") @@ -379,8 +399,11 @@ function(vcpkg_cmake_configure) "-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_INSTALL_PREFIX=${CURRENT_PACKAGES_DIR}/debug" WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg" - LOGNAME "${arg_LOGFILE_BASE}-${TARGET_TRIPLET}-dbg" + LOGNAME "${arg_LOGFILE_BASE}-dbg" ) + list(APPEND config_logs + "${CURRENT_BUILDTREES_DIR}/${arg_LOGFILE_BASE}-dbg-out.log" + "${CURRENT_BUILDTREES_DIR}/${arg_LOGFILE_BASE}-dbg-err.log") endif() if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release") @@ -397,7 +420,42 @@ function(vcpkg_cmake_configure) WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel" LOGNAME "${arg_LOGFILE_BASE}-rel" ) + list(APPEND config_logs + "${CURRENT_BUILDTREES_DIR}/${arg_LOGFILE_BASE}-rel-out.log" + "${CURRENT_BUILDTREES_DIR}/${arg_LOGFILE_BASE}-rel-err.log") + endif() + endif() + + set(all_unused_variables) + foreach(config_log IN LISTS config_logs) + if(NOT EXISTS "${config_log}") + continue() endif() + file(READ "${config_log}" log_contents) + debug_message("Reading configure log ${config_log}...") + if(NOT log_contents MATCHES "Manually-specified variables were not used by the project:\n\n(( [^\n]*\n)*)") + continue() + endif() + string(STRIP "${CMAKE_MATCH_1}" unused_variables) # remove leading ` ` and trailing `\n` + string(REPLACE "\n " ";" unused_variables "${unused_variables}") + debug_message("unused variables: ${unused_variables}") + foreach(unused_variable IN LISTS unused_variables) + if(unused_variable IN_LIST manually_specified_variables) + debug_message("manually specified unused variable: ${unused_variable}") + list(APPEND all_unused_variables "${unused_variable}") + else() + debug_message("unused variable (not manually specified): ${unused_variable}") + endif() + endforeach() + endforeach() + + if(DEFINED all_unused_variables) + list(REMOVE_DUPLICATES all_unused_variables) + list(JOIN all_unused_variables "\n " all_unused_variables) + message(WARNING "The following variables are not used in CMakeLists.txt: + ${all_unused_variables} +Please recheck them and remove the unnecessary options from the `vcpkg_cmake_configure` call. +If these options should still be passed for whatever reason, please use the `MAYBE_UNUSED_VARIABLES` argument.") endif() set(Z_VCPKG_CMAKE_GENERATOR "${generator}" CACHE INTERNAL "The generator which was used to configure CMake.") diff --git a/scripts/cmake/vcpkg_configure_cmake.cmake b/scripts/cmake/vcpkg_configure_cmake.cmake index 1eb50e852b9699..bce3f6af583629 100644 --- a/scripts/cmake/vcpkg_configure_cmake.cmake +++ b/scripts/cmake/vcpkg_configure_cmake.cmake @@ -15,6 +15,7 @@ vcpkg_configure_cmake( [OPTIONS <-DUSE_THIS_IN_ALL_BUILDS=1>...] [OPTIONS_RELEASE <-DOPTIMIZE=1>...] [OPTIONS_DEBUG <-DDEBUGGABLE=1>...] + [MAYBE_UNUSED_VARIABLES ...] ) ``` @@ -53,6 +54,9 @@ Additional options passed to CMake during the Release configuration. These are i ### OPTIONS_DEBUG Additional options passed to CMake during the Debug configuration. These are in addition to `OPTIONS`. +### MAYBE_UNUSED_VARIABLES +Any CMake variables which are explicitly passed in, but which may not be used on all platforms. + ### LOGNAME Name of the log to write the output of the configure call to. @@ -73,9 +77,9 @@ function(vcpkg_configure_cmake) endif() cmake_parse_arguments(PARSE_ARGV 0 arg - "PREFER_NINJA;DISABLE_PARALLEL_CONFIGURE;NO_CHARSET_FLAG" + "PREFER_NINJA;DISABLE_PARALLEL_CONFIGURE;NO_CHARSET_FLAG;Z_VCPKG_IGNORE_UNUSED_VARIABLES" "SOURCE_PATH;GENERATOR;LOGNAME" - "OPTIONS;OPTIONS_DEBUG;OPTIONS_RELEASE" + "OPTIONS;OPTIONS_DEBUG;OPTIONS_RELEASE;MAYBE_UNUSED_VARIABLES" ) if(NOT VCPKG_PLATFORM_TOOLSET) @@ -87,6 +91,18 @@ function(vcpkg_configure_cmake) set(arg_LOGNAME config-${TARGET_TRIPLET}) endif() + set(manually_specified_variables "") + if(NOT arg_Z_VCPKG_IGNORE_UNUSED_VARIABLES) + foreach(option IN LISTS arg_OPTIONS arg_OPTIONS_RELEASE arg_OPTIONS_DEBUG) + if(option MATCHES "^-D([^:=]*)[:=]") + list(APPEND manually_specified_variables "${CMAKE_MATCH_1}") + endif() + endforeach() + list(REMOVE_DUPLICATES manually_specified_variables) + list(REMOVE_ITEM manually_specified_variables ${arg_MAYBE_UNUSED_VARIABLES}) + debug_message("manually specified variables: ${manually_specified_variables}") + endif() + if(CMAKE_HOST_WIN32) if(DEFINED ENV{PROCESSOR_ARCHITEW6432}) set(arg_HOST_ARCHITECTURE $ENV{PROCESSOR_ARCHITEW6432}) @@ -326,6 +342,10 @@ function(vcpkg_configure_cmake) WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel/vcpkg-parallel-configure LOGNAME ${arg_LOGNAME} ) + + list(APPEND config_logs + "${CURRENT_BUILDTREES_DIR}/${arg_LOGNAME}-out.log" + "${CURRENT_BUILDTREES_DIR}/${arg_LOGNAME}-err.log") else() if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug") message(STATUS "Configuring ${TARGET_TRIPLET}-dbg") @@ -335,6 +355,9 @@ function(vcpkg_configure_cmake) WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg LOGNAME ${arg_LOGNAME}-dbg ) + list(APPEND config_logs + "${CURRENT_BUILDTREES_DIR}/${arg_LOGNAME}-dbg-out.log" + "${CURRENT_BUILDTREES_DIR}/${arg_LOGNAME}-dbg-err.log") endif() if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release") @@ -345,7 +368,44 @@ function(vcpkg_configure_cmake) WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel LOGNAME ${arg_LOGNAME}-rel ) + list(APPEND config_logs + "${CURRENT_BUILDTREES_DIR}/${arg_LOGNAME}-rel-out.log" + "${CURRENT_BUILDTREES_DIR}/${arg_LOGNAME}-rel-err.log") + endif() + endif() + + # Check unused variables + set(all_unused_variables) + foreach(config_log IN LISTS config_logs) + if (NOT EXISTS "${config_log}") + continue() + endif() + file(READ "${config_log}" log_contents) + debug_message("Reading configure log ${config_log}...") + if(NOT log_contents MATCHES "Manually-specified variables were not used by the project:\n\n(( [^\n]*\n)*)") + continue() endif() + string(STRIP "${CMAKE_MATCH_1}" unused_variables) # remove leading ` ` and trailing `\n` + string(REPLACE "\n " ";" unused_variables "${unused_variables}") + debug_message("unused variables: ${unused_variables}") + + foreach(unused_variable IN LISTS unused_variables) + if(unused_variable IN_LIST manually_specified_variables) + debug_message("manually specified unused variable: ${unused_variable}") + list(APPEND all_unused_variables "${unused_variable}") + else() + debug_message("unused variable (not manually specified): ${unused_variable}") + endif() + endforeach() + endforeach() + + if(DEFINED all_unused_variables) + list(REMOVE_DUPLICATES all_unused_variables) + list(JOIN all_unused_variables "\n " all_unused_variables) + message(WARNING "The following variables are not used in CMakeLists.txt: + ${all_unused_variables} +Please recheck them and remove the unnecessary options from the `vcpkg_configure_cmake` call. +If these options should still be passed for whatever reason, please use the `MAYBE_UNUSED_VARIABLES` argument.") endif() set(Z_VCPKG_CMAKE_GENERATOR "${GENERATOR}" PARENT_SCOPE) diff --git a/scripts/cmake/vcpkg_internal_get_cmake_vars.cmake b/scripts/cmake/vcpkg_internal_get_cmake_vars.cmake index 6c705ae8f8f429..030d74120d88f5 100644 --- a/scripts/cmake/vcpkg_internal_get_cmake_vars.cmake +++ b/scripts/cmake/vcpkg_internal_get_cmake_vars.cmake @@ -53,6 +53,7 @@ function(vcpkg_internal_get_cmake_vars) OPTIONS_RELEASE "-DVCPKG_OUTPUT_FILE:PATH=${CURRENT_BUILDTREES_DIR}/cmake-vars-${TARGET_TRIPLET}-rel.cmake.log" PREFER_NINJA LOGNAME get-cmake-vars-${TARGET_TRIPLET} + Z_VCPKG_IGNORE_UNUSED_VARIABLES ) set(_include_string) diff --git a/versions/baseline.json b/versions/baseline.json index 88a4bcd85a9ef8..591333a43bc32e 100644 --- a/versions/baseline.json +++ b/versions/baseline.json @@ -6538,7 +6538,7 @@ }, "vcpkg-cmake": { "baseline": "2021-06-25", - "port-version": 4 + "port-version": 5 }, "vcpkg-cmake-config": { "baseline": "2021-05-22", diff --git a/versions/v-/vcpkg-cmake.json b/versions/v-/vcpkg-cmake.json index 96d5eb75da9fca..c632ab855e8929 100644 --- a/versions/v-/vcpkg-cmake.json +++ b/versions/v-/vcpkg-cmake.json @@ -1,5 +1,10 @@ { "versions": [ + { + "git-tree": "07c3e68ce9ae8f30bcc0b21def7a528dbb8ecb07", + "version-date": "2021-06-25", + "port-version": 5 + }, { "git-tree": "acc25ec22f8fd8887a865705580b1d6de041616d", "version-date": "2021-06-25",