Search code examples
c++scons

SCONS: Run target always, not only if updated


Based on SCONS run target I created a basic SCONS script to build and then run my unit tests:

test = env.Program(
    target='artifacts/unittest',
    source= sources + tests
)

Alias(
    'test',
    test,
    test[0].abspath
)

After I modified the code, this works great:

> scons test
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ [...]
[Lots of compiler output]
unittest.exe
===============================================================================
All tests passed (22 assertions in 4 test cases)

If I run scons test now again without changing the code, it sees no need for building again, which is correct, but also doesn't run the tests again:

> scons test
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `test' is up to date.
scons: done building targets.

But, obviously, I want the tests to be run even if they are not rebuilt:

> scons test
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `test' is up to date.
scons: done building targets.
unittest.exe
===============================================================================
All tests passed (22 assertions in 4 test cases)

Is what would be the easiest way wo get this result?


Solution

  • Fixed it now by Creating a PseudoBuilder using AddMethod to concat a Builder and a Command:

    env.AddMethod(
        lambda env, target, source:
            [
                env.Program(
                    target=target,
                    source=source
                )[0],
                env.Command(
                    target,
                    source,
                    '.\\$TARGET'
                )[0]
            ]
        ,
        'BuildAndRun'
    )
    
    env.Alias(
        'test',
        env.BuildAndRun(
            target='artifacts/unittest',
            source= sources + tests
        )
    )
    

    Works as expected so far:

    > scons test
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    g++ [...]
    [Lots of compiler output]
    .\artifacts\unittest
    ===============================================================================
    All tests passed (22 assertions in 4 test cases)
    > scons test
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    .\artifacts\unittest
    ===============================================================================
    All tests passed (22 assertions in 4 test cases)
    

    Only drawback: the backslash in the command makes me windows-dependent.

    Any Opinions?