3

I have added this custom target to my CMakeList.txt file.

System: Windows 7, TDMGCC MinGW32, and Ninja latest from GitHub.

ADD_CUSTOM_TARGET(unittest_run
    COMMAND test1.exe > result.testresult
    COMMAND test2.exe >> result.testresult
    COMMAND type result.testresult
)

The problem is that when test1.exe fails I generate a fail output, but it seems that there is also some error code coming which causes a problem. ninja: build stopped: subcommand failed.

How can I tell CMake it should ignore return errors?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mgiaco
  • 319
  • 1
  • 5
  • 17

2 Answers2

4

You can try use a conditional OR statement, which will be run only if the preceding statement fails, and generate a successful return code from the secondary statement

From this page on "Conditional Execution" you can use || to conditionally execute a secondary statement if the first fails

Execute command2 only if command1 fails (OR)

    command1 || command2

From this SO answer it is possible to generate a successful return code using (exit 0)

true is roughly equivalent to (exit 0) (the parentheses create a subshell that exits with status 0, instead of exiting your current shell.

Putting it all together:

ADD_CUSTOM_TARGET(unittest_run
    COMMAND test1.exe > result.testresult  || (exit 0)
    COMMAND test2.exe >> result.testresult || (exit 0)
    COMMAND type result.testresult
)
Community
  • 1
  • 1
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
0

It works only this way:

COMMAND 
test1.exe > result.testresult & 
test2.exe >> result.testresult || 
(exit 0) &
type result.testresult
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mgiaco
  • 319
  • 1
  • 5
  • 17
  • Correct syntax uses _&&_ instead of _&_ – dyomas Nov 21 '18 at 11:29
  • @dyomas, it's not unlikely that mgiaco was thinking that by running those in the background you can run all three... I'm not so sure the result is what the OP would want, of course... But it can be fund like this too. – Alexis Wilke Apr 10 '19 at 06:53