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 15import("//build_overrides/pigweed.gni") 16 17import("$dir_pw_build/input_group.gni") 18import("$dir_pw_build/python_action.gni") 19 20declare_args() { 21 # Whether or not the current target should build docs. 22 pw_docgen_BUILD_DOCS = false 23 24 # Set to enable Google Analytics tracking of generated docs. 25 pw_docgen_GOOGLE_ANALYTICS_ID = "" 26 27 # Set to define the number of parallel threads to use during the Sphinx build. 28 pw_docgen_THREADS = "" 29} 30 31# Defines a group of documentation files and assets. 32# 33# Args: 34# sources: Source files for the documentation (.rst or .md). 35# inputs: Additional resource files for the docs, such as images. 36# group_deps: Other pw_doc_group targets on which this group depends. 37# report_deps: Report card targets on which documentation depends. 38# other_deps: Dependencies on any other types of targets. 39template("pw_doc_group") { 40 assert(defined(invoker.sources) || defined(invoker.inputs), 41 "pw_doc_group requires at least one of sources or inputs") 42 43 # Create a group containing the source and input files so that docs are 44 # rebuilt on file modifications. 45 pw_input_group(target_name) { 46 _sources = [] 47 if (defined(invoker.sources)) { 48 _sources = invoker.sources 49 } 50 51 _inputs = [] 52 if (defined(invoker.inputs)) { 53 _inputs = invoker.inputs 54 } 55 56 metadata = { 57 pw_doc_sources = rebase_path(_sources, root_build_dir) 58 pw_doc_inputs = rebase_path(_inputs, root_build_dir) 59 } 60 61 deps = [] 62 if (defined(invoker.group_deps)) { 63 deps += invoker.group_deps 64 } 65 if (defined(invoker.report_deps)) { 66 deps += invoker.report_deps 67 } 68 if (defined(invoker.other_deps)) { 69 deps += invoker.other_deps 70 } 71 72 inputs = _sources + _inputs 73 } 74} 75 76# Creates a target to build HTML documentation from groups of sources. 77# 78# Args: 79# deps: List of pw_doc_group targets. 80# python_deps: Additional Python package dependencies that must be 81# fully built before sphinx can run. This will set the 82# PYTHONPATH when sphinx is run so automodule, 83# autoclass or autofunction RST directives work for 84# in-tree pw_python_package targets. 85# python_metadata_deps: Python-related dependencies that are only 86# used as deps for generating Python package 87# metadata list, not the overall documentation 88# generation. This only modifies PYTHONPATH so 89# sphinx can import in-tree Python files. This 90# should rarely be used by non-Pigweed code. 91# sources: Top-level documentation .rst source files. 92# conf: Configuration script (conf.py) for Sphinx. 93# output_directory: Path to directory to which HTML output is rendered. 94template("pw_doc_gen") { 95 assert(defined(invoker.deps), 96 "pw_doc_gen requires doc groups as dependencies") 97 assert(defined(invoker.sources) && invoker.sources != [], 98 "pw_doc_gen requires a 'sources' list with at least one .rst source") 99 assert(defined(invoker.conf), 100 "pw_doc_gen requires a 'conf' argument pointing a top-level conf.py") 101 assert(defined(invoker.output_directory), 102 "pw_doc_gen requires an 'output_directory' argument") 103 104 if (pw_docgen_BUILD_DOCS) { 105 # Collects all dependency metadata into a single JSON file. 106 _metadata_file_target = "${target_name}_metadata" 107 generated_file(_metadata_file_target) { 108 outputs = [ "$target_gen_dir/$target_name.json" ] 109 data_keys = [ 110 "pw_doc_sources", 111 "pw_doc_inputs", 112 ] 113 output_conversion = "json" 114 deps = invoker.deps 115 } 116 117 pw_python_action(target_name) { 118 module = "pw_docgen.docgen" 119 args = [ 120 "--gn-root", 121 rebase_path("//", root_build_dir), 122 "--gn-gen-root", 123 rebase_path(root_gen_dir, root_build_dir) + "/", 124 "--sphinx-build-dir", 125 rebase_path("$target_gen_dir/pw_docgen_tree", root_build_dir), 126 "--conf", 127 rebase_path(invoker.conf, root_build_dir), 128 "--out-dir", 129 rebase_path(invoker.output_directory, root_build_dir), 130 ] 131 132 # Enable Google Analytics if a measurement ID is provided 133 if (pw_docgen_GOOGLE_ANALYTICS_ID != "") { 134 args += [ 135 "--google-analytics-id", 136 pw_docgen_GOOGLE_ANALYTICS_ID, 137 ] 138 } 139 140 # Override the default number of threads for the Sphinx build. 141 if (pw_docgen_THREADS != "") { 142 args += [ 143 "-j", 144 pw_docgen_THREADS, 145 ] 146 } 147 148 # Metadata JSON file path. 149 args += [ "--metadata" ] + 150 rebase_path(get_target_outputs(":$_metadata_file_target"), 151 root_build_dir) 152 153 args += rebase_path(invoker.sources, root_build_dir) 154 155 python_deps = [ "$dir_pw_docgen/py" ] 156 deps = [ ":$_metadata_file_target" ] 157 158 # Additional Python deps that may be required for sphinx to run 159 # successfully. These deps will be fully built. 160 if (defined(invoker.python_deps)) { 161 python_deps += invoker.python_deps 162 } 163 164 # Required to set the PYTHONPATH for any automodule, autoclass, 165 # and autofunction RST directives. These deps will have their 166 # in-tree directories added to PYTHONPATH only. They will not be 167 # generated or built in the out directory first. 168 python_metadata_deps = [ "$dir_pw_docgen/py" ] 169 if (defined(invoker.python_metadata_deps)) { 170 python_metadata_deps += invoker.python_metadata_deps 171 } 172 173 inputs = [ invoker.conf ] + invoker.sources 174 stamp = true 175 } 176 } else { 177 group(target_name) { 178 } 179 } 180} 181