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 | |
Alexei Frolov | 69ad192 | 2019-12-13 13:11:32 -0800 | [diff] [blame] | 15 | import("$dir_pw_build/input_group.gni") |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 16 | import("$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. |
| 25 | template("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 Frolov | 69ad192 | 2019-12-13 13:11:32 -0800 | [diff] [blame] | 42 | # 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 45 | 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 Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 50 | inputs = invoker.sources + _inputs |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 51 | } |
| 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 Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 58 | # sources: Top-level documentation .rst source files. |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 59 | # conf: Configuration script (conf.py) for Sphinx. |
| 60 | # output_directory: Path to directory to which HTML output is rendered. |
| 61 | template("pw_doc_gen") { |
| 62 | assert(defined(invoker.deps), |
| 63 | "pw_doc_gen requires doc groups as dependencies") |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 64 | assert(defined(invoker.sources) && invoker.sources != [], |
| 65 | "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] | 66 | 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 Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 74 | outputs = [ "$target_gen_dir/$target_name.json" ] |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 75 | 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 Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 85 | rebase_path("//", root_out_dir), |
| 86 | "--gn-gen-root", |
| 87 | rebase_path(root_gen_dir, root_out_dir) + "/", |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 88 | "--sphinx-build-dir", |
| 89 | get_path_info("$target_gen_dir/pw_docgen_tree", "abspath"), |
| 90 | "--conf", |
| 91 | get_path_info(invoker.conf, "abspath"), |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 92 | "--out-dir", |
| 93 | get_path_info(invoker.output_directory, "abspath"), |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 94 | "--metadata", |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 95 | ] |
| 96 | |
| 97 | # Metadata JSON file path. |
| 98 | _script_args += |
| 99 | get_path_info(get_target_outputs(":$_metadata_file_target"), "abspath") |
| 100 | |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 101 | foreach(path, invoker.sources) { |
| 102 | _script_args += [ get_path_info(path, "abspath") ] |
| 103 | } |
| 104 | |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 105 | pw_python_script(target_name) { |
| 106 | script = "$dir_pw_docgen/py/docgen.py" |
| 107 | args = _script_args |
Rob Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 108 | deps = [ ":$_metadata_file_target" ] |
| 109 | inputs = [ invoker.conf ] |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 110 | inputs += invoker.sources |
Alexei Frolov | baaa2d6 | 2019-11-12 16:20:51 -0800 | [diff] [blame] | 111 | stamp = true |
| 112 | } |
| 113 | } |