Skip to content

Commit

Permalink
Replace go_name_hack with a usage of Label
Browse files Browse the repository at this point in the history
The Label constructor can be used to transform a label string such as
@io_bazel_rules_go//foo:bar into the canonical label referencing this
target from the repository in which it is called.

For example, `str(Label("@io_bazel_rules_go//foo:bar"))` returns
`"@io_bazel_rules_go//foo:bar"` if rules_go is used as an external
repository from another main workspace, but returns `"//foo:bar"` when
rules_go is the main repository (e.g., when running the tests in the
CI).

This requires raising the minimum version of Bazel from 4.2.0 to 4.2.1
due to bazelbuild/bazel#13890.
  • Loading branch information
fmeum committed Dec 20, 2021
1 parent d0d326e commit c536218
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
tasks:
ubuntu1804_bazel400:
platform: ubuntu1804
bazel: 4.2.0 # test minimum supported version of bazel
bazel: 4.2.1 # test minimum supported version of bazel
shell_commands:
- tests/core/cgo/generate_imported_dylib.sh
build_targets:
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies(is_rules_go = True)
go_rules_dependencies()

go_register_toolchains(version = "1.17")

Expand Down
2 changes: 1 addition & 1 deletion go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get_versioned_shared_lib_extension(path):
# something like 1.2.3, or so.1.2, or dylib.1.2, or foo.1.2
return ""

MINIMUM_BAZEL_VERSION = "4.2.0"
MINIMUM_BAZEL_VERSION = "4.2.1"

def as_list(v):
"""Returns a list, tuple, or depset as a list."""
Expand Down
33 changes: 1 addition & 32 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load("//go/private:nogo.bzl", "DEFAULT_NOGO", "go_register_nogo")
load("//proto:gogo.bzl", "gogo_special_proto")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def go_rules_dependencies(is_rules_go = False):
def go_rules_dependencies():
"""Declares workspaces the Go rules depend on. Workspaces that use
rules_go should call this.
Expand Down Expand Up @@ -285,37 +285,6 @@ def go_rules_dependencies(is_rules_go = False):
nogo = DEFAULT_NOGO,
)

go_name_hack(
name = "io_bazel_rules_go_name_hack",
is_rules_go = is_rules_go,
)

def _maybe(repo_rule, name, **kwargs):
if name not in native.existing_rules():
repo_rule(name = name, **kwargs)

def _go_name_hack_impl(ctx):
build_content = """\
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "def",
srcs = ["def.bzl"],
visibility = ["//visibility:public"],
)
"""
ctx.file("BUILD.bazel", build_content)
content = "IS_RULES_GO = {}".format(ctx.attr.is_rules_go)
ctx.file("def.bzl", content)

go_name_hack = repository_rule(
implementation = _go_name_hack_impl,
attrs = {
"is_rules_go": attr.bool(),
},
doc = """go_name_hack records whether the main workspace is rules_go.
See documentation for _filter_transition_label in
go/private/rules/transition.bzl.
""",
)
1 change: 0 additions & 1 deletion go/private/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ bzl_library(
"@io_bazel_rules_go//go/private:mode",
"@io_bazel_rules_go//go/private:platforms",
"@io_bazel_rules_go//go/private:providers",
"@io_bazel_rules_go_name_hack//:def",
],
)

Expand Down
53 changes: 22 additions & 31 deletions go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,24 @@ load(
"GoLibrary",
"GoSource",
)
load(
"@io_bazel_rules_go_name_hack//:def.bzl",
"IS_RULES_GO",
)
load(
"@io_bazel_rules_go//go/platform:crosstool.bzl",
"platform_from_crosstool",
)

def filter_transition_label(label):
def transform_transition_label(label):
"""Transforms transition labels for the current workspace.
This is a workaround for bazelbuild/bazel#10499. If a transition refers to
a build setting in the same workspace, for example
@io_bazel_rules_go//go/config:goos, it must use a label without a workspace
name if and only if the workspace is the main workspace.
All Go build settings and transitions are in io_bazel_rules_go. So if
io_bazel_rules_go is the main workspace (for development and testing),
go_transition must use a label like //go/config:goos. If io_bazel_rules_go
is not the main workspace (almost always), go_transition must use a label
like @io_bazel_rules_go//go/config:goos.
This works around bazelbuild/bazel#10499 by automatically using the correct
way to refer to this repository (@io_bazel_rules_go from another workspace,
but only repo-relative labels if this repository is the main workspace).
"""
if IS_RULES_GO and label.startswith("@io_bazel_rules_go"):
return label[len("@io_bazel_rules_go"):]
else:
if label.startswith("//command_line_option:"):
# This is a special label used to refer to Bazel command-line options in
# transitions and should not be modified in any way.
return label
else:
return str(Label(label))

def go_transition_wrapper(kind, transition_kind, name, **kwargs):
"""Wrapper for rules that may use transitions.
Expand Down Expand Up @@ -132,17 +123,17 @@ def _go_transition_impl(settings, attr):
if pure == "on":
fail('race = "on" cannot be set when pure = "on" is set. race requires cgo.')
pure = "off"
settings[filter_transition_label("@io_bazel_rules_go//go/config:pure")] = False
settings[transform_transition_label("@io_bazel_rules_go//go/config:pure")] = False
if msan == "on":
if pure == "on":
fail('msan = "on" cannot be set when msan = "on" is set. msan requires cgo.')
pure = "off"
settings[filter_transition_label("@io_bazel_rules_go//go/config:pure")] = False
settings[transform_transition_label("@io_bazel_rules_go//go/config:pure")] = False
if pure == "on":
race = "off"
settings[filter_transition_label("@io_bazel_rules_go//go/config:race")] = False
settings[transform_transition_label("@io_bazel_rules_go//go/config:race")] = False
msan = "off"
settings[filter_transition_label("@io_bazel_rules_go//go/config:msan")] = False
settings[transform_transition_label("@io_bazel_rules_go//go/config:msan")] = False
cgo = pure == "off"

