Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push / pop a cmake variable

Tags:

cmake

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?

like image 657
Timmmm Avatar asked Jan 17 '17 15:01

Timmmm


1 Answers

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 ()
like image 175
Florian Avatar answered Oct 13 '22 19:10

Florian