本文介绍了在bazel构建中使用生成的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ python gencpp.py

此命令在工作目录中生成一个cpp文件foo.cpp.

This command generates a cpp file foo.cpp in the working directory.

在构建为能够将foo.cpp包含在cc_binarysrcs属性中之前,我想先在bazel中运行此命令.

I'd like to run this command in bazel before building to be able to include foo.cpp in cc_binary's srcs attribute.

我尝试过的事情:

genrule(
    name = 'foo',
    outs = ['foo.cpp'],
    cmd = 'python gencpp.py',
)

cc_library(
    srcs = ['foo.cpp'], # tried also with :foo
    ...
)

我知道有一种解决方案,需要对gencpp.py进行一些修改,但这不是我想要的.

I know that there is a solution that requires gencpp.py to be modified a little bit, but it's not what I'm looking for.

推荐答案

我建议您更改此设置,以便:

I would recommend that you change this, so that either:

  • 您将输出写入命令行标志指定的文件中
  • 您将输出写入标准输出,然后将标准输出重定向到文件

然后您的genrule命令可以是:

Then your genrule command can be either:

python gencpp.py --outputFile=$@

python gencpp.py > $@

(我个人的一般偏好是后者,尽管在需要编写多个输出的情况下无法使用.)

(My general personal preference is for the latter, although that can't be used in cases where multiple outputs need to be written.)

分别.

Ulf Adams 指出:

因此,最好避免编写bazel不直接知道的输出文件.

So, writing output files which bazel doesn't directly know about is best avoided.

这篇关于在bazel构建中使用生成的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:46