Set a variable in current scope and PARENT_SCOPE?
Asked Answered
B

2

8

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})
Breccia answered 1/12, 2015 at 19:16 Comment(0)
B
6

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)
Blakeblakelee answered 15/6, 2016 at 14:43 Comment(1)
That's exactly what I said the goal was to avoid in the original question, right?Breccia
P
2

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.

Plafker answered 1/12, 2015 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.