cmake_host_system_information: add Windows registry query
Currently, accessing Windows registry from CMake is problematic because:
- It is not possible to control which registry view(s) is used: 32bit or 64bit
- Registry path syntax is problematic: usage of the semicolon character as separator, so no way to express a key including a semicolon
- Lacking of useful capabilities: no way to browse values or subkeys of a registry key
- types support is limited (for example
REG_MULTI_SZ
is not supported) - current capabilities are not documented
So, very often, it is required to directly call reg.exe
tool to interact with the registry which is painful because:
- it is not integrated to CMake
- outputs of this tool are complex to parse efficiently and with robustness
Some issues (#22775 (closed), #21240 (closed)) were already created to discuss these limitations.
The goal of this issue is to describe an efficient way to query the registry.
For that purpose, it is proposed to extend cmake_host_system_information
command:
cmake_host_system_information(RESULT <variable>
QUERY WINDOWS_REGISTRY <key> [VALUE <name>|VALUES|SUBKEYS]
[VIEW (32|64|32_64|64_32|HOST|TARGET)]
[ERROR_VARIABLE <result>])
This command will query the registry for the key <key>
- without any optional sub-options, query the default value of key
<key>
-
VALUE <name>
specified: query the value<name>
of key<key>
-
VALUES
specified: returns the list of values of key<key>
-
SUBKEYS
specified: returns the list of subkeys of key<key>
VIEW
optional argument specifies which registry view is used for the query:
-
32
: 32bit registry only -
64
: 64bit registry only. Raise an error on 32bit Windows (or returnNOTFOUND
?) -
32_64
: ready 32bit registry and, if not found, read 64bit registry. Same as32
onWindows
32bit -
64_32
: ready 64bit registry and, if not found, read 32bit registry. Same as32
onWindows
32bit - HOST: read 64bit registry on
Windows
64bit and 32bit registry onWindows
32bit - TARGET: rely on
CMAKE_SIZEOF_VOID_P
value to determine which registry to read. If not defined, fallback toHOST
behavior
If VIEW
is not specified, apply current CMake behavior used to read registry: if CMAKE_SIZEOF_VOID_P
equals 8
use 64_32
, else use 32_64
.
Note: current behavior is a bit awkward: When CMAKE_SIZEOF_VOID_P
is not defined (i.e. no language specified), use 32_64
even on Windows
64bit. Maybe more appropriate to use the following approach:
-
CMAKE_SIZEOF_VOID_P
==8
, use64_32
-
CMAKE_SIZEOF_VOID_P
==4
, use32_64
-
CMAKE_SIZEOF_VOID_P
not defined, use64_32
onWindows
64bit and32_64
onWindows
32bit
ERROR_VARIABLE
can be used to get some information about possible errors raised during registry access.