goos = getattr(attr, "goos", "auto")
Expand All @@ -169,14 +160,14 @@ def _go_transition_impl(settings, attr):

tags = getattr(attr, "gotags", [])
if tags:
tags_label = filter_transition_label("@io_bazel_rules_go//go/config:tags")
tags_label = transform_transition_label("@io_bazel_rules_go//go/config:tags")
settings[tags_label] = tags

linkmode = getattr(attr, "linkmode", "auto")
if linkmode != "auto":
if linkmode not in LINKMODES:
fail("linkmode: invalid mode {}; want one of {}".format(linkmode, ", ".join(LINKMODES)))
linkmode_label = filter_transition_label("@io_bazel_rules_go//go/config:linkmode")
linkmode_label = transform_transition_label("@io_bazel_rules_go//go/config:linkmode")
settings[linkmode_label] = linkmode

return settings
Expand All @@ -192,20 +183,20 @@ def _request_nogo_transition(settings, attr):
request_nogo to be true to provide the project configured nogo.
"""
settings = dict(settings)
settings[filter_transition_label("@io_bazel_rules_go//go/private:request_nogo")] = True
settings[transform_transition_label("@io_bazel_rules_go//go/private:request_nogo")] = True
return settings

request_nogo_transition = transition(
implementation = _request_nogo_transition,
inputs = [],
outputs = [filter_transition_label(label) for label in [
outputs = [transform_transition_label(label) for label in [
"@io_bazel_rules_go//go/private:request_nogo",
]],
)

go_transition = transition(
implementation = _go_transition_impl,
inputs = [filter_transition_label(label) for label in [
inputs = [transform_transition_label(label) for label in [
"//command_line_option:cpu",
"//command_line_option:crosstool_top",
"//command_line_option:platforms",
Expand All @@ -216,7 +207,7 @@ go_transition = transition(
"@io_bazel_rules_go//go/config:tags",
"@io_bazel_rules_go//go/config:linkmode",
]],
outputs = [filter_transition_label(label) for label in [
outputs = [transform_transition_label(label) for label in [
"//command_line_option:platforms",
"@io_bazel_rules_go//go/config:static",
"@io_bazel_rules_go//go/config:msan",
Expand All @@ -239,7 +230,7 @@ _reset_transition_dict = {
"@io_bazel_rules_go//go/private:bootstrap_nogo": True,
}

_reset_transition_keys = sorted([filter_transition_label(label) for label in _reset_transition_dict.keys()])
_reset_transition_keys = sorted([transform_transition_label(label) for label in _reset_transition_dict.keys()])

def _go_reset_transition_impl(settings, attr):
"""Sets Go settings to default values so tools can be built safely.
Expand All @@ -253,7 +244,7 @@ def _go_reset_transition_impl(settings, attr):
"""
settings = dict(settings)
for label, value in _reset_transition_dict.items():
settings[filter_transition_label(label)] = value
settings[transform_transition_label(label)] = value
return settings

go_reset_transition = transition(
Expand Down Expand Up @@ -328,6 +319,6 @@ def _set_ternary(settings, attr, name):
value = getattr(attr, name, "auto")
_check_ternary(name, value)
if value != "auto":
label = filter_transition_label("@io_bazel_rules_go//go/config:{}".format(name))
label = transform_transition_label("@io_bazel_rules_go//go/config:{}".format(name))
settings[label] = value == "on"
return value

0 comments on commit c536218

Please sign in to comment.