简体   繁体   English

Bazel-获取当前的OS和CPU类型作为要在构建规则中使用的变量

[英]Bazel - Get current OS and CPU type as variables to be used in build rules

In our current setup, we use Bazel as the overall build tool, but for individual Java projects, we use Maven for building them. 在当前设置中,我们使用Bazel作为整体构建工具,但是对于单个Java项目,我们使用Maven进行构建。 An example build rule for a Java project will look like this: Java项目的示例构建规则如下所示:

genrule (
    name = "build-core",
    srcs = [
        ":deps-core",
    ],
    outs = [
        "core-1.0.jar",
    ],
    cmd = "rm -rf $(@D)/src && cd project/ && umask 0000"
        + " && mvn -U clean install -Djavacpp.platform=linux-x86_64 -pl :core"
        + " && cp core/target/core-1.0.jar ../$(@D)"
)

As you can notice, we are currently hardcoding the -Djavacpp.platform to linux-x86_64 . 如您-Djavacpp.platform ,我们目前正在将-Djavacpp.platform硬编码为linux-x86_64 Is there a way to get this value automatically from Bazel, maybe as a variable, so that we can use the same build rules to build on MacOSX and Windows systems too? 是否有一种方法可以从Bazel中自动获取此值(可能作为变量),以便我们也可以使用相同的构建规则在MacOSX和Windows系统上进行构建?

It would be better to build all of the libraries with Bazel directly, then you would get this behaviour for free. 最好直接使用Bazel构建所有库,然后免费获得此行为。 See Migrating from Maven to Bazel . 请参阅从Maven迁移到Bazel

However, if this is not an option, you can access some of this information using Make Variables : 但是,如果这不是一个选择,则可以使用Make Variables访问某些信息:

genrule(
  name = "g",
  outs = [
    "g.txt",
  ],
  cmd = "echo \"$(TARGET_CPU) $(COMPILATION_MODE) $(JAVA) $(JAVABASE)\" > $@",
  toolchains = [
    "@bazel_tools//tools/jdk:current_java_runtime",
  ],
)

I could not find the operating system in these variables, however. 但是,我在这些变量中找不到操作系统。

Perhaps a better idea is to use select statements: 也许更好的主意是使用select语句:

genrule(
  name = "f",
  outs = [
    "f.txt",
  ],
  cmd = select({
    "@bazel_tools//src/conditions:darwin": "echo 'macOS' > $@",
    "//conditions:default": "echo 'Linux' > $@",
  }),
)

You would then write a different cmd for each platform. 然后,您将为每个平台编写不同的cmd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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