Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nanopb_cc_proto_library.bzl « bazel « extra - github.com/nanopb/nanopb.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1a58d593442c1f0d650395c8fe8125b6c2c158e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Apache License, Version 2.0, January 2004, http://www.apache.org/licenses/
# Adapted from: https://github.com/rules-proto-grpc/rules_proto_grpc/

load("@rules_proto_grpc//internal:filter_files.bzl", "filter_files")
load("@rules_cc//cc:defs.bzl", "cc_library")
load(
    "@rules_proto_grpc//:defs.bzl",
    "ProtoPluginInfo",
    "proto_compile_attrs",
    "proto_compile",
)

def cc_nanopb_proto_compile_impl(ctx):
    """Nanopb proto compile implementation to add options files."""
    extra_protoc_args = getattr(ctx.attr, "extra_protoc_args", [])
    extra_protoc_files = getattr(ctx.files, "extra_protoc_files", [])
    for options_target in ctx.attr.nanopb_options_files:
        for options_file in options_target.files.to_list():
            extra_protoc_args = extra_protoc_args + [
                "--nanopb_plugin_opt=-f{}".format(options_file.path)]
            extra_protoc_files = extra_protoc_files + [options_file]
    return proto_compile(ctx, ctx.attr.options, extra_protoc_args, extra_protoc_files)


nanopb_proto_compile_attrs = dict(
    nanopb_options_files = attr.label_list(
        allow_files = [".options"],
        doc = "An optional list of additional nanopb options files to apply",
    ),
    **proto_compile_attrs,
)


# Create compile rule
cc_nanopb_proto_compile = rule(
    implementation = cc_nanopb_proto_compile_impl,
    attrs = dict(
        nanopb_proto_compile_attrs,
        _plugins = attr.label_list(
            providers = [ProtoPluginInfo],
            default = [
                Label("@com_github_nanopb_nanopb//:nanopb_plugin"),
            ],
            doc = "List of protoc plugins to apply",
        ),
    ),
    toolchains = [str(Label("@rules_proto_grpc//protobuf:toolchain_type"))],
)


def cc_nanopb_proto_library(name, **kwargs):  # buildifier: disable=function-docstring
    # Compile protos
    name_pb = name + "_pb"
    cc_nanopb_proto_compile(
        name = name_pb,
        **{
            k: v
            for (k, v) in kwargs.items()
            if k in nanopb_proto_compile_attrs.keys()
        }  # Forward args
    )

    # Filter files to sources and headers
    filter_files(
        name = name_pb + "_srcs",
        target = name_pb,
        extensions = ["c"],
    )

    filter_files(
        name = name_pb + "_hdrs",
        target = name_pb,
        extensions = ["h"],
    )

    # Create c library
    cc_library(
        name = name,
        srcs = [name_pb + "_srcs"],
        deps = PROTO_DEPS + kwargs.get("deps", []),
        hdrs = [name_pb + "_hdrs"],
        includes = [name_pb],
        alwayslink = kwargs.get("alwayslink"),
        copts = kwargs.get("copts"),
        defines = kwargs.get("defines"),
        include_prefix = kwargs.get("include_prefix"),
        linkopts = kwargs.get("linkopts"),
        linkstatic = kwargs.get("linkstatic"),
        local_defines = kwargs.get("local_defines"),
        nocopts = kwargs.get("nocopts"),
        strip_include_prefix = kwargs.get("strip_include_prefix"),
        visibility = kwargs.get("visibility"),
        tags = kwargs.get("tags"),
    )

PROTO_DEPS = [
    "@com_github_nanopb_nanopb//:nanopb",
]