Search code examples
installationcmaketargetexecute

CMake : execute target Debug or Release at install time


In my CMake project I generate a target "MyProgram" with

ADD_EXECUTABLE(MyProgram ...)

Then, at install time, I would like to execute this program (with some arguments) so I use

INSTALL(SCRIPT MyScript.cmake)

But with Windows + MSVC, I can't find a way to call the right executable :

  • In Debug mode I want to call ${EXECUTABLE_OUTPUT_PATH}/Debug/MyProgram.exe
  • In Release mode I want to call ${EXECUTABLE_OUTPUT_PATH}/Release/MyProgram.exe

I know a little about cmake-generator-expressions, but I couldn't make it work with install(SCRIPT).

Any help would be greatly appreciated !


Solution

  • Either generate different script files knowing about the respective locations of the executables and use e.g. install(SCRIPT "MyScript$<CONFIG>.cmake") to use MyScriptDebug.cmake or MyScriptRelease.cmake for Debug or Release configurations respectively or alternatively use install(CODE) allowing you to pass parameters to the script:

    CMakeLists.txt

    install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" -D \"MY_PROGRAM=$<TARGET_FILE:MyProgram>\" -P MyScript.cmake)")
    

    MyScript.cmake

    execute_process(COMMAND ${MY_PROGRAM} ...)
    ...
    

    For calling a single program you may not need a script file though; you may simply be able to call the program directly using

    install(CODE "execute_process(COMMAND \"$<TARGET_FILE:MyProgram>\" ...)")