Skip to content

Commit 13ce192

Browse files
authored
sphinxdocs: add docs; support sources from other directories (bazel-contrib#2128)
Documents how to use Sphinx syntax when writing docs. There's a variety of features the `sphinx_bzl` plugin enables, but without docs, they're somewhat hard to discover and figure out how to use. Because sphinxdocs is almost entirely separate, adding its docs under `//sphinxdocs/docs` is a more natural fit. Unfortunately, this became very verbose, repetitive, and tedious, for two reasons: 1. The only way `sphinx_docs` could accept files from other directories was using the `rename_srcs` arg and manually renaming files one-by-one. 2. Similarly, `sphinx_stardocs` required a one-by-one mapping of each bzl file to its output file, which then had to be repeated in `rename_srcs`. To fix (1), the `sphinx_docs.deps` attribute and `sphinx_docs_library` rule are added. The library targets collect files, and `sphinx_docs` moves then into the final Sphinx sources directory. To fix (2), the `sphinx_stardoc.srcs` attribute is added, which accepts `bzl_library` targets. I noticed that, in almost all cases, the output name was simply the input name with the `.md` extension, so the rule now does that by default. For special cases, the `sphinx_stardoc` (singular) rule can be called directly. Also: * Adds `bzl:rule` as a cross reference lookup role * Removes some defunct stuff relating to the stardoc template files that aren't used anymore. * Disables warnings from the autosectionlabel extension. These were spamming warnings because CHANGELOG.md has many headers with the same name. * Adds more entries to bazel inventory (all of native and ctx)
1 parent 425dfb0 commit 13ce192

24 files changed

+1030
-213
lines changed

docs/sphinx/BUILD.bazel

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: dis
1919
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
2020
load("//sphinxdocs:readthedocs.bzl", "readthedocs_install")
2121
load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs")
22-
load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardocs")
22+
load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardoc", "sphinx_stardocs")
2323

2424
# We only build for Linux and Mac because:
2525
# 1. The actual doc process only runs on Linux
@@ -38,9 +38,7 @@ _TARGET_COMPATIBLE_WITH = select({
3838
# * `ibazel build //docs/sphinx:docs` to automatically rebuild docs
3939
sphinx_docs(
4040
name = "docs",
41-
srcs = [
42-
":bzl_api_docs",
43-
] + glob(
41+
srcs = glob(
4442
include = [
4543
"*.md",
4644
"**/*.md",
@@ -66,43 +64,56 @@ sphinx_docs(
6664
strip_prefix = package_name() + "/",
6765
tags = ["docs"],
6866
target_compatible_with = _TARGET_COMPATIBLE_WITH,
67+
deps = [
68+
":bzl_api_docs",
69+
"//sphinxdocs/docs:docs_lib",
70+
],
6971
)
7072

7173
sphinx_stardocs(
7274
name = "bzl_api_docs",
73-
docs = {
74-
"api/python/cc/py_cc_toolchain.md": dict(
75-
dep = "//python/private:py_cc_toolchain_bzl",
76-
input = "//python/private:py_cc_toolchain_rule.bzl",
77-
public_load_path = "//python/cc:py_cc_toolchain.bzl",
78-
),
79-
"api/python/cc/py_cc_toolchain_info.md": "//python/cc:py_cc_toolchain_info_bzl",
80-
"api/python/defs.md": "//python:defs_bzl",
81-
"api/python/entry_points/py_console_script_binary.md": "//python/entry_points:py_console_script_binary_bzl",
82-
"api/python/packaging.md": "//python:packaging_bzl",
83-
"api/python/pip.md": "//python:pip_bzl",
84-
"api/python/private/common/py_binary_rule_bazel.md": "//python/private/common:py_binary_rule_bazel_bzl",
85-
"api/python/private/common/py_library_rule_bazel.md": "//python/private/common:py_library_rule_bazel_bzl",
86-
"api/python/private/common/py_runtime_rule.md": "//python/private/common:py_runtime_rule_bzl",
87-
"api/python/private/common/py_test_rule_bazel.md": "//python/private/common:py_test_rule_bazel_bzl",
88-
"api/python/py_binary.md": "//python:py_binary_bzl",
89-
"api/python/py_cc_link_params_info.md": "//python:py_cc_link_params_info_bzl",
90-
"api/python/py_library.md": "//python:py_library_bzl",
91-
"api/python/py_runtime.md": "//python:py_runtime_bzl",
92-
"api/python/py_runtime_info.md": "//python:py_runtime_info_bzl",
93-
"api/python/py_runtime_pair.md": dict(
94-
dep = "//python/private:py_runtime_pair_rule_bzl",
95-
input = "//python/private:py_runtime_pair_rule.bzl",
96-
public_load_path = "//python:py_runtime_pair.bzl",
97-
),
98-
"api/python/py_test.md": "//python:py_test_bzl",
99-
} | ({
75+
srcs = [
76+
"//python:defs_bzl",
77+
"//python:packaging_bzl",
78+
"//python:pip_bzl",
79+
"//python:py_binary_bzl",
80+
"//python:py_cc_link_params_info_bzl",
81+
"//python:py_library_bzl",
82+
"//python:py_runtime_bzl",
83+
"//python:py_runtime_info_bzl",
84+
"//python:py_test_bzl",
85+
"//python/cc:py_cc_toolchain_info_bzl",
86+
"//python/entry_points:py_console_script_binary_bzl",
87+
"//python/private/common:py_binary_rule_bazel_bzl",
88+
"//python/private/common:py_library_rule_bazel_bzl",
89+
"//python/private/common:py_runtime_rule_bzl",
90+
"//python/private/common:py_test_rule_bazel_bzl",
91+
] + ([
10092
# Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension
101-
"api/python/extensions/python.md": "//python/extensions:python_bzl",
102-
} if IS_BAZEL_7_OR_HIGHER else {}) | ({
93+
"//python/extensions:python_bzl",
94+
] if IS_BAZEL_7_OR_HIGHER else []) + ([
10395
# This depends on @pythons_hub, which is only created under bzlmod,
104-
"api/python/extensions/pip.md": "//python/extensions:pip_bzl",
105-
} if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else {}),
96+
"//python/extensions:pip_bzl",
97+
] if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else []),
98+
prefix = "api/",
99+
tags = ["docs"],
100+
target_compatible_with = _TARGET_COMPATIBLE_WITH,
101+
)
102+
103+
sphinx_stardoc(
104+
name = "py_cc_toolchain",
105+
src = "//python/private:py_cc_toolchain_rule.bzl",
106+
prefix = "api/",
107+
public_load_path = "//python/cc:py_cc_toolchain.bzl",
108+
tags = ["docs"],
109+
target_compatible_with = _TARGET_COMPATIBLE_WITH,
110+
deps = ["//python/cc:py_cc_toolchain_bzl"],
111+
)
112+
113+
sphinx_stardoc(
114+
name = "py_runtime_pair",
115+
src = "//python/private:py_runtime_pair_rule_bzl",
116+
public_load_path = "//python:py_runtime_pair.bzl",
106117
tags = ["docs"],
107118
target_compatible_with = _TARGET_COMPATIBLE_WITH,
108119
)

docs/sphinx/_stardoc_footer.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

docs/sphinx/conf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@
144144
# -- Options for EPUB output
145145
epub_show_urls = "footnote"
146146

147-
suppress_warnings = []
147+
suppress_warnings = [
148+
# The autosectionlabel extension turns header titles into referencable
149+
# names. Unfortunately, CHANGELOG.md has many duplicate header titles,
150+
# which creates lots of warning spam. Just ignore them.
151+
"autosectionlabel.*"
152+
]
148153

149154

150155
def setup(app):

docs/sphinx/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ support
6767
Changelog <changelog>
6868
api/index
6969
environment-variables
70+
Sphinxdocs <sphinxdocs/index>
7071
glossary
7172
genindex
7273
```

sphinxdocs/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ bzl_library(
4747
deps = ["//sphinxdocs/private:sphinx_bzl"],
4848
)
4949

50+
bzl_library(
51+
name = "sphinx_docs_library_bzl",
52+
srcs = ["sphinx_docs_library.bzl"],
53+
deps = ["//sphinxdocs/private:sphinx_docs_library_macro_bzl"],
54+
)
55+
5056
bzl_library(
5157
name = "sphinx_stardoc_bzl",
5258
srcs = ["sphinx_stardoc.bzl"],

sphinxdocs/docs/BUILD.bazel

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
2+
load("//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
3+
load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardocs")
4+
5+
package(default_visibility = ["//:__subpackages__"])
6+
7+
# We only build for Linux and Mac because:
8+
# 1. The actual doc process only runs on Linux
9+
# 2. Mac is a common development platform, and is close enough to Linux
10+
# it's feasible to make work.
11+
# Making CI happy under Windows is too much of a headache, though, so we don't
12+
# bother with that.
13+
_TARGET_COMPATIBLE_WITH = select({
14+
"@platforms//os:linux": [],
15+
"@platforms//os:macos": [],
16+
"//conditions:default": ["@platforms//:incompatible"],
17+
}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"]
18+
19+
sphinx_docs_library(
20+
name = "docs_lib",
21+
deps = [
22+
":artisian_api_docs",
23+
":artisian_docs",
24+
":bzl_docs",
25+
],
26+
)
27+
28+
sphinx_docs_library(
29+
name = "artisian_docs",
30+
srcs = glob(
31+
["**/*.md"],
32+
exclude = ["api/**"],
33+
),
34+
prefix = "sphinxdocs/",
35+
)
36+
37+
sphinx_docs_library(
38+
name = "artisian_api_docs",
39+
srcs = glob(
40+
["api/**/*.md"],
41+
),
42+
)
43+
44+
sphinx_stardocs(
45+
name = "bzl_docs",
46+
srcs = [
47+
"//sphinxdocs:readthedocs_bzl",
48+
"//sphinxdocs:sphinx_bzl",
49+
"//sphinxdocs:sphinx_docs_library_bzl",
50+
"//sphinxdocs:sphinx_stardoc_bzl",
51+
"//sphinxdocs/private:sphinx_docs_library_bzl",
52+
],
53+
prefix = "api/",
54+
target_compatible_with = _TARGET_COMPATIBLE_WITH,
55+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
:::{bzl:currentfile} //sphinxdocs:BUILD.bazel
2+
:::
3+
4+
# //sphinxdocs
5+
6+
:::{bzl:flag} extra_defines
7+
Additional `-D` values to add to every Sphinx build.
8+
9+
This is a list flag. Multiple uses are accumulated.
10+
11+
This is most useful for overriding e.g. the version when performing
12+
release builds.
13+
:::
14+
15+
:::{bzl:flag} extra_env
16+
Additional environment variables to for every Sphinx build.
17+
18+
This is a list flag. Multiple uses are accumulated. Values are `key=value`
19+
format.
20+
:::
21+
22+
:::{bzl:flag} quiet
23+
Whether to add the `-q` arg to Sphinx invocations.
24+
25+
This is a boolean flag.
26+
27+
This is useful for debugging invocations or developing extensions. The Sphinx
28+
`-q` flag causes sphinx to produce additional output on stdout.
29+
:::
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
:::{bzl:currentfile} //sphinxdocs/inventories:BUILD.bazel
2+
:::
3+
4+
# //sphinxdocs/inventories
5+
6+
:::{bzl:target} bazel_inventory
7+
A Sphinx inventory of Bazel objects.
8+
9+
By including this target in your Sphinx build and enabling intersphinx, cross
10+
references to builtin Bazel objects can be written.
11+
:::

sphinxdocs/docs/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Docgen using Sphinx with Bazel
2+
3+
The `sphinxdocs` project allows using Bazel to run Sphinx to generate
4+
documentation. It comes with:
5+
6+
* Rules for running Sphinx
7+
* Rules for generating documentation for Starlark code.
8+
* A Sphinx plugin for documenting Starlark and Bazel objects.
9+
* Rules for readthedocs build integration.
10+
11+
While it is primarily oriented towards docgen for Starlark code, the core of it
12+
is agnostic as to what is being documented.
13+
14+
15+
```{toctree}
16+
:hidden:
17+
18+
starlark-docgen
19+
sphinx-bzl
20+
```

0 commit comments

Comments
 (0)