Skip to content

Commit c5d1df3

Browse files
committed
Redesign safeguard_properties() to be a macro instead of a function
1 parent 43febe9 commit c5d1df3

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

cmake/rsp/helpers.cmake

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -90,45 +90,28 @@ if (NOT COMMAND "safeguard_properties")
9090

9191
#! safeguard_properties : Invoke a "risky" callback whilst "safeguarding" properties
9292
#
93-
# Function copies the values of the specified properties, invokes the callback, and
94-
# restores the properties' values.
93+
# Macro copies the values of the specified properties, invokes the callback, and
94+
# restores the properties' original values.
9595
#
96-
# Caution: This function does NOT prevent properties from being force-cached.
96+
# Caution: This macro does NOT prevent properties from being force-cached.
9797
# Environment variables are NOT prevented changed.
9898
#
99-
# Alternatively, consider using cmake's `block()`.
99+
# Alternatively, consider using cmake's `block()` or a function with its own variable
100+
# scope.
100101
#
101102
# @see https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#variables
102103
# @see https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#environment-variables
103104
# @see https://cmake.org/cmake/help/latest/command/block.html#block
104105
#
105-
# @param [CALLBACK <command>] Risky command or macro to be invoked.
106-
# @param [PROPERTIES <variable>...] One or more properties to safeguard.
106+
# @param <command> callback Risky command or macro to be invoked.
107+
# @param <list> properties List of variable names - variables to safeguard from
108+
# undesired value changes.
107109
#
108-
# @return
109-
# [PROPERTIES <variable>...] Restored properties
110-
#
111-
function(safeguard_properties)
112-
set(options "") # N/A
113-
set(oneValueArgs CALLBACK)
114-
set(multiValueArgs PROPERTIES)
115-
116-
cmake_parse_arguments(INPUT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
117-
requires_arguments("CALLBACK;PROPERTIES" INPUT)
118-
119-
# ---------------------------------------------------------------------------------------------- #
120-
121-
# Abort if callback not defined
122-
if (NOT COMMAND "${INPUT_CALLBACK}")
123-
message(FATAL_ERROR "Callback \"${INPUT_CALLBACK}()\" does not exist")
124-
endif ()
125-
126-
# ---------------------------------------------------------------------------------------------- #
127-
110+
macro(safeguard_properties callback properties)
128111
set(prefix "original_")
129112

130113
# Copy each provided property
131-
foreach (prop ${INPUT_PROPERTIES})
114+
foreach (prop ${properties})
132115
message(VERBOSE "Safeguarding: ${prop}, original value: ${${prop}}")
133116

134117
set("${prefix}${prop}" "${${prop}}")
@@ -137,17 +120,17 @@ if (NOT COMMAND "safeguard_properties")
137120
# ---------------------------------------------------------------------------------------------- #
138121

139122
# Invoke the risky callback
140-
message(VERBOSE "Invoking risky callback: ${INPUT_CALLBACK}")
141-
cmake_language(CALL "${INPUT_CALLBACK}")
123+
message(VERBOSE "Invoking risky callback: ${callback}")
124+
cmake_language(CALL "${callback}")
142125

143126
# ---------------------------------------------------------------------------------------------- #
144127

145128
# Restore each provided property
146-
foreach (prop ${INPUT_PROPERTIES})
129+
foreach (prop ${properties})
147130
message(VERBOSE "Restoring: ${prop} from: ${${prop}}, to original value: ${${prefix}${prop}}")
148131

149-
# Ensure that property is set on parent scope
150-
set("${prop}" "${${prefix}${prop}}" PARENT_SCOPE)
132+
# Restore property's original value
133+
set("${prop}" "${${prefix}${prop}}")
151134
endforeach ()
152-
endfunction()
135+
endmacro()
153136
endif ()

0 commit comments

Comments
 (0)