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"], )