commit 2195fb6941861ea9e2d8baad10f1f851ff9dfeba Author: Adam Bouhenguel Date: Fri Apr 9 15:05:01 2021 -0700 Initial commit with minimal examples diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..4d17dd2 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,15 @@ +build --symlink_prefix=dist/ +test --test_output=errors +test:debug --test_output=streamed --test_strategy=exclusive --test_timeout=9999 --nocache_test_results --define=VERBOSE_LOGS=1 +run:debug --define=VERBOSE_LOGS=1 -- --node_options=--inspect-brk +build:debug --compilation_mode=dbg +build --nolegacy_external_runfiles +build --incompatible_strict_action_env +run --incompatible_strict_action_env +try-import %workspace%/.bazelrc.user +build --worker_sandboxing +build --incompatible_enable_cc_toolchain_resolution +build --incompatible_use_cc_configure_from_rules_cc +build --incompatible_use_platforms_repo_for_constraints + +build --javabase=@bazel_tools//tools/jdk:remote_jdk11 diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..fcdb2e1 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +4.0.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95983ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bazel-out +dist diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..64b7184 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +To run the example locally, first make sure `bazelisk` is on your PATH. Then + +Test that things work at all: + +``` +$ bazelisk run //test:hello +... +Hello World! +``` + +Then test that you can run docker images: +``` +$ bazelisk run //test:hello_image +... +Hello World! +``` + +Now we can check if C++ exceptions work locally: +``` +$ bazelisk run //test:exception +... +will throw and expect to catch an error... +caught: error +done +``` + +And whether or not they work in a docker container: +``` +$ bazelisk run //test:exception_image +``` + +If they *work*, then you'll see the same output as above. If not, you'll see: + +``` +will throw and expect to catch an error... +libc++abi: terminating with uncaught exception of type char const* +``` diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel new file mode 100644 index 0000000..7498f26 --- /dev/null +++ b/WORKSPACE.bazel @@ -0,0 +1,103 @@ +workspace( + name = "zig-cc-bazel-exceptions", +) + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + + +rules_python_version = "812c48356881b34f3df68301b839a879c4d5ddf9" +http_archive( + name = "rules_python", + sha256 = "f8a50280b9034b3933c4a41c61d0cf2309d473e3da39d92b38dac37d54bd4f82", + strip_prefix = "rules_python-{}".format(rules_python_version), + url = "https://github.com/bazelbuild/rules_python/archive/{}.zip".format(rules_python_version), +) + +http_archive( + name = "io_bazel_rules_go", + sha256 = "7c10271940c6bce577d51a075ae77728964db285dac0a46614a7934dc34303e6", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.26.0/rules_go-v0.26.0.tar.gz", + "https://github.com/bazelbuild/rules_go/releases/download/v0.26.0/rules_go-v0.26.0.tar.gz", + ], +) + +http_archive( + name = "bazel_gazelle", + sha256 = "62ca106be173579c0a167deb23358fdfe71ffa1e4cfdddf5582af26520f1c66f", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz", + ], +) + +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") + +go_rules_dependencies() + +go_register_toolchains(version = "1.16") + +gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel") + +http_archive( + name = "com_google_protobuf", + sha256 = "bf0e5070b4b99240183b29df78155eee335885e53a8af8683964579c214ad301", + strip_prefix = "protobuf-3.14.0", + urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.14.0.zip"], +) + +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") +protobuf_deps() + +http_archive( + name = "com_github_bazelbuild_buildtools", + strip_prefix = "buildtools-master", + url = "https://github.com/bazelbuild/buildtools/archive/master.zip", +) + +http_archive( + name = "rules_pkg", + sha256 = "6b5969a7acd7b60c02f816773b06fcf32fbe8ba0c7919ccdc2df4f8fb923804a", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz", + "https://github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz", + ], +) + +load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") +rules_pkg_dependencies() + + +http_archive( + name = "io_bazel_rules_docker", + patches = ["//:patches/rules_docker/transitions.patch"], + patch_args = ["-p1"], + sha256 = "95d39fd84ff4474babaf190450ee034d958202043e366b9fc38f438c9e6c3334", + strip_prefix = "rules_docker-0.16.0", + urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.16.0/rules_docker-v0.16.0.tar.gz"], +) + +load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories") +container_repositories() + + +load("@io_bazel_rules_docker//cc:image.bzl", _cc_image_repos = "repositories") +_cc_image_repos() + +load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps") +container_deps() + +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") +git_repository( + name = "com_github_nelhage_rules_boost", + commit = "c13a880269cc044c4b5e90046625339836771d77", + remote = "https://github.com/nelhage/rules_boost", + shallow_since = "1591047380 -0700", +) +load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps") +boost_deps() + +load("//zig-toolchains:defs.bzl", zig_register_all_toolchains = "register_all_toolchains", zig_toolchain_repositories = "toolchain_repositories") +zig_toolchain_repositories() +zig_register_all_toolchains() diff --git a/patches/rules_docker/transitions.patch b/patches/rules_docker/transitions.patch new file mode 100644 index 0000000..eff5b67 --- /dev/null +++ b/patches/rules_docker/transitions.patch @@ -0,0 +1,149 @@ +diff --git a/lang/image.bzl b/lang/image.bzl +index c2db8f4..16a1f57 100644 +--- a/lang/image.bzl ++++ b/lang/image.bzl +@@ -34,8 +34,8 @@ def _binary_name(ctx): + # /app/foo/bar/baz/blah + return "/".join([ + ctx.attr.directory, +- ctx.attr.binary.label.package, +- ctx.attr.binary.label.name, ++ ctx.attr.binary[0].label.package, ++ ctx.attr.binary[0].label.name, + ]) + + def _runfiles_dir(ctx): +@@ -148,7 +148,7 @@ def _app_layer_impl(ctx, runfiles = None, emptyfiles = None): + parent_parts = _get_layers(ctx, ctx.attr.name, ctx.attr.base) + filepath = _final_file_path if ctx.attr.binary else layer_file_path + emptyfilepath = _final_emptyfile_path if ctx.attr.binary else _layer_emptyfile_path +- dep = ctx.attr.dep or ctx.attr.binary ++ dep = (ctx.attr.dep or ctx.attr.binary)[0] + top_layer = ctx.attr.binary and not ctx.attr.dep + + if ctx.attr.create_empty_workspace_dir: +@@ -239,6 +239,18 @@ def _app_layer_impl(ctx, runfiles = None, emptyfiles = None): + null_cmd = args == [], + ) + ++def _container_transition_impl(settings, attr): ++ _ignore = (settings, attr) ++ return { ++ "//command_line_option:platforms": "@io_bazel_rules_docker//platforms:linux_amd64", ++ } ++ ++container_transition = transition( ++ implementation = _container_transition_impl, ++ inputs = [], ++ outputs = ["//command_line_option:platforms"], ++) ++ + image = struct( + attrs = dicts.add(_container.image.attrs, { + # The base image on which to overlay the dependency layers. +@@ -250,7 +262,7 @@ image = struct( + # the runfiles dir. + "binary": attr.label( + executable = True, +- cfg = "target", ++ cfg = container_transition, + ), + # Set this to true to create an empty workspace directory under the + # app directory specified as the 'directory' attribute. +@@ -263,11 +275,14 @@ image = struct( + # The dependency whose runfiles we're appending. + # If not specified, then the layer will be treated as the top layer, + # and all remaining deps of "binary" will be added under runfiles. +- "dep": attr.label(), ++ "dep": attr.label(cfg = container_transition), + "directory": attr.string(default = "/app"), + "entrypoint": attr.string_list(default = []), + "legacy_run_behavior": attr.bool(default = False), + "workdir": attr.string(default = ""), ++ "_whitelist_function_transition": attr.label( ++ default = "//tools/whitelists/function_transition_whitelist", ++ ), + }), + outputs = _container.image.outputs, + toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"], +diff --git a/platforms/BUILD b/platforms/BUILD +index 50b8593..b4f25c9 100644 +--- a/platforms/BUILD ++++ b/platforms/BUILD +@@ -56,3 +56,11 @@ platform( + ], + parents = ["@buildkite_config//config:platform"], + ) ++ ++platform( ++ name = "linux_amd64", ++ constraint_values = [ ++ "@platforms//os:linux", ++ "@platforms//cpu:x86_64", ++ ], ++) +diff --git a/toolchains/BUILD b/toolchains/BUILD +index 55f7560..bf56cd0 100644 +--- a/toolchains/BUILD ++++ b/toolchains/BUILD +@@ -56,13 +56,13 @@ toolchain( + name = "rbe_container_cc_toolchain", + exec_compatible_with = [ + "@io_bazel_rules_docker//platforms:run_in_container", +- "@bazel_tools//platforms:x86_64", +- "@bazel_tools//platforms:linux", ++ "@platforms//cpu:x86_64", ++ "@platforms//os:linux", + "@bazel_tools//tools/cpp:clang", + ], + target_compatible_with = [ +- "@bazel_tools//platforms:linux", +- "@bazel_tools//platforms:x86_64", ++ "@platforms//os:linux", ++ "@platforms//cpu:x86_64", + ], + toolchain = "@buildkite_config//cc:cc-compiler-k8", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +diff --git a/toolchains/docker/BUILD b/toolchains/docker/BUILD +index 42e83e1..b7c9c5f 100644 +--- a/toolchains/docker/BUILD ++++ b/toolchains/docker/BUILD +@@ -42,7 +42,7 @@ docker_toolchain( + toolchain( + name = "default_linux_toolchain", + target_compatible_with = [ +- "@bazel_tools//platforms:linux", ++ "@platforms//os:linux", + ], + toolchain = "@docker_config//:toolchain", + toolchain_type = ":toolchain_type", +@@ -51,7 +51,7 @@ toolchain( + toolchain( + name = "default_windows_toolchain", + target_compatible_with = [ +- "@bazel_tools//platforms:windows", ++ "@platforms//os:windows", + ], + toolchain = "@docker_config//:toolchain", + toolchain_type = ":toolchain_type", +@@ -60,7 +60,7 @@ toolchain( + toolchain( + name = "default_osx_toolchain", + target_compatible_with = [ +- "@bazel_tools//platforms:osx", ++ "@platforms//os:osx", + ], + toolchain = "@docker_config//:toolchain", + toolchain_type = ":toolchain_type", +diff --git a/tools/whitelists/function_transition_whitelist/BUILD b/tools/whitelists/function_transition_whitelist/BUILD +new file mode 100644 +index 0000000..640d44f +--- /dev/null ++++ b/tools/whitelists/function_transition_whitelist/BUILD +@@ -0,0 +1,6 @@ ++package_group( ++ name = "function_transition_whitelist", ++ packages = [ ++ "//lang/...", ++ ], ++) diff --git a/test/BUILD.bazel b/test/BUILD.bazel new file mode 100644 index 0000000..85db9eb --- /dev/null +++ b/test/BUILD.bazel @@ -0,0 +1,35 @@ +load("@io_bazel_rules_docker//cc:image.bzl", "cc_image") + +cc_binary( + name = "hello", + srcs = ["hello.cpp"], +) + +cc_image( + name = "hello_image", + binary = ":hello", +) + +cc_binary( + name = "boost_exception", + srcs = ["boost_exception.cpp"], + copts = ["-fexceptions", "-frtti"], + deps = [ + "@boost//:lexical_cast", + ], +) + +cc_image( + name = "boost_exception_image", + binary = ":boost_exception", +) + +cc_binary( + name = "exception", + srcs = ["exception.cpp"], +) + +cc_image( + name = "exception_image", + binary = ":exception", +) diff --git a/test/boost_exception.cpp b/test/boost_exception.cpp new file mode 100644 index 0000000..eed9d8b --- /dev/null +++ b/test/boost_exception.cpp @@ -0,0 +1,20 @@ +#include + +#include + +int main() { + std::cout << "about to cast \"1\" to double!" << std::endl; + std::cout << boost::lexical_cast("1") << std::endl; + + std::cout << "about to cast \"z\" to double, but expecting to catch bad_lexical_cast" << std::endl; + try { + std::cout << boost::lexical_cast("z"); + std::cout << "uh-oh, should have thrown an exception before here." << std::endl; + } catch (const boost::bad_lexical_cast &e) { + std::cout << "caught bad_lexical_cast" << std::endl; + } + + std::cout << "about to cast \"z\" to double, should see an uncaught exception." << std::endl; + std::cout << boost::lexical_cast("z"); + std::cout << "uh-oh, should have thrown an exception before here." << std::endl; +} diff --git a/test/exception.cpp b/test/exception.cpp new file mode 100644 index 0000000..7be1ee2 --- /dev/null +++ b/test/exception.cpp @@ -0,0 +1,12 @@ +#include + +int main() { + std::cerr << "will throw and expect to catch an error..." << std::endl; + + try { + throw "error"; + } catch (const char* msg) { + std::cerr << "caught: " << msg << std::endl; + } + std::cerr << "done" << std::endl; +} diff --git a/test/hello.cpp b/test/hello.cpp new file mode 100644 index 0000000..ccbda56 --- /dev/null +++ b/test/hello.cpp @@ -0,0 +1,5 @@ +#include + +int main() { + std::cout << "Hello World!" << std::endl; +} \ No newline at end of file diff --git a/zig-toolchains/BUILD.bazel b/zig-toolchains/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/zig-toolchains/defs.bzl b/zig-toolchains/defs.bzl new file mode 100644 index 0000000..df2812d --- /dev/null +++ b/zig-toolchains/defs.bzl @@ -0,0 +1,266 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load(":zig_toolchain.bzl", "zig_cc_toolchain_config") + +DEFAULT_TOOL_PATHS = { + "ar": "llvm-ar", # TODO this should be "build-lib", see https://github.com/ziglang/zig/issues/7915 + "cpp": "c++", + "gcc": "cc", + + # TODO See https://github.com/ziglang/zig/issues/7917 for zig issue to implement these other tools. + "gcov": "/usr/bin/false", + "nm": "/usr/bin/false", + "objdump": "/usr/bin/false", + "strip": "/usr/bin/false", +}.items() + +DEFAULT_INCLUDE_DIRECTORIES = [ + "lib/zig/include", + # "lib/zig/libcxx/include", + # "lib/zig/libcxxabi/include", + # "lib/zig/libunwind/include", +] + +# https://github.com/ziglang/zig/blob/0cfa39304b18c6a04689bd789f5dc4d035ec43b0/src/main.zig#L2962-L2966 +TARGET_CONFIGS = [ + struct( + target="x86_64-linux-gnu.2.28", + includes=[ + "lib/zig/libcxx/include", + "lib/zig/libcxxabi/include", + "lib/zig/libunwind/include", + "lib/zig/libc/include/generic-glibc", + "lib/zig/libc/include/any-linux-any", + "lib/zig/libc/include/x86_64-linux-gnu", + "lib/zig/libc/include/x86_64-linux-any", + ], + linkopts=["-lc++", "-lc++abi"], + copts=[], + constraint_values=["@platforms//os:linux", "@platforms//cpu:x86_64"], + tool_paths={"ld": "ld.lld"}, + ), + # struct( + # target="x86_64-linux-musl", + # includes=[ + # "lib/zig/libcxx/include", + # "lib/zig/libcxxabi/include", + # "lib/zig/libc/include/generic-musl", + # "lib/zig/libc/include/any-linux-any", + # "lib/zig/libc/include/x86_64-linux-musl", + # "lib/zig/libc/include/x86_64-linux-any", + # ], + # linkopts=[], + # # linkopts=["-lc++", "-lc++abi"], + # copts=["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"], + # constraint_values=["@platforms//os:linux", "@platforms//cpu:x86_64"], + # tool_paths={"ld": "ld.lld"}, + # ), +] + +ZIG_TOOL_PATH = "tools/{zig_tool}" +ZIG_TOOL_WRAPPER = """#!/bin/bash +export HOME=$TMPDIR +exec "{zig}" "{zig_tool}" "$@" +""" + +ZIG_TOOLS = [ + "c++", + "cc", + "build-lib", # https://github.com/ziglang/zig/issues/7915 + # List of ld tools: https://github.com/ziglang/zig/blob/0cfa39304b18c6a04689bd789f5dc4d035ec43b0/src/main.zig#L2962-L2966 + # and also: https://github.com/ziglang/zig/issues/3257 + "ld.lld", # ELF + "ld64.lld", # Mach-O + "lld-link", # COFF + "wasm-ld", # WebAssembly +] + +TOOLS = ZIG_TOOLS + [ + "llvm-ar", +] + +BUILD = """ +load("@zig-cc-bazel-exceptions//zig-toolchains:defs.bzl", "zig_build_macro") +load("@{llvm_tools_repo}//:noop.bzl", "noop") +noop() +package(default_visibility = ["//visibility:public"]) +zig_build_macro(absolute_path={absolute_path}) +""" + +def toolchain_repositories(): + # We need llvm-ar for now, so get it. + llvm_patch_cmds = [ + "mv bin/llvm-ar .", + "rm -r include lib libexec share bin", + "echo 'def noop(): pass' > noop.bzl", + ] + + llvm_build_file_content = """ +package(default_visibility = ["//visibility:public"]) +exports_files(glob["**"]) +""" + http_archive( + name = "com_github_ziglang_zig_llvm_tools_macos_x86_64", + # sha256 = "", + patch_cmds = llvm_patch_cmds, + build_file_content = llvm_build_file_content, + strip_prefix = "clang+llvm-11.0.0-x86_64-apple-darwin", + urls = [ + "https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/clang+llvm-11.0.0-x86_64-apple-darwin.tar.xz", + ], + ) + + http_archive( + name = "com_github_ziglang_zig_llvm_tools_linux_x86_64", + # sha256 = "", + patch_cmds = llvm_patch_cmds, + build_file_content = llvm_build_file_content, + strip_prefix = "clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-20.04", + urls = [ + "https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz", + ], + ) + + zig_repository( + name = "com_github_ziglang_zig", + version = "0.7.1", + sha256 = "845cb17562978af0cf67e3993f4e33330525eaf01ead9386df9105111e3bc519", + ) + +def register_all_toolchains(): + for target_config in TARGET_CONFIGS: + native.register_toolchains( + "@com_github_ziglang_zig//:%s_toolchain" % target_config.target, + ) + +def _zig_repository_impl(repository_ctx): + if repository_ctx.os.name.lower().startswith("mac os"): + llvm_tools_repo = "com_github_ziglang_zig_llvm_tools_macos_x86_64" + host_platform = "macos-x86_64" + else: + host_platform = "linux-x86_64" + llvm_tools_repo = "com_github_ziglang_zig_llvm_tools_linux_x86_64" + + format_vars = { + "version" : repository_ctx.attr.version, + "host_platform" : host_platform, + } + + repository_ctx.download_and_extract( + url = "https://ziglang.org/download/{version}/zig-{host_platform}-{version}.tar.xz".format(**format_vars), + stripPrefix = "zig-{host_platform}-{version}/".format(**format_vars), + sha256 = repository_ctx.attr.sha256, + ) + + # TODO Use llvm-ar for host platform until we have https://github.com/ziglang/zig/issues/7915 + llvm_tools_dir = str(repository_ctx.path("")) + "/../" + llvm_tools_repo + "/llvm-ar" + repository_ctx.symlink(llvm_tools_dir, ZIG_TOOL_PATH.format(zig_tool="llvm-ar")) + + for zig_tool in ZIG_TOOLS: + repository_ctx.file( + ZIG_TOOL_PATH.format(zig_tool=zig_tool), + ZIG_TOOL_WRAPPER.format(zig=str(repository_ctx.path("zig")), zig_tool=zig_tool), + ) + + absolute_path = json.encode(str(repository_ctx.path(""))) + repository_ctx.file( + "BUILD", + BUILD.format(absolute_path=absolute_path, llvm_tools_repo=llvm_tools_repo), + ) + +zig_repository = repository_rule( + attrs = { + "url": attr.string(), + "version": attr.string(), + "sha256": attr.string(), + }, + implementation = _zig_repository_impl, +) + +def filegroup(name, **kwargs): + native.filegroup(name = name, **kwargs) + return ":" + name + +def zig_build_macro(absolute_path): + filegroup(name="empty") + filegroup(name="zig_compiler", srcs=["zig"]) + filegroup(name="lib/std", srcs=native.glob(["lib/std/**"])) + + lazy_filegroups = {} + + for target_config in TARGET_CONFIGS: + target = target_config.target + native.platform(name = target, constraint_values = target_config.constraint_values) + + all_srcs = [] + ar_srcs = [":zig_compiler"] + linker_srcs = [":zig_compiler"] + compiler_srcs = [":zig_compiler"] + tool_srcs = {"gcc": compiler_srcs, "cpp": compiler_srcs, "ld": linker_srcs, "ar": ar_srcs} + + cxx_builtin_include_directories = [] + for d in DEFAULT_INCLUDE_DIRECTORIES + target_config.includes: + if d not in lazy_filegroups: + lazy_filegroups[d] = filegroup(name=d, srcs=native.glob([d + "/**"])) + compiler_srcs.append(lazy_filegroups[d]) + cxx_builtin_include_directories.append(absolute_path + "/" + d) + + absolute_tool_paths = {} + for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS: + if path[0] == "/": + absolute_tool_paths[name] = path + continue + tool_path = ZIG_TOOL_PATH.format(zig_tool=path) + absolute_tool_paths[name] = "%s/%s" % (absolute_path, tool_path) + tool_srcs[name].append(tool_path) + + ar_files = filegroup(name=target + "_ar_files", srcs=ar_srcs) + linker_files = filegroup(name=target + "_linker_files", srcs=linker_srcs) + compiler_files = filegroup(name=target + "_compiler_files", srcs=compiler_srcs) + all_files = filegroup(name=target + "_all_files", srcs=all_srcs + [ar_files, linker_files, compiler_files]) + + zig_cc_toolchain_config( + name = target + "_cc_toolchain_config", + target = target, + tool_paths = absolute_tool_paths, + cxx_builtin_include_directories = cxx_builtin_include_directories, + copts = target_config.copts, + linkopts = target_config.linkopts, + target_system_name = "unknown", + # TODO don't hardcode this to k8 + target_cpu = "k8", + target_libc = "unknown", + compiler = "clang", + abi_version = "unknown", + abi_libc_version = "unknown", + ) + + native.cc_toolchain( + name = target + "_cc_toolchain", + toolchain_identifier = target + "-toolchain", + toolchain_config = ":%s_cc_toolchain_config" % target, + all_files = all_files, + ar_files = ar_files, + compiler_files = compiler_files, + linker_files = linker_files, + dwp_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + ) + + native.cc_toolchain_suite( + name = target + "_cc_toolchain_suite", + toolchains = { + # TODO don't hardcode this to k8 + "k8": ":%s_cc_toolchain" % target, + }, + tags = ["manual"] + ) + + native.toolchain( + name = target + "_toolchain", + exec_compatible_with = None, + target_compatible_with = target_config.constraint_values, + toolchain = ":%s_cc_toolchain" % target, + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", + ) diff --git a/zig-toolchains/zig_toolchain.bzl b/zig-toolchains/zig_toolchain.bzl new file mode 100644 index 0000000..66f38e1 --- /dev/null +++ b/zig-toolchains/zig_toolchain.bzl @@ -0,0 +1,112 @@ +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "feature", + "flag_group", + "flag_set", + "tool_path", + "tool", +) + +all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +all_compile_actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.cc_flags_make_variable, + ACTION_NAMES.clif_match, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.lto_backend, + ACTION_NAMES.preprocess_assemble, +] + +def _zig_cc_toolchain_config_impl(ctx): + default_compiler_flags = feature( + name = "default_compiler_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-I" + d + for d in ctx.attr.cxx_builtin_include_directories + ] + [ + "-target", ctx.attr.target, + "-no-canonical-prefixes", + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ] + ctx.attr.copts, + ), + ], + ), + ], + ) + + default_linker_flags = feature( + name = "default_linker_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = ([ + flag_group( + flags = [ + "-target", ctx.attr.target, + ] + ctx.attr.linkopts, + ), + ]), + ), + ], + ) + + features = [ + default_compiler_flags, + default_linker_flags, + ] + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + toolchain_identifier = "%s-toolchain" % ctx.attr.target, + host_system_name = "local", + target_system_name = ctx.attr.target_system_name, + target_cpu = ctx.attr.target_cpu, + target_libc = ctx.attr.target_libc, + compiler = ctx.attr.compiler, + abi_version = ctx.attr.abi_version, + abi_libc_version = ctx.attr.abi_libc_version, + tool_paths = [ + tool_path(name=name, path=path) + for name, path in ctx.attr.tool_paths.items() + ], + cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories, + ) + +zig_cc_toolchain_config = rule( + implementation = _zig_cc_toolchain_config_impl, + attrs = { + "cxx_builtin_include_directories": attr.string_list(), + "linkopts": attr.string_list(), + "copts": attr.string_list(), + "tool_paths": attr.string_dict(), + "target": attr.string(), + "target_system_name": attr.string(), + "target_cpu": attr.string(), + "target_libc": attr.string(), + "compiler": attr.string(), + "abi_version": attr.string(), + "abi_libc_version": attr.string(), + }, + provides = [CcToolchainConfigInfo], +)