1*60517a1eSAndroid Build Coastguard Worker# Starlark docgen 2*60517a1eSAndroid Build Coastguard Worker 3*60517a1eSAndroid Build Coastguard WorkerUsing the `sphinx_stardoc` rule, API documentation can be generated from bzl 4*60517a1eSAndroid Build Coastguard Workersource code. This rule requires both MyST-based markdown and the `sphinx_bzl` 5*60517a1eSAndroid Build Coastguard WorkerSphinx extension are enabled. This allows source code to use Markdown and 6*60517a1eSAndroid Build Coastguard WorkerSphinx syntax to create rich documentation with cross references, types, and 7*60517a1eSAndroid Build Coastguard Workermore. 8*60517a1eSAndroid Build Coastguard Worker 9*60517a1eSAndroid Build Coastguard Worker 10*60517a1eSAndroid Build Coastguard Worker## Configuring Sphinx 11*60517a1eSAndroid Build Coastguard Worker 12*60517a1eSAndroid Build Coastguard WorkerWhile the `sphinx_stardoc` rule doesn't require Sphinx itself, the source 13*60517a1eSAndroid Build Coastguard Workerit generates requires some additional Sphinx plugins and config settings. 14*60517a1eSAndroid Build Coastguard Worker 15*60517a1eSAndroid Build Coastguard WorkerWhen defining the `sphinx_build_binary` target, also depend on: 16*60517a1eSAndroid Build Coastguard Worker* `@rules_python//sphinxdocs/src/sphinx_bzl:sphinx_bzl` 17*60517a1eSAndroid Build Coastguard Worker* `myst_parser` (e.g. `@pypi//myst_parser`) 18*60517a1eSAndroid Build Coastguard Worker* `typing_extensions` (e.g. `@pypi//myst_parser`) 19*60517a1eSAndroid Build Coastguard Worker 20*60517a1eSAndroid Build Coastguard Worker``` 21*60517a1eSAndroid Build Coastguard Workersphinx_build_binary( 22*60517a1eSAndroid Build Coastguard Worker name = "sphinx-build", 23*60517a1eSAndroid Build Coastguard Worker deps = [ 24*60517a1eSAndroid Build Coastguard Worker "@rules_python//sphinxdocs/src/sphinx_bzl", 25*60517a1eSAndroid Build Coastguard Worker "@pypi//myst_parser", 26*60517a1eSAndroid Build Coastguard Worker "@pypi//typing_extensions", 27*60517a1eSAndroid Build Coastguard Worker ... 28*60517a1eSAndroid Build Coastguard Worker ] 29*60517a1eSAndroid Build Coastguard Worker) 30*60517a1eSAndroid Build Coastguard Worker``` 31*60517a1eSAndroid Build Coastguard Worker 32*60517a1eSAndroid Build Coastguard WorkerIn `conf.py`, enable the `sphinx_bzl` extension, `myst_parser` extension, 33*60517a1eSAndroid Build Coastguard Workerand the `colon_fence` MyST extension. 34*60517a1eSAndroid Build Coastguard Worker 35*60517a1eSAndroid Build Coastguard Worker``` 36*60517a1eSAndroid Build Coastguard Workerextensions = [ 37*60517a1eSAndroid Build Coastguard Worker "myst_parser", 38*60517a1eSAndroid Build Coastguard Worker "sphinx_bzl.bzl", 39*60517a1eSAndroid Build Coastguard Worker] 40*60517a1eSAndroid Build Coastguard Worker 41*60517a1eSAndroid Build Coastguard Workermyst_enable_extensions = [ 42*60517a1eSAndroid Build Coastguard Worker "colon_fence", 43*60517a1eSAndroid Build Coastguard Worker] 44*60517a1eSAndroid Build Coastguard Worker``` 45*60517a1eSAndroid Build Coastguard Worker 46*60517a1eSAndroid Build Coastguard Worker## Generating docs from bzl files 47*60517a1eSAndroid Build Coastguard Worker 48*60517a1eSAndroid Build Coastguard WorkerTo convert the bzl code to Sphinx doc sources, `sphinx_stardocs` is the primary 49*60517a1eSAndroid Build Coastguard Workerrule to do so. It takes a list of `bzl_library` targets or files and generates docs for 50*60517a1eSAndroid Build Coastguard Workereach. When a `bzl_library` target is passed, the `bzl_library.srcs` value can only 51*60517a1eSAndroid Build Coastguard Workerhave a single file. 52*60517a1eSAndroid Build Coastguard Worker 53*60517a1eSAndroid Build Coastguard WorkerExample: 54*60517a1eSAndroid Build Coastguard Worker 55*60517a1eSAndroid Build Coastguard Worker``` 56*60517a1eSAndroid Build Coastguard Workersphinx_stardocs( 57*60517a1eSAndroid Build Coastguard Worker name = "my_docs", 58*60517a1eSAndroid Build Coastguard Worker srcs = [ 59*60517a1eSAndroid Build Coastguard Worker ":binary_bzl", 60*60517a1eSAndroid Build Coastguard Worker ":library_bzl", 61*60517a1eSAndroid Build Coastguard Worker ] 62*60517a1eSAndroid Build Coastguard Worker) 63*60517a1eSAndroid Build Coastguard Worker 64*60517a1eSAndroid Build Coastguard Workerbzl_library( 65*60517a1eSAndroid Build Coastguard Worker name = "binary_bzl", 66*60517a1eSAndroid Build Coastguard Worker srcs = ["binary.bzl"], 67*60517a1eSAndroid Build Coastguard Worker deps = ... 68*60517a1eSAndroid Build Coastguard Worker) 69*60517a1eSAndroid Build Coastguard Worker 70*60517a1eSAndroid Build Coastguard Workerbzl_library( 71*60517a1eSAndroid Build Coastguard Worker name = "library_bzl", 72*60517a1eSAndroid Build Coastguard Worker srcs = ["library.bzl"], 73*60517a1eSAndroid Build Coastguard Worker deps = ... 74*60517a1eSAndroid Build Coastguard Worker) 75*60517a1eSAndroid Build Coastguard Worker``` 76