• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2009-2021, Google LLC
2# All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8"""Internal rules for building upb."""
9
10load("//bazel:upb_proto_library.bzl", "GeneratedSrcsInfo")
11
12# upb_amalgamation() rule, with file_list aspect.
13
14SrcList = provider(
15    fields = {
16        "srcs": "list of srcs",
17    },
18)
19
20def _file_list_aspect_impl(target, ctx):
21    if GeneratedSrcsInfo in target:
22        srcs = target[GeneratedSrcsInfo]
23        return [SrcList(srcs = srcs.srcs + srcs.hdrs)]
24
25    srcs = []
26    for src in ctx.rule.attr.srcs:
27        srcs += src.files.to_list()
28    for hdr in ctx.rule.attr.hdrs:
29        srcs += hdr.files.to_list()
30    for hdr in ctx.rule.attr.textual_hdrs:
31        srcs += hdr.files.to_list()
32    return [SrcList(srcs = srcs)]
33
34_file_list_aspect = aspect(
35    implementation = _file_list_aspect_impl,
36)
37
38def _upb_amalgamation(ctx):
39    inputs = []
40    for lib in ctx.attr.libs:
41        inputs += lib[SrcList].srcs
42    srcs = [src for src in inputs if not src.path.endswith("hpp")]
43    ctx.actions.run(
44        inputs = inputs,
45        outputs = ctx.outputs.outs,
46        arguments = [f.path for f in ctx.outputs.outs] + [f.path for f in srcs],
47        progress_message = "Making amalgamation",
48        executable = ctx.executable._amalgamator,
49    )
50    return []
51
52upb_amalgamation = rule(
53    attrs = {
54        "_amalgamator": attr.label(
55            executable = True,
56            cfg = "exec",
57            default = "//upb/bazel:amalgamate",
58        ),
59        "prefix": attr.string(
60            default = "",
61        ),
62        "libs": attr.label_list(aspects = [_file_list_aspect]),
63        "outs": attr.output_list(),
64        "strip_import_prefix": attr.string_list(),
65    },
66    implementation = _upb_amalgamation,
67)
68