blob: b0ae93a161f4eb1631827be00a3d4c231be27d12 [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
Chad Norvell9357f332022-02-18 12:07:24 -080023
24 # Set to enable Google Analytics tracking of generated docs.
25 pw_docs_google_analytics_id = ""
Alexei Frolov258fc1b2020-06-10 16:24:50 -070026}
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080027
28# Defines a group of documentation files and assets.
29#
30# Args:
31# sources: Source files for the documentation (.rst or .md).
32# inputs: Additional resource files for the docs, such as images.
33# group_deps: Other pw_doc_group targets on which this group depends.
34# report_deps: Report card targets on which documentation depends.
Wyatt Hepler830d26d2021-02-17 09:07:43 -080035# other_deps: Dependencies on any other types of targets.
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080036template("pw_doc_group") {
37 assert(defined(invoker.sources), "pw_doc_group requires a list of sources")
38
39 if (defined(invoker.inputs)) {
40 _inputs = invoker.inputs
41 } else {
42 _inputs = []
43 }
44
45 _all_deps = []
46 if (defined(invoker.group_deps)) {
47 _all_deps += invoker.group_deps
48 }
49 if (defined(invoker.report_deps)) {
50 _all_deps += invoker.report_deps
51 }
Wyatt Hepler830d26d2021-02-17 09:07:43 -080052 if (defined(invoker.other_deps)) {
53 _all_deps += invoker.other_deps
54 }
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080055
Alexei Frolov69ad1922019-12-13 13:11:32 -080056 # Create a group containing the source and input files so that docs are
57 # rebuilt on file modifications.
58 pw_input_group(target_name) {
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080059 metadata = {
60 pw_doc_sources = rebase_path(invoker.sources, root_build_dir)
61 pw_doc_inputs = rebase_path(_inputs, root_build_dir)
62 }
63 deps = _all_deps
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080064 inputs = invoker.sources + _inputs
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080065 }
66}
67
68# Creates a target to build HTML documentation from groups of sources.
69#
70# Args:
71# deps: List of pw_doc_group targets.
Wyatt Heplerb82f9952019-11-25 13:56:31 -080072# sources: Top-level documentation .rst source files.
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080073# conf: Configuration script (conf.py) for Sphinx.
74# output_directory: Path to directory to which HTML output is rendered.
75template("pw_doc_gen") {
76 assert(defined(invoker.deps),
77 "pw_doc_gen requires doc groups as dependencies")
Wyatt Heplerb82f9952019-11-25 13:56:31 -080078 assert(defined(invoker.sources) && invoker.sources != [],
79 "pw_doc_gen requires a 'sources' list with at least one .rst source")
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080080 assert(defined(invoker.conf),
81 "pw_doc_gen requires a 'conf' argument pointing a top-level conf.py")
82 assert(defined(invoker.output_directory),
83 "pw_doc_gen requires an 'output_directory' argument")
84
85 # Collects all dependency metadata into a single JSON file.
86 _metadata_file_target = "${target_name}_metadata"
87 generated_file(_metadata_file_target) {
Rob Mohra0ba54f2020-02-27 11:43:49 -080088 outputs = [ "$target_gen_dir/$target_name.json" ]
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080089 data_keys = [
90 "pw_doc_sources",
91 "pw_doc_inputs",
92 ]
93 output_conversion = "json"
94 deps = invoker.deps
95 }
96
97 _script_args = [
98 "--gn-root",
Alexei Frolov258fc1b2020-06-10 16:24:50 -070099 rebase_path("//", root_build_dir),
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800100 "--gn-gen-root",
Alexei Frolov258fc1b2020-06-10 16:24:50 -0700101 rebase_path(root_gen_dir, root_build_dir) + "/",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800102 "--sphinx-build-dir",
Michael Spangc8b93902021-05-30 15:53:56 -0400103 rebase_path("$target_gen_dir/pw_docgen_tree", root_build_dir),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800104 "--conf",
Michael Spangc8b93902021-05-30 15:53:56 -0400105 rebase_path(invoker.conf, root_build_dir),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800106 "--out-dir",
Michael Spangc8b93902021-05-30 15:53:56 -0400107 rebase_path(invoker.output_directory, root_build_dir),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800108 ]
109
Chad Norvell9357f332022-02-18 12:07:24 -0800110 # Enable Google Analytics if a measurement ID is provided
111 if (pw_docs_google_analytics_id != "") {
112 _script_args += [
113 "--google-analytics-id",
114 pw_docs_google_analytics_id,
115 ]
116 }
117
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800118 # Metadata JSON file path.
Chad Norvell9357f332022-02-18 12:07:24 -0800119 _script_args += [ "--metadata" ]
Michael Spangc8b93902021-05-30 15:53:56 -0400120 _script_args +=
121 rebase_path(get_target_outputs(":$_metadata_file_target"), root_build_dir)
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800122
Michael Spangc8b93902021-05-30 15:53:56 -0400123 _script_args += rebase_path(invoker.sources, root_build_dir)
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800124
Alexei Frolov258fc1b2020-06-10 16:24:50 -0700125 if (pw_docgen_BUILD_DOCS) {
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700126 pw_python_action(target_name) {
Wyatt Hepler96992c72020-10-23 08:05:33 -0700127 script = "$dir_pw_docgen/py/pw_docgen/docgen.py"
Alexei Frolov844ff0f2020-05-06 12:15:29 -0700128 args = _script_args
129 deps = [ ":$_metadata_file_target" ]
130 inputs = [ invoker.conf ]
131 inputs += invoker.sources
132 stamp = true
133 }
134 } else {
135 group(target_name) {
136 }
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800137 }
138}