Is there a way to temporarily alter a CMake variable while executing another CMakeLists.txt
file. Something like this?
PUSH ( SOME_VARIABLE )
SET ( SOME_VARIABLE "temporary value" )
ADD_SUBDIRECTORY( "subdir" )
POP ( SOME_VARIABLE )
I know you can do it like this:
SET ( SOME_VARIABLE_TMP "${SOME_VARIABLE}" )
SET ( SOME_VARIABLE "temporary value" )
ADD_SUBDIRECTORY( "subdir" )
SET ( SOME_VARIABLE "${SOME_VARIABLE_TMP} )
And I guess I could even make some hacky functions to do it myself, something like this (untested):
FUNCTION ( PUSH VARNAME )
SET ( ${VARNAME}_TMP "${${VARNAME}}" PARENT_SCOPE )
ENDFUNCTION ()
FUNCTION ( POP VARNAME )
SET ( ${VARNAME} "${${VARNAME}_TMP}" PARENT_SCOPE )
ENDFUNCTION ()
You could maybe even extend that so it works if you do push push pop pop. But I want to know is there a nice way to do it natively?
No, there isn't a nice way to do it natively.
You could just move the ADD_SUBDIRECTORY()
and all variable modifications into a FUNCTION()
itself. That would give you your own variable scope:
FUNCTION( ADD_MY_SUBDIR )
SET ( SOME_VARIABLE "temporary value" )
ADD_SUBDIRECTORY( "subdir" )
ENDFUNCTION ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With