Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 894
Kristopher Ives Avatar asked Oct 09 '18 22:10

Kristopher Ives


People also ask

How does find_ package work CMake?

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. It is responsible for finding the package, checking the version, and producing any needed messages.

Where does Find_package search CMake?

CMake ships with its own set of built-in find_package scripts, and their location is in the default CMAKE_MODULE_PATH. The more normal use case for dependent projects that have been CMakeified would be to use CMake's external_project command and then include the Use[Project].

What is CMAKE_MODULE_PATH?

Semicolon-separated list of directories specifying a search path for CMake modules to be loaded by the include() or find_package() commands before checking the default modules that come with CMake. By default it is empty, it is intended to be set by the project.

Where are CMake modules stored?

Using Modules For CMake, these sections are called cmake-modules and can be found in the Modules subdirectory of your installation.

Where does CMake look for modules?

" CMake will look for modules in the directories specified by CMAKE_MODULE_PATH; if it cannot find it there, it will look in the Modules subdirectory. " The book doesn't do too a good job of explaining where this directory is though.

How to work with CMake_module_path?

The proper way to work with CMAKE_MODULE_PATH is to only ever use APPEND / INSERT on it: list (APPEND CMAKE_MODULE_PATH $ {CMAKE_CURRENT_LIST_DIR} /cmake) It is improper to overwrite it with a plain set () or a set (CACHE).

How do I add additional configuration to a CMake file?

To add an additional configuration, right click CMakeSettings.json and choose Add Configuration. You can also edit the file using the CMake Settings Editor. Right-click on CMakeSettings.json in Solution Explorer and choose Edit CMake Settings. Or, select Manage Configurations from the configuration drop-down at the top of the editor window.

How do I create a custom environment for CMake?

To create a custom environment, choose the Edit JSON link in the upper right corner of the Settings editor, and edit the CMakeSettings.json file directly. Path to the CMake toolchain file. This path is passed to CMake as "-DCMAKE_TOOLCHAIN_FILE = <filepath>".


1 Answers

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")
like image 141
KamilCuk Avatar answered Sep 28 '22 04:09

KamilCuk