From d27be977308c1c08f1debd4f2e2ffcab09eae8b8 Mon Sep 17 00:00:00 2001 From: gnish Date: Mon, 2 May 2022 01:44:25 -0700 Subject: [PATCH] Move cc_proto_library to builtins. PiperOrigin-RevId: 445872984 --- .../common/cc/cc_proto_library.bzl | 64 +++++++++++++++++++ .../builtins_bzl/common/cc/semantics.bzl | 13 ++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl new file mode 100644 index 00000000000000..29f86bd0aa76bf --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl @@ -0,0 +1,64 @@ +# Copyright 2021 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""cc_proto_library rule.""" + +load(":common/cc/semantics.bzl", "semantics") + +cc_common = _builtins.toplevel.cc_common +CcInfo = _builtins.toplevel.CcInfo +ProtoInfo = _builtins.toplevel.ProtoInfo + +def _rule_impl(ctx): + if len(ctx.attr.deps) == 0: + fail("no deps attribute found; expected one.") + + if len(ctx.attr.deps) > 1: + # actually label_list is used only for consistency with other deps attributes. + fail("more than one deps attribute found; expected only one.") + dep = ctx.attr.deps[0] + + files = semantics.get_proto_cc_files(dep) + + cc_files = [f for f in files if f.basename.endswith("pb.cc") or + f.basename.endswith("pb.h") or f.basename.endswith("proto.h")] + + cc_files_provider = semantics.get_cc_files_provider(cc_files) + files_provider = DefaultInfo(files = depset(cc_files)) + + if CcInfo in dep: + cc_info_provider = dep[CcInfo] + else: + fail("proto_library rule must generate CcInfo (have cc_api_version > 0).") + + return [cc_files_provider, files_provider, cc_info_provider] + +def _create_cc_proto_library_rule(): + aspects = semantics.get_proto_aspects() + return rule( + output_to_genfiles = True, + implementation = _rule_impl, + attrs = { + "deps": attr.label_list( + # aspects = [_cc_proto_aspect], todo(dbabkin): return aspet after fix b/123988498 + # TODO(cmita): use Starlark aspect after b/145508948, or when proto_library + # doesn't need to propagate + aspects = aspects, + allow_rules = ["proto_library"], + providers = [ProtoInfo, CcInfo], # todo(dbabkin): remove CcInfo after fix b/123988498 + ), + }, + ) + +cc_proto_library = _create_cc_proto_library_rule() diff --git a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl index 5b9ea242c2b3b6..4124e21114d332 100644 --- a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl @@ -18,6 +18,16 @@ load(":common/cc/cc_helper.bzl", "cc_helper") cc_common = _builtins.toplevel.cc_common +def _get_proto_aspects(): + return [] + +# buildifier: disable=unused-variable +def _get_cc_files_provider(cc_files): + return None + +def _get_proto_cc_files(dep): + return dep[DefaultInfo].files.to_list() + def _should_create_empty_archive(): return False @@ -181,4 +191,7 @@ semantics = struct( should_use_legacy_cc_test = _should_use_legacy_cc_test, get_coverage_attrs = _get_coverage_attrs, get_coverage_env = _get_coverage_env, + get_proto_cc_files = _get_proto_cc_files, + get_cc_files_provider = _get_cc_files_provider, + get_proto_aspects = _get_proto_aspects, )