Match configuration file paths with CMake before and after installation

I have different libraries and executables as subprojects in my hierarchy. Most of them are demons. Therefore, I have the appropriate files cfgthat these applications read during startups.

I have two questions.

  • allows you to have a simple scenario: a single project without a hierarchy. I need to run the application with / without install. Therefore, if I put the installation directive to save cfg to /etc, I can get the constant cfg path. But in cases that I might not want to install. I want to run an executable file right after execution make. So how to manage cfg in a way that works with / without install?
  • My next scenario is where I have a hierarchy of subprojects. where the need for monitoring both a.cfgand b.cfg. and all cfg are saved separately in another subproject. Is this design useful? Can I specify paths to cfg files from a, band monitorin this scenario?

However, I can include include directives inside monitor.cfg to enable a.cfg and b.cfg. But it comes down to the same problem. address paths to cfg files with / without installation.

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 save the set of global search paths in a view cfg/includes/cfg/defs.hthat first searches in .and then searches in /etc. However, configuration files stored in another project .will not work.

+4
1

: , ( ). , . :

  • . cfg . , CMAKE_RUNTIME_OUTPUT_DIRECTORY - . , . ( add_subdirectory(), ). , :

    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)
    

    , CFG_OUTPUT_DIRECTORY .

  • point to cfg-file. , , cfg :

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

    ( ).

  • . - : , .

+1

Source: https://habr.com/ru/post/1610284/


All Articles