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

问题描述 投票:0回答:1

我有一个有点复杂的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放入其中,同时创建带有“Hello World”或其他虚拟程序的main.cpp

path scons build-system
1个回答
1
投票

SCons Action(命令中的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
)
© www.soinside.com 2019 - 2024. All rights reserved.