1load(
2    "@nix_config//:config.bzl",
3    "CLANG",
4    "CLANG_LIB",
5    "CLANG_LIB_VERSION",
6    "LIBCLANG_RT_WASM32",
7    "LLVM",
8    "SDK_WASM32_WASI",
9)
10load("@rules_cc//cc:defs.bzl", "cc_toolchain")
11load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config")
12
13llvm_cc_toolchain_config(
14    name = "cc_toolchain_config",
15    archive_flags = [],
16    builtin_executable_objects = [],
17    builtin_include_directories = [
18        "{}/wasi-sysroot/include/c++/v1".format(SDK_WASM32_WASI),
19        "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION),
20        "{}/wasi-sysroot/include".format(SDK_WASM32_WASI),
21    ],
22    builtin_libraries = [
23        "c",
24        "c++",
25        "c++abi",
26        "m",
27    ],
28    builtin_library_directories = [
29        "{}/wasi-sysroot/lib/wasm32-wasi".format(SDK_WASM32_WASI),
30    ],
31    clang = CLANG,
32    compile_flags = [
33        "-fno-exceptions",
34        "-Werror",
35        "-Wall",
36        "-Wthread-safety",
37        "-Wself-assign",
38    ],
39    dbg_compile_flags = [],
40    dbg_link_flags = [],
41    fastbuild_compile_flags = [],
42    fastbuild_link_flags = [],
43    link_flags = [
44        "--fatal-warnings",
45        # `wasm-ld` doesn't respect the verbatim `-l:<name>` syntax.
46        "{}/lib/wasi/libclang_rt.builtins-wasm32.a".format(LIBCLANG_RT_WASM32),
47        "{}/wasi-sysroot/lib/wasm32-wasi/crt1.o".format(SDK_WASM32_WASI),
48    ],
49    llvm = LLVM,
50    opt_compile_flags = [],
51    opt_link_flags = [
52        "--gc-sections",
53    ],
54    supports_start_end_lib = False,
55    tags = ["manual"],
56    target = "wasm32-wasi",
57)
58
59cc_toolchain(
60    name = "cc_toolchain",
61    all_files = "@nix_config//:config.bzl",
62    ar_files = "@nix_config//:config.bzl",
63    as_files = "@nix_config//:config.bzl",
64    compiler_files = "@nix_config//:config.bzl",
65    coverage_files = "@nix_config//:config.bzl",
66    dwp_files = "@nix_config//:config.bzl",
67    linker_files = "@nix_config//:config.bzl",
68    objcopy_files = "@nix_config//:config.bzl",
69    strip_files = "@nix_config//:config.bzl",
70    tags = ["manual"],
71    toolchain_config = "cc_toolchain_config",
72)
73
74toolchain(
75    name = "toolchain",
76    exec_compatible_with = [
77        "@platforms//cpu:x86_64",
78        "@platforms//os:nixos",
79    ],
80    target_compatible_with = [
81        "@platforms//cpu:wasm32",
82        "@platforms//os:wasi",
83    ],
84    toolchain = ":cc_toolchain",
85    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
86)
87