在bazel构建中使用生成的代码 [英] Use generated code in a bazel build

查看:166
本文介绍了在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
    ...
)

声明的输出'external/somelib/foo.cpp'不是genrule创建的.这可能是因为genrule实际上没有创建此输出,或者是因为该输出是目录,并且该genrule是远程运行的(请注意,只有声明的文件输出的内容是从genrules远程运行的).

declared output 'external/somelib/foo.cpp' was not created by genrule. This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely).

我知道有一种解决方案,需要对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.

推荐答案

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

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

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并行运行多个动作,并且如果同一规则是工具和应用程序的依存关系,则它可能会尝试同时运行两者,并且它们彼此覆盖,可能非常结果不好.

Bazel runs multiple actions in parallel, and if the same rule is a dependency of a tool as well as of an app, it may try to run both at the same time, and they'd overwrite each other, with potentially very bad results.

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