Search code examples
linuxcmakeinstallation

CMake: How to execute a command before make install?


This is the way I install the config files:

file(GLOB ConfigFiles  ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.xml
                            ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.xsd
                            ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.conf)

install(FILES ${ConfigFiles} DESTINATION ${INSTDIR})

But I need to convert one of the xml files before installing it. There is an executable that can do this job for me:

./Convertor a.xml a-converted.xml

How can I automatically convert the xml file before installing it? It should be a custom command or target that installing depends on it, I just don't know how to make the install command depend on it though. Any advice would be appreciated!


Solution

  • Take a look at the SCRIPT version of install:

    The SCRIPT and CODE signature:

    install([[SCRIPT <file>] [CODE <code>]] [...])

    The SCRIPT form will invoke the given CMake script files during installation. If the script file name is a relative path it will be interpreted with respect to the current source directory. The CODE form will invoke the given CMake code during installation. Code is specified as a single argument inside a double-quoted string.

    For example:

    install(CODE "execute_process(COMMAND \"./Convertor a.xml a-converted.xml\")")
    install(FILES a-converted.xml DESTINATION ${INSTDIR})
    

    Be sure to checkout the entry for execute_process in the manual. Also be aware that macro expansion inside the CODE parameter can be a bit tricky to get right. Check the generated cmake_install.cmake in your build directory where the generated code will be placed.