python - 如何从测试脚本中运行 WAF 编译的 C++ 程序?

标签 python c++ testing waf

尊敬的 WAF 构建系统专家,

假设您使用 WAF 构建系统构建库 fooLib 和程序 fooProg。然后,您想通过检查 fooProg 输出的 Python 脚本 fooProgTest 检查程序 fooProg

这是 fooLibfooProg 的最小示例:

$ cat fooLib/fooLib.cpp 
int foo()
{
    return 42;
}
$ cat fooProg/fooProg.cpp 
#include <iostream>

extern int foo();

int main()
{
    std::cout << foo() << std::endl;
    return 0;
}

在这个例子中,我的目标是拥有一个 Python 脚本来检查 fooProg 输出 42。 这是我不太干净的解决方案:

import os
from waflib.Tools import waf_unit_test


def options(opt):
    opt.load("compiler_cxx waf_unit_test python")


def configure(cnf):
    cnf.load("compiler_cxx waf_unit_test python")

def build(bld):
    bld.add_post_fun(waf_unit_test.summary)
    bld.options.clear_failed_tests= True

    bld(features= "cxx cxxshlib",
        target= "fooLib",
        source= "fooLib/fooLib.cpp")

    bld(features= "cxx cxxprogram",
        target= "fooProg/fooProg",
        source= "fooProg/fooProg.cpp",
        use= "fooLib")

    testEnv= os.environ.copy()
    testEnv["FOO_EXE"]= bld.path.find_or_declare("fooProg/fooProg").abspath()
    bld(features= "test_scripts",
        test_scripts_source= "fooProgTest/fooProgTest.py",
        test_scripts_template= "${PYTHON} ${SRC[0].abspath()}",
        test_scripts_paths= {
            "LD_LIBRARY_PATH": bld.bldnode.abspath()
        },
        test_scripts_env= testEnv
       ) 
cat fooProgTest/fooProgTest.py 
#!/usr/bin/env python

import os
import subprocess

assert subprocess.check_output("{}".format(
        os.environ["FOO_EXE"])).startswith("42")

我的问题如下:

  • 你们中有人知道如何避免手动设置 LD_LIBRARY_PATH 吗?
  • 如何避免通过环境变量“FOO_EXE”设置fooProg的路径?

非常感谢!

最佳答案

Does anyone of you know how to avoid setting LD_LIBRARY_PATH manually?

您可以为可执行文件指定运行时搜索路径。假设文件 fooLib.sofooProg 位于同一目录中,对您的 wscript 进行以下更改就足够了:


    bld(features= "cxx cxxprogram",
        target= "fooProg/fooProg",
        source= "fooProg/fooProg.cpp",
        use= "fooLib",
        rpath= "$ORIGIN")

这使得 LD 在搜索共享对象时也会考虑存储可执行文件的目录。

How to avoid setting the path of fooProg via the environment variable "FOO_EXE"?

使用 subprocess.check_output 您可以传递多个参数。即


    subprocess.check_output([
        "your_executable_to_launch",
        "arg1",
        "arg2"
    ])

在您的测试脚本中,您必须使用 sys.argvargparse 读取参数。


额外:

启动解释器来启动您的应用程序似乎有点老套。相反,定义一个自定义任务(实现 waflib.Task.Task),然后运行 ​​subprocess.check_output1


1 AFAIK waf 为您提供了一种启动进程的便捷方法,尽管我记不起它的名字了。

关于python - 如何从测试脚本中运行 WAF 编译的 C++ 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62346564/

相关文章:

python - 如何将 python 对象 pickle 到 csv 文件中?

python - 为什么我们在 super.save() 中使用 * args 和 **kwargs

C++线性搜索算法,判断数组元素个数

.net - 为 web 服务、soap 和 rest 创建测试用例

Python:如何对两个列表中的元素进行乘法和求和(不使用库)?

python - 使用 pyparsing 在 python 中解析文本文件

c++ - 如何从插件中的单独 C++ 线程调用发射器回调?

c++ - Orocos LTTng 符号查找错误 :/path/to/liborocos-rtt-traces-gnulinux. so: undefined symbol: lttng_probe_register

jquery - 如何在Vue jest测试中等待引导模式动画完成

reactjs - 用 Jest 模拟react-router-dom useHistory和其他钩子(Hook)