From cc8f113489b1c82dd019e7fc8b10e3481f9de54e Mon Sep 17 00:00:00 2001 From: Jeremy Volkman Date: Sat, 17 Dec 2022 15:27:21 -0800 Subject: [PATCH] Use artifact_name_pattern to specify proper macos and windows artifact names. On macos, dynamic libraries are generated as "libfoo.dylib". On windows, executables end with ".exe", static libraries end with ".lib", and dynamic libraries end with ".dll". --- README.md | 11 ----------- toolchain/private/cc_toolchains.bzl | 5 +++++ toolchain/private/defs.bzl | 26 ++++++++++++++++++++++++++ toolchain/zig_toolchain.bzl | 8 ++++++++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c447ae8..3931247 100644 --- a/README.md +++ b/README.md @@ -338,17 +338,6 @@ is currently not implemented. target macos.10 (Catalina), macos.11 (Big Sur) or macos.12 (Monterey). It currently targets the lowest version, without ability to change it. -## Windows only: output file extensions - -For Windows targets Bazel uses Unix extensions for output binaries. Those may -need to be renamed before deploying to the Windows system. Here is a primer: - -| Binary type | Bazel extension | Windows extension | -|----------------|-----------------|-------------------| -| Static library | .a | .lib | -| Shared library | .so | .dll | -| Executable | (no extension) | .exe | - # Known Issues In Upstream This section lists issues that I've stumbled into when using `zig cc`, and is diff --git a/toolchain/private/cc_toolchains.bzl b/toolchain/private/cc_toolchains.bzl index 3fa0b93..a935ef5 100644 --- a/toolchain/private/cc_toolchains.bzl +++ b/toolchain/private/cc_toolchains.bzl @@ -39,6 +39,10 @@ def declare_cc_toolchains(os, zig_sdk_path): for incl in getattr(target_config, "compiler_extra_includes", []): copts = copts + ["-include", zig_sdk_path + "/" + incl] + # We can't pass a list of structs to a rule, so we use json encoding. + artifact_name_patterns = getattr(target_config, "artifact_name_patterns", []) + artifact_name_pattern_strings = [json.encode(p) for p in artifact_name_patterns] + zig_cc_toolchain_config( name = zigtarget + "_cc_config", target = zigtarget, @@ -53,6 +57,7 @@ def declare_cc_toolchains(os, zig_sdk_path): compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", + artifact_name_patterns = artifact_name_pattern_strings, visibility = ["//visibility:private"], ) diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl index 8571621..9409f5e 100644 --- a/toolchain/private/defs.bzl +++ b/toolchain/private/defs.bzl @@ -72,6 +72,13 @@ def _target_macos(gocpu, zigcpu): "@platforms//cpu:{}".format(zigcpu), ], tool_paths = {"ld": "ld64.lld"}, + artifact_name_patterns = [ + { + "category_name": "dynamic_library", + "prefix": "lib", + "extension": ".dylib", + }, + ], ) def _target_windows(gocpu, zigcpu): @@ -92,6 +99,23 @@ def _target_windows(gocpu, zigcpu): "@platforms//cpu:{}".format(zigcpu), ], tool_paths = {"ld": "ld64.lld"}, + artifact_name_patterns = [ + { + "category_name": "static_library", + "prefix": "", + "extension": ".lib", + }, + { + "category_name": "dynamic_library", + "prefix": "", + "extension": ".dll", + }, + { + "category_name": "executable", + "prefix": "", + "extension": ".exe", + }, + ], ) def _target_linux_gnu(gocpu, zigcpu, glibc_version): @@ -126,6 +150,7 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version): ], libc_constraint = "@zig_sdk//libc:{}".format(glibc_suffix), tool_paths = {"ld": "ld.lld"}, + artifact_name_patterns = [], ) def _target_linux_musl(gocpu, zigcpu): @@ -151,4 +176,5 @@ def _target_linux_musl(gocpu, zigcpu): ], libc_constraint = "@zig_sdk//libc:musl", tool_paths = {"ld": "ld.lld"}, + artifact_name_patterns = [], ) diff --git a/toolchain/zig_toolchain.bzl b/toolchain/zig_toolchain.bzl index 2578973..6ec3396 100644 --- a/toolchain/zig_toolchain.bzl +++ b/toolchain/zig_toolchain.bzl @@ -1,6 +1,7 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load( "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "artifact_name_pattern", "feature", "feature_set", "flag_group", @@ -140,6 +141,11 @@ def _zig_cc_toolchain_config_impl(ctx): supports_dynamic_linker, ] + _compilation_mode_features(ctx) + artifact_name_patterns = [ + artifact_name_pattern(**json.decode(p)) + for p in ctx.attr.artifact_name_patterns + ] + return cc_common.create_cc_toolchain_config_info( ctx = ctx, features = features, @@ -156,6 +162,7 @@ def _zig_cc_toolchain_config_impl(ctx): for name, path in ctx.attr.tool_paths.items() ], cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories, + artifact_name_patterns = artifact_name_patterns, ) zig_cc_toolchain_config = rule( @@ -174,6 +181,7 @@ zig_cc_toolchain_config = rule( "compiler": attr.string(), "abi_version": attr.string(), "abi_libc_version": attr.string(), + "artifact_name_patterns": attr.string_list(), }, provides = [CcToolchainConfigInfo], )