blob: 25a5fcc3f9e2670924e78ba9310a827f477e8542 [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
Alexei Frolov69ad1922019-12-13 13:11:32 -080015import("$dir_pw_build/input_group.gni")
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080016import("$dir_pw_build/python_script.gni")
17
18# Defines a group of documentation files and assets.
19#
20# Args:
21# sources: Source files for the documentation (.rst or .md).
22# inputs: Additional resource files for the docs, such as images.
23# group_deps: Other pw_doc_group targets on which this group depends.
24# report_deps: Report card targets on which documentation depends.
25template("pw_doc_group") {
26 assert(defined(invoker.sources), "pw_doc_group requires a list of sources")
27
28 if (defined(invoker.inputs)) {
29 _inputs = invoker.inputs
30 } else {
31 _inputs = []
32 }
33
34 _all_deps = []
35 if (defined(invoker.group_deps)) {
36 _all_deps += invoker.group_deps
37 }
38 if (defined(invoker.report_deps)) {
39 _all_deps += invoker.report_deps
40 }
41
Alexei Frolov69ad1922019-12-13 13:11:32 -080042 # Create a group containing the source and input files so that docs are
43 # rebuilt on file modifications.
44 pw_input_group(target_name) {
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080045 metadata = {
46 pw_doc_sources = rebase_path(invoker.sources, root_build_dir)
47 pw_doc_inputs = rebase_path(_inputs, root_build_dir)
48 }
49 deps = _all_deps
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080050 inputs = invoker.sources + _inputs
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080051 }
52}
53
54# Creates a target to build HTML documentation from groups of sources.
55#
56# Args:
57# deps: List of pw_doc_group targets.
Wyatt Heplerb82f9952019-11-25 13:56:31 -080058# sources: Top-level documentation .rst source files.
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080059# conf: Configuration script (conf.py) for Sphinx.
60# output_directory: Path to directory to which HTML output is rendered.
61template("pw_doc_gen") {
62 assert(defined(invoker.deps),
63 "pw_doc_gen requires doc groups as dependencies")
Wyatt Heplerb82f9952019-11-25 13:56:31 -080064 assert(defined(invoker.sources) && invoker.sources != [],
65 "pw_doc_gen requires a 'sources' list with at least one .rst source")
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080066 assert(defined(invoker.conf),
67 "pw_doc_gen requires a 'conf' argument pointing a top-level conf.py")
68 assert(defined(invoker.output_directory),
69 "pw_doc_gen requires an 'output_directory' argument")
70
71 # Collects all dependency metadata into a single JSON file.
72 _metadata_file_target = "${target_name}_metadata"
73 generated_file(_metadata_file_target) {
Rob Mohra0ba54f2020-02-27 11:43:49 -080074 outputs = [ "$target_gen_dir/$target_name.json" ]
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080075 data_keys = [
76 "pw_doc_sources",
77 "pw_doc_inputs",
78 ]
79 output_conversion = "json"
80 deps = invoker.deps
81 }
82
83 _script_args = [
84 "--gn-root",
Wyatt Heplerb82f9952019-11-25 13:56:31 -080085 rebase_path("//", root_out_dir),
86 "--gn-gen-root",
87 rebase_path(root_gen_dir, root_out_dir) + "/",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080088 "--sphinx-build-dir",
89 get_path_info("$target_gen_dir/pw_docgen_tree", "abspath"),
90 "--conf",
91 get_path_info(invoker.conf, "abspath"),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080092 "--out-dir",
93 get_path_info(invoker.output_directory, "abspath"),
Wyatt Heplerb82f9952019-11-25 13:56:31 -080094 "--metadata",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080095 ]
96
97 # Metadata JSON file path.
98 _script_args +=
99 get_path_info(get_target_outputs(":$_metadata_file_target"), "abspath")
100
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800101 foreach(path, invoker.sources) {
102 _script_args += [ get_path_info(path, "abspath") ]
103 }
104
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800105 pw_python_script(target_name) {
106 script = "$dir_pw_docgen/py/docgen.py"
107 args = _script_args
Rob Mohra0ba54f2020-02-27 11:43:49 -0800108 deps = [ ":$_metadata_file_target" ]
109 inputs = [ invoker.conf ]
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800110 inputs += invoker.sources
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800111 stamp = true
112 }
113}