2

I am doing this:

    add_custom_target(Target1
        COMMAND Command1
    )
    add_custom_target(Target2
        COMMAND Command2
    )
    add_dependencies(Target2 Target1)

but if Command1 has a non-zero exit code, Command2 is never run. Is there a way to make sure that Target2 runs after Target1, but no matter what the exit code of Command1 is?

David Doria
  • 9,873
  • 17
  • 85
  • 147
  • you can always execute some simple statement at the end of the command so it would reuturn 0 exit code(like echo, for example) – ixSci Feb 04 '16 at 15:04
  • @DavideSpataro I just want a sequential ordering (Target1 must be run before Target2) - I don't want Target2 to depend on Target1 succeeding. – David Doria Feb 04 '16 at 15:05
  • @ixSci But the add_custom_target ends immediately as soon as one of the COMMANDs returns non-zero, right? So the second COMMAND of 'echo' would never be reached. – David Doria Feb 04 '16 at 15:05
  • I mean do it as part of the first command you can do a few command as one by piping them. So it should not see any return value untill all the commands are complete – ixSci Feb 04 '16 at 15:14
  • Or you can put the commands, with echo as the last one, in some script file and invoke it in the COMMAND – ixSci Feb 04 '16 at 15:16
  • @ixSci I tried this: ` add_custom_target(Target1 COMMAND ctest | echo "echoing" )` but it just echos and does not run 'ctest'. – David Doria Feb 04 '16 at 15:21
  • hm, what about `add_custom_target(Target1 COMMAND ctest COMMAND echo "echoing" )`, won't help? – ixSci Feb 04 '16 at 15:24
  • @ixSci Nope, the first COMMAND fails and the second is never run. – David Doria Feb 04 '16 at 15:27
  • The solution here looks reasonable: http://stackoverflow.com/questions/15322547/catch-return-value-in-cmake-add-custom-command – RjOllos Feb 04 '16 at 19:30

1 Answers1

0

I ended up with this:

    file(WRITE ${CMAKE_BINARY_DIR}/NoExitCodeTests.cmake "execute_process(COMMAND ctest)")

    add_custom_target(Target1
        COMMAND ${CMAKE_COMMAND} -P NoExitCodeTests.cmake
    )

This will make Target1 always return 0 no matter what happens in ctest. Ugly, but sometimes CMake is ugly :)

David Doria
  • 9,873
  • 17
  • 85
  • 147