简体   繁体   中英

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#. The C++ part is a single source and a single header file -- in addition there is one SWIG interface file. The output from SWIG consists of 5 C# source files and 1 C++ source file.

Doing this with make is fairly simple, but I'm having a few problems wrapping my head around bazel.

How can I tell bazel that those 6 files are all generated using the same command? Also, while I'm at it, how I do I tell bazel to actually invoke that command.

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.

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 .

If you use a genrule , you specify all the expected outputs in the outs attribute. 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.

(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)" .)

Writing your own rules is more advanced so I won't describe that here, you can read about them in the 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. See for example the Getting started guide, on how to build a C++ project .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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