xref: /aosp_15_r20/external/bazelbuild-rules_python/sphinxdocs/docs/sphinx-bzl.md (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Bazel plugin for Sphinx
2*60517a1eSAndroid Build Coastguard Worker
3*60517a1eSAndroid Build Coastguard WorkerThe `sphinx_bzl` Python package is a Sphinx plugin that defines a custom domain
4*60517a1eSAndroid Build Coastguard Worker("bzl") in the Sphinx system. This provides first-class integration with Sphinx
5*60517a1eSAndroid Build Coastguard Workerand allows code comments to provide rich information and allows manually writing
6*60517a1eSAndroid Build Coastguard Workerdocs for objects that aren't directly representable in bzl source code. For
7*60517a1eSAndroid Build Coastguard Workerexample, the fields of a provider can use `:type:` to indicate the type of a
8*60517a1eSAndroid Build Coastguard Workerfield, or manually written docs can use the `{bzl:target}` directive to document
9*60517a1eSAndroid Build Coastguard Workera well known target.
10*60517a1eSAndroid Build Coastguard Worker
11*60517a1eSAndroid Build Coastguard Worker## Configuring Sphinx
12*60517a1eSAndroid Build Coastguard Worker
13*60517a1eSAndroid Build Coastguard WorkerTo enable the plugin in Sphinx, depend on
14*60517a1eSAndroid Build Coastguard Worker`@rules_python//sphinxdocs/src/sphinx_bzl` and enable it in `conf.py`:
15*60517a1eSAndroid Build Coastguard Worker
16*60517a1eSAndroid Build Coastguard Worker```
17*60517a1eSAndroid Build Coastguard Workerextensions = [
18*60517a1eSAndroid Build Coastguard Worker    "sphinx_bzl.bzl",
19*60517a1eSAndroid Build Coastguard Worker]
20*60517a1eSAndroid Build Coastguard Worker```
21*60517a1eSAndroid Build Coastguard Worker
22*60517a1eSAndroid Build Coastguard Worker## Brief introduction to Sphinx terminology
23*60517a1eSAndroid Build Coastguard Worker
24*60517a1eSAndroid Build Coastguard WorkerTo aid understanding how to write docs, lets define a few common terms:
25*60517a1eSAndroid Build Coastguard Worker
26*60517a1eSAndroid Build Coastguard Worker* **Role**: A role is the "bzl:obj" part when writing ``{bzl:obj}`ref` ``.
27*60517a1eSAndroid Build Coastguard Worker  Roles mark inline text as needing special processing. There's generally
28*60517a1eSAndroid Build Coastguard Worker  two types of processing: creating cross references, or role-specific custom
29*60517a1eSAndroid Build Coastguard Worker  rendering. For example `{bzl:obj}` will create a cross references, while
30*60517a1eSAndroid Build Coastguard Worker  `{bzl:default-value}` indicates the default value of an argument.
31*60517a1eSAndroid Build Coastguard Worker* **Directive**: A directive is indicated with `:::` and allows defining an
32*60517a1eSAndroid Build Coastguard Worker  entire object and its parts. For example, to describe a function and its
33*60517a1eSAndroid Build Coastguard Worker  arguments, the `:::{bzl:function}` directive is used.
34*60517a1eSAndroid Build Coastguard Worker* **Directive Option**: A directive option is the "type" part when writing
35*60517a1eSAndroid Build Coastguard Worker  `:type:` within a directive. Directive options are how directives are told
36*60517a1eSAndroid Build Coastguard Worker  the meaning of certain values, such as the type of a provider field. Depending
37*60517a1eSAndroid Build Coastguard Worker  on the object being documented, a directive option may be used instead of
38*60517a1eSAndroid Build Coastguard Worker  special role to indicate semantic values.
39*60517a1eSAndroid Build Coastguard Worker
40*60517a1eSAndroid Build Coastguard WorkerMost often, you'll be using roles to refer other objects or indicate special
41*60517a1eSAndroid Build Coastguard Workervalues in doc strings. For directives, you're likely to only use them when
42*60517a1eSAndroid Build Coastguard Workermanually writing docs to document flags, targets, or other objects that
43*60517a1eSAndroid Build Coastguard Worker`sphinx_stardoc` generates for you.
44*60517a1eSAndroid Build Coastguard Worker
45*60517a1eSAndroid Build Coastguard Worker## MyST vs RST
46*60517a1eSAndroid Build Coastguard Worker
47*60517a1eSAndroid Build Coastguard WorkerBy default, Sphinx uses ReStructured Text (RST) syntax for its documents.
48*60517a1eSAndroid Build Coastguard WorkerUnfortunately, RST syntax is very different than the popular Markdown syntax. To
49*60517a1eSAndroid Build Coastguard Workerbridge the gap, MyST translates Markdown-style syntax into the RST equivalents.
50*60517a1eSAndroid Build Coastguard WorkerThis allows easily using Markdown in bzl files.
51*60517a1eSAndroid Build Coastguard Worker
52*60517a1eSAndroid Build Coastguard WorkerWhile MyST isn't required for the core `sphinx_bzl` plugin to work, this
53*60517a1eSAndroid Build Coastguard Workerdocument uses MyST syntax because `sphinx_stardoc` bzl doc gen rule requires
54*60517a1eSAndroid Build Coastguard WorkerMyST.
55*60517a1eSAndroid Build Coastguard Worker
56*60517a1eSAndroid Build Coastguard WorkerThe main difference in syntax is:
57*60517a1eSAndroid Build Coastguard Worker* MyST directives use `:::{name}` with closing `:::` instead of `.. name::` with
58*60517a1eSAndroid Build Coastguard Worker  indented content.
59*60517a1eSAndroid Build Coastguard Worker* MyST roles use `{role:name}` instead of `:role:name:`
60*60517a1eSAndroid Build Coastguard Worker
61*60517a1eSAndroid Build Coastguard Worker## Type expressions
62*60517a1eSAndroid Build Coastguard Worker
63*60517a1eSAndroid Build Coastguard WorkerSeveral roles or fields accept type expressions. Type expressions use
64*60517a1eSAndroid Build Coastguard WorkerPython-style annotation syntax to describe data types. For example `None | list[str]`
65*60517a1eSAndroid Build Coastguard Workerdescribes a type of "None or a list of strings". Each component of the
66*60517a1eSAndroid Build Coastguard Workerexpression is parsed and cross reference to its associated type definition.
67*60517a1eSAndroid Build Coastguard Worker
68*60517a1eSAndroid Build Coastguard Worker## Cross references
69*60517a1eSAndroid Build Coastguard Worker
70*60517a1eSAndroid Build Coastguard WorkerIn brief, to reference bzl objects, use the `bzl:obj` role and use the
71*60517a1eSAndroid Build Coastguard WorkerBazel label string you would use to refer to the object in Bazel (using `%` to
72*60517a1eSAndroid Build Coastguard Workerdenote names within a file). For example, to unambiguously refer to `py_binary`:
73*60517a1eSAndroid Build Coastguard Worker
74*60517a1eSAndroid Build Coastguard Worker```
75*60517a1eSAndroid Build Coastguard Worker{bzl:obj}`@rules_python//python:py_binary.bzl%py_binary`
76*60517a1eSAndroid Build Coastguard Worker```
77*60517a1eSAndroid Build Coastguard Worker
78*60517a1eSAndroid Build Coastguard WorkerThe above is pretty long, so shorter names are also supported, and `sphinx_bzl`
79*60517a1eSAndroid Build Coastguard Workerwill try to find something that matches. Additionally, in `.bzl` code, the
80*60517a1eSAndroid Build Coastguard Worker`bzl:` prefix is set as the default. The above can then be shortened to:
81*60517a1eSAndroid Build Coastguard Worker
82*60517a1eSAndroid Build Coastguard Worker```
83*60517a1eSAndroid Build Coastguard Worker{obj}`py_binary`
84*60517a1eSAndroid Build Coastguard Worker```
85*60517a1eSAndroid Build Coastguard Worker
86*60517a1eSAndroid Build Coastguard WorkerThe text that is displayed can be customized by putting the reference string in
87*60517a1eSAndroid Build Coastguard Workerchevrons (`<>`):
88*60517a1eSAndroid Build Coastguard Worker
89*60517a1eSAndroid Build Coastguard Worker```
90*60517a1eSAndroid Build Coastguard Worker{obj}`the binary rule <py_binary>`
91*60517a1eSAndroid Build Coastguard Worker```
92*60517a1eSAndroid Build Coastguard Worker
93*60517a1eSAndroid Build Coastguard WorkerSpecific types of objects (rules, functions, providers, etc) can be
94*60517a1eSAndroid Build Coastguard Workerspecified to help disambiguate short names:
95*60517a1eSAndroid Build Coastguard Worker
96*60517a1eSAndroid Build Coastguard Worker```
97*60517a1eSAndroid Build Coastguard Worker{function}`py_binary`  # Refers to the wrapping macro
98*60517a1eSAndroid Build Coastguard Worker{rule}`py_binary`  # Refers to the underlying rule
99*60517a1eSAndroid Build Coastguard Worker```
100*60517a1eSAndroid Build Coastguard Worker
101*60517a1eSAndroid Build Coastguard WorkerFinally, objects built into Bazel can be explicitly referenced by forcing
102*60517a1eSAndroid Build Coastguard Workera lookup outside the local project using `{external}`. For example, the symbol
103*60517a1eSAndroid Build Coastguard Worker`toolchain` is a builtin Bazel function, but it could also be the name of a tag
104*60517a1eSAndroid Build Coastguard Workerclass in the local project. To force looking up the builtin Bazel `toolchain` rule,
105*60517a1eSAndroid Build Coastguard Worker`{external:bzl:rule}` can be used, e.g.:
106*60517a1eSAndroid Build Coastguard Worker
107*60517a1eSAndroid Build Coastguard Worker```
108*60517a1eSAndroid Build Coastguard Worker{external:bzl:obj}`toolchain`
109*60517a1eSAndroid Build Coastguard Worker```
110*60517a1eSAndroid Build Coastguard Worker
111*60517a1eSAndroid Build Coastguard WorkerThose are the basics of cross referencing. Sphinx has several additional
112*60517a1eSAndroid Build Coastguard Workersyntaxes for finding and referencing objects; see
113*60517a1eSAndroid Build Coastguard Worker[the MyST docs for supported
114*60517a1eSAndroid Build Coastguard Workersyntaxes](https://myst-parser.readthedocs.io/en/latest/syntax/cross-referencing.html#reference-roles)
115*60517a1eSAndroid Build Coastguard Worker
116*60517a1eSAndroid Build Coastguard Worker### Cross reference roles
117*60517a1eSAndroid Build Coastguard Worker
118*60517a1eSAndroid Build Coastguard WorkerA cross reference role is the `obj` portion of `{bzl:obj}`. It affects what is
119*60517a1eSAndroid Build Coastguard Workersearched and matched.
120*60517a1eSAndroid Build Coastguard Worker
121*60517a1eSAndroid Build Coastguard Worker:::{note}
122*60517a1eSAndroid Build Coastguard WorkerThe documentation renders using RST notation (`:foo:role:`), not
123*60517a1eSAndroid Build Coastguard WorkerMyST notation (`{foo:role}`.
124*60517a1eSAndroid Build Coastguard Worker:::
125*60517a1eSAndroid Build Coastguard Worker
126*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:arg
127*60517a1eSAndroid Build Coastguard WorkerRefer to a function argument.
128*60517a1eSAndroid Build Coastguard Worker:::
129*60517a1eSAndroid Build Coastguard Worker
130*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:attr
131*60517a1eSAndroid Build Coastguard WorkerRefer to a rule attribute.
132*60517a1eSAndroid Build Coastguard Worker:::
133*60517a1eSAndroid Build Coastguard Worker
134*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:flag
135*60517a1eSAndroid Build Coastguard WorkerRefer to a flag.
136*60517a1eSAndroid Build Coastguard Worker:::
137*60517a1eSAndroid Build Coastguard Worker
138*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:obj
139*60517a1eSAndroid Build Coastguard WorkerRefer to any type of Bazel object
140*60517a1eSAndroid Build Coastguard Worker:::
141*60517a1eSAndroid Build Coastguard Worker
142*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:rule
143*60517a1eSAndroid Build Coastguard WorkerRefer to a rule.
144*60517a1eSAndroid Build Coastguard Worker:::
145*60517a1eSAndroid Build Coastguard Worker
146*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:target
147*60517a1eSAndroid Build Coastguard WorkerRefer to a target.
148*60517a1eSAndroid Build Coastguard Worker:::
149*60517a1eSAndroid Build Coastguard Worker
150*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:type
151*60517a1eSAndroid Build Coastguard WorkerRefer to a type or type expression; can also be used in argument documentation.
152*60517a1eSAndroid Build Coastguard Worker
153*60517a1eSAndroid Build Coastguard Worker```
154*60517a1eSAndroid Build Coastguard Workerdef func(arg):
155*60517a1eSAndroid Build Coastguard Worker    """Do stuff
156*60517a1eSAndroid Build Coastguard Worker
157*60517a1eSAndroid Build Coastguard Worker    Args:
158*60517a1eSAndroid Build Coastguard Worker      arg: {type}`int | str` the arg
159*60517a1eSAndroid Build Coastguard Worker    """
160*60517a1eSAndroid Build Coastguard Worker    print(arg + 1)
161*60517a1eSAndroid Build Coastguard Worker```
162*60517a1eSAndroid Build Coastguard Worker:::
163*60517a1eSAndroid Build Coastguard Worker
164*60517a1eSAndroid Build Coastguard Worker## Special roles
165*60517a1eSAndroid Build Coastguard Worker
166*60517a1eSAndroid Build Coastguard WorkerThere are several special roles that can be used to annotate parts of objects,
167*60517a1eSAndroid Build Coastguard Workersuch as the type of arguments or their default values.
168*60517a1eSAndroid Build Coastguard Worker
169*60517a1eSAndroid Build Coastguard Worker:::{note}
170*60517a1eSAndroid Build Coastguard WorkerThe documentation renders using RST notation (`:foo:role:`), not
171*60517a1eSAndroid Build Coastguard WorkerMyST notation (`{foo:role}`.
172*60517a1eSAndroid Build Coastguard Worker:::
173*60517a1eSAndroid Build Coastguard Worker
174*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:default-value
175*60517a1eSAndroid Build Coastguard Worker
176*60517a1eSAndroid Build Coastguard WorkerIndicate the default value for a function argument or rule attribute. Use it in
177*60517a1eSAndroid Build Coastguard Workerthe Args doc of a function or the doc text of an attribute.
178*60517a1eSAndroid Build Coastguard Worker
179*60517a1eSAndroid Build Coastguard Worker```
180*60517a1eSAndroid Build Coastguard Workerdef func(arg=1):
181*60517a1eSAndroid Build Coastguard Worker   """Do stuff
182*60517a1eSAndroid Build Coastguard Worker
183*60517a1eSAndroid Build Coastguard Worker   Args:
184*60517a1eSAndroid Build Coastguard Worker     foo: {default-value}`1` the arg
185*60517a1eSAndroid Build Coastguard Worker
186*60517a1eSAndroid Build Coastguard Workermy_rule = rule(attrs = {
187*60517a1eSAndroid Build Coastguard Worker    "foo": attr.string(doc="{default-value}`bar`)
188*60517a1eSAndroid Build Coastguard Worker})
189*60517a1eSAndroid Build Coastguard Worker
190*60517a1eSAndroid Build Coastguard Worker```
191*60517a1eSAndroid Build Coastguard Worker:::
192*60517a1eSAndroid Build Coastguard Worker
193*60517a1eSAndroid Build Coastguard Worker:::{rst:role} bzl:return-type
194*60517a1eSAndroid Build Coastguard Worker
195*60517a1eSAndroid Build Coastguard WorkerIndicates the return type for a function. Use it in the Returns doc of a
196*60517a1eSAndroid Build Coastguard Workerfunction.
197*60517a1eSAndroid Build Coastguard Worker
198*60517a1eSAndroid Build Coastguard Worker```
199*60517a1eSAndroid Build Coastguard Workerdef func():
200*60517a1eSAndroid Build Coastguard Worker    """Do stuff
201*60517a1eSAndroid Build Coastguard Worker
202*60517a1eSAndroid Build Coastguard Worker    Returns:
203*60517a1eSAndroid Build Coastguard Worker      {return-type}`int`
204*60517a1eSAndroid Build Coastguard Worker    """
205*60517a1eSAndroid Build Coastguard Worker    return 1
206*60517a1eSAndroid Build Coastguard Worker```
207*60517a1eSAndroid Build Coastguard Worker:::
208*60517a1eSAndroid Build Coastguard Worker
209*60517a1eSAndroid Build Coastguard Worker## Directives
210*60517a1eSAndroid Build Coastguard Worker
211*60517a1eSAndroid Build Coastguard WorkerMost directives are automatically generated by `sphinx_stardoc`. Here, we only
212*60517a1eSAndroid Build Coastguard Workerdocument ones that must be manually written.
213*60517a1eSAndroid Build Coastguard Worker
214*60517a1eSAndroid Build Coastguard WorkerTo write a directive, a line starts with 3 to 6 colons (`:`), followed by the
215*60517a1eSAndroid Build Coastguard Workerdirective name in braces (`{}`), and eventually ended by the same number of
216*60517a1eSAndroid Build Coastguard Workercolons on their own line. For example:
217*60517a1eSAndroid Build Coastguard Worker
218*60517a1eSAndroid Build Coastguard Worker```
219*60517a1eSAndroid Build Coastguard Worker:::{bzl:target} //my:target
220*60517a1eSAndroid Build Coastguard Worker
221*60517a1eSAndroid Build Coastguard WorkerDoc about target
222*60517a1eSAndroid Build Coastguard Worker:::
223*60517a1eSAndroid Build Coastguard Worker```
224*60517a1eSAndroid Build Coastguard Worker
225*60517a1eSAndroid Build Coastguard Worker:::{note}
226*60517a1eSAndroid Build Coastguard WorkerThe documentation renders using RST notation (`.. directive::`), not
227*60517a1eSAndroid Build Coastguard WorkerMyST notation.
228*60517a1eSAndroid Build Coastguard Worker:::
229*60517a1eSAndroid Build Coastguard Worker
230*60517a1eSAndroid Build Coastguard Worker:::{rst:directive} .. bzl:currentfile:: file
231*60517a1eSAndroid Build Coastguard Worker
232*60517a1eSAndroid Build Coastguard WorkerThis directive indicates the Bazel file that objects defined in the current
233*60517a1eSAndroid Build Coastguard Workerdocumentation file are in. This is required for any page that defines Bazel
234*60517a1eSAndroid Build Coastguard Workerobjects. The format of `file` is Bazel label syntax, e.g. `//foo:bar.bzl` for bzl
235*60517a1eSAndroid Build Coastguard Workerfiles, and `//foo:BUILD.bazel` for things in BUILD files.
236*60517a1eSAndroid Build Coastguard Worker
237*60517a1eSAndroid Build Coastguard Worker:::
238*60517a1eSAndroid Build Coastguard Worker
239*60517a1eSAndroid Build Coastguard Worker
240*60517a1eSAndroid Build Coastguard Worker:::{rst:directive} .. bzl:target:: target
241*60517a1eSAndroid Build Coastguard Worker
242*60517a1eSAndroid Build Coastguard WorkerDocuments a target. It takes no directive options. The format of `target`
243*60517a1eSAndroid Build Coastguard Workercan either be a fully qualified label (`//foo:bar`), or the base target name
244*60517a1eSAndroid Build Coastguard Workerrelative to `{bzl:currentfile}`.
245*60517a1eSAndroid Build Coastguard Worker
246*60517a1eSAndroid Build Coastguard Worker```
247*60517a1eSAndroid Build Coastguard Worker:::{bzl:target} //foo:target
248*60517a1eSAndroid Build Coastguard Worker
249*60517a1eSAndroid Build Coastguard WorkerMy docs
250*60517a1eSAndroid Build Coastguard Worker:::
251*60517a1eSAndroid Build Coastguard Worker```
252*60517a1eSAndroid Build Coastguard Worker
253*60517a1eSAndroid Build Coastguard Worker:::{rst:directive} .. bzl:flag:: target
254*60517a1eSAndroid Build Coastguard Worker
255*60517a1eSAndroid Build Coastguard WorkerDocuments a flag. It has the same format as `{bzl:target}`
256*60517a1eSAndroid Build Coastguard Worker:::
257*60517a1eSAndroid Build Coastguard Worker
258