Cygon
Cygon

Reputation: 9610

How to Run just-compiled program in SConscript

I have a somewhat complex SCons build script that an some point does the following two steps:

# 1: builds unit tests (googletest, shell executable)
compile_tests = some_environment.Program(executable_path, test_sources)

# 2: runs unit tests (call earlier compiled program)
run_tests = other_environment.Command(
    source = executable_path,
    action = executable_path + ' --gtest_output=xml:' + test_results_path,
    target = test_results_path
)

Depends(run_tests, compile_tests)

This is working fine if I run scons with this build script on its own.

If I, however, invoke it via environment.SConscript() from another SConstruct file one directory level up, then step 1 adjusts the path to the project's location while step 2 doesn't. See this output:

scons: Building targets ...
g++ -o Nuclex.Game.Native/obj/gcc-7-amd64-release/NuclexGameNativeTests -z defs -Bsymbolic Nuclex.Game.Native/obj/gcc-7-amd64-release/Tests/Timing/ClockTests.o -LNuclex.Game.Native/obj/gcc-7-amd64-release -LReferences/googletest/gcc-7-amd64-release -lNuclexGameNativeStatic -lgoogletest -lgoogletest_main -lpthread
obj/gcc-7-amd64-release/NuclexGameNativeTests --gtest_output=xml:bin/gcc-7-amd64-release/gtest-results.xml
sh: obj/gcc-7-amd64-release/NuclexGameNativeTests: No such file or directory

Line 2 builds the executable into Nuclex.Game.Native/obj/gcc-7-amd64-release/ while line 3 tries to call it in obj/gcc-7-amd64-release/, forgetting the project directory.

Should I use another way to invoke my unit test executable? Or can I query the SCons environment for its base path?


Update: reproduction case, place https://pastebin.com/W08yZuF9 as SConstruct in root directory, create subdirectory somelib and place https://pastebin.com/eiP63Yxh as SConstruct therein, also create main.cpp with a "Hello World" or other dummy program.

Upvotes: 1

Views: 445

Answers (1)

dmoody256
dmoody256

Reputation: 583

A SCons Action (the action parameter in the Command) will use the SCons variables to substitute sources and targets in correctly, taking into account VariantDirs and SConscript directories automatically. You can find more info on these source and target substitutions here: https://scons.org/doc/HTML/scons-man.html#variable_substitution

There is a section which explains using this in regards to SConscript and VariantDirs:

SConscript('src/SConscript', variant_dir='sub/dir')
$SOURCE => sub/dir/file.x
${SOURCE.srcpath} => src/file.x
${SOURCE.srcdir} => src

So in your example I think you want to replace executable_path with $SOURCE in the action string:

# 2: runs unit tests (call earlier compiled program)
run_tests = other_environment.Command(
    source = executable_path,
    action = '$SOURCE --gtest_output=xml:$TARGET',
    target = test_results_path
)

Upvotes: 1

Related Questions