# -*- python -*-

load("//tools/lint:lint.bzl", "add_lint_tests")

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

config_setting(
    name = "apple",
    constraint_values = ["@bazel_tools//platforms:osx"],
)

config_setting(
    name = "apple_debug",
    constraint_values = ["@bazel_tools//platforms:osx"],
    values = {"compilation_mode": "dbg"},
)

config_setting(
    name = "debug",
    values = {"compilation_mode": "dbg"},
)

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

filegroup(
    name = "toolchain_deps",
    data = select({
        ":apple": ["@local_config_cc//:cc_wrapper"],
        "//conditions:default": [],
    }),
    visibility = ["//common:__pkg__"],
)

# Utilities.
genrule(
    name = "capture_cc",
    outs = ["capture_cc.env"],
    cmd = "printf \"BAZEL_CC=$(CC)\nBAZEL_CC_FLAGS=$(CC_FLAGS)\n\" > '$@'",
    toolchains = [
        "@bazel_tools//tools/cpp:cc_flags",
        "@bazel_tools//tools/cpp:current_cc_toolchain",
    ],
    visibility = ["//common:__pkg__"],
)

# Utility script to indicate which compiler is being used.
sh_binary(
    name = "print_host_settings",
    srcs = ["print_host_settings.sh"],
    args = ["$(location :capture_cc.env)"],
    data = [
        ":capture_cc.env",
        ":toolchain_deps",
    ],
)

# Test the above script; if CROSSTOOL is removed or changed, $(CC) may be an
# invalid binary (related to #7763), and CI should catch this.
# N.B. We have to use a proxy script since `print_host_settings` has `data`.
sh_test(
    name = "print_host_settings_test",
    srcs = ["test/print_host_settings_test.sh"],
    data = [":print_host_settings"],
    # Valgrind Memcheck reports numerous leaks in the python executable.
    tags = ["no_memcheck"],
)

add_lint_tests()
