Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vcpkg-cmake] Add check for unused cmake variables #18201

Closed
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ce4c584
[vcpkg-config-cmake] Add check for unused cmake variables
May 31, 2021
cc3b3f6
continue when the contents are not found
May 31, 2021
dbb3430
Update ports/vcpkg-cmake/vcpkg_cmake_configure.cmake
JackBoosY May 31, 2021
be7307e
Update scripts/cmake/vcpkg_configure_cmake.cmake
JackBoosY May 31, 2021
13f2fca
Improve logical
Jun 1, 2021
0e80fea
add option OPTIONS_CHECK_SKIP to vcpkg_cmake_configure and vcpkg_conf…
Jun 1, 2021
3c3ba18
Update docs
Jun 1, 2021
ca20f55
Improve the warning message output
Jun 1, 2021
bcfdc0f
update warning message.
Jun 3, 2021
2464a3e
Make Nicole's changes to vcpkg_cmake_configure
strega-nil Jun 3, 2021
209dffc
remove regexness
strega-nil Jun 3, 2021
2bb22fb
nicole CRs
strega-nil Jun 3, 2021
82472e8
Update docs
Jun 4, 2021
63616c6
Update ports/vcpkg-cmake/vcpkg.json
JackBoosY Jun 9, 2021
fdce668
Merge branch 'master' into dev/jack/check-cmake-ununsed-vars
JackBoosY Jun 9, 2021
40faf9e
Update versions/v-/vcpkg-cmake.json
JackBoosY Jun 15, 2021
02b8226
Update scripts/cmake/vcpkg_configure_cmake.cmake
JackBoosY Jun 22, 2021
9cf00d5
Nicole's CR
strega-nil-ms Jun 30, 2021
d2962ca
Update docs
Jul 1, 2021
f256a83
Update versions/v-/vcpkg-cmake.json
JackBoosY Jul 2, 2021
0b0cd52
Update versions/v-/vcpkg-cmake.json
JackBoosY Jul 2, 2021
66fcd57
Merge branch 'master' into dev/jack/check-cmake-ununsed-vars
JackBoosY Jul 2, 2021
2ab78a5
Update versions/v-/vcpkg-cmake.json
JackBoosY Jul 2, 2021
966d74c
update port version
Jul 5, 2021
5788f05
version stuff
Jul 5, 2021
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
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
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 @@ -362,8 +379,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 @@ -377,8 +397,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 @@ -395,7 +418,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 @@ -325,6 +341,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 @@ -334,6 +354,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 @@ -344,7 +367,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
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": "6404018e597cf6703e4cf3d46a3a4011d8c93efd",
"version-date": "2021-02-28",
"port-version": 5
},
{
"git-tree": "0e8bb94599a00fd9c61fd0ae524c22a067c21420",
"version-date": "2021-02-28",
Expand Down