1
This commit is contained in:
Mantas Jonaitis 2021-11-05 19:29:07 +02:00 committed by Motiejus Jakštys
parent 47d91cbbed
commit 523943bd35
5 changed files with 39 additions and 11 deletions

2
BUILD
View File

@ -1,6 +1,8 @@
load("@bazel_gazelle//:def.bzl", "gazelle") load("@bazel_gazelle//:def.bzl", "gazelle")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier") 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:build_file_name BUILD
# gazelle:prefix github.com/motiejus/bazel-zig-cc # gazelle:prefix github.com/motiejus/bazel-zig-cc
gazelle(name = "gazelle") gazelle(name = "gazelle")

View File

@ -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 ## incorrect glibc version autodetection
**Severity: Low** **Severity: Low**

0
rules/BUILD Normal file
View File

View File

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

View File

@ -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( go_library(
name = "hello_lib", name = "hello_lib",
@ -7,22 +8,14 @@ go_library(
importpath = "github.com/motiejus/bazel-zig-cc/test", importpath = "github.com/motiejus/bazel-zig-cc/test",
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
"@com_github_datadog_zstd//:go_default_library", "@com_github_datadog_zstd//:zstd",
"@com_github_mattn_go_sqlite3//:go_default_library", "@com_github_mattn_go_sqlite3//:go-sqlite3",
], ],
) )
go_binary( go_binary(
name = "hello", name = "hello",
embed = [":hello_lib"], embed = [":hello_lib"],
gc_linkopts = select({
"@platforms//os:macos": [
"-s",
"-w",
"-buildmode=pie",
],
"//conditions:default": [],
}),
static = "on", static = "on",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )