cmake

How can I set CMAKE_MODULE_PATH for doing regular and out-of-source builds in CMake?


I have a line like this in my CMakeLists.txt

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "cmake")

This is so that some custom libraries can be found with a directory tree that looks like this:

CMakeLists.txt
cmake/
|-- FindSomeLibrary.cmake
|-- FindAnotherLibrary.cmake

Normally I build simply like this:

cmake .

Which works fine. However, I want to provide a debug and release build using a script like this:

mkdir release
cd release
cmake -DCMAKE_BUILD_TYPE=Release ../

However, now it cannot find the cmake modules.

Is there a way to set CMAKE_MODULE_PATH such that it works for both in-source and out-of-source builds?


Solution

  • As proposed in the comments:

    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
    

    and always try to set all paths relative to some CMAKE_* directory. That way you will miss many errors. ; )

    As CMAKE_MODULE_PATH is a list, so it's better to use:

    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")