-
Notifications
You must be signed in to change notification settings - Fork 211
Re-export CMAKE_MODULE_PATH
#532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey thanks for the PR, I wasn't aware of this mechanism in packages! I don't see how this can break anything, so in general the implementation seems fine. Some ideas for improvement:
- Can you add a comment in
cpm_add_subdirectoryexplaining why the modules are exported to the parent scope there? - Would it be possible to move the parent scope export of the module path to the
cpm_export_variablesmacro? That way we don't need to remember to call it after eachcpm_add_subdirectory - To avoid regressions, do you think there's an easy way we could add a simple integration test for this?
Hi @TheLartians and @threeal
I too have some issues with my package, in regards to appending a module path to CMAKE_MODULE_PATH.
Basically, whenever I install the package using cmake's native FetchContent_Declare, it appears to work (which might just be luck - only works if I use FetchContent from my consuming project's root level CMakeLists.txt file.). This is most likely because FetchContent_MakeAvailable() appears to include my package's CMakeLists.txt via add_subdirectory().
include(FetchContent) FetchContent_Declare( "rsp-cmake-scripts" GIT_REPOSITORY "https://github.com/rsps/cmake-scripts" GIT_TAG "43febe92df4a06a31338e380eb2dc64683f8a273" # Commit hash for now, version not yet released. ) FetchContent_MakeAvailable("rsp-cmake-scripts") include("rsp/debug") # Works dump(CMAKE_MODULE_PATH) # Works
But, when trying to fetch my package using CPM, any changes to CMAKE_MODULE_PATH are not propagated out of the CPMAddPackage() function. E.g.:
CPMAddPackage( NAME "rsp-cmake-scripts" GIT_TAG "43febe92df4a06a31338e380eb2dc64683f8a273" # Commit hash for now, version not yet released. # VERSION "${RSP_CMAKE_SCRIPTS_VERSION}" GITHUB_REPOSITORY "rsps/cmake-scripts" OPTIONS "RSP_ENABLE_ANSI true" ) include("rsp/debug") # CMake Error: include could not find requested file: rsp/debug dump(CMAKE_MODULE_PATH) # ...not reached
Experiment A (failed)
I have locally attempted to set the CMAKE_MODULE_PATH in the parent scope, by modifying the cpm_export_variables() macro:
macro(cpm_export_variables name) # ... previous not shown ... # Attempt to propagate eventual appended CMAKE_MODULE_PATH paths to parent set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE) endmacro()
Sadly, the above change had no effect. My package's module path isn't available.
Experiment B
Same as "Experiment A", but with an additional modification to cpm_fetch_package():
function(cpm_fetch_package PACKAGE DOWNLOAD_ONLY populated) # ... previous not shown ... set(${PACKAGE}_SOURCE_DIR ${${lower_case_name}_SOURCE_DIR} PARENT_SCOPE ) set(${PACKAGE}_BINARY_DIR ${${lower_case_name}_BINARY_DIR} PARENT_SCOPE ) # Attempt to propagate eventual appended CMAKE_MODULE_PATH paths to parent - allows # cpm_export_variables() to propagate change upwards... set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE) endfunction()
This seems to work in my case, yet I'm not sure if other CPM functions need to be modified?
For now, I do not see any viable work-around on how to make this work as desired. It is not uncommon for packages, libraries, ...etc, to append paths to CMAKE_MODULE_PATH, such that consuming projects are able to make use of cmake utilities, custom paths, or resources that are somehow made available.
In any case, I really hope that CPM can make this work.
aedart
commented
Feb 10, 2025
In addition to the previous comment, the problem is actually not just for appended paths to CMAKE_MODULE_PATH, but basically every kind of variable that a "package" attempts to expose in a parent scope!
I'm not sure what a correct solution would be (I'm not a CMake expert... I'm still struggling to understand the basics).
This pull request re-export the
CMAKE_MODULE_PATHvariable to the parent scope from theadd_subdirectoryfunction. This function is useful because some packages, like Catch2, export theCMAKE_MODULE_PATHof their script files to the parent scope, as could be seen here:image
While this implementation may be somewhat unconventional and a bit hacky, I don't know of any better ways to solve this issue. It allows script files to be directly used instead of having to delve deep into the project source files. Previously, if we wanted to use the
catch_discover_testsfunction with Catch2, we had to do the following: