blob: 50bf8d6be43f997a31da41c8572c4f05dfe0f8d8 [file] [log] [blame]
Alexei Frolovbaaa2d62019-11-12 16:20:51 -08001# Copyright 2019 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
16
Alexei Frolov69ad1922019-12-13 13:11:32 -080017import("$dir_pw_build/input_group.gni")
Wyatt Hepler51ded742020-10-19 14:45:27 -070018import("$dir_pw_build/python_action.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -070019
Alexei Frolov258fc1b2020-06-10 16:24:50 -070020declare_args() {
21 # Whether or not the current target should build docs.
22 pw_docgen_BUILD_DOCS = false
23}
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080024
25# Defines a group of documentation files and assets.
26#
27# Args:
28# sources: Source files for the documentation (.rst or .md).
29# inputs: Additional resource files for the docs, such as images.
30# group_deps: Other pw_doc_group targets on which this group depends.
31# report_deps: Report card targets on which documentation depends.
Wyatt Hepler830d26d2021-02-17 09:07:43 -080032# other_deps: Dependencies on any other types of targets.
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080033template("pw_doc_group") {
34 assert(defined(invoker.sources), "pw_doc_group requires a list of sources")
35
36 if (defined(invoker.inputs)) {
37 _inputs = invoker.inputs
38 } else {
39 _inputs = []
40 }
41
42 _all_deps = []
43 if (defined(invoker.group_deps)) {
44 _all_deps += invoker.group_deps
45 }
46 if (defined(invoker.report_deps)) {
47 _all_deps += invoker.report_deps
48 }
Wyatt Hepler830d26d2021-02-17 09:07:43 -080049 if (defined(invoker.other_deps)) {
50 _all_deps += invoker.other_deps
51 }
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080052
Alexei Frolov69ad1922019-12-13 13:11:32 -080053 # Create a group containing the source and input files so that docs are
54 # rebuilt on file modifications.
55 pw_input_group(target_name) {
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080056 metadata = {
57 pw_doc_sources = rebase_path(invoker.sources, root_build_dir)
58 pw_doc_inputs = rebase_path(_inputs, root_build_dir)
59 }
60 deps = _all_deps
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080061 inputs = invoker.sources + _inputs
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080062 }
63}
64
65# Creates a target to build HTML documentation from groups of sources.
66#
67# Args:
68# deps: List of pw_doc_group targets.
Wyatt Heplerb82f9952019-11-25 13:56:31 -080069# sources: Top-level documentation .rst source files.
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080070# conf: Configuration script (conf.py) for Sphinx.
71# output_directory: Path to directory to which HTML output is rendered.
72template("pw_doc_gen") {
73 assert(defined(invoker.deps),
74 "pw_doc_gen requires doc groups as dependencies")
Wyatt Heplerb82f9952019-11-25 13:56:31 -080075 assert(defined(invoker.sources) && invoker.sources != [],
76 "pw_doc_gen requires a 'sources' list with at least one .rst source")
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080077 assert(defined(invoker.conf),
78 "pw_doc_gen requires a 'conf' argument pointing a top-level conf.py")
79 assert(defined(invoker.output_directory),
80 "pw_doc_gen requires an 'output_directory' argument")
81
82 # Collects all dependency metadata into a single JSON file.
83 _metadata_file_target = "${target_name}_metadata"
84 generated_file(_metadata_file_target) {
Rob Mohra0ba54f2020-02-27 11:43:49 -080085 outputs = [ "$target_gen_dir/$target_name.json" ]
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080086 data_keys = [
87 "pw_doc_sources",
88 "pw_doc_inputs",
89 ]
90 output_conversion = "json"
91 deps = invoker.deps
92 }
93
94 _script_args = [
95 "--gn-root",
Alexei Frolov258fc1b2020-06-10 16:24:50 -070096 rebase_path("//", root_build_dir),
Wyatt Heplerb82f9952019-11-25 13:56:31 -080097 "--gn-gen-root",
Alexei Frolov258fc1b2020-06-10 16:24:50 -070098 rebase_path(root_gen_dir, root_build_dir) + "/",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080099 "--sphinx-build-dir",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700100 rebase_path("$target_gen_dir/pw_docgen_tree"),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800101 "--conf",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700102 rebase_path(invoker.conf),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800103 "--out-dir",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700104 rebase_path(invoker.output_directory),
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800105 "--metadata",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800106 ]
107
108 # Metadata JSON file path.
Wyatt Hepler8224a642020-07-29 08:55:56 -0700109 _script_args += rebase_path(get_target_outputs(":$_metadata_file_target"))
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800110
Wyatt Hepler8224a642020-07-29 08:55:56 -0700111 _script_args += rebase_path(invoker.sources)
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800112
Alexei Frolov258fc1b2020-06-10 16:24:50 -0700113 if (pw_docgen_BUILD_DOCS) {
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700114 pw_python_action(target_name) {
Wyatt Hepler96992c72020-10-23 08:05:33 -0700115 script = "$dir_pw_docgen/py/pw_docgen/docgen.py"
Alexei Frolov844ff0f2020-05-06 12:15:29 -0700116 args = _script_args
117 deps = [ ":$_metadata_file_target" ]
118 inputs = [ invoker.conf ]
119 inputs += invoker.sources
120 stamp = true
121 }
122 } else {
123 group(target_name) {
124 }
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800125 }
126}