1

I want to run some test targets after they have been built. Currently, I'm using add_custom_target:

add_custom_target( runTests ALL
    COMMAND testExec1
    COMMAND testExec2
    DEPENDS
        testExec1
        testExec2
    COMMENT "-- Running tests..."
)

With ALL, this executes every time a build command is used, including when no dependencies have changed. If I remove ALL, it never executes. The documentation shows ALL to be optional, but doesn't give any alternatives or go into detail what happens when you leave it out.

If I use add_custom_command( TARGET ... POST_BUILD ) instead, e.g.

add_custom_command( TARGET testExec1 POST_BUILD
    COMMAND testExec1
    COMMAND testExec2
    DEPENDS
        testExec1
        testExec2
    COMMENT "-- Running tests..."
)

then it only runs if the specified target (here testExec1) has been built, not when a different dependendy changed.

How can I let my tests run if and only if one or more of them have changed?

I would prefer not to create some dummy file with add_custom_command( OUTPUT ... ), as shown here.

RL-S
  • 734
  • 6
  • 21
  • "I would prefer not to create some dummy file ..." - But OUTPUT something is the the only way for do something **only if** some other files are changed. Imagine yourself to be a built system (e.g. Make). A user asks you to run command COMM only if file FILE1 has been changed since the last run of that command. But how a build system could deduce that the file has been changed? A build system doesn't store a timestamp of the last command running. If the command COMM creates some file (say, FILE2), then build system could check, whether FILE1 is newer than FILE2, and run COMM only in this case. – Tsyvarev Nov 09 '21 at 15:57
  • It knows whether or not the dependencies need building, right? It compares the target timestamp with the sources'. And it is able to propagate that to dependent targets, e.g. re-linking an executable when a library it depends on has been recompiled. – RL-S Nov 09 '21 at 16:55
  • Think of all targets **independently**, so "timestamp propagation" is not usable. Let `A` to be a default target and dependent on `B` and `B` depends on `C`. **By default** (with plain `make` running) `C` will be checked to be newer than `B`. What if a user firstly runs `make B`, which in case of modified `C` will rebuild `B`. And then a user runs `make A`. The latter run will find that `B` is not older than `C`, so `B` is not needed to be rebuilt. But how should it decide, whether to rebuild `A` or not? Without knowing timestamp of `A` it is impossible. – Tsyvarev Nov 09 '21 at 19:07
  • I understand how it's done, I just hoped that there would be a way, e.g. by specifying more than one target using `add_custom_command( TARGET t1, t2, t3 POST_BUILD ...)` – RL-S Nov 10 '21 at 19:06

0 Answers0