bazelstarlarkskylark

How can I pass an alias value to a bazel function?


I have the following alias defined in a BUILD file:

alias(
    name = "platform",
    actual = select({
        ":macos_x86_64": "macos_x86_64",
        ":linux_x86_64": "linux_x86_64",
        ":linux_aarch64": "linux_aarch64",
    }),
    visibility = ["//visibility:public"],
)

How can I pass the "actual" string to a function like this?

def myFunction(platform): ...

I am trying the most naive thing and it does not understand that it's an alias, not a string:

myFunction("//bazel/config:platform")

Solution

  • def myFunction creates a macro, not a rule. Macros can't access the values of a select, only add other selects to it and pass it to rules. Another way to think about the difference: there are very few things a macro can do which can't be done directly in a BUILD file.

    Also, alias.actual is a Label. Even when you pass it to a rule, the values need to be valid labels, and will be handled as such. You can use Label.name to get the name back though.