Subject: [PATCH bazel-zig-cc v2] add go_binary override

From: ~mjonaitis <[email-masked]>
Date: Thu, 04 Nov 2021 18:54:42 +0200

From: Mantas Jonaitis <[email-masked]>

---
 BUILD                        |  2 ++
 README.md                    |  7 +++++++
 rules/BUILD                  |  0
 rules/go_binary_override.bzl | 26 ++++++++++++++++++++++++++
 test/BUILD                   | 15 ++++-----------
 5 files changed, 39 insertions(+), 11 deletions(-)
 create mode 100644 rules/BUILD
 create mode 100644 rules/go_binary_override.bzl

diff --git a/BUILD b/BUILD
index 07e6a04..a34acae 100644
--- a/BUILD
+++ b/BUILD
@@ -1,6 +1,8 @@
 load("@bazel_gazelle//:def.bzl", "gazelle")
 load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
 
+# gazelle:map_kind go_binary go_binary //rules:go_binary_override.bzl
+
 # gazelle:build_file_name BUILD
 # gazelle:prefix github.com/motiejus/bazel-zig-cc
 gazelle(name = "gazelle")
diff --git a/README.md b/README.md
index 8ae3935..7c83752 100644
--- a/README.md
+++ b/README.md
@@ -116,6 +116,13 @@ go_binary(
 )
 ```
 
+A workaround with [gazelle](https://github.com/bazelbuild/bazel-gazelle) `map_kind` directive is possible. This will override rules_go `go_binary` rule with `go_binary` which contains default `gc_linkopts` parameters. Add this line to your BUILD files (usually adding to root BUILD file will suffice) and run `gazelle` to automatically generate `load` statements.
+
+```
+# gazelle:map_kind go_binary go_binary @bazel-zig-cc//rules:go_binary_override.bzl
+```
+
+
 ## incorrect glibc version autodetection
 
 **Severity: Low**
diff --git a/rules/BUILD b/rules/BUILD
new file mode 100644
index 0000000..e69de29
diff --git a/rules/go_binary_override.bzl b/rules/go_binary_override.bzl
new file mode 100644
index 0000000..cb37807
--- /dev/null
+++ b/rules/go_binary_override.bzl
@@ -0,0 +1,26 @@
+load("@io_bazel_rules_go//go:def.bzl", go_binary_rule = "go_binary")
+
+"""
+go_binary overrides go_binary from rules_go and provides default
+gc_linkopts values that are needed to compile for macos target.
+To use it, add this map_kind gazelle directive to your BUILD.bazel files
+where target binary needs to be compiled with zig toolchain.
+
+Example: if this toolchain is registered as bazel-zig-cc in your WORKSPACE, add this to
+your root BUILD file
+# gazelle:map_kind go_binary go_binary @bazel-zig-cc//rules:go_binary_override.bzl
+"""
+def go_binary(**args):
+  new_args = {}
+  new_args["gc_linkopts"] = select({
+      "@platforms//os:macos": [
+          "-s",
+          "-w",
+          "-buildmode=pie",
+      ],
+      "//conditions:default": [],
+  })
+  for k, v in args.items():
+    new_args[k] = v
+  go_binary_rule(**new_args)
+
diff --git a/test/BUILD b/test/BUILD
index 133fe5a..415b9c2 100644
--- a/test/BUILD
+++ b/test/BUILD
@@ -1,4 +1,5 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+load("//rules:go_binary_override.bzl", "go_binary")
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
 
 go_library(
     name = "hello_lib",
@@ -7,22 +8,14 @@ go_library(
     importpath = "github.com/motiejus/bazel-zig-cc/test",
     visibility = ["//visibility:private"],
     deps = [
-        "@com_github_datadog_zstd//:go_default_library",
-        "@com_github_mattn_go_sqlite3//:go_default_library",
+        "@com_github_datadog_zstd//:zstd",
+        "@com_github_mattn_go_sqlite3//:go-sqlite3",
     ],
 )
 
 go_binary(
     name = "hello",
     embed = [":hello_lib"],
-    gc_linkopts = select({
-        "@platforms//os:macos": [
-            "-s",
-            "-w",
-            "-buildmode=pie",
-        ],
-        "//conditions:default": [],
-    }),
     static = "on",
     visibility = ["//visibility:public"],
 )
-- 
2.32.0

---

From: Motiejus Jakštys <[email-masked]>
Date: Fri, 5 Nov 2021 19:51:54 +0200

>  create mode 100644 rules/go_binary_override.bzl

Applied[1], thank you. Although I left only the acknowledgement in the
README; see [2] for why.

Motiejus

[1]: https://git.sr.ht/~motiejus/bazel-zig-cc/commit/523943bd35d33f96f946be653311787426149135
[2]: https://git.sr.ht/~motiejus/bazel-zig-cc/commit/22b5d3e8e38e762a35adbfd4e13bfe5c2fb07972