xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/bazel/proto.bzl (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li# Copyright 2019 Google LLC
2*ec63e07aSXin Li#
3*ec63e07aSXin Li# Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li# you may not use this file except in compliance with the License.
5*ec63e07aSXin Li# You may obtain a copy of the License at
6*ec63e07aSXin Li#
7*ec63e07aSXin Li#     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li#
9*ec63e07aSXin Li# Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li# distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li# See the License for the specific language governing permissions and
13*ec63e07aSXin Li# limitations under the License.
14*ec63e07aSXin Li
15*ec63e07aSXin Li"""Generates proto targets in various languages."""
16*ec63e07aSXin Li
17*ec63e07aSXin Liload("@rules_proto//proto:defs.bzl", "proto_library")
18*ec63e07aSXin Li
19*ec63e07aSXin Lidef _cc_proto_library_name_from_proto_name(name):
20*ec63e07aSXin Li    """Converts proto name to cc_proto_library name.
21*ec63e07aSXin Li
22*ec63e07aSXin Li    Several suffixes will be considered.
23*ec63e07aSXin Li    Args:
24*ec63e07aSXin Li      name: the proto name
25*ec63e07aSXin Li    Returns:
26*ec63e07aSXin Li      The cc_proto_library name.
27*ec63e07aSXin Li    """
28*ec63e07aSXin Li    name = name.replace("-", "_")
29*ec63e07aSXin Li    if name == "proto":
30*ec63e07aSXin Li        # replace 'proto' with 'cc_proto'
31*ec63e07aSXin Li        return "cc_proto"
32*ec63e07aSXin Li    for suffix in [
33*ec63e07aSXin Li        ".protolib",
34*ec63e07aSXin Li        "protolib",
35*ec63e07aSXin Li        "proto2lib",
36*ec63e07aSXin Li        "proto_lib",
37*ec63e07aSXin Li        "proto2",
38*ec63e07aSXin Li        "protos",
39*ec63e07aSXin Li        "proto2_lib",
40*ec63e07aSXin Li        "libproto",
41*ec63e07aSXin Li        "genproto",
42*ec63e07aSXin Li        "proto",
43*ec63e07aSXin Li    ]:
44*ec63e07aSXin Li        # replace 'suffix' or '_suffix' with '_cc_proto'
45*ec63e07aSXin Li        if name.endswith("_" + suffix):
46*ec63e07aSXin Li            return name[:-len("_" + suffix)] + "_cc_proto"
47*ec63e07aSXin Li        elif name.endswith(suffix):
48*ec63e07aSXin Li            return name[:-len(suffix)] + "_cc_proto"
49*ec63e07aSXin Li
50*ec63e07aSXin Li    # no match; simply append '_cc_proto' to the end
51*ec63e07aSXin Li    return name + "_cc_proto"
52*ec63e07aSXin Li
53*ec63e07aSXin Lidef sapi_proto_library(
54*ec63e07aSXin Li        name,
55*ec63e07aSXin Li        srcs = [],
56*ec63e07aSXin Li        deps = [],
57*ec63e07aSXin Li        alwayslink = False,
58*ec63e07aSXin Li        **kwargs):
59*ec63e07aSXin Li    """Generates proto library and C++ targets.
60*ec63e07aSXin Li
61*ec63e07aSXin Li    Args:
62*ec63e07aSXin Li      name: Name for proto_library and base for the cc_proto_library name, name +
63*ec63e07aSXin Li            "_cc_proto".
64*ec63e07aSXin Li      srcs: Same as proto_library deps.
65*ec63e07aSXin Li      deps: Same as proto_library deps.
66*ec63e07aSXin Li      alwayslink: Same as cc_library.
67*ec63e07aSXin Li      **kwargs: proto_library arguments.
68*ec63e07aSXin Li    """
69*ec63e07aSXin Li    if kwargs.get("has_services", False):
70*ec63e07aSXin Li        fail("Services are not currently supported.")
71*ec63e07aSXin Li
72*ec63e07aSXin Li    proto_library(
73*ec63e07aSXin Li        name = name,
74*ec63e07aSXin Li        srcs = srcs,
75*ec63e07aSXin Li        deps = deps,
76*ec63e07aSXin Li    )
77*ec63e07aSXin Li    native.cc_proto_library(
78*ec63e07aSXin Li        name = name + "_sapi_cc_proto",
79*ec63e07aSXin Li        deps = [name],
80*ec63e07aSXin Li    )
81*ec63e07aSXin Li    native.cc_library(
82*ec63e07aSXin Li        name = _cc_proto_library_name_from_proto_name(name),
83*ec63e07aSXin Li        deps = [name + "_sapi_cc_proto"],
84*ec63e07aSXin Li        alwayslink = alwayslink,
85*ec63e07aSXin Li        **kwargs
86*ec63e07aSXin Li    )
87