Last active
June 17, 2023 05:59
-
-
Save oliora/4961727299ed67337aba to your computer and use it in GitHub Desktop.
Forward added/changed variables to the function's parent scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# forward_variables usage example | |
include(forward_variables.cmake) | |
# After call to this function, all variables set by find_package will be forwarded to | |
# function caller scope (i.e. into parent scope). | |
function(find_boost_package) | |
start_track_variables() | |
set(bla_bla 1) | |
find_package(Boost 1.55 COMPONENTS system filesystem) | |
forward_changed_variables_to_parent_scope(bla_bla) | |
endfunction() | |
find_boost_package() | |
message(STATUS "${Boost_LIBRARIES}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start to track variables for change or adding. | |
# Note that variables starting with underscore are ignored. | |
macro(start_track_variables) | |
get_cmake_property(_fnvtps_cache_vars CACHE_VARIABLES) | |
get_cmake_property(_fnvtps_old_vars VARIABLES) | |
foreach(_i ${_fnvtps_old_vars}) | |
if (NOT "x${_i}" MATCHES "^x_.*$") | |
list(FIND _fnvtps_cache_vars ${_i} _fnvtps_is_in_cache) | |
if(${_fnvtps_is_in_cache} EQUAL -1) | |
set("_fnvtps_old${_i}" ${${_i}}) | |
#message(STATUS "_fnvtps_old${_i} = ${_fnvtps_old${_i}}") | |
endif() | |
endif() | |
endforeach() | |
endmacro() | |
# forward_changed_variables_to_parent_scope([exclusions]) | |
# Forwards variables that was added/changed since last call to start_track_variables() to the parent scope. | |
# Note that variables starting with underscore are ignored. | |
macro(forward_changed_variables_to_parent_scope) | |
get_cmake_property(_fnvtps_cache_vars CACHE_VARIABLES) | |
get_cmake_property(_fnvtps_vars VARIABLES) | |
set(_fnvtps_cache_vars ${_fnvtps_cache_vars} ${ARGN}) | |
foreach(_i ${_fnvtps_vars}) | |
if (NOT "x${_i}" MATCHES "^x_.*$") | |
list(FIND _fnvtps_cache_vars ${_i} _fnvtps_is_in_cache) | |
if (${_fnvtps_is_in_cache} EQUAL -1) | |
list(FIND _fnvtps_old_vars ${_i} _fnvtps_is_old) | |
if(${_fnvtps_is_old} EQUAL -1 OR NOT "${${_i}}" STREQUAL "${_fnvtps_old${_i}}") | |
set(${_i} ${${_i}} PARENT_SCOPE) | |
#message(STATUS "forwarded var ${_i}") | |
endif() | |
endif() | |
endif() | |
endforeach() | |
endmacro() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have an upgraded version of the forwarding up to new level with underscore, cache, ARGx variables support. Can you take a look?
https://github.com/andry81/tacklelib/tree/HEAD/cmake/tacklelib
Search in the
ForwardVariables.cmake
fortkl_track_vars_begin
/tkl_forward_changed_vars_to_parent_scope
/tkl_track_vars_end
functions.