Reputation: 73255
I have written a CMake module that contains a couple of useful macros that I would like to use across a number of other CMake projects. However, I'm not sure where to put the module.
I would like to be able to do this inside each project that uses the macro:
include(MyModule)
However, I'm not sure if there is an easy and cross-platform way of achieving this. In fact, I can't even get it to work on Unix. I put the module (MyModule.cmake
) in the following locations:
/usr/lib/cmake/
/usr/lib/cmake/Modules
/usr/local/lib/cmake
/usr/local/lib/cmake/Modules
...and the project with the include()
was unable to load the module.
What is the correct location for this module? Is there a better approach?
I should also point out that the macros are not related to "finding" a third-party library and therefore have nothing to do with find_package()
.
Upvotes: 4
Views: 185
Reputation: 51055
Put the module in a directory of your choice, and then add that directory to CMAKE_MODULE_PATH
using list(APPEND)
.
You can even host that module somewhere and then download it via file(DOWNLOAD)
. If you download it to the same directory as the current CMake script being processed, you just include(MyModule.cmake)
and don't need to modify CMAKE_MODULE_PATH
.
You could download the file to a common location on disk and then add a check using if(EXISTS "${module_location_on_disk}")
to skip the download if it's already downloaded. Of course, more logic will be required if your module changes, or you want to have a common location and multiple versions of the module, but that's out of those scope of your question.
Upvotes: 0