Skip to content

Commit

Permalink
[rollup:2021-07-16 1/7] PR microsoft#18201 (@JackBoosY)
Browse files Browse the repository at this point in the history
[vcpkg-cmake] Add check for unused cmake variables
  • Loading branch information
strega-nil committed Jul 19, 2021
1 parent 111220b commit 9da31a4
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/maintainers/ports/vcpkg-cmake/vcpkg_cmake_configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ vcpkg_cmake_configure(
<configure-setting>...]
[OPTIONS_DEBUG
<configure-setting>...]
[MAYBE_UNUSED_VARIABLES
<variable-name>...]
)
```

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions docs/maintainers/vcpkg_configure_cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <option-name>...]
)
```

Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion ports/vcpkg-cmake/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vcpkg-cmake",
"version-date": "2021-06-25",
"port-version": 4
"port-version": 5
}
66 changes: 62 additions & 4 deletions ports/vcpkg-cmake/vcpkg_cmake_configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ vcpkg_cmake_configure(
<configure-setting>...]
[OPTIONS_DEBUG
<configure-setting>...]
[MAYBE_UNUSED_VARIABLES
<variable-name>...]
)
```
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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})
Expand All @@ -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}")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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.")
Expand Down
64 changes: 62 additions & 2 deletions scripts/cmake/vcpkg_configure_cmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <option-name>...]
)
```
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions scripts/cmake/vcpkg_internal_get_cmake_vars.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion versions/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -6538,7 +6538,7 @@
},
"vcpkg-cmake": {
"baseline": "2021-06-25",
"port-version": 4
"port-version": 5
},
"vcpkg-cmake-config": {
"baseline": "2021-05-22",
Expand Down
5 changes: 5 additions & 0 deletions versions/v-/vcpkg-cmake.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"versions": [
{
"git-tree": "07c3e68ce9ae8f30bcc0b21def7a528dbb8ecb07",
"version-date": "2021-06-25",
"port-version": 5
},
{
"git-tree": "acc25ec22f8fd8887a865705580b1d6de041616d",
"version-date": "2021-06-25",
Expand Down

0 comments on commit 9da31a4

Please sign in to comment.