blob: 41493009423e33e63236b85477bad8234d1c8c99 [file] [log] [blame]
Lorenz Brundcfc6782021-11-30 05:27:48 +01001load("@rules_proto//proto:defs.bzl", "ProtoInfo")
2
3def _proto_docs(ctx):
4 protos = [proto[ProtoInfo] for proto in ctx.attr.protos]
5 transitive_sources = depset(transitive = [proto.transitive_sources for proto in protos])
6 transitive_proto_path = depset(transitive = [proto.transitive_proto_path for proto in protos])
7
8 out = ctx.actions.declare_file(ctx.label.name + ".html")
9
10 args = []
11 args.append("--plugin")
12 args.append(ctx.executable._protoc_gen_doc.path)
13 args.append("--doc_out")
14 args.append(out.dirname)
15 args.append("--doc_opt=html," + out.basename)
16
17 for include_path in transitive_proto_path.to_list():
18 args.append("-I")
19 args.append(include_path)
20
21 for src in transitive_sources.to_list():
22 # Due to the built-in import path for well-known types (see AddDefaultProtoPaths
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020023 # in @protobuf//src/google/protobuf/compiler:command_line_interface.cc)
Lorenz Brundcfc6782021-11-30 05:27:48 +010024 # in protoc the Bazel-generated well-known protos are considered to contain
25 # "duplicate" types.
26 # Since generating documentation for well-known types is not that useful just
27 # skip them.
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020028 if src.path.find("/external/protobuf~/") != -1:
Lorenz Brundcfc6782021-11-30 05:27:48 +010029 continue
30 args.append(src.path)
31
Tim Windelschmidt1fc5eb02024-11-12 13:21:52 +010032 protoc = ctx.toolchains["@rules_proto//proto:toolchain_type"].proto.proto_compiler.executable
33
Lorenz Brundcfc6782021-11-30 05:27:48 +010034 ctx.actions.run(
35 tools = [ctx.executable._protoc_gen_doc],
36 inputs = transitive_sources,
37 outputs = [out],
Tim Windelschmidt1fc5eb02024-11-12 13:21:52 +010038 executable = protoc,
Lorenz Brundcfc6782021-11-30 05:27:48 +010039 arguments = args,
40 )
41 return [DefaultInfo(files = depset([out]))]
42
43proto_docs = rule(
44 implementation = _proto_docs,
45 doc = """
46 Generate a single HTML documentation file documenting all types and services from the transitive set of
47 Protobuf files referenced by all proto_libraries passed into `protos`.
48 """,
49 attrs = {
50 "protos": attr.label_list(
51 doc = "A list of protobuf libraries for which (and their dependencies) documentation should be generated for",
52 providers = [ProtoInfo],
53 default = [],
54 ),
Lorenz Brundcfc6782021-11-30 05:27:48 +010055 "_protoc_gen_doc": attr.label(
56 default = Label("@com_github_pseudomuto_protoc_gen_doc//cmd/protoc-gen-doc"),
57 cfg = "exec",
58 executable = True,
59 allow_files = True,
60 ),
61 },
Tim Windelschmidt1fc5eb02024-11-12 13:21:52 +010062 toolchains = ["@rules_proto//proto:toolchain_type"]
Lorenz Brundcfc6782021-11-30 05:27:48 +010063)