如何在SConscript中运行刚编译的程序

时间:2019-03-15 19:51:40

标签: path scons build-system

我有一个稍微复杂的SCons构建脚本,该脚本执行以下两个步骤:

# 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)

如果我单独使用此构建脚本运行scons,这将很好地工作。

但是,如果我是通过environment.SConscript()从上一级目录的另一个SConstruct文件中调用它的,则步骤1将路径调整为项目位置,而步骤2没有。看到此输出:

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

第2行将可执行文件构建到Nuclex.Game.Native/obj/gcc-7-amd64-release/中,而第3行尝试在obj/gcc-7-amd64-release/中调用可执行文件,而忘记了项目目录。

我应该使用另一种方式来调用我的单元测试可执行文件吗?还是可以查询SCons环境的基本路径?


更新:复制案例,将https://pastebin.com/W08yZuF9放置为SConstruct到根目录中,创建子目录somelib并将https://pastebin.com/eiP63Yxh放置为SConstruct,同时创建{{ 1}}和“ Hello World”或其他虚拟程序。

1 个答案:

答案 0 :(得分:1)

SCons动作(命令中的action参数)将使用SCons变量正确替换源和目标,并自动考虑VariantDirs和SConscript目录。您可以在以下位置找到有关这些源和目标替代的更多信息:https://scons.org/doc/HTML/scons-man.html#variable_substitution

有一节说明了有关SConscript和VariantDirs的用法:

  

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

因此,在您的示例中,我认为您想在操作字符串中将executable_path替换为$SOURCE

# 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
)