load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//bazel/uv:defs.bzl", "dependency")
load(":separate_debug.bzl", "gdb_index_message")
load(":test_exec_properties_test.bzl", "test_exec_properties_test_suite")

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

# Expose script for external usage through bazel.
exports_files([
    "utils.bzl",
    "hermetic_container/hermetic_container.py",
    "hermetic_container/container_paths.py",
    "install_rules.py",
    "run_under_working_dir.sh",
])

py_library(
    name = "container_paths",
    srcs = ["hermetic_container/container_paths.py"],
)

py_test(
    name = "container_paths_test",
    srcs = ["hermetic_container/container_paths_test.py"],
    deps = [":container_paths"],
)

sh_binary(
    name = "lint",
    srcs = ["lint.sh"],
    visibility = ["//visibility:public"],
)

sh_binary(
    name = "gdb-add-index-noop",
    srcs = ["gdb_add_index_noop.sh"],
    visibility = ["//visibility:public"],
)

sh_binary(
    name = "gdb-generate-index-noop",
    srcs = ["gdb_add_index_noop.sh"],
    visibility = ["//visibility:public"],
)

sh_binary(
    name = "gdb-apply-index",
    srcs = ["gdb_apply_index.sh"],
    visibility = ["//visibility:public"],
)

gdb_index_message(
    name = "gdb-index-message",
    enabled = select({
        "//bazel/config:gdb_index_enabled_linux": True,
        "//conditions:default": False,
    }),
)

py_binary(
    name = "bazelisk",
    srcs = ["bazelisk.py"],
)

py_library(
    name = "bazelisk_lib",
    srcs = ["bazelisk.py"],
    imports = ["."],
)

py_binary(
    name = "merge_tidy_configs",
    srcs = [
        "merge_tidy_configs.py",
    ],
    main = "merge_tidy_configs.py",
    visibility = ["//visibility:public"],
    deps = [
        dependency(
            "pyyaml",
            group = "core",
        ),
    ],
)

py_binary(
    name = "gpg_signer",
    srcs = ["gpg_signer.py"],
    main = "gpg_signer.py",
    visibility = ["//visibility:public"],
)

py_test(
    name = "gpg_signer_test",
    srcs = ["gpg_signer_test.py"],
    data = ["gpg_signer.py"],
    main = "gpg_signer_test.py",
)

filegroup(
    name = "test_wrapper",
    srcs = ["test_wrapper.sh"],
)

# The default --run_under for bazel test on Windows (see .bazelrc). Bazel's test runner starts it
# from inside each test's runfiles tree and cannot start executables at paths of 256 characters or
# more once 8.3 short names are disabled, as on the CI hosts. The deepest test's runfiles directory
# is ~200 characters, so the outputs get short names (bazel/wtl.exe is 222 characters in it).
# The native entry passes an argument payload to wtl.ps1 without invoking cmd.exe.
copy_file(
    name = "windows_test_launcher_ps1",
    src = "//bazel/windows_test_launcher:windows_test_launcher.ps1",
    out = "wtl.ps1",
)

native_binary(
    name = "windows_test_launcher",
    src = "//bazel/windows_test_launcher:windows_test_launcher_native",
    out = "wtl.exe",
    data = [":windows_test_launcher_ps1"],
    target_compatible_with = ["@platforms//os:windows"],
)

# TODO(SERVER-129799): add these tests to CI.
test_exec_properties_test_suite(name = "test_exec_properties_tests")