load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain")
load("@rules_cc//cc/toolchains:cc_toolchain_suite.bzl", "cc_toolchain_suite")
load("//build_defs:platforms.bzl", "PLATFORMS")
load(":cc_toolchain_config.bzl", "cc_toolchain_config")

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

filegroup(name = "empty")

bool_flag(
    name = "release",
    build_setting_default = False,
)

config_setting(
    name = "is_cross_compiling",
    flag_values = {":release": "True"},
)

[
    cc_toolchain(
        name = name + "-cc_toolchain",
        all_files = ":empty",
        compiler_files = ":empty",
        dwp_files = ":empty",
        dynamic_runtime_lib = ":empty",
        linker_files = ":empty",
        objcopy_files = ":empty",
        output_licenses = ["restricted"],
        static_runtime_lib = ":empty",
        strip_files = ":empty",
        toolchain_config = ":" + name + "-config",
        toolchain_identifier = name + "-cc_toolchain",
    )
    for name in PLATFORMS.keys()
]

[
    toolchain(
        name = name + "-toolchain",
        target_compatible_with = constraints,
        target_settings = [":is_cross_compiling"],
        toolchain = ":" + name + "-cc_toolchain",
        toolchain_type = "@rules_cc//cc:toolchain_type",
    )
    for name, constraints in PLATFORMS.items()
]

cc_toolchain_config(
    name = "k8-config",
    linker_path = "/usr/bin/ld",
    sysroot = "/opt/manylinux/2014/x86_64",
    target_cpu = "x86_64",
    target_full_name = "x86_64-linux-gnu",
)

cc_toolchain_config(
    name = "linux-aarch_64-config",
    linker_path = "/usr/bin/ld",
    sysroot = "/opt/manylinux/2014/aarch64",
    target_cpu = "aarch64",
    target_full_name = "aarch64-linux-gnu",
)

cc_toolchain_config(
    name = "linux-ppcle_64-config",
    linker_path = "/usr/bin/ld",
    sysroot = "/opt/manylinux/2014/ppc64le",
    target_cpu = "ppc64",
    target_full_name = "powerpc64le-linux-gnu",
)

cc_toolchain_config(
    name = "linux-s390_64-config",
    linker_path = "/usr/bin/ld",
    sysroot = "/opt/manylinux/2014/s390x",
    target_cpu = "systemz",
    target_full_name = "s390x-linux-gnu",
)

cc_toolchain_config(
    name = "linux-x86_32-config",
    linker_path = "/usr/bin/ld",
    sysroot = "/opt/manylinux/2014/i686",
    target_cpu = "x86_32",
    target_full_name = "i386-linux-gnu",
)

cc_toolchain_config(
    name = "linux-x86_64-config",
    linker_path = "/usr/bin/ld",
    sysroot = "/opt/manylinux/2014/x86_64",
    target_cpu = "x86_64",
    target_full_name = "x86_64-linux-gnu",
)

cc_toolchain_config(
    name = "osx-aarch_64-config",
    extra_compiler_flags = [
        "-isystem/usr/tools/xcode_14_0/macosx/usr/include/c++/v1",
        "-isystem/usr/tools/xcode_14_0/macosx/usr/include",
        "-iframework/usr/tools/xcode_14_0/macosx/System/Library/Frameworks",
        "-Wno-error=nullability-completeness",
        "-Wno-error=availability",
        "-Wno-error=elaborated-enum-base",
        "-Wno-error=undef",
        "-Wno-error=shadow",
        "-Wno-error=unknown-pragmas",
    ],
    extra_linker_flags = ["-framework CoreFoundation"],
    linker_path = "/usr/tools",
    sysroot = "/usr/tools/xcode_14_0/macosx",
    target_cpu = "aarch64",
    target_full_name = "aarch64-apple-macosx12.0",
)

cc_toolchain_config(
    name = "osx-x86_64-config",
    extra_compiler_flags = [
        "-isystem/usr/tools/xcode_14_0/macosx/usr/include/c++/v1",
        "-isystem/usr/tools/xcode_14_0/macosx/usr/include",
        "-iframework/usr/tools/xcode_14_0/macosx/System/Library/Frameworks",
        "-Wno-error=nullability-completeness",
        "-Wno-error=availability",
        "-Wno-error=elaborated-enum-base",
        "-Wno-error=undef",
        "-Wno-error=shadow",
        "-Wno-error=unknown-pragmas",
    ],
    extra_linker_flags = ["-framework CoreFoundation"],
    linker_path = "/usr/tools",
    sysroot = "/usr/tools/xcode_14_0/macosx",
    target_cpu = "x86_64",
    target_full_name = "x86_64-apple-macosx12.0",
)

cc_toolchain_config(
    name = "win32-config",
    extra_compiler_flags = [
        "-isystem/usr/lib/gcc/i686-w64-mingw32/10-posix/include/c++",
        "-isystem/usr/lib/gcc/i686-w64-mingw32/10-posix/include/c++/i686-w64-mingw32",
        "-Wno-error=undef",
        "-Wno-error=shadow",
        "-Wno-error=unknown-pragmas",
    ],
    extra_include = "/usr/lib/gcc/i686-w64-mingw32",
    extra_linker_flags = [
        "-L/usr/lib/gcc/i686-w64-mingw32/10-posix",
        "-ldbghelp",
        "-pthread",
    ],
    linker_path = "/usr/bin/ld",
    sysroot = "/usr/i686-w64-mingw32",
    target_cpu = "x86_32",
    target_full_name = "i686-w64-mingw32",
)

cc_toolchain_config(
    name = "win64-config",
    extra_compiler_flags = [
        "-isystem/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include/c++/",
        "-isystem/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include/c++/x86_64-w64-mingw32",
        "-Wno-error=undef",
        "-Wno-error=shadow",
        "-Wno-error=unknown-pragmas",
    ],
    extra_include = "/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include",
    extra_linker_flags = [
        "-L/usr/lib/gcc/x86_64-w64-mingw32/10-posix",
        "-ldbghelp",
    ],
    linker_path = "/usr/bin/ld",
    sysroot = "/usr/x86_64-w64-mingw32",
    target_cpu = "x86_64",
    target_full_name = "x86_64-w64-mingw32",
)
