gobazelbazel-rules

Difficulty adding Go bazel build options for multiple platforms


Starting from the example here: https://github.com/PiotrSikora/bazel-zig-cc/blob/main/rules/rules_go.bzl

I am trying to extend it to add the following:

  1. Same flags for linux targets
  2. "-l" and "-w" for BSD target
  3. Add "-trimpath" for all targets

However, I am having difficulty even achieving (1). Here is my code:

_MACOS_GC_LINKOPTS = ["-s", "-w", "-buildmode=pie","-trimpath"]
_LINUX_GC_LINKOPTS = ["-s", "-w", "-buildmode=pie","-trimpath"]

def go_binary(**kwargs):
    kwargs["gc_linkopts"] = select({
        "@platforms//os:macos": _MACOS_GC_LINKOPTS,
        "@platforms//os:linux": _LINUX_GC_LINKOPTS,
        "//conditions:default": [],
    }) + kwargs.pop("gc_linkopts", [])
    go_binary_rule(**kwargs)

The error I get is:

flag provided but not defined: -trimpath

Solution

  • The options in the gc_linkopts are passed to the Go linker. -trimpath is an option of the toplevel go driver, which rules_go bypasses, and the underlying compiler not the linker.