From c5362180bc9cd200f884dac4c382b613bda5e41f Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sun, 19 Dec 2021 16:54:26 +0100 Subject: [PATCH] Replace go_name_hack with a usage of Label 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 https://github.com/bazelbuild/bazel/issues/13890. --- .bazelci/presubmit.yml | 2 +- WORKSPACE | 2 +- go/private/common.bzl | 2 +- go/private/repositories.bzl | 33 +------------------- go/private/rules/BUILD.bazel | 1 - go/private/rules/transition.bzl | 53 ++++++++++++++------------------- 6 files changed, 26 insertions(+), 67 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 162ca149d7..6079c6526d 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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: diff --git a/WORKSPACE b/WORKSPACE index 710e18196e..9c03c0bf82 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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") diff --git a/go/private/common.bzl b/go/private/common.bzl index df3358ec0d..50414dce1b 100644 --- a/go/private/common.bzl +++ b/go/private/common.bzl @@ -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.""" diff --git a/go/private/repositories.bzl b/go/private/repositories.bzl index 9d32871767..430be198dc 100644 --- a/go/private/repositories.bzl +++ b/go/private/repositories.bzl @@ -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. @@ -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. -""", -) diff --git a/go/private/rules/BUILD.bazel b/go/private/rules/BUILD.bazel index 4c3bd9c79b..39c7ce451b 100644 --- a/go/private/rules/BUILD.bazel +++ b/go/private/rules/BUILD.bazel @@ -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", ], ) diff --git a/go/private/rules/transition.bzl b/go/private/rules/transition.bzl index 8c9a90d4c0..62ece36173 100644 --- a/go/private/rules/transition.bzl +++ b/go/private/rules/transition.bzl @@ -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. @@ -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") @@ -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 @@ -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", @@ -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", @@ -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. @@ -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( @@ -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