Edit: The accepted answer actually shows that it is pretty normally possible to set CMAKE_MODULE_PATH as any other CMake variable e.g. via the -DCMAKE_MODULE_PATH path
CLI parameter. It seems that in my case there is some included CMake script that calls set(CMAKE_MODULE_PATH /library_path)
, which erases all previous paths set to the variable. That's why I couldn't get the variable to do what I wanted it to do. I'll leave the question here in case anybody else faces this kind of situation.
I'm building a (3rd party) project that uses the Protobuf library (but this question is general). My system has a system-wide install of a newer version of Protobuf than the project is compatible with. So I've downloaded and compiled from source an older version of Protobuf.
The project uses CMake, and in its CMakeLists.txt, there is:
find_package(Protobuf REQUIRED)
Which, however, finds the (incompatible) system install. Of course, CMake doesn't know about my custom build of Protobuf. But how do I tell it?
I've created a FindProtobuf.cmake
file in, say, ~/usr/share/cmake-3.0/Modules/
and want the build process to use this one for finding Protobuf. But I haven't succeeded forcing CMake to pick up this one and not the system one. I think the reason is quite obvious from the CMake docs of find_package
:
The command has two modes by which it searches for packages: “Module” mode and “Config” mode. Module mode is available when the command is invoked with the above reduced signature. CMake searches for a file called Find<package>.cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. ... If no module is found and the MODULE option is not given the command proceeds to Config mode.
So until I succeed to change CMAKE_MODULE_PATH
, CMake will just pick up the FindProtobuf.cmake
installed to the default system path and won't ever proceed to the "Config" mode where I could probably make use of CMAKE_PREFIX_PATH
.
It's important for me to not edit the CMakeLists.txt
since it belongs to a 3rd party project I don't maintain.
What I've tried (all without success):
- calling
CMAKE_MODULE_PATH=~/usr/share/cmake-3.0/Modules cmake ...
(the env. variable is not "transferred" to the CMake variable with the same name) - calling
cmake -DCMAKE_MODULE_PATH=~/usr/share/cmake-3.0/Modules ...
(doesn't work, probably by design?) - calling
Protobuf_DIR=path/to/my/protobuf cmake ...
(the project doesn't support this kind of override for Protobuf)
It seems to me that, unfortunately, the only way to alter the CMAKE_MODULE_PATH
used by find_package
is to alter it from within CMakeLists.txt
, which is exactly what I want to avoid.
Do you have any ideas/workarounds on how not to touch the CMakeLists.txt
and still convince find_package
to find my custom Protobuf?
For reference, the CMake part of this project is on github .