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 |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 23 | |
| 24 | # Set to enable Google Analytics tracking of generated docs. |
| 25 | pw_docs_google_analytics_id = "" |
Alexei Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 26 | } |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 27 | |
| 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 Hepler | 830d26d | 2021-02-17 09:07:43 -0800 | [diff] [blame] | 35 | # other_deps: Dependencies on any other types of targets. |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 36 | template("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 Hepler | 830d26d | 2021-02-17 09:07:43 -0800 | [diff] [blame] | 52 | if (defined(invoker.other_deps)) { |
| 53 | _all_deps += invoker.other_deps |
| 54 | } |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 55 | |
Alexei Frolov | 69ad192 | 2019-12-13 13:11:32 -0800 | [diff] [blame] | 56 | # 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 59 | 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 64 | inputs = invoker.sources + _inputs |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 65 | } |
| 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 Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 72 | # sources: Top-level documentation .rst source files. |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 73 | # conf: Configuration script (conf.py) for Sphinx. |
| 74 | # output_directory: Path to directory to which HTML output is rendered. |
| 75 | template("pw_doc_gen") { |
| 76 | assert(defined(invoker.deps), |
| 77 | "pw_doc_gen requires doc groups as dependencies") |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 78 | assert(defined(invoker.sources) && invoker.sources != [], |
| 79 | "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] | 80 | 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 Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 88 | outputs = [ "$target_gen_dir/$target_name.json" ] |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 89 | 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 Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 99 | rebase_path("//", root_build_dir), |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 100 | "--gn-gen-root", |
Alexei Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 101 | rebase_path(root_gen_dir, root_build_dir) + "/", |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 102 | "--sphinx-build-dir", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 103 | rebase_path("$target_gen_dir/pw_docgen_tree", root_build_dir), |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 104 | "--conf", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 105 | rebase_path(invoker.conf, root_build_dir), |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 106 | "--out-dir", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 107 | rebase_path(invoker.output_directory, root_build_dir), |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 108 | ] |
| 109 | |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 110 | # 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 118 | # Metadata JSON file path. |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 119 | _script_args += [ "--metadata" ] |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 120 | _script_args += |
| 121 | rebase_path(get_target_outputs(":$_metadata_file_target"), root_build_dir) |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 122 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 123 | _script_args += rebase_path(invoker.sources, root_build_dir) |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 124 | |
Alexei Frolov | 258fc1b | 2020-06-10 16:24:50 -0700 | [diff] [blame] | 125 | if (pw_docgen_BUILD_DOCS) { |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 126 | pw_python_action(target_name) { |
Wyatt Hepler | 96992c7 | 2020-10-23 08:05:33 -0700 | [diff] [blame] | 127 | script = "$dir_pw_docgen/py/pw_docgen/docgen.py" |
Alexei Frolov | 844ff0f | 2020-05-06 12:15:29 -0700 | [diff] [blame] | 128 | 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 137 | } |
| 138 | } |