Neel Basu
Neel Basu

Reputation: 12904

consistent configure file paths with CMake before and after install

I have different libraries and executables as sub projects in my hierarchy. Most of them are daemons. So I have their corresponding cfg files, that these applications read during startups.

I have two questions.

  1. lets have a simple scenario: single project no hierarchy. I have to run the application with/without install . So If I put a install directive to keep the cfg in /etc I can get a cfg path which is constant. But in cases I may wish not to install. I want to run the executable right after doing make. So how to manage the cfg in such a way that works with/without install ?
  2. Next is my scenario where I have a hierarchy of sub projects. where monitor needs both a.cfg and b.cfg. and all cfg's are kept seperately in another sub project. Is this design usable ? Can I address paths to cfg files from a, b, and monitor in this scenario ?

However I can put include directives inside monitor.cfg to include a.cfg and b.cfg. But that comes down to the same problem. addressing paths to the cfg files with/without install.

abcd
    cfg                         # configuration files
        abcd                    # to be copied to /etc/abcd on install
            a.cfg               # configuration 
            b.cfg
            monitor.cfg
        includes/cfg
        sources
        -> libabcd-cfg.so       # target library that abstracts configurations as objects
    components
        a: cfg                  # sub project a (requires abcd/a.cfg)
            -> a                # target executable
        b: cfg                  # sub project b (requires abcd/b.cfg)
            -> b                # target executable
        monitor: cfg            # sub project monitor (requires abcd/a.cfg, b.cfg, monitor.cfg)
            -> monitor          # target executable

One simple solution is to keep a set of global search paths in something like cfg/includes/cfg/defs.h, that first searches in . and then searches in /etc. However with configuration files kept in a different project . will not work.

Upvotes: 4

Views: 155

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

Some times ago I faced similar problem: I wanted to have project testable immediately after build(without install). As far as I know, there is no common pattern for that problem. Possible ways are:

  1. Reproduce relative paths from installation tree within build one. In that case executables should use relative paths for address cfg-files. If you install all executables into same directory, you may set variable CMAKE_RUNTIME_OUTPUT_DIRECTORY to some directory inside binary tree. So all binaries will be generated in the given directory. (If subprojects are build using add_subdirectory() approach, then setting variable in the top-level project will affect on all subprojects). As for configuration files, you can make all subprojects to use same variable for build them:

    if(NOT DEFINED CFG_OUTPUT_DIRECTORY)
         # Use in-subproject path by default.
        set(CFG_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/cfg")
    endif()
    
    configure_file(a.cfg.in `${CFG_OUTPUT_DIRECTORY}`/a.cfg)
    

    Again, setting such CFG_OUTPUT_DIRECTORY variable in the top-level project will affect on subprojects.

  2. Use optional environment variable for point to cfg-file. So, for run executable from build tree, you can set this variable pointed to cfg-file within build tree:

    A_CFG_PATH=${CMAKE_CURRENT_BINARY_DIR}/a.cfg a.exe <args>
    

    (For run installed executable you needn't to set environment variables at all).

  3. Do not support running executables from build tree at all. It has reason from the sence of testing: Why should we test build tree, while intended user will use install tree.

Upvotes: 1

Related Questions