I'm striving to understand how to create / modify a global variable (whose prefix is not <PACKAGE>_) from within a Find<package>.cmake file, so that it can be shared among different modules and reused by the global CMakeLists.txt.
Let's say I have a FindMyModule.cmake file:
set (MyModule_include_dirs "...") # <-- This is visible from CMakeLists.txt caller
set (LIBS_INCLUDE_DIRS "${LIBS_INCLUDE_DIRS} ${MyModule_include_dirs}") <-- This is not
In CMakeLists.txt
find_package(MyModule)
message("My Module include dirs: "${MyModule_include_dirs}) # <-- Prints "My Module include dirs: ..."
message("Libs include dirs: " ${LIBS_INCLUDE_DIRS}) # <-- Prints "Libs include dirs: "
Googling and so-ing around, I've tried CACHE, INTERNAL and PARENT_SCOPE but no successful result so far.
I really don't understand why would you want to change a global variable from within a
findXYZ.cmakescript, as you are tightly coupling other scripts to this script or visa versa.If I recall correctly, as long as you set a variable as
CACHE INTERNALbefore you start processing anything it should be available at all time to the following CMake instructions. We could've fixed that easily if you'd provide a minimal reproducible example.The other options that you have for "GLOBAL variables" is to use
set_property()i.e.:And then everywhere else:
To demonstrate that BOTH WORK:
CMakeLists.txt
findMODULE_VARIABLE.cmake
Both variables output the same: