Skip to content

Commit 769ba47

Browse files
authored
Merge pull request #28 from TendTo/feat/custom-outputs
Feat: custom outputs
2 parents 79751df + 0ca326b commit 769ba47

File tree

16 files changed

+355
-5
lines changed

16 files changed

+355
-5
lines changed

.bcr/presubmit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bcr_test_module:
1212
- "//base:doxygen"
1313
- "//latex:doxygen"
1414
- "//doxyfile:doxygen"
15+
- "//doxylink:doxygen"
1516
- "//nested:doxygen"
1617
- "//custom:doxygen"
1718
- "//awesome:doxygen"

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
base,
2121
kwargs,
2222
doxyfile,
23+
doxylink,
2324
latex,
2425
nested,
2526
custom,

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.bazelversion
12
bazel-*
23
.vscode/
34
MODULE.bazel.lock

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.4.1]
9+
10+
### Added
11+
12+
- `Doxyfile` is now included in the `doxygen` rule DefaultInfo provider
13+
- Mnemonic `DoxygenBuild` added to the `ctx.run` in the `doxygen` rule
14+
- Added support by default for the `$(OUTDIR)` make variable in the `doxygen` rule [#28](https://github.com/TendTo/rules_doxygen/pull/28) (thanks to @kaycebasques)
15+
- `doxylink` example in the documentation
16+
17+
### Changed
18+
19+
- Updated documentation
20+
- More information in the progress message of the `doxygen` rule
21+
822
## [2.4.0]
923

1024
### Changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ doxygen(
171171
)
172172
```
173173

174+
> [!Tip]
175+
> The `doxygen` rule supports [Make variables](https://bazel.build/reference/be/make-variables) substitutions.
176+
> By default, only `OUTDIR` and the [predefined ones](https://bazel.build/reference/be/make-variables#predefined_variables) are available, but you can add your own, as shown in the [examples](examples/substitutions).
177+
174178
> [!Note]
175179
> `srcs` and `deps` attributes are **not** interchangeable.
176180
> Use `srcs` with files and when you want to capture the output of another rule, and use `deps` when you want to capture the source files of other rules transitively.

docs/doxygen_doc.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ Depending on the type of the attribute, the macro will convert it to the appropr
107107

108108
For the complete list of Doxygen configuration options, please refer to the [Doxygen documentation](https://www.doxygen.nl/manual/config.html).
109109

110+
### Make variables
111+
112+
There are cases where you need access to information about the build environment, such as the output directory doxygen is writing to.
113+
In such cases, you can use make variables in the parameters of the macro.
114+
The `doxygen` rule will take care of expanding them to the appropriate values.
115+
116+
By default, the following make variables are available:
117+
118+
- `$(OUTDIR)`: The output directory where the documentation will be generated.
119+
- All the predefined variables indicated in the [Bazel documentation](https://bazel.build/reference/be/make-variables#predefined_variables).
120+
121+
The former is particularly useful when `doxygen` needs to generate unusual files, such as [tag files](https://www.doxygen.nl/manual/config.html#cfg_generate_tagfile).
122+
For example, you can use it to generate a tag file in the output directory:
123+
124+
```bzl
125+
doxygen(
126+
name = "doxygen",
127+
srcs = ["README.md"] + glob(["*.h", "*.cpp"]),
128+
outs = ["html", "tags"],
129+
generate_tagfile = "$(OUTDIR)/tags/PolyVox.tag",
130+
)
131+
```
132+
133+
> [!NOTE]
134+
> Make sure that generated files are put in some directory and that directory is included in the `outs` attribute.
135+
136+
You can add your own substitutions by adding a rule that returns a TemplateVariableInfo provider in the `toolchains` attribute of the `doxygen` rule.
137+
See [this example](../examples/substitutions/) for more details.
138+
110139
### Differences between `srcs` and `deps`
111140

112141
The `srcs` and `deps` attributes work differently and are not interchangeable.

doxygen/doxygen.bzl

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"""Doxygen rule for Bazel."""
22

3-
def _expand_make_variables(string, ctx):
3+
def _expand_make_variables(string, ctx, extra_sub_vars = {}):
44
"""Replace make variables in a string with their values.
55
66
Args:
77
string: The string to expand.
88
ctx: The context object.
9+
extra_sub_vars: A dictionary of additional variables to replace in the string.
910
"""
1011
if "$(" in string:
1112
for variable, value in ctx.var.items():
1213
string = string.replace("$(%s)" % variable, value)
14+
for variable, value in extra_sub_vars.items():
15+
string = string.replace("$(%s)" % variable, value)
1316
return string
1417

1518
TransitiveSourcesInfo = provider(
@@ -61,7 +64,7 @@ def _doxygen_impl(ctx):
6164
outs.append(output_dir)
6265
output_group_info |= {out: depset([output_dir])}
6366

64-
configurations = [_expand_make_variables(conf, ctx) for conf in ctx.attr.configurations]
67+
configurations = [_expand_make_variables(conf, ctx, {"OUTDIR": doxyfile.dirname}) for conf in ctx.attr.configurations]
6568

6669
if len(outs) == 0:
6770
fail("At least one output folder must be specified")
@@ -83,12 +86,13 @@ def _doxygen_impl(ctx):
8386
inputs = ctx.files.srcs + deps + [doxyfile],
8487
outputs = outs,
8588
arguments = [doxyfile.path] + ctx.attr.doxygen_extra_args,
86-
progress_message = "Running doxygen",
8789
executable = ctx.executable._executable,
90+
mnemonic = "DoxygenBuild",
91+
progress_message = "Building doxygen documentation for rule '%s'" % ctx.label.name,
8892
)
8993

9094
return [
91-
DefaultInfo(files = depset(outs)),
95+
DefaultInfo(files = depset(outs + [doxyfile])),
9296
OutputGroupInfo(**output_group_info),
9397
]
9498

@@ -498,6 +502,35 @@ def doxygen(
498502
499503
For the complete list of Doxygen configuration options, please refer to the [Doxygen documentation](https://www.doxygen.nl/manual/config.html).
500504
505+
### Make variables
506+
507+
There are cases where you need access to information about the build environment, such as the output directory doxygen is writing to.
508+
In such cases, you can use make variables in the parameters of the macro.
509+
The `doxygen` rule will take care of expanding them to the appropriate values.
510+
511+
By default, the following make variables are available:
512+
513+
- `$(OUTDIR)`: The output directory where the documentation will be generated.
514+
- All the predefined variables indicated in the [Bazel documentation](https://bazel.build/reference/be/make-variables#predefined_variables).
515+
516+
The former is particularly useful when `doxygen` needs to generate unusual files, such as [tag files](https://www.doxygen.nl/manual/config.html#cfg_generate_tagfile).
517+
For example, you can use it to generate a tag file in the output directory:
518+
519+
```bzl
520+
doxygen(
521+
name = "doxygen",
522+
srcs = ["README.md"] + glob(["*.h", "*.cpp"]),
523+
outs = ["html", "tags"],
524+
generate_tagfile = "$(OUTDIR)/tags/PolyVox.tag",
525+
)
526+
```
527+
528+
> [!NOTE]
529+
> Make sure that generated files are put in some directory and that directory is included in the `outs` attribute.
530+
531+
You can add your own substitutions by adding a rule that returns a TemplateVariableInfo provider in the `toolchains` attribute of the `doxygen` rule.
532+
See [this example](../examples/substitutions/) for more details.
533+
501534
### Differences between `srcs` and `deps`
502535
503536
The `srcs` and `deps` attributes work differently and are not interchangeable.

examples/MODULE.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ local_path_override(
99
bazel_dep(name = "rules_cc", version = "0.1.1")
1010
bazel_dep(name = "aspect_bazel_lib", version = "2.10.0")
1111
bazel_dep(name = "bazel_skylib", version = "1.7.1")
12+
bazel_dep(name = "rules_python", version = "1.2.0")
13+
14+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
15+
pip.parse(
16+
hub_name = "pypi",
17+
python_version = "3.11",
18+
requirements_lock = "//doxylink:requirements.txt",
19+
)
20+
use_repo(pip, "pypi")
1221

1322
doxygen_extension = use_extension("@rules_doxygen//:extensions.bzl", "doxygen_extension")
1423

examples/doxyfile/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Keep in mind that the file will undergo the same template processing as the defa
1414
Namely, the following expressions will be replaced:
1515

1616
- `# {{INPUT}}`: Subpackage directory in the sandbox.
17+
- `# {{DOT_PATH}}`: Indicate to doxygen the location of the `dot_executable`
1718
- `# {{ADDITIONAL PARAMETERS}}`: Additional parameters given in the `configurations` attribute.
1819
- `# {{OUTPUT DIRECTORY}}`: The directory provided in the `outs` attribute.
1920

