Files
Daksh Mamodiya 7fdf315ba3 cmake: Populate help string from documentation table for -D entries
When a built-in cache variable is set via `-D <var>=<value>` with no
help text supplied, populate `HELPSTRING` from the built-in
documentation table (sourced from `Help/variable/*.rst` and the
per-language pattern manuals).  Project-defined and unknown variables
continue to receive the legacy "No help, variable specified on the
command line." sentinel.

The cache entry's type and value are left untouched: type stays
`UNINITIALIZED` for bare `-D` and an explicit `-D <var>:TYPE=<value>`
is honored as before.  The `cmCacheManager::LoadCache` path is not
modified, so reload of existing caches is a fixed point.

Fixes: #27830
2026-05-28 09:25:41 -04:00

50 lines
2.2 KiB
CMake

include(RunCMake)
# All cases below exercise ``cmake::ProcessCacheArg``: when a built-in
# cache variable is set via ``-D <var>=<value>`` with no ``:TYPE=`` and
# no help text, ``ProcessCacheArg`` consults the built-in documentation
# table (``cmCacheDocumentationTable`` and ``cmCachePatternTable``,
# seeded from ``Help/variable/*.rst`` and the per-language pattern
# manuals) and installs the table's Summary as the entry's HELPSTRING.
# The cache entry's type and value are left untouched.
function(run_define_case case)
set(RunCMake-check-file CheckCache.cmake)
run_cmake(${case})
endfunction()
# Bare ``-D <var>=<value>`` of a documented built-in: HELPSTRING is
# populated from the table's Summary; type stays UNINITIALIZED because
# the user did not pin one.
set(RunCMake_TEST_OPTIONS "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON")
run_define_case(DefineSetsTypeAndHelp)
unset(RunCMake_TEST_OPTIONS)
# ``-D <var>:STRING=<value>`` of a documented built-in: user's explicit
# type pin is honored; HELPSTRING is still populated from the table.
set(RunCMake_TEST_OPTIONS "-DCMAKE_COMPILE_WARNING_AS_ERROR:STRING=ON")
run_define_case(DefineTypePreserved)
unset(RunCMake_TEST_OPTIONS)
# Bare ``-D <var>=<value>`` of a project-specific name: no table match,
# so the entry stays ``:UNINITIALIZED=`` with the legacy sentinel
# HELPSTRING preserved for backwards compatibility.
set(RunCMake_TEST_OPTIONS "-DMY_CUSTOM_VAR_XYZZY=x")
run_define_case(DefineUnknownVarUninitialized)
unset(RunCMake_TEST_OPTIONS)
# A built-in variable name absent from the exact-match table but present
# in the pattern table (``CMAKE_<LANG>_CLANG_TIDY``): HELPSTRING comes
# from the pattern's Summary; type stays UNINITIALIZED.
set(RunCMake_TEST_OPTIONS "-DCMAKE_CXX_CLANG_TIDY=clang-tidy")
run_define_case(PatternCxxClangTidy)
unset(RunCMake_TEST_OPTIONS)
# A built-in variable name matching a pattern whose manual documents a
# per-language restriction CMake does not enforce at match time
# (cppcheck applies only to C/CXX, but here we set HIP). The matcher
# is intentionally permissive; the restriction text travels in the
# Summary prose.
set(RunCMake_TEST_OPTIONS "-DCMAKE_HIP_CPPCHECK=anything")
run_define_case(PatternHipCppcheck)
unset(RunCMake_TEST_OPTIONS)