淡褐色自动生成的cpp/hpp文件 [英] Bazel & automatically generated cpp / hpp files

查看:125
本文介绍了淡褐色自动生成的cpp/hpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Bazel作为我的C ++项目构建系统.

I am starting to use Bazel as my C++ project build system.

但是我遇到以下问题:

我处于自动生成file.hpp file.cpp(文化编程)的情况.

I am in a scenario where I automatically generate the file.hpp file.cpp (literate programming).

要重现我的问题,只需使用此最小生成器即可:

To reproduce my problem one can simply use this minimal generator:

-- file.sh --
#!/bin/sh
echo "int foo();" >> file.hpp
echo "#include \"myLib/file.hpp\"\n\nint foo() { return 2017; }" >> file.cpp

我的项目存储库是:(WORKSPACE是一个空文件)

My project repo is: (WORKSPACE is an empty file)

├── myLib
│   ├── BUILD
│   └── file.sh
└── WORKSPACE

BUILD文件为

genrule(
  name = "tangle_file",
  srcs = ["file.sh"],
  outs = ["file.cpp","file.hpp"],
  cmd =  "./$(location file.sh);cp file.cpp $(@D);cp file.hpp $(@D);"
)

cc_library(
    name = "file",
    srcs = ["file.cpp"],
    hdrs = ["file.hpp"],
#    deps = [":tangle_file"],
    visibility = ["//bin:__pkg__"],
)

我有两个问题:

问题(A),涉及genrule()部分:

我必须使用

cmd =  "./$(location file.sh);cp file.cpp $(@D);cp file.hpp $(@D);"

非常神秘.

我的第一次尝试是:

cmd =  "./$(location file.sh)"

但是在这种情况下,我会收到以下错误消息:

However in that case I get the following error:

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

declared output 'myLib/file.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)

问题(B),涉及cc_library()部分

我不知道如何使Bazel意识到:file 目标取决于:tangle_file 目标.

I do not know how to make Bazel aware of that the :file target depends on the :tangle_file target.

如果我取消评论:

deps = [":tangle_file"],

我收到以下错误:

在cc_library规则//myLib:file:genrule规则'//myLib:tangle_file'的deps属性中放错了位置(期望的cc_inc_library,cc_library,objc_library,experimental_objc_library或cc_proto_library).

in deps attribute of cc_library rule //myLib:file: genrule rule '//myLib:tangle_file' is misplaced here (expected cc_inc_library, cc_library, objc_library, experimental_objc_library or cc_proto_library).

推荐答案

问题(A)

您看到的错误是因为genrule cmd不在其输出目录中运行.如果您在file.sh脚本中对bazel-out/local-fastbuild/genfiles/myLib/file.cpp而不是file.cpp进行了硬编码,那么它将起作用.但是,建议的方法是让脚本以其输出目录作为参数.

Question (A)

The error that you are seeing is because the genrule cmd is not run inside of its output directory. If you hardcoded bazel-out/local-fastbuild/genfiles/myLib/file.cpp instead of file.cpp in your file.sh script, it would work. However, the recommended approach would be for your script to takes its output directory as an argument.

例如,

genrule(
  name = "tangle_file",
  srcs = ["file.sh"],
  outs = ["file.cpp","file.hpp"],
  cmd =  "./$(location file.sh) $(@D)"
)

#!/bin/sh
echo "int foo();" >> $1/file.hpp
echo "#include \"myLib/file.hpp\"\n\nint foo() { return 2017; }" >> $1/file.cpp

问题(B)

您拥有的事实

Question (B)

The fact that you have

srcs = ["file.cpp"],
hdrs = ["file.hpp"],

cc_library中的

告诉Bazel它取决于genrule,因为genrule创建了这些文件.如果您想使其更明确,则可以使用标签语法,其作用相同:

in your cc_library is what tells Bazel that it depends on the genrule, since the genrule creates those files. If you want to make it more explicit, you could use the label syntax, which does the same thing:

srcs = ["//myLib:file.cpp"],
hdrs = ["//myLib:file.hpp"],

这篇关于淡褐色自动生成的cpp/hpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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