20-
It is highly recommended to at least use the `# {{OUTPUT DIRECTORY}}` expression at the very end of your doxyfile, since the exact path of the output is computed at runtime by Bazel and may be different on different platforms.
21+
It is highly recommended to at least use the `# {{OUTPUT DIRECTORY}}` expression at the very end of your Doxyfile, since the exact path of the output is computed at runtime by Bazel and it may differ on each platform.

examples/doxylink/BUILD.bazel

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
load("@doxygen//:doxygen.bzl", "doxygen")
2+
load("@rules_python//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs")
3+
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
4+
load("@pypi//:requirements.bzl", "requirement")
5+
6+
exports_files(["requirements.txt"])
7+
8+
doxygen(
9+
name = "doxygen",
10+
srcs = glob([
11+
"*.h",
12+
"*.cpp",
13+
]),
14+
outs = [
15+
"html",
16+
# Mark `tags` as an output folder/group, and ensure its creation
17+
"tags",
18+
],
19+
extract_all = True,
20+
# Add the `$(OUTDIR)` make variable to specify the root output directory
21+
# where to generate the file
22+
generate_tagfile = "$(OUTDIR)/tags/lib.tag",
23+
)
24+
25+
# Define the `sphinx` build binary that will be used to build the documentation
26+
sphinx_build_binary(
27+
name = "sphinx_bin",
28+
deps = [
29+
requirement("sphinx"),
30+
requirement("sphinxcontrib-doxylink"),
31+
],
32+
)
33+
34+
# Capture the Doxygen output files and put them in the `doxygen` subdirectory
35+
sphinx_docs_library(
36+
name = "sphinx_doxygen_tags",
37+
srcs = [":doxygen"],
38+
prefix = "doxygen/",
39+
)
40+
41+
# Use sphinx to generate HTML documentation
42+
sphinx_docs(
43+
name = "sphinx",
44+
srcs = ["index.rst"],
45+
config = "conf.py",
46+
formats = ["html"],
47+
sphinx = ":sphinx_bin",
48+
# This ensures that we are in the `sphinx/_source` directory,
49+
# without more nesting
50+
strip_prefix = package_name() + "/",
51+
deps = [":sphinx_doxygen_tags"],
52+
)

0 commit comments

Comments
 (0)