Debian Codegraph @

extra/protobuf/protobuf-24.4/pkg/build_systems.bzl

raw
1 # Starlark utilities for working with other build systems
2
3 load("@rules_pkg//:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
4 load(":cc_dist_library.bzl", "CcFileList")
5
6 ################################################################################
7 # Macro to create CMake and Automake source lists.
8 ################################################################################
9
10 def gen_file_lists(name, out_stem, **kwargs):
11 gen_cmake_file_lists(
12 name = name + "_cmake",
13 out = out_stem + ".cmake",
14 source_prefix = "${protobuf_SOURCE_DIR}/",
15 **kwargs
16 )
17 native.filegroup(
18 name = name,
19 srcs = [
20 out_stem + ".cmake",
21 ],
22 visibility = ["//src:__pkg__"],
23 )
24
25 ################################################################################
26 # Aspect that extracts srcs, hdrs, etc.
27 ################################################################################
28
29 ProtoFileList = provider(
30 doc = "List of proto files and generated code to be built into a library.",
31 fields = {
32 # Proto files:
33 "proto_srcs": "proto file sources",
34
35 # Generated sources:
36 "hdrs": "header files that are expected to be generated",
37 "srcs": "source files that are expected to be generated",
38 },
39 )
40
41 def _flatten_target_files(targets):
42 files = []
43 for target in targets:
44 for tfile in target.files.to_list():
45 files.append(tfile)
46 return files
47
48 def _file_list_aspect_impl(target, ctx):
49 # We're going to reach directly into the attrs on the traversed rule.
50 rule_attr = ctx.rule.attr
51 providers = []
52
53 # Extract sources from a `proto_library`:
54 if ProtoInfo in target:
55 proto_srcs = []
56 srcs = []
57 hdrs = []
58 for src in _flatten_target_files(rule_attr.srcs):
59 proto_srcs.append(src)
60 srcs.append("%s/%s.pb.cc" % (src.dirname, src.basename))
61 hdrs.append("%s/%s.pb.h" % (src.dirname, src.basename))
62
63 providers.append(ProtoFileList(
64 proto_srcs = proto_srcs,
65 srcs = srcs,
66 hdrs = hdrs,
67 ))
68
69 return providers
70
71 file_list_aspect = aspect(
72 doc = """
73 Aspect to provide the list of sources and headers from a rule.
74
75 Output is CcFileList and/or ProtoFileList. Example:
76
77 cc_library(
78 name = "foo",
79 srcs = [
80 "foo.cc",
81 "foo_internal.h",
82 ],
83 hdrs = ["foo.h"],
84 textual_hdrs = ["foo_inl.inc"],
85 )
86 # produces:
87 # CcFileList(
88 # hdrs = [File("foo.h")],
89 # textual_hdrs = [File("foo_inl.inc")],
90 # internal_hdrs = [File("foo_internal.h")],
91 # srcs = [File("foo.cc")],
92 # )
93
94 proto_library(
95 name = "bar_proto",
96 srcs = ["bar.proto"],
97 )
98 # produces:
99 # ProtoFileList(
100 # proto_srcs = ["bar.proto"],
101 # # Generated filenames are synthesized:
102 # hdrs = ["bar.pb.h"],
103 # srcs = ["bar.pb.cc"],
104 # )
105 """,
106 implementation = _file_list_aspect_impl,
107 )
108
109 ################################################################################
110 # Generic source lists generation
111 #
112 # This factory creates a rule implementation that is parameterized by a
113 # fragment generator function.
114 ################################################################################
115
116 def _create_file_list_impl(ctx, fragment_generator):
117 # `fragment_generator` is a function like:
118 # def fn(originating_rule: Label,
119 # varname: str,
120 # source_prefix: str,
121 # path_strings: [str]) -> str
122 #
123 # It returns a string that defines `varname` to `path_strings`, each
124 # prepended with `source_prefix`.
125 #
126 # When dealing with `File` objects, the `short_path` is used to strip
127 # the output prefix for generated files.
128
129 out = ctx.outputs.out
130
131 fragments = []
132 for srcrule, libname in ctx.attr.src_libs.items():
133 if CcFileList in srcrule:
134 cc_file_list = srcrule[CcFileList]
135
136 # Turn depsets of files into sorted lists.
137 srcs = sorted(cc_file_list.srcs.to_list())
138 hdrs = sorted(
139 depset(transitive = [
140 cc_file_list.textual_hdrs,
141 cc_file_list.hdrs,
142 ]).to_list(),
143 )
144
145 fragments.extend([
146 fragment_generator(
147 srcrule.label,
148 libname + "_srcs",
149 ctx.attr.source_prefix,
150 [f.short_path for f in srcs],
151 ),
152 fragment_generator(
153 srcrule.label,
154 libname + "_hdrs",
155 ctx.attr.source_prefix,
156 [f.short_path for f in hdrs],
157 ),
158 ])
159
160 if ProtoFileList in srcrule:
161 proto_file_list = srcrule[ProtoFileList]
162 fragments.extend([
163 fragment_generator(
164 srcrule.label,
165 libname + "_proto_srcs",
166 ctx.attr.source_prefix,
167 [f.short_path for f in proto_file_list.proto_srcs],
168 ),
169 fragment_generator(
170 srcrule.label,
171 libname + "_srcs",
172 ctx.attr.source_prefix,
173 proto_file_list.srcs,
174 ),
175 fragment_generator(
176 srcrule.label,
177 libname + "_hdrs",
178 ctx.attr.source_prefix,
179 proto_file_list.hdrs,
180 ),
181 ])
182
183 files = {}
184
185 if PackageFilegroupInfo in srcrule:
186 for pkg_files_info, origin in srcrule[PackageFilegroupInfo].pkg_files:
187 # keys are the destination path:
188 files.update(pkg_files_info.dest_src_map)
189
190 if PackageFilesInfo in srcrule:
191 # keys are the destination:
192 files.update(srcrule[PackageFilesInfo].dest_src_map)
193
194 if files == {} and DefaultInfo in srcrule and CcFileList not in srcrule:
195 # This could be an individual file or filegroup.
196 # We explicitly ignore rules with CcInfo, since their
197 # output artifacts are libraries or binaries.
198 files.update(
199 {
200 f.short_path: 1
201 for f in srcrule[DefaultInfo].files.to_list()
202 },
203 )
204
205 if files:
206 fragments.append(
207 fragment_generator(
208 srcrule.label,
209 libname + "_files",
210 ctx.attr.source_prefix,
211 sorted(files.keys()),
212 ),
213 )
214
215 generator_label = "@//%s:%s" % (ctx.label.package, ctx.label.name)
216 ctx.actions.write(
217 output = out,
218 content = (ctx.attr._header % generator_label) + "\n".join(fragments),
219 )
220
221 return [DefaultInfo(files = depset([out]))]
222
223 # Common rule attrs for rules that use `_create_file_list_impl`:
224 # (note that `_header` is also required)
225 _source_list_common_attrs = {
226 "out": attr.output(
227 doc = (
228 "The generated filename. This should usually have a build " +
229 "system-specific extension, like `out.am` or `out.cmake`."
230 ),
231 mandatory = True,
232 ),
233 "src_libs": attr.label_keyed_string_dict(
234 doc = (
235 "A dict, {target: libname} of libraries to include. " +
236 "Targets can be C++ rules (like `cc_library` or `cc_test`), " +
237 "`proto_library` rules, files, `filegroup` rules, `pkg_files` " +
238 "rules, or `pkg_filegroup` rules. " +
239 "The libname is a string, and used to construct the variable " +
240 "name in the `out` file holding the target's sources. " +
241 "For generated files, the output root (like `bazel-bin/`) is not " +
242 "included. " +
243 "For `pkg_files` and `pkg_filegroup` rules, the destination path " +
244 "is used."
245 ),
246 mandatory = True,
247 providers = [
248 [CcFileList],
249 [DefaultInfo],
250 [PackageFilegroupInfo],
251 [PackageFilesInfo],
252 [ProtoFileList],
253 ],
254 aspects = [file_list_aspect],
255 ),
256 "source_prefix": attr.string(
257 doc = "String to prepend to each source path.",
258 ),
259 }
260
261 ################################################################################
262 # CMake source lists generation
263 ################################################################################
264
265 def _cmake_var_fragment(owner, varname, prefix, entries):
266 """Returns a single `set(varname ...)` fragment (CMake syntax).
267
268 Args:
269 owner: Label, the rule that owns these srcs.
270 varname: str, the var name to set.
271 prefix: str, prefix to prepend to each of `entries`.
272 entries: [str], the entries in the list.
273
274 Returns:
275 A string.
276 """
277 return (
278 "# @//{package}:{name}\n" +
279 "set({varname}\n" +
280 "{entries}\n" +
281 ")\n"
282 ).format(
283 package = owner.package,
284 name = owner.name,
285 varname = varname,
286 # Strip out "wkt/google/protobuf/" from the well-known type file paths.
287 # This is currently necessary to allow checked-in and generated
288 # versions of the well-known type generated code to coexist.
289 entries = "\n".join([" %s%s" % (prefix, f.replace("wkt/google/protobuf/", "")) for f in entries]),
290 )
291
292 def _cmake_file_list_impl(ctx):
293 _create_file_list_impl(ctx, _cmake_var_fragment)
294
295 gen_cmake_file_lists = rule(
296 doc = """
297 Generates a CMake-syntax file with lists of files.
298
299 The generated file defines variables with lists of files from `srcs`. The
300 intent is for these files to be included from a non-generated CMake file
301 which actually defines the libraries based on these lists.
302
303 For C++ rules, the following are generated:
304 {libname}_srcs: contains srcs.
305 {libname}_hdrs: contains hdrs and textual_hdrs.
306
307 For proto_library, the following are generated:
308 {libname}_proto_srcs: contains the srcs from the `proto_library` rule.
309 {libname}_srcs: contains syntesized paths for generated C++ sources.
310 {libname}_hdrs: contains syntesized paths for generated C++ headers.
311
312 """,
313 implementation = _cmake_file_list_impl,
314 attrs = dict(
315 _source_list_common_attrs,
316 _header = attr.string(
317 default = """\
318 # Auto-generated by %s
319 #
320 # This file contains lists of sources based on Bazel rules. It should
321 # be included from a hand-written CMake file that defines targets.
322 #
323 # Changes to this file will be overwritten based on Bazel definitions.
324
325 if(${CMAKE_VERSION} VERSION_GREATER 3.10 OR ${CMAKE_VERSION} VERSION_EQUAL 3.10)
326 include_guard()
327 endif()
328
329 """,
330 ),
331 ),
332 )