[CMake] Set directory path in release mode

Michael Wild themiwi at gmail.com
Sat Jan 16 11:34:30 EST 2010


On 16. Jan, 2010, at 17:02 , Arturo Caissut wrote:

> Hi all,
> I have a little problem with a C++ project I'm writing using CMake features.
> I need to define two constants in one of the header files of my
> projects, I found a solution for this reading "Mastering CMake". As they
> suggest I wrote a config.h.in file with pre-compiler instructions:
> 
> |#define ATLAS_TXT_PATH "@ATLAS_TXT_PATH@"
> #define ATLAS_HDR_PATH "@ATLAS_HDR_PATH@"
> |
> and I added to my CMakeLists.txt file the following lines:
> 
> |set (ATLAS_TXT_PATH "${PROJECT_BINARY_DIR}/ProgramData/Atlas/aal.txt")
> set (ATLAS_HDR_PATH "${PROJECT_BINARY_DIR}/ProgramData/Atlas/aal.hdr")
> 
> configure||_file (
> "${PROJECT_SOURCE_DIR}/path_to_config_file/Config.h.in"
> "${PROJECT_BINARY_DIR}/Config.h")
> 
> |
> This perfectly works as long as I'm using a Debug local version of the
> program I'm working at, but how can I provide the path constants to
> change in order to work as well when I build a Release version of the
> program, I package it and I install it to another computer?
> Package CMake code is working fine, Install id good written as well, but
> I need a way to define path constants to be useful when I'm installing
> the program.
> 
> Could you suggest me an elegant way to do this in CMake? Maybe I should
> add an IF/THEN switch to distinguish Debug building path values and
> Release ones?
> 
> 
> Bests,
> Arturo

Usually one configures such config.h files for the installation paths, not the build directory. For my programs I do it like this to find some file:

- check whether the environment variable ATLAS_CONFIG_DIR is set and the file $ATLAS_CONFIG_DIR/aal.txt exists
- check whether the file $HOME/.config/Atlas/aal.txt exists (or similar)
- check whether the configured path (e.g. ${CMAKE_INSTALL_PREFIX}/etc/Atlas/aal.txt exists
- abort and scold the user

This way the user (and you, while debugging!) can override the location of the file(s), provide a user-specific version in $HOME/.config/Atlas (doesn't make sense for some kind of configuration/data files) or simply use the default, installed one.


HTH

Michael



More information about the CMake mailing list