# -*- bazel -*-

load("@bazel_skylib//lib:selects.bzl", "selects")

package(default_visibility = ["//visibility:private"])

licenses(["notice"])  # Apache-2.0

config_setting(
    name = "darwin",
    constraint_values = ["@platforms//os:osx"],
)

config_setting(
    name = "linux",
    constraint_values = ["@platforms//os:linux"],
)

config_setting(
    name = "arm64",
    constraint_values = ["@platforms//cpu:arm64"],
)

config_setting(
    name = "amd64",
    constraint_values = ["@platforms//cpu:x86_64"],
)

selects.config_setting_group(
    name = "darwin-amd64",
    match_all = [
        ":darwin",
        ":amd64",
    ],
)

selects.config_setting_group(
    name = "darwin-arm64",
    match_all = [
        ":darwin",
        ":arm64",
    ],
)

selects.config_setting_group(
    name = "linux-amd64",
    match_all = [
        ":linux",
        ":amd64",
    ],
)

selects.config_setting_group(
    name = "linux-arm64",
    match_all = [
        ":linux",
        ":arm64",
    ],
)

genrule(
    name = "select_buildifier",
    srcs = select({
        ":darwin-amd64": ["buildifier-darwin-amd64"],
        ":darwin-arm64": ["buildifier-darwin-arm64"],
        ":linux-amd64": ["buildifier-linux-amd64"],
        ":linux-arm64": ["buildifier-linux-arm64"],
    }),
    outs = ["buildifier"],
    executable = True,
    cmd = "cp $< $@ && chmod a+x $@",
    visibility = ["//visibility:public"],
)
