简体   繁体   English

如何使用bazel从同一组输入文件中产生多个文件

[英]How do I use bazel to produce multiple files from the same set of input files

I have a simple project which uses SWIG to make a small C++ library available to C#. 我有一个简单的项目,该项目使用SWIG使C#可以使用小型C ++库。 The C++ part is a single source and a single header file -- in addition there is one SWIG interface file. C ++部分是一个源代码和一个头文件-此外,还有一个SWIG接口文件。 The output from SWIG consists of 5 C# source files and 1 C++ source file. SWIG的输出包含5个C#源文件和1个C ++源文件。

Doing this with make is fairly simple, but I'm having a few problems wrapping my head around bazel. 使用make此操作非常简单,但是我在将头部缠绕在bazel上时遇到了一些问题。

How can I tell bazel that those 6 files are all generated using the same command? 我怎么能告诉巴泽勒,那些6个文件使用相同的命令产生的? Also, while I'm at it, how I do I tell bazel to actually invoke that command. 另外,在我处理过程中,我如何告诉bazel实际调用该命令。

The end product, that I'm ultimately interested in, is a .net dll file which only depend on the interface file and the original C++ header file. 我最终对最终产品感兴趣的是一个.net dll文件,该文件仅取决于接口文件和原始C ++头文件。

Bazel doesn't have a built-in rule to generate SWIG from C++, but you can either use a general-purpose rule ( genrule ) or teach Bazel how to build a SWIG library by writing your own rule . Bazel没有内置规则可以从C ++生成SWIG,但是您可以使用通用规则( genrule ),也可以教Bazel如何通过编写自己的规则来构建SWIG库。

If you use a genrule , you specify all the expected outputs in the outs attribute. 如果使用genrule ,请在outs属性中指定所有预期的输出。 Your rule will look something like this: 您的规则如下所示:

genrule(
    name = "cc_swig",
    srcs = [
        "lib.cc",
        "lib.h",
    ],
    outs = [
        "file1.cs",
        ...
        "fileN.cc",
    ],
    tools = [
        "//path/to/swig/compiler:bin",
    ],
    cmd = "$(location //path/to/swig/compiler:bin) --src=$(location lib.cc) --header=$(location lib.h) --out1=$(location file1.cs) ... --outN=$(location fileN.cc)",
)

The $(location) construct in cmd is a required placeholder, Bazel replaces those with the run-time path of the referenced file. cmd$(location)构造是必需的占位符,Bazel将其替换为引用文件的运行时路径。

(If the SWIG compiler won't let you specify where to put its outputs, you can add more commands to cmd that mv the output files to their final location, eg cmd = "... && mv outputs/lib.cs $(location file1.cs)" .) (如果SWIG编译器不会让你指定把它的输出,你可以添加更多命令cmdmv的文件输出到它们的最终位置,例如cmd = "... && mv outputs/lib.cs $(location file1.cs)" 。)

Writing your own rules is more advanced so I won't describe that here, you can read about them in the docs . 编写自己的规则更为高级,因此在这里我将不进行描述,您可以在docs中了解它们。

On how to get Bazel to build the library -- if the SWIG-compiling rule is a dependency of your top-level target (ie whatever you "bazel build"), then Bazel will build it. 关于如何使Bazel生成库的方法-如果SWIG编译规则是您的顶级目标的依赖项(即“ Bazel构建”的任何内容),则Bazel将对其进行构建。 See for example the Getting started guide, on how to build a C++ project . 例如请参阅入门指南,了解如何构建C ++项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM