mirror of
https://github.com/Kitware/CMake.git
synced 2026-04-23 14:48:19 -05:00
de4f1f26b0
This property allows projects to modify environment variables at test
time rather than trying to guess what the state should be based on what
is present at configure time. Of particular interest is the ability to
use a `PATH` present at test time while adding entries known to be
necessary for the test itself.
There are multiple operations provided to modify variables, including:
- setting and unsetting
- appending and prepending as:
- strings
- path lists
- CMake lists
Additionally, a `reset` action is provided to cancel any prior
modifications to that particular variable in the case of incremental
additions to the test property.
56 lines
1.5 KiB
CMake
56 lines
1.5 KiB
CMake
execute_process(
|
|
COMMAND ${CMAKE_COMMAND} -E environment
|
|
OUTPUT_VARIABLE out
|
|
ERROR_VARIABLE err
|
|
RESULT_VARIABLE res)
|
|
|
|
if (res)
|
|
message(FATAL_ERROR "Failed with exit code ${res}: ${err}")
|
|
endif ()
|
|
|
|
if (CMAKE_HOST_WIN32)
|
|
set(path_sep ";")
|
|
else ()
|
|
set(path_sep ":")
|
|
endif ()
|
|
|
|
set(unexpect_UNSET_EXPLICIT "")
|
|
set(unexpect_UNSET_VIA_RESET "")
|
|
set(expect_DIRECT "new")
|
|
set(expect_STRING_MANIP "prefix-pre-core-post-suffix")
|
|
set(expect_PATH_MANIP "prefix${path_sep}pre${path_sep}core${path_sep}post${path_sep}suffix")
|
|
set(expect_CMAKE_LIST_MANIP "prefix;pre;core;post;suffix")
|
|
|
|
set(expected_vars
|
|
DIRECT
|
|
STRING_MANIP
|
|
PATH_MANIP
|
|
CMAKE_LIST_MANIP)
|
|
|
|
while (out)
|
|
string(FIND "${out}" "\n" nl_pos)
|
|
string(SUBSTRING "${out}" 0 "${nl_pos}" line)
|
|
math(EXPR line_next "${nl_pos} + 1")
|
|
string(SUBSTRING "${out}" "${line_next}" -1 out)
|
|
|
|
string(FIND "${line}" "=" eq_pos)
|
|
string(SUBSTRING "${line}" 0 "${eq_pos}" name)
|
|
math(EXPR value_start "${eq_pos} + 1")
|
|
string(SUBSTRING "${line}" "${value_start}" -1 value)
|
|
|
|
if (DEFINED "unexpect_${name}")
|
|
message(SEND_ERROR "Found `${name}=${value}` when it should have been unset")
|
|
elseif (DEFINED "expect_${name}")
|
|
list(REMOVE_ITEM expected_vars "${name}")
|
|
if (expect_${name} STREQUAL value)
|
|
message(STATUS "Found `${name}=${value}` as expected")
|
|
else ()
|
|
message(SEND_ERROR "Found `${name}=${value}` when it should have been ${expect_${name}}")
|
|
endif ()
|
|
endif ()
|
|
endwhile ()
|
|
|
|
if (expected_vars)
|
|
message(SEND_ERROR "Did not test expected variables: ${expected_vars}")
|
|
endif ()
|