blob: 34af7721be1124e283c06ff7e69487b410c0b128 [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
15import("$dir_pw_build/python_script.gni")
16
17# Defines a group of documentation files and assets.
18#
19# Args:
20# sources: Source files for the documentation (.rst or .md).
21# inputs: Additional resource files for the docs, such as images.
22# group_deps: Other pw_doc_group targets on which this group depends.
23# report_deps: Report card targets on which documentation depends.
24template("pw_doc_group") {
25 assert(defined(invoker.sources), "pw_doc_group requires a list of sources")
26
27 if (defined(invoker.inputs)) {
28 _inputs = invoker.inputs
29 } else {
30 _inputs = []
31 }
32
33 _all_deps = []
34 if (defined(invoker.group_deps)) {
35 _all_deps += invoker.group_deps
36 }
37 if (defined(invoker.report_deps)) {
38 _all_deps += invoker.report_deps
39 }
40
41 # Creates an action which depends on all of the source and input files so that
42 # docs are rebuilt on file modifications. The action does not do anything.
43 pw_python_script(target_name) {
44 metadata = {
45 pw_doc_sources = rebase_path(invoker.sources, root_build_dir)
46 pw_doc_inputs = rebase_path(_inputs, root_build_dir)
47 }
48 deps = _all_deps
49 script = "$dir_pw_build/py/nop.py"
50 inputs = invoker.sources + _inputs
51 stamp = true
52 }
53}
54
55# Creates a target to build HTML documentation from groups of sources.
56#
57# Args:
58# deps: List of pw_doc_group targets.
Wyatt Heplerb82f9952019-11-25 13:56:31 -080059# sources: Top-level documentation .rst source files.
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080060# conf: Configuration script (conf.py) for Sphinx.
61# output_directory: Path to directory to which HTML output is rendered.
62template("pw_doc_gen") {
63 assert(defined(invoker.deps),
64 "pw_doc_gen requires doc groups as dependencies")
Wyatt Heplerb82f9952019-11-25 13:56:31 -080065 assert(defined(invoker.sources) && invoker.sources != [],
66 "pw_doc_gen requires a 'sources' list with at least one .rst source")
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080067 assert(defined(invoker.conf),
68 "pw_doc_gen requires a 'conf' argument pointing a top-level conf.py")
69 assert(defined(invoker.output_directory),
70 "pw_doc_gen requires an 'output_directory' argument")
71
72 # Collects all dependency metadata into a single JSON file.
73 _metadata_file_target = "${target_name}_metadata"
74 generated_file(_metadata_file_target) {
75 outputs = [
76 "$target_gen_dir/$target_name.json",
77 ]
78 data_keys = [
79 "pw_doc_sources",
80 "pw_doc_inputs",
81 ]
82 output_conversion = "json"
83 deps = invoker.deps
84 }
85
86 _script_args = [
87 "--gn-root",
Wyatt Heplerb82f9952019-11-25 13:56:31 -080088 rebase_path("//", root_out_dir),
89 "--gn-gen-root",
90 rebase_path(root_gen_dir, root_out_dir) + "/",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080091 "--sphinx-build-dir",
92 get_path_info("$target_gen_dir/pw_docgen_tree", "abspath"),
93 "--conf",
94 get_path_info(invoker.conf, "abspath"),
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080095 "--out-dir",
96 get_path_info(invoker.output_directory, "abspath"),
Wyatt Heplerb82f9952019-11-25 13:56:31 -080097 "--metadata",
Alexei Frolovbaaa2d62019-11-12 16:20:51 -080098 ]
99
100 # Metadata JSON file path.
101 _script_args +=
102 get_path_info(get_target_outputs(":$_metadata_file_target"), "abspath")
103
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800104 foreach(path, invoker.sources) {
105 _script_args += [ get_path_info(path, "abspath") ]
106 }
107
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800108 pw_python_script(target_name) {
109 script = "$dir_pw_docgen/py/docgen.py"
110 args = _script_args
111 deps = [
112 ":$_metadata_file_target",
113 ]
114 inputs = [
Alexei Frolov0efdb112019-11-14 17:22:08 -0800115 invoker.conf,
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800116 ]
Wyatt Heplerb82f9952019-11-25 13:56:31 -0800117 inputs += invoker.sources
Alexei Frolovbaaa2d62019-11-12 16:20:51 -0800118 stamp = true
119 }
120}