Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a variable in current scope and PARENT_SCOPE?

Tags:

cmake

If I do:

set(SourceDir ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
message("SourceDir: " ${SourceDir})

The message says that SourceDir is empty. Presumably it is being set in the parent scope only. Is there a way to set the variable in the current scope AND the parent scope? So that I don't have to do:

set(SourceDir ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
set(SourceDir ${CMAKE_CURRENT_SOURCE_DIR})
message("SourceDir: " ${SourceDir})
like image 221
David Doria Avatar asked Dec 01 '15 19:12

David Doria


People also ask

How do you set a variable value in cmake?

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.

How do you set a variable in Cmakelists?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .

What is set in Cmakelist?

Set a normal, cache, or environment variable to a given value. See the cmake-language(7) variables documentation for the scopes and interaction of normal variables and cache entries. Signatures of this command that specify a <value>... placeholder expect zero or more arguments.

What is the scope of a variable that is defined in the main body of a script?

Variables defined in the script are only available to the script scope and not the global or parent scope.


2 Answers

When you set it for PARENT_SCOPE, it doesn't do it for current scope somehow. But a two step process works. The first line sets it locally, the second line exports it to the parent scope.

set (LIB_VER 6)
set (LIB_VER ${LIB_VER} PARENT_SCOPE)
like image 175
Sharath Avatar answered Oct 15 '22 02:10

Sharath


I think you can not. The documentation says:

Each new directory or function creates a new scope.

If I understand it correctly when your SET gets executed, its scope is already created by copying the parent scope. So no matter what you do to the original (PARENT_SCOPE) your local scope won't change.

You'd better ask the question on CMake's user list to verify that they don't do fallback to parent when a variable is not defined in the local scope. If they do however, this is a bug.

like image 1
Doncho Gunchev Avatar answered Oct 15 '22 03:10

Doncho Gunchev