【问题标题】:How to Run just-compiled program in SConscript如何在 SConscript 中运行刚刚编译的程序
【发布时间】:2019-03-15 19:51:40
【问题描述】:

我有一个有点复杂的 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程序。

【问题讨论】:

  • 我不会将您目前拥有的称为最小、完整和可验证的示例。这些环境(some_environment、other_environment)是否以任何方式修改过? executable_path 和 test_results_path 的值是多少?
  • 对 SConscript 的调用也有任何参数吗?
  • 我相信这个例子足以理解和重现问题。假设环境是普通的,如果你愿意,用ab 替换executable_pathtest_results_path。我现在还添加了两个最小的 SConstruct 文件来重现问题底部的问题。
  • 我试用了您的复制案例,一切似乎都正常工作。你确定你没有在任何地方使用变体目录吗?你的路径obj/gcc-7-amd64-release/ 表明你是?并且环境没有差异,那为什么不使用相同的环境呢?
  • 我的实际构建设置确实使用了 VariantDir,但问题取决于 environment.SConscript() 调用。在我的系统上,sh: ./example: No such file or directory 的最小复制案例错误。

标签: path scons build-system


【解决方案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} => 源代码

因此,在您的示例中,我认为您想在操作字符串中将 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
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多