简体   繁体   中英

How to “properly” invoke executable from usr/bin via Starlark?

In one of my rules, I want to unzip a file via unzip -d so that I can access some headers.

Currently, I am doing so by using whatever unzip is on my PATH.

ctx.actions.run(
      ...
      executable = "unzip",
)

Alternatively, I could use /usr/bin/unzip to avoid relying on my PATH. However, it's unclear what the idiomatic approach should be. Based on my reading of the docs, I would ideally have some sort Bazel binary target for unzip that the rule would invoke. It's somewhat unclear, though, how I would go about defining that binary target, perhaps an sh_binary .

The right solution would be having an unzip utility in your repo. (Either in the source tree or imported as an external repository.)

Without that, I think the most principled approach is to write a repository rule that looks up unzip from the PATH using repository_ctx.which (or just expects "/usr/bin/unzip" to exist) and creates a BUILD file ( repository_ctx.file ) with a sh_binary that wraps the unzip tool.

In both cases you have to add a label attribute "_unzip_tool" to the consuming rule:

"_unzip_tool": attr.label(
                   executable = True,
                   cfg = "host",
                   default = Label("@unzip//:unzip_tool"),
               ),

and use it like so:

ctx.actions.run(
    ...,
    executable = ctx.executable._unzip_tool,
)

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