Subject: [PATCH bazel-zig-cc v3 0/1] Use artifact_name_pattern to specify proper macos and windows artifact names

From: ~jvolkman <[email-masked]>
Date: Sun, 18 Dec 2022 00:18:23 +0000

Changelog:
# v2: should apply cleanly to the main branch
# v3: fix a linter error

I found that Bazel's cc toolchain system allows specifying artifact name
patterns. So we can make shared libraries end with ".dylib" on macos and
".dll" on Windows. This is likely a breaking change for anyone relying
on the defaults, but it seems like the proper thing to do, and the
built-in toolchains do the same thing.

There wasn't an obvious way to pass complex values from the
target_structs entries to the zig_cc_toolchain_config rule, so I opted
to pass a list of json-encoded strings. One other way I thought of was
to break the artifact_name_pattern parameters into
"artifact_name_prefixes" and "artifact_name_suffixes" dicts which would
fit into an attr.string_dict, but it was adding complexity for not much
gain IMO.

Jeremy Volkman (1):
  Use artifact_name_pattern to specify proper macos and windows artifact
    names.

 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(-)

-- 
2.34.5

---

From: ~jvolkman <[email-masked]>
Date: Sat, 17 Dec 2022 15:27:21 -0800

From: Jeremy Volkman <[email-masked]>

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 96830ca..3ff4cd0 100644
--- a/toolchain/private/defs.bzl
+++ b/toolchain/private/defs.bzl
@@ -67,6 +67,13 @@ def _target_darwin(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):
@@ -87,6 +94,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):
@@ -121,6 +145,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):
@@ -146,4 +171,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 f76aa80..bebd1da 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",
@@ -134,6 +135,11 @@ def _zig_cc_toolchain_config_impl(ctx):
         default_linker_flags,
     ] + _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,
@@ -150,6 +156,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(
@@ -168,6 +175,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],
 )
-- 
2.34.5

---

From: "builds.sr.ht" <[email-masked]>
Date: Sun, 18 Dec 2022 00:36:24 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 17m59s

[Use artifact_name_pattern to specify proper macos and windows artifact names][0] v3 from [~jvolkman][1]

[0]: https://lists.sr.ht/~motiejus/bazel-zig-cc/patches/37615
[1]: mailto:[email-masked]

✓ #907052 SUCCESS bazel-zig-cc/patches/.build.yml https://builds.sr.ht/~motiejus/job/907052

---

From: Motiejus Jakštys <[email-masked]>
Date: Sun, 18 Dec 2022 05:33:04 +0200

>  4 files changed, 39 insertions(+), 11 deletions(-)

Thanks! Applied all 4 patches verbatim.

Motiejus