jvtrudel
jvtrudel

Reputation: 1345

How to load user-specific configuration for CMake project

I like to use a configuration file that sets several cached variables. The purpose is to reuse it for every projects running on a machine or to select different library versions for testing or special purpose.

I can achieve it with a CMake file like this one:

set(path_to_lib_one path/to/lib/one)
set(option1 dont_want_to_bother_setting_this_option)
set(option2 that_option_have_to_be_set_again)

And call include(myConfigfile).

But I would like to know if their is a cache-like way of doing it and what are the best practices to manage user/setup specific configurations.

Upvotes: 2

Views: 4365

Answers (1)

usr1234567
usr1234567

Reputation: 23422

Use the initial cache option offered by CMake. You store your options in the right format (set withCACHE`) and call

cmake -C <cacheFile> <pathToSourceDir>

Self-contained example

The CMakeLists.txt looks like

project(blabla)
cmake_minimum_required(VERSION 3.2)

message("${path_to_lib_one} / ${option1} / ${option2}")

and you want to pre-set the three variables. The cacheFile.txt looks like

set(path_to_lib_one path/to/lib/one CACHE FILEPATH "some path")
set(option1 "dont_want_to_bother_setting_this_option" CACHE STRING "some option 1")
set(option2 42 CACHE INT "and an integer")

and your CMake call (from a directory build below the source directory)

cmake -C cacheFile.txt ..

The output is

loading initial cache file ../cacheFile.txt
[..]
path/to/lib/one / dont_want_to_bother_setting_this_option / 42

Documentation: https://cmake.org/cmake/help/latest/manual/cmake.1.html#options

Load external cache files

Additionally, CMake offer a way to read in a cache file, that was created by another project. The command is load_cache. You can use it to just load the variables from the external cache or to copy them to the cache of the current project.

Documentation: https://cmake.org/cmake/help/latest/command/load_cache.html

Upvotes: 4

Related Questions