Skip to content

Commit

Permalink
Move cc_proto_library to builtins.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 445872984
  • Loading branch information
buildbreaker2021 authored and copybara-github committed May 2, 2022
1 parent 17cfa01 commit d27be97
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)

0 comments on commit d27be97

Please sign in to comment.