From 5c4b746790b93cf0fd9be156d35f2e37b4e1843e Mon Sep 17 00:00:00 2001 From: Yannic Bonenberger Date: Sat, 7 Mar 2020 21:42:29 +0100 Subject: [PATCH] Convert *:go_default_library to be alias of *:xyz_go_proto Fixes #123 --- WORKSPACE | 4 + build/bazel/remote/asset/v1/BUILD | 12 +- .../bazel/remote/asset/v1/remote_asset.proto | 6 +- build/bazel/remote/execution/v2/BUILD | 12 +- build/bazel/semver/BUILD | 12 +- repository_rules.bzl | 111 ++++++++---------- 6 files changed, 77 insertions(+), 80 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 2ef5ba7d..26f09805 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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", diff --git a/build/bazel/remote/asset/v1/BUILD b/build/bazel/remote/asset/v1/BUILD index aadc6e4d..86ab44e8 100644 --- a/build/bazel/remote/asset/v1/BUILD +++ b/build/bazel/remote/asset/v1/BUILD @@ -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"]) @@ -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, ) diff --git a/build/bazel/remote/asset/v1/remote_asset.proto b/build/bazel/remote/asset/v1/remote_asset.proto index 60be7641..de3b94cf 100644 --- a/build/bazel/remote/asset/v1/remote_asset.proto +++ b/build/bazel/remote/asset/v1/remote_asset.proto @@ -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 @@ -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 @@ -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 diff --git a/build/bazel/remote/execution/v2/BUILD b/build/bazel/remote/execution/v2/BUILD index 9e60b328..55d7386e 100644 --- a/build/bazel/remote/execution/v2/BUILD +++ b/build/bazel/remote/execution/v2/BUILD @@ -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"]) @@ -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, ) diff --git a/build/bazel/semver/BUILD b/build/bazel/semver/BUILD index 27c0cb23..d0946a86 100644 --- a/build/bazel/semver/BUILD +++ b/build/bazel/semver/BUILD @@ -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"]) @@ -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, ) diff --git a/repository_rules.bzl b/repository_rules.bzl index 2b36886e..bb0319ad 100644 --- a/repository_rules.bzl +++ b/repository_rules.bzl @@ -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 = {}, ), }, ) @@ -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)