Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 1 | # 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 Montanez | fb3d3fb | 2020-06-09 18:12:12 -0700 | [diff] [blame] | 15 | import("//build_overrides/pigweed.gni") |
| 16 | |
Alexei Frolov | 69ad192 | 2019-12-13 13:11:32 -0800 | [diff] [blame] | 17 | import("$dir_pw_build/input_group.gni") |
Wyatt Hepler | 51ded74 | 2020-10-19 14:45:27 -0700 | [diff] [blame] | 18 | import("$dir_pw_build/python_action.gni") |
Wyatt Hepler | d49f8fe | 2020-10-15 10:13:47 -0700 | [diff] [blame] | 19 | |
Alexei Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 20 | declare_args() { |
| 21 | # Whether or not the current target should build docs. |
| 22 | pw_docgen_BUILD_DOCS = false |
| 23 | } |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 24 | |
| 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 Hepler | 830d26d | 2021-02-17 09:07:43 -0800 | [diff] [blame] | 32 | # other_deps: Dependencies on any other types of targets. |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 33 | template("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 Hepler | 830d26d | 2021-02-17 09:07:43 -0800 | [diff] [blame] | 49 | if (defined(invoker.other_deps)) { |
| 50 | _all_deps += invoker.other_deps |
| 51 | } |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 52 | |
Alexei Frolov | 69ad192 | 2019-12-13 13:11:32 -0800 | [diff] [blame] | 53 | # 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 56 | 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 61 | inputs = invoker.sources + _inputs |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 62 | } |
| 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 Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 69 | # sources: Top-level documentation .rst source files. |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 70 | # conf: Configuration script (conf.py) for Sphinx. |
| 71 | # output_directory: Path to directory to which HTML output is rendered. |
| 72 | template("pw_doc_gen") { |
| 73 | assert(defined(invoker.deps), |
| 74 | "pw_doc_gen requires doc groups as dependencies") |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 75 | assert(defined(invoker.sources) && invoker.sources != [], |
| 76 | "pw_doc_gen requires a 'sources' list with at least one .rst source") |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 77 | 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 Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 85 | outputs = [ "$target_gen_dir/$target_name.json" ] |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 86 | 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 Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 96 | rebase_path("//", root_build_dir), |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 97 | "--gn-gen-root", |
Alexei Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 98 | rebase_path(root_gen_dir, root_build_dir) + "/", |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 99 | "--sphinx-build-dir", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 100 | rebase_path("$target_gen_dir/pw_docgen_tree", root_build_dir), |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 101 | "--conf", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 102 | rebase_path(invoker.conf, root_build_dir), |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 103 | "--out-dir", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 104 | rebase_path(invoker.output_directory, root_build_dir), |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 105 | "--metadata", |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 106 | ] |
| 107 | |
| 108 | # Metadata JSON file path. |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 109 | _script_args += |
| 110 | rebase_path(get_target_outputs(":$_metadata_file_target"), root_build_dir) |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 111 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 112 | _script_args += rebase_path(invoker.sources, root_build_dir) |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 113 | |
Alexei Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 114 | if (pw_docgen_BUILD_DOCS) { |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 115 | pw_python_action(target_name) { |
Wyatt Hepler | 96992c7 | 2020-10-23 08:05:33 -0700 | [diff] [blame] | 116 | script = "$dir_pw_docgen/py/pw_docgen/docgen.py" |
Alexei Frolov | 844ff0f | 2020-05-06 12:15:29 -0700 | [diff] [blame] | 117 | args = _script_args |
| 118 | deps = [ ":$_metadata_file_target" ] |
| 119 | inputs = [ invoker.conf ] |
| 120 | inputs += invoker.sources |
| 121 | stamp = true |
| 122 | } |
| 123 | } else { |
| 124 | group(target_name) { |
| 125 | } |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 126 | } |
| 127 | } |