Skip to content

Commit

Permalink
Refactor away references to @io_bazel_rules_go (#3185)
Browse files Browse the repository at this point in the history
* Use repo-relative labels in load statements

* Introduce a constant for the Go toolchain requirement

Gets rid of a common source of repo-absolute labels. Repo-relative
labels for toolchains aren't supported with Bazel 4. Now that
config_common.toolchain_type exists, the string label may also be
replaced with a more complex Starlark type in the future.

* Use Label(...) in macros

This resolves a label literal relative to the call site of the Label
constructor.

* Use repo-relative labels in rule attribute defaults
  • Loading branch information
fmeum authored Jun 7, 2022
1 parent 15e43af commit 10e90ba
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 29 deletions.
6 changes: 5 additions & 1 deletion extras/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ bzl_library(
name = "embed_data",
srcs = ["embed_data.bzl"],
visibility = ["//visibility:public"],
deps = ["@io_bazel_rules_go//go/private:context"],
deps = [
"//go/private:context",
"//go/private:go_toolchain",
],
)

bzl_library(
Expand All @@ -41,6 +44,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
"//go/private:context",
"//go/private:go_toolchain",
"//go/private:providers",
"//go/private/rules:wrappers",
],
Expand Down
8 changes: 6 additions & 2 deletions extras/bindata.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
"""bindata.bzl provides the bindata rule for embedding data in .go files"""

load(
"@io_bazel_rules_go//go:def.bzl",
"//go:def.bzl",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)

def _bindata_impl(ctx):
go = go_context(ctx)
Expand Down Expand Up @@ -78,5 +82,5 @@ bindata = rule(
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)
8 changes: 6 additions & 2 deletions extras/embed_data.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
# limitations under the License.

load(
"@io_bazel_rules_go//go/private:context.bzl", #TODO: This ought to be def
"//go/private:context.bzl", #TODO: This ought to be def
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)

_DOC = """`go_embed_data` generates a .go file that contains data from a file or a
list of files. It should be consumed in the srcs list of one of the
Expand Down Expand Up @@ -135,6 +139,6 @@ go_embed_data = rule(
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)
# See /docs/go/extras/extras.md#go_embed_data for full documentation.
2 changes: 1 addition & 1 deletion extras/embed_data_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def go_embed_data_dependencies():
remote = "https://github.com/kevinburke/go-bindata",
# v3.13.0+incompatible, "latest" as of 2019-07-08
commit = "53d73b98acf3bd9f56d7f9136ed8e1be64756e1d",
patches = ["@io_bazel_rules_go//third_party:com_github_kevinburke_go_bindata-gazelle.patch"],
patches = [Label("//third_party:com_github_kevinburke_go_bindata-gazelle.patch")],
patch_args = ["-p1"],
shallow_since = "1545009224 +0000",
# gazelle args: -go_prefix github.com/kevinburke/go-bindata
Expand Down
13 changes: 7 additions & 6 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# DO NOT USE IT.

load("//go/private:context.bzl", "go_context")
load("//go/private:go_toolchain.bzl", "GO_TOOLCHAIN")
load("//go/private/rules:wrappers.bzl", go_binary = "go_binary_macro")
load("//go/private:providers.bzl", "GoLibrary")
load("@bazel_skylib//lib:paths.bzl", "paths")
Expand Down Expand Up @@ -143,10 +144,10 @@ _gomock_source = rule(
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)

def gomock(name, library, out, source = None, interfaces = [], package = "", self_package = "", aux_files = {}, mockgen_tool = _MOCKGEN_TOOL, imports = {}, copyright_file = None, mock_names = {}):
Expand Down Expand Up @@ -274,10 +275,10 @@ _gomock_prog_gen = rule(
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)

def _gomock_prog_exec_impl(ctx):
Expand Down Expand Up @@ -356,10 +357,10 @@ _gomock_prog_exec = rule(
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)

def _handle_shared_args(ctx, args):
Expand Down
6 changes: 5 additions & 1 deletion go/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bzl_library(
":mode",
":providers",
"//go/platform:apple",
"//go/private:go_toolchain",
"//go/private/rules:transition",
"@bazel_skylib//lib:paths",
"@bazel_skylib//rules:common_settings",
Expand All @@ -68,7 +69,10 @@ bzl_library(
bzl_library(
name = "go_toolchain",
srcs = ["go_toolchain.bzl"],
visibility = ["//go:__subpackages__"],
visibility = [
"//extras:__pkg__", # Manually added
"//go:__subpackages__",
],
deps = [
"//go/private:platforms",
"//go/private:providers",
Expand Down
8 changes: 6 additions & 2 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load(
"OBJCPP_COMPILE_ACTION_NAME",
"OBJC_COMPILE_ACTION_NAME",
)
load(
":go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
":providers.bzl",
"CgoContextInfo",
Expand Down Expand Up @@ -354,7 +358,7 @@ def go_context(ctx, attr = None):
"""
if not attr:
attr = ctx.attr
toolchain = ctx.toolchains["@io_bazel_rules_go//go:toolchain"]
toolchain = ctx.toolchains[GO_TOOLCHAIN]
cgo_context_info = None
go_config_info = None
stdlib = None
Expand Down Expand Up @@ -546,7 +550,7 @@ go_context_data = rule(
},
doc = """go_context_data gathers information about the build configuration.
It is a common dependency of all Go targets.""",
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
cfg = request_nogo_transition,
)

Expand Down
4 changes: 3 additions & 1 deletion go/private/go_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ load("//go/private/actions:link.bzl", "emit_link")
load("//go/private/actions:pack.bzl", "emit_pack")
load("//go/private/actions:stdlib.bzl", "emit_stdlib")

GO_TOOLCHAIN = "@io_bazel_rules_go//go:toolchain"

def _go_toolchain_impl(ctx):
sdk = ctx.attr.sdk[GoSDK]
cross_compile = ctx.attr.goos != sdk.goos or ctx.attr.goarch != sdk.goarch
Expand Down Expand Up @@ -131,7 +133,7 @@ def declare_toolchains(host, sdk, builder):
)
native.toolchain(
name = toolchain_name,
toolchain_type = "@io_bazel_rules_go//go:toolchain",
toolchain_type = GO_TOOLCHAIN,
exec_compatible_with = [
"@io_bazel_rules_go//go/toolchain:" + host_goos,
"@io_bazel_rules_go//go/toolchain:" + host_goarch,
Expand Down
6 changes: 5 additions & 1 deletion go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ load(
"cgo_exts",
"go_exts",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"GoLibrary",
Expand Down Expand Up @@ -380,7 +384,7 @@ _go_binary_kwargs = {
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
"toolchains": ["@io_bazel_rules_go//go:toolchain"],
"toolchains": [GO_TOOLCHAIN],
"doc": """This builds an executable from a set of source files,
which must all be in the `main` package. You can run the binary with
`bazel run`, or you can build it with `bazel build` and run it directly.<br><br>
Expand Down
6 changes: 5 additions & 1 deletion go/private/rules/info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ load(
"//go/private:context.bzl",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)

def _go_info_impl(ctx):
go = go_context(ctx)
Expand Down Expand Up @@ -46,7 +50,7 @@ _go_info = rule(
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)

def go_info():
Expand Down
8 changes: 6 additions & 2 deletions go/private/rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ load(
"//go/private:context.bzl",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"GoLibrary",
Expand Down Expand Up @@ -176,7 +180,7 @@ go_library = rule(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
doc = """This builds a Go library from a set of source files that are all part of
the same package.<br><br>
***Note:*** For targets generated by Gazelle, `name` is typically the last component of the path,
Expand Down Expand Up @@ -207,7 +211,7 @@ go_tool_library = rule(
"_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
"_stdlib": attr.label(default = "//:stdlib"),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)
# This is used instead of `go_library` for dependencies of the `nogo` rule and
# packages that are depended on implicitly by code generated within the Go rules.
Expand Down
6 changes: 5 additions & 1 deletion go/private/rules/nogo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ load(
"//go/private:context.bzl",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"EXPORT_PATH",
Expand Down Expand Up @@ -102,7 +106,7 @@ _nogo = rule(
default = "@bazel_tools//tools/whitelists/function_transition_whitelist",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
cfg = go_tool_transition,
)

Expand Down
6 changes: 5 additions & 1 deletion go/private/rules/source.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ load(
"//go/private:context.bzl",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"GoLibrary",
Expand Down Expand Up @@ -78,7 +82,7 @@ go_source = rule(
"_go_config": attr.label(default = "//:go_config"),
"_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
doc = """This declares a set of source files and related dependencies that can be embedded into one of the
other rules.
This is used as a way of easily declaring a common set of sources re-used in multiple rules.<br><br>
Expand Down
6 changes: 5 additions & 1 deletion go/private/rules/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ load(
"//go/private:context.bzl",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"CgoContextInfo",
Expand All @@ -38,5 +42,5 @@ stdlib = rule(
},
doc = """stdlib builds the standard library for the target configuration
or uses the precompiled standard library from the SDK if it is suitable.""",
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)
6 changes: 5 additions & 1 deletion go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ load(
"pkg_dir",
"split_srcs",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private/rules:binary.bzl",
"gc_linkopts",
Expand Down Expand Up @@ -415,7 +419,7 @@ _go_test_kwargs = {
},
"executable": True,
"test": True,
"toolchains": ["@io_bazel_rules_go//go:toolchain"],
"toolchains": [GO_TOOLCHAIN],
"doc": """This builds a set of tests that can be run with `bazel test`.<br><br>
To run all tests in the workspace, and print output on failure (the
equivalent of `go test ./...`), run<br>
Expand Down
2 changes: 1 addition & 1 deletion go/tools/bazel_testing/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def go_bazel_test(rule_files = None, **kwargs):
"""

if not rule_files:
rule_files = ["@io_bazel_rules_go//:all_files"]
rule_files = [Label("//:all_files")]

# Add dependency on bazel_testing library.
kwargs.setdefault("deps", [])
Expand Down
6 changes: 5 additions & 1 deletion proto/compiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ load(
"GoLibrary",
"go_context",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private/rules:transition.bzl",
"go_reset_target",
Expand Down Expand Up @@ -206,7 +210,7 @@ _go_proto_compiler = rule(
default = "//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)

def go_proto_compiler(name, **kwargs):
Expand Down
8 changes: 6 additions & 2 deletions proto/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ load(
"GoProtoCompiler",
"proto_path",
)
load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"INFERRED_PATH",
Expand Down Expand Up @@ -178,14 +182,14 @@ go_proto_library = rule(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
toolchains = [GO_TOOLCHAIN],
)
# go_proto_library is a rule that takes a proto_library (in the proto
# attribute) and produces a go library for it.

def go_grpc_library(**kwargs):
# TODO: Deprecate once gazelle generates just go_proto_library
go_proto_library(compilers = ["@io_bazel_rules_go//proto:go_grpc"], **kwargs)
go_proto_library(compilers = [Label("//proto:go_grpc")], **kwargs)

def proto_register_toolchains():
print("You no longer need to call proto_register_toolchains(), it does nothing")
Loading

0 comments on commit 10e90ba

Please sign in to comment.