Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert *:go_default_library to be alias of *:xyz_go_proto #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ switched_rules_by_language(
java = True,
)

load("//:remote_apis_deps.bzl", "remote_apis_go_deps")

remote_apis_go_deps()

# Needed for the googleapis protos.
http_archive(
name = "googleapis",
Expand Down
12 changes: 6 additions & 6 deletions build/bazel/remote/asset/v1/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package(default_visibility = ["//visibility:public"])

load("@bazel_remote_apis_imports//:imports.bzl", "cc_grpc_library", "go_enabled", "go_proto_library", "java_proto_library", "maybe_alias")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@bazel_remote_apis_imports//:imports.bzl", "java_proto_library", "go_library", "go_proto_library", "cc_grpc_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

Expand Down Expand Up @@ -49,8 +49,8 @@ go_proto_library(
],
)

go_library(
maybe_alias(
name = "go_default_library",
embed = [":remote_asset_go_proto"],
importpath = "github.com/bazelbuild/remote-apis/build/bazel/remote/asset/v1",
actual = ":remote_asset_go_proto",
enabled = go_enabled,
)
6 changes: 3 additions & 3 deletions build/bazel/remote/asset/v1/remote_asset.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ option java_package = "build.bazel.remote.asset.v1";
option objc_class_prefix = "RA";

// The Remote Asset API provides a mapping from a URI and Qualifiers to
// Digests.
// Digests.
//
// Multiple URIs may be used to refer to the same content. For example, the
// same tarball may exist at multiple mirrors and thus be retrievable from
Expand Down Expand Up @@ -76,7 +76,7 @@ message Qualifier {
}

// The Fetch service resolves or fetches assets referenced by URI and
// Qualifiers, returning a Digest for the content in
// Qualifiers, returning a Digest for the content in
// [ContentAddressableStorage][build.bazel.remote.execution.v2.ContentAddressableStorage].
//
// As with other services in the Remote Execution API, any call may return an
Expand Down Expand Up @@ -175,7 +175,7 @@ message FetchBlobRequest {
google.protobuf.Duration timeout = 2;

// The oldest content the client is willing to accept, as measured from the
// time it was Push'd or when the underlying retrieval from origin was
// time it was Push'd or when the underlying retrieval from origin was
// started.
// Upon retries of Fetch requests that cannot be completed within a single
// RPC, clients *SHOULD* provide the same value for subsequent requests as the
Expand Down
12 changes: 6 additions & 6 deletions build/bazel/remote/execution/v2/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package(default_visibility = ["//visibility:public"])

load("@bazel_remote_apis_imports//:imports.bzl", "cc_grpc_library", "go_enabled", "go_proto_library", "java_proto_library", "maybe_alias")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@bazel_remote_apis_imports//:imports.bzl", "java_proto_library", "go_library", "go_proto_library", "cc_grpc_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

Expand Down Expand Up @@ -52,8 +52,8 @@ go_proto_library(
],
)

go_library(
maybe_alias(
name = "go_default_library",
embed = [":remote_execution_go_proto"],
importpath = "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2",
actual = ":remote_execution_go_proto",
enabled = go_enabled,
)
12 changes: 6 additions & 6 deletions build/bazel/semver/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package(default_visibility = ["//visibility:public"])

load("@bazel_remote_apis_imports//:imports.bzl", "cc_grpc_library", "go_enabled", "go_proto_library", "java_proto_library", "maybe_alias")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@bazel_remote_apis_imports//:imports.bzl", "java_proto_library", "go_library", "go_proto_library", "cc_grpc_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

Expand Down Expand Up @@ -30,8 +30,8 @@ go_proto_library(
proto = ":semver_proto",
)

go_library(
maybe_alias(
name = "go_default_library",
embed = [":semver_go_proto"],
importpath = "github.com/bazelbuild/remote-apis/build/bazel/semver",
actual = ":semver_go_proto",
enabled = go_enabled,
)
111 changes: 52 additions & 59 deletions repository_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,16 @@ This is adapted from
https://github.com/googleapis/googleapis/blob/master/repository_rules.bzl
"""

load("//:remote_apis_deps.bzl", "remote_apis_go_deps")

def _switched_rules_impl(ctx):
disabled_rule_script = """
def {rule_name}(**kwargs):
pass
"""
enabled_native_rule_script = """
{rule_name} = {native_rule_name}
"""
enabled_rule_script = """
load("{file_label}", _{rule_name} = "{rule_name}")
"""
elabled_rule_scrip_alias = """
{rule_name} = _{rule_name}
"""
load_rules = [] # load() must go before everything else in .bzl files since Bazel 0.25.0
rules = []

for rule_name, value in ctx.attr.rules.items():
if not value:
rules.append(disabled_rule_script.format(rule_name = rule_name))
elif value.startswith("@"):
load_rules.append(enabled_rule_script.format(file_label = value, rule_name = rule_name))
rules.append(elabled_rule_scrip_alias.format(rule_name = rule_name))
elif value.startswith("native."):
rules.append(
enabled_native_rule_script.format(rule_name = rule_name, native_rule_name = value),
)
else:
rules.append(value)

ctx.file("BUILD.bazel", "")
ctx.file("imports.bzl", "".join(load_rules + rules))
ctx.file("imports.bzl", "\n".join(ctx.attr.lines))

switched_rules = repository_rule(
implementation = _switched_rules_impl,
attrs = {
"rules": attr.string_dict(
"lines": attr.string_list(
allow_empty = True,
mandatory = False,
default = {},
),
},
)
Expand Down Expand Up @@ -91,35 +59,60 @@ def switched_rules_by_language(
rules_override (dict): Custom rule overrides (for advanced usage).
"""

rules = {}

rules["java_proto_library"] = _switch(
java,
"native.java_proto_library",
)

rules["go_proto_library"] = _switch(
go,
"@io_bazel_rules_go//proto:def.bzl",
)
rules["go_library"] = _switch(
go,
"@io_bazel_rules_go//go:def.bzl",
)
loads = {}
symbols = {}

rules["cc_grpc_library"] = _switch(
cc,
"@com_github_grpc_grpc//bazel:cc_grpc_library.bzl",
)
symbols["java_enabled"] = java
if java:
loads["java_proto_library"] = "@rules_java//java:defs.bzl"
symbols["java_proto_library"] = "_java_proto_library"

rules.update(rules_override)
symbols["go_enabled"] = go
if go:
loads["go_proto_library"] = "@io_bazel_rules_go//proto:def.bzl"
symbols["go_proto_library"] = "_go_proto_library"

symbols["cc_enabled"] = cc
if cc:
loads["cc_grpc_library"] = "@com_github_grpc_grpc//bazel:cc_grpc_library.bzl"
symbols["cc_grpc_library"] = "_cc_grpc_library"

extra_lines = []
for k, v in rules_override:
if not v:
if k in loads:
loads.pop(k)
if k in symbols:
symbols.pop(k)
elif v.startswith("@"):
loads[k] = v
symbols[k] = "_" + k
elif v.startswith("native."):
if k in loads:
loads.pop(k)
loads[k] = v
else:
extra_lines.append(v)

load_lines = [_load(v, k) for k, v in loads.items()]
symbol_lines = ["{} = {}".format(k, v) for k, v in symbols.items()]
switched_rules(
name = name,
rules = rules,
lines = load_lines + [
"def maybe_alias(name, actual, enabled, visibility = None):",
" if enabled:",
" native.alias(",
" name = name,",
" actual = actual,",
" visibility = visibility,",
" )",
"",
"# Export symbols",
] + symbol_lines + [
"",
"# Extra statements from rules_override",
] + extra_lines,
)
if go:
remote_apis_go_deps()

def _switch(enabled, enabled_value):
return enabled_value if enabled else ""
def _load(pkg, symbol):
return "load('{0}', _{1} = '{1}')".format(pkg, symbol)