我希望与Bazel一起为深奥计算平台(CP/M,如果您有兴趣)构建磁盘图像。
查看了与Bazel进行交叉兼容的文档之后,它看起来已经为项目编译的情况而设置了,您不知道您要编译的平台直到您调用Bazel(该平台在命令行中传递)。
我没有这种情况。我的代码只能为此平台构建,并且我无法在其上跨编译普通代码。它需要一个非常具体的工具链,根本不像GCC一样。据我所知,这使得我大多数Bazel的工具链选择对我来说都是毫无用处的。我应该能够使用Skylark过渡规则来做些事情,但是我发现该文档非常沉重。
我迫切想做的是:
cc_binary(
name = "some_host_binary",
srcs = [...]
)
cc_binary(
name = "some_target_binary",
srcs = [...],
...some property here to generate a target binary...
)
genrule(
name = "output_artifact",
data = [":some_host_binary", ":some_target_binary"]
)
令人困惑,工具链上的页面 /code> cc_binary
的属性看起来正是我想要的,但似乎这实际上并不存在,并且只是在那里作为示例吗?
相反,如果我想依靠Bazel的自动工具链分辨率,那么我认为我需要能够为 some_target_binary
的平台指定平台,以强迫它选择正确的工具链;但是我觉得这只会导致构建故障,因为 genrule
不能说我希望这两个依赖关系是不同体系结构的二进制文件。
我开始觉得自己严重考虑了这一点。有什么建议,最好是例子吗?
I wish to build disk images for an esoteric computing platform (CP/M, if you're interested!) with bazel.
Having looked at the documentation for doing cross-compilation with bazel, it looks like it's set up for the case where the project can compile for any platform, and you don't know what platform you're compiling on until you invoke bazel (the platform is passed in on the command line).
I don't have this situation. My code can only build for this platform, and I cannot cross-compile normal code on it. It needs a single very specific toolchain which is not at all gcc-like. This makes, as far as I can tell, most of bazel's toolchain selection logic useless to me. I should be able to do something with Skylark transition rules, but I'm finding the documentation very heavy going.
What I desperately want to do is this:
cc_binary(
name = "some_host_binary",
srcs = [...]
)
cc_binary(
name = "some_target_binary",
srcs = [...],
...some property here to generate a target binary...
)
genrule(
name = "output_artifact",
data = [":some_host_binary", ":some_target_binary"]
)
Confusingly, the page on toolchains describes a compiler
attribute on cc_binary
which looks like it's precisely what I want, but it seems that this doesn't actually exist and is only there as an example?
If, instead, I want to rely on bazel's automatic toolchain resolution, then I think that I need to be able to specify the platform for some_target_binary
to force it to pick the right toolchain; but I feel like this would just cause a build failure as the genrule
can't tell that I want the two dependencies to be binaries for different architectures.
I'm beginning to feel that I'm grossly overthinking this. Any suggestions, and preferably examples?
发布评论
评论(1)
与 genrule.data 将为目标平台构建。我认为这是您想要的最大作品。这样写:
您可以使用
-
- -
- - -platforms
标志。将其作为
构建-platforms = // my:target
在project-level 。bazelrc 在构建时将自动设置它。如果您曾经使用通配符构建(例如
// ...
),则需要避免包括some_host_binary
或Bazel会尝试为目标平台构建它(在如果需要为目标平台构建其他目标,则还需要进行主机平台。在所有仅主机目标上指定target_compatible_with
将自动跳过为目标平台构建它们,不兼容的目标跳过有详细信息。如果要编写在构建系统上执行的测试(例如),则还需要指定
target_compatible_with
仅针对目标的事物来跳过它们。如果您永远不会没有- platforms = // my:target
,那么让Bazel认为它可以为任何平台构建目标二进制文件(默认值target_compatible_with
)是无害的,因为除非您要求它,否则它永远不会做。平台文档页面具有更多详细信息。
Genrule在宿主二进制中所做的更一般形式称为过渡。
cc_binary.compiler
用于旧的预平台C ++ - 仅目标选择,这不是您应该开始的新项目的地方。它仍然支持向后兼容,但积极地转移了。您需要更改- CPU
无论如何。Binaries used with
genrule.tools
will be built for the host platform, andgenrule.data
will be built for the target platform. I think that is the biggest piece you're looking for. Write it like this:You can set the target platform on the command line with
--platforms
flag. Putting this asbuild --platforms=//my:target
in the project-level .bazelrc will set it automatically when building.If you ever build with a wildcard (like
//...
), you'll want to avoid includingsome_host_binary
or bazel will try building it for the target platform (in addition to the host platform if that's necessary to build other targets for the target platform). Specifyingtarget_compatible_with
on all the host-only targets will automatically skip building them for the target platform, incompatible target skipping has details.If you want to write tests that execute on the build system (for example), you'll also need to specify
target_compatible_with
for target-only things to skip them. If you never build without--platforms=//my:target
, then letting Bazel think it can build target binaries for any platform (the default without atarget_compatible_with
) is harmless, because it will never do that unless you ask it to.The Platforms documentation page has more details.
The more general form of what genrule is doing for the host binaries is called a transition. The documentation on user-defined transitions has some details on ways to do fancier things in custom rules.
cc_binary.compiler
is for the old pre-platforms C++-only target selection, which is not where you should start for a new project. It's still supported for backwards compatibility, but actively being moved away from. You'd want to change--cpu
anyways.