簡體   English   中英

如何從測試腳本中運行 WAF 編譯的 C++ 程序?

[英]How to run WAF compiled C++ program from within test script?

尊敬的 WAF 構建系統專家,

假設您使用 WAF 構建系統來構建庫fooLib和程序fooProg 然后,您想通過 Python 腳本fooProgTest檢查程序fooProg ,該腳本檢查 fooProg 的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的路徑?

非常感謝!

你們中有人知道如何避免手動設置 LD_LIBRARY_PATH 嗎?

您可以為可執行文件指定運行時搜索路徑。 假設文件fooLib.sofooProg位於同一目錄中,對 wscript 進行以下更改就足夠了:


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

這使得 LD 在搜索共享對象時也會考慮存儲可執行文件的目錄。

如何避免通過環境變量“FOO_EXE”設置 fooProg 的路徑?

使用subprocess.check_output ,您可以傳遞多個 arguments。 IE


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

在您的測試腳本中,您必須使用sys.argvargparse閱讀 arguments 。


額外

啟動解釋器來啟動您的應用程序似乎有點 hacky。 相反,定義一個自定義任務(實現waflib.Task.Task ),然后運行subprocess.check_output 1


1 AFAIK waf 為您提供了一種啟動進程的便捷方法,盡管我不記得它的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM