load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//bazel/go:platform.bzl", "platform_binary")
load("//bazel/patchelf:patchelf.bzl", "patchelf")

go_library(
    name = "cmd_lib",
    srcs = ["main.go"],
    importpath = "github.com/edgelesssys/constellation/v2/disk-mapper/cmd",
    visibility = ["//visibility:private"],
    deps = [
        "//disk-mapper/internal/diskencryption",
        "//disk-mapper/internal/recoveryserver",
        "//disk-mapper/internal/rejoinclient",
        "//disk-mapper/internal/setup",
        "//internal/attestation/choose",
        "//internal/attestation/tdx",
        "//internal/attestation/variant",
        "//internal/attestation/vtpm",
        "//internal/cloud/aws",
        "//internal/cloud/azure",
        "//internal/cloud/cloudprovider",
        "//internal/cloud/gcp",
        "//internal/cloud/metadata",
        "//internal/cloud/openstack",
        "//internal/cloud/qemu",
        "//internal/constants",
        "//internal/grpc/dialer",
        "//internal/kms/setup",
        "//internal/logger",
        "//internal/role",
        "@com_github_spf13_afero//:afero",
        "@org_uber_go_zap//:zap",
    ],
)

go_binary(
    name = "cmd",
    embed = [":cmd_lib"],
    visibility = ["//visibility:public"],
)

platform_binary(
    name = "disk-mapper_linux_amd64",
    platform = "//bazel/platforms:constellation_os",
    target_file = ":cmd",
    visibility = ["//visibility:public"],
)

patchelf(
    name = "disk-mapper_patched",
    src = ":disk-mapper_linux_amd64",
    out = "disk-mapper_with_nix_rpath",
    interpreter = "@cryptsetup_x86_64-linux//:dynamic-linker",
    rpath = "@cryptsetup_x86_64-linux//:rpath",
)

pkg_tar(
    name = "disk-mapper-package",
    srcs = [
        ":disk-mapper_patched",
    ],
    mode = "0755",
    remap_paths = {"/disk-mapper_with_nix_rpath": "/usr/sbin/disk-mapper"},
    visibility = ["//visibility:public"],
)
