简体   繁体   中英

Bazel - depend on generated outputs

I have a yaml file in a Bazel monorepo that has constants I'd like to use in several languages. This is kind of like how protobuffs are created and used.

How can I parse this yaml file at build time and depend on the outputs?

For instance:

item1: "hello"
item2: "world"
nested:
  nested1: "I'm nested"
  nested2: "I'm also nested"

I then need to parse this yaml file so it can be used in many different languages (eg, Rust, TypeScript, Python, etc.). For instance, here's the desired output for TypeScript:

export default {
  item1: "hello",
  item2: "world",
  nested: {
    nested1: "I'm nested",
    nested2: "I'm also nested",
  }
}

Notice, I don't want TypeScript code that reads the yaml file and converts it into an object. That conversion should be done in the build process.

For the actual conversion, I'm thinking of writing that in Python, but it doesn't need to be. This would then mean the python also needs to run at build time.


PS I care mostly about the functionality, so I'm flexible with the exactly how it's done. I'm even fine using another file format aside from yaml.

Thanks to help from @oakad, I was able to figure it out. Essentially, you can use genrule to create outputs.

So, assuming you have some target like python setup to generate the output named parse_config , you can just do this:

genrule(
    name = "generated_output",
    srcs = [],
    outs = ["output.txt"],
    cmd = "./$(locations :parse_config) > $@" % name,
    tools = [":parse_config"],
    visibility = ["//visibility:public"],
)

The generated file is output.txt . And you can access it via //lib/config:generated_output .

Note, essentially the cmd is piping the stdout into the file contents. In Python that means anything printed will appear in the generated file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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