1
- # Copyright 2017 The Bazel Authors. All rights reserved.
1
+ # Copyright 2023 The Bazel Authors. All rights reserved.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
5
5
# You may obtain a copy of the License at
6
6
#
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
8
#
9
9
# Unless required by applicable law or agreed to in writing, software
10
10
# distributed under the License is distributed on an "AS IS" BASIS,
13
13
# limitations under the License.
14
14
15
15
load ("@bazel_skylib//:bzl_library.bzl" , "bzl_library" )
16
+ load ("@bazel_skylib//rules:write_file.bzl" , "write_file" )
17
+ load ("@dev_pip//:requirements.bzl" , "requirement" )
18
+ load ("//python:py_binary.bzl" , "py_binary" )
19
+ load ("//python/private:bzlmod_enabled.bzl" , "BZLMOD_ENABLED" ) # buildifier: disable=bzl-visibility
20
+ load ("//python/private:util.bzl" , "IS_BAZEL_7_OR_HIGHER" ) # buildifier: disable=bzl-visibility
21
+ load ("//sphinxdocs:readthedocs.bzl" , "readthedocs_install" )
22
+ load ("//sphinxdocs:sphinx.bzl" , "sphinx_build_binary" , "sphinx_docs" )
23
+ load ("//sphinxdocs:sphinx_stardoc.bzl" , "sphinx_stardoc" , "sphinx_stardocs" )
16
24
17
- # NOTE: Only public visibility for historical reasons.
18
- # This package is only for rules_python to generate its own docs.
19
- package (default_visibility = ["//visibility:public" ])
25
+ package (default_visibility = ["//:__subpackages__" ])
26
+
27
+ # We only build for Linux and Mac because:
28
+ # 1. The actual doc process only runs on Linux
29
+ # 2. Mac is a common development platform, and is close enough to Linux
30
+ # it's feasible to make work.
31
+ # Making CI happy under Windows is too much of a headache, though, so we don't
32
+ # bother with that.
33
+ _TARGET_COMPATIBLE_WITH = select ({
34
+ "@platforms//os:linux" : [],
35
+ "@platforms//os:macos" : [],
36
+ "//conditions:default" : ["@platforms//:incompatible" ],
37
+ }) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible" ]
38
+
39
+ # See README.md for instructions. Short version:
40
+ # * `bazel run //docs:docs.serve` in a separate terminal
41
+ # * `ibazel build //docs:docs` to automatically rebuild docs
42
+ sphinx_docs (
43
+ name = "docs" ,
44
+ srcs = glob (
45
+ include = [
46
+ "*.md" ,
47
+ "**/*.md" ,
48
+ "_static/**" ,
49
+ "_includes/**" ,
50
+ ],
51
+ exclude = [
52
+ "README.md" ,
53
+ "_*" ,
54
+ "*.inv*" ,
55
+ ],
56
+ ),
57
+ config = "conf.py" ,
58
+ formats = [
59
+ "html" ,
60
+ ],
61
+ renamed_srcs = {
62
+ "//:CHANGELOG.md" : "changelog.md" ,
63
+ "//:CONTRIBUTING.md" : "contributing.md" ,
64
+ "//sphinxdocs/inventories:bazel_inventory" : "bazel_inventory.inv" ,
65
+ },
66
+ sphinx = ":sphinx-build" ,
67
+ strip_prefix = package_name () + "/" ,
68
+ tags = ["docs" ],
69
+ target_compatible_with = _TARGET_COMPATIBLE_WITH ,
70
+ deps = [
71
+ ":bzl_api_docs" ,
72
+ "//sphinxdocs/docs:docs_lib" ,
73
+ ],
74
+ )
75
+
76
+ sphinx_stardocs (
77
+ name = "bzl_api_docs" ,
78
+ srcs = [
79
+ "//python:defs_bzl" ,
80
+ "//python:packaging_bzl" ,
81
+ "//python:pip_bzl" ,
82
+ "//python:py_binary_bzl" ,
83
+ "//python:py_cc_link_params_info_bzl" ,
84
+ "//python:py_library_bzl" ,
85
+ "//python:py_runtime_bzl" ,
86
+ "//python:py_runtime_info_bzl" ,
87
+ "//python:py_test_bzl" ,
88
+ "//python/cc:py_cc_toolchain_info_bzl" ,
89
+ "//python/entry_points:py_console_script_binary_bzl" ,
90
+ "//python/private/common:py_binary_rule_bazel_bzl" ,
91
+ "//python/private/common:py_library_rule_bazel_bzl" ,
92
+ "//python/private/common:py_runtime_rule_bzl" ,
93
+ "//python/private/common:py_test_rule_bazel_bzl" ,
94
+ ] + ([
95
+ # Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension
96
+ "//python/extensions:python_bzl" ,
97
+ ] if IS_BAZEL_7_OR_HIGHER else []) + ([
98
+ # This depends on @pythons_hub, which is only created under bzlmod,
99
+ "//python/extensions:pip_bzl" ,
100
+ ] if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else []),
101
+ prefix = "api/" ,
102
+ tags = ["docs" ],
103
+ target_compatible_with = _TARGET_COMPATIBLE_WITH ,
104
+ )
105
+
106
+ sphinx_stardoc (
107
+ name = "py_cc_toolchain" ,
108
+ src = "//python/private:py_cc_toolchain_rule.bzl" ,
109
+ prefix = "api/" ,
110
+ public_load_path = "//python/cc:py_cc_toolchain.bzl" ,
111
+ tags = ["docs" ],
112
+ target_compatible_with = _TARGET_COMPATIBLE_WITH ,
113
+ deps = ["//python/cc:py_cc_toolchain_bzl" ],
114
+ )
115
+
116
+ sphinx_stardoc (
117
+ name = "py_runtime_pair" ,
118
+ src = "//python/private:py_runtime_pair_rule_bzl" ,
119
+ public_load_path = "//python:py_runtime_pair.bzl" ,
120
+ tags = ["docs" ],
121
+ target_compatible_with = _TARGET_COMPATIBLE_WITH ,
122
+ )
123
+
124
+ readthedocs_install (
125
+ name = "readthedocs_install" ,
126
+ docs = [":docs" ],
127
+ target_compatible_with = _TARGET_COMPATIBLE_WITH ,
128
+ )
129
+
130
+ sphinx_build_binary (
131
+ name = "sphinx-build" ,
132
+ target_compatible_with = _TARGET_COMPATIBLE_WITH ,
133
+ deps = [
134
+ requirement ("sphinx" ),
135
+ requirement ("sphinx_rtd_theme" ),
136
+ requirement ("myst_parser" ),
137
+ requirement ("readthedocs_sphinx_ext" ),
138
+ requirement ("typing_extensions" ),
139
+ "//sphinxdocs/src/sphinx_bzl" ,
140
+ ],
141
+ )
142
+
143
+ _REQUIREMENTS_TARGET_COMPATIBLE_WITH = select ({
144
+ "@platforms//os:linux" : [],
145
+ "@platforms//os:macos" : [],
146
+ "@platforms//os:windows" : [],
147
+ "//conditions:default" : ["@platforms//:incompatible" ],
148
+ }) if BZLMOD_ENABLED else ["@platforms//:incompatible" ]
149
+
150
+ # Run bazel run //docs:requirements.update
151
+ genrule (
152
+ name = "requirements" ,
153
+ srcs = ["pyproject.toml" ],
154
+ outs = ["_requirements.txt" ],
155
+ cmd = "$(UV_BIN) pip compile " + " " .join ([
156
+ "--custom-compile-command='bazel run //docs:requirements.update'" ,
157
+ "--generate-hashes" ,
158
+ "--universal" ,
159
+ "--emit-index-url" ,
160
+ "--no-strip-extras" ,
161
+ "--no-build" ,
162
+ "--python=$(PYTHON3)" ,
163
+ "$<" ,
164
+ "--output-file=$@" ,
165
+ # Always try upgrading
166
+ "--upgrade" ,
167
+ ]),
168
+ tags = [
169
+ "local" ,
170
+ "manual" ,
171
+ "no-cache" ,
172
+ ],
173
+ target_compatible_with = _REQUIREMENTS_TARGET_COMPATIBLE_WITH ,
174
+ toolchains = [
175
+ "//python/uv:current_toolchain" ,
176
+ "//python:current_py_toolchain" ,
177
+ ],
178
+ )
179
+
180
+ # Write a script that can be used for updating the in-tree version of the
181
+ # requirements file
182
+ write_file (
183
+ name = "gen_update_requirements" ,
184
+ out = "requirements.update.py" ,
185
+ content = [
186
+ "from os import environ" ,
187
+ "from pathlib import Path" ,
188
+ "from sys import stderr" ,
189
+ "" ,
190
+ 'src = Path(environ["REQUIREMENTS_FILE"])' ,
191
+ 'dst = Path(environ["BUILD_WORKSPACE_DIRECTORY"]) / "docs" / "requirements.txt"' ,
192
+ 'print(f"Writing requirements contents from {src} to {dst}", file=stderr)' ,
193
+ "dst.write_text(src.read_text())" ,
194
+ 'print("Success!", file=stderr)' ,
195
+ ],
196
+ target_compatible_with = _REQUIREMENTS_TARGET_COMPATIBLE_WITH ,
197
+ )
198
+
199
+ py_binary (
200
+ name = "requirements.update" ,
201
+ srcs = ["requirements.update.py" ],
202
+ data = [":requirements" ],
203
+ env = {
204
+ "REQUIREMENTS_FILE" : "$(location :requirements)" ,
205
+ },
206
+ tags = ["manual" ],
207
+ target_compatible_with = _REQUIREMENTS_TARGET_COMPATIBLE_WITH ,
208
+ )
20
209
21
210
licenses (["notice" ]) # Apache 2.0
22
211
@@ -26,18 +215,21 @@ alias(
26
215
name = "defs" ,
27
216
actual = "//python:defs_bzl" ,
28
217
deprecation = "Use //python:defs_bzl instead; targets under //docs are internal." ,
218
+ visibility = ["//visibility:public" ],
29
219
)
30
220
31
221
alias (
32
222
name = "bazel_repo_tools" ,
33
223
actual = "//python/private:bazel_tools_bzl" ,
34
224
deprecation = "Use @bazel_tools//tools:bzl_srcs instead; targets under //docs are internal." ,
225
+ visibility = ["//visibility:public" ],
35
226
)
36
227
37
228
bzl_library (
38
229
name = "pip_install_bzl" ,
39
230
deprecation = "Use //python:pip_bzl or //python/pip_install:pip_repository_bzl instead; " +
40
231
"targets under //docs are internal." ,
232
+ visibility = ["//visibility:public" ],
41
233
deps = [
42
234
"//python:pip_bzl" ,
43
235
"//python/pip_install:pip_repository_bzl" ,
@@ -49,4 +241,5 @@ alias(
49
241
actual = "//python/pip_install:pip_repository_bzl" ,
50
242
deprecation = "Use //python/pip_install:pip_repository_bzl instead; Both the requirements " +
51
243
"parser and targets under //docs are internal" ,
244
+ visibility = ["//visibility:public" ],
52
245
)
0 commit comments