Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 1 | # Copyright 2021 The Pigweed Authors |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 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 | |
| 15 | import("//build_overrides/pigweed.gni") |
| 16 | |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 17 | import("$dir_pw_build/input_group.gni") |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 18 | import("$dir_pw_build/mirror_tree.gni") |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 19 | import("$dir_pw_build/python_action.gni") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 20 | |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 21 | # Python packages provide the following targets as $target_name.$subtarget. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 22 | pw_python_package_subtargets = [ |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 23 | "tests", |
| 24 | "lint", |
| 25 | "lint.mypy", |
| 26 | "lint.pylint", |
| 27 | "install", |
| 28 | "wheel", |
| 29 | ] |
| 30 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 31 | # Internal template that runs Mypy. |
| 32 | template("_pw_python_static_analysis_mypy") { |
| 33 | pw_python_action(target_name) { |
| 34 | module = "mypy" |
| 35 | args = [ |
| 36 | "--pretty", |
| 37 | "--show-error-codes", |
| 38 | ] |
| 39 | |
| 40 | if (defined(invoker.mypy_ini)) { |
| 41 | args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ] |
| 42 | inputs = [ invoker.mypy_ini ] |
| 43 | } |
| 44 | |
| 45 | args += rebase_path(invoker.sources) |
| 46 | |
| 47 | # Use this environment variable to force mypy to colorize output. |
| 48 | # See https://github.com/python/mypy/issues/7771 |
| 49 | environment = [ "MYPY_FORCE_COLOR=1" ] |
| 50 | |
| 51 | directory = invoker.directory |
| 52 | stamp = true |
| 53 | |
| 54 | deps = invoker.deps |
| 55 | |
| 56 | foreach(dep, invoker.python_deps) { |
| 57 | deps += [ string_replace(dep, "(", ".lint.mypy(") ] |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | # Internal template that runs Pylint. |
| 63 | template("_pw_python_static_analysis_pylint") { |
| 64 | # Create a target to run pylint on each of the Python files in this |
| 65 | # package and its dependencies. |
| 66 | pw_python_action_foreach(target_name) { |
| 67 | module = "pylint" |
| 68 | args = [ |
| 69 | rebase_path(".") + "/{{source_target_relative}}", |
| 70 | "--jobs=1", |
| 71 | "--output-format=colorized", |
| 72 | ] |
| 73 | |
| 74 | if (defined(invoker.pylintrc)) { |
| 75 | args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ] |
| 76 | inputs = [ invoker.pylintrc ] |
| 77 | } |
| 78 | |
| 79 | if (host_os == "win") { |
| 80 | # Allow CRLF on Windows, in case Git is set to switch line endings. |
| 81 | args += [ "--disable=unexpected-line-ending-format" ] |
| 82 | } |
| 83 | |
| 84 | sources = invoker.sources |
| 85 | directory = invoker.directory |
| 86 | |
| 87 | stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed" |
| 88 | |
| 89 | public_deps = invoker.deps |
| 90 | |
| 91 | foreach(dep, invoker.python_deps) { |
| 92 | public_deps += [ string_replace(dep, "(", ".lint.pylint(") ] |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 97 | # Defines a Python package. GN Python packages contain several GN targets: |
| 98 | # |
| 99 | # - $name - Provides the Python files in the build, but does not take any |
| 100 | # actions. All subtargets depend on this target. |
| 101 | # - $name.lint - Runs static analyis tools on the Python code. This is a group |
| 102 | # of two subtargets: |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 103 | # - $name.lint.mypy - Runs mypy (if enabled). |
| 104 | # - $name.lint.pylint - Runs pylint (if enabled). |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 105 | # - $name.tests - Runs all tests for this package. |
Rob Mohr | fcf6037 | 2020-11-04 11:41:36 -0800 | [diff] [blame] | 106 | # - $name.install - Installs the package in a venv. |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 107 | # - $name.wheel - Builds a Python wheel for the package. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 108 | # |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 109 | # All Python packages are instantiated with the default toolchain, regardless of |
| 110 | # the current toolchain. |
| 111 | # |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 112 | # Args: |
| 113 | # setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg), |
| 114 | # which must all be in the same directory. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 115 | # generate_setup: As an alternative to 'setup', generate setup files with the |
| 116 | # keywords in this scope. 'name' is required. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 117 | # sources: Python sources files in the package. |
| 118 | # tests: Test files for this Python package. |
| 119 | # python_deps: Dependencies on other pw_python_packages in the GN build. |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 120 | # python_test_deps: Test-only pw_python_package dependencies. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 121 | # other_deps: Dependencies on GN targets that are not pw_python_packages. |
| 122 | # inputs: Other files to track, such as package_data. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 123 | # proto_library: A pw_proto_library target to embed in this Python package. |
| 124 | # generate_setup is required in place of setup if proto_library is used. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 125 | # static_analysis: List of static analysis tools to run; "*" (default) runs |
| 126 | # all tools. The supported tools are "mypy" and "pylint". |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 127 | # pylintrc: Optional path to a pylintrc configuration file to use. If not |
| 128 | # provided, Pylint's default rcfile search is used. Pylint is executed |
| 129 | # from the package's setup directory, so pylintrc files in that directory |
| 130 | # will take precedence over others. |
| 131 | # mypy_ini: Optional path to a mypy configuration file to use. If not |
| 132 | # provided, mypy's default configuration file search is used. mypy is |
| 133 | # executed from the package's setup directory, so mypy.ini files in that |
| 134 | # directory will take precedence over others. |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 135 | # |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 136 | template("pw_python_package") { |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 137 | # The Python targets are always instantiated in the default toolchain. Use |
| 138 | # fully qualified labels so that the toolchain is not lost. |
| 139 | _other_deps = [] |
| 140 | if (defined(invoker.other_deps)) { |
| 141 | foreach(dep, invoker.other_deps) { |
| 142 | _other_deps += [ get_label_info(dep, "label_with_toolchain") ] |
| 143 | } |
| 144 | } |
| 145 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 146 | _python_deps = [] |
| 147 | if (defined(invoker.python_deps)) { |
| 148 | foreach(dep, invoker.python_deps) { |
| 149 | _python_deps += [ get_label_info(dep, "label_with_toolchain") ] |
| 150 | } |
| 151 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 152 | |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 153 | # pw_python_script uses pw_python_package, but with a limited set of features. |
| 154 | # _pw_standalone signals that this target is actually a pw_python_script. |
| 155 | _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone) |
| 156 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 157 | _generate_package = false |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 158 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 159 | # Check the generate_setup and import_protos args to determine if this package |
| 160 | # is generated. |
| 161 | if (_is_package) { |
| 162 | assert(defined(invoker.generate_setup) != defined(invoker.setup), |
| 163 | "Either 'setup' or 'generate_setup' (but not both) must provided") |
| 164 | |
| 165 | if (defined(invoker.proto_library)) { |
| 166 | assert(invoker.proto_library != "", "'proto_library' cannot be empty") |
| 167 | assert(defined(invoker.generate_setup), |
| 168 | "Python packages that import protos with 'proto_library' must " + |
| 169 | "use 'generate_setup' instead of 'setup'") |
| 170 | |
| 171 | _import_protos = [ invoker.proto_library ] |
| 172 | } else if (defined(invoker.generate_setup)) { |
| 173 | _import_protos = [] |
| 174 | } |
| 175 | |
| 176 | if (defined(invoker.generate_setup)) { |
| 177 | _generate_package = true |
| 178 | _setup_dir = "$target_gen_dir/$target_name.generated_python_package" |
| 179 | |
| 180 | if (defined(invoker.strip_prefix)) { |
| 181 | _source_root = invoker.strip_prefix |
| 182 | } else { |
| 183 | _source_root = "." |
| 184 | } |
| 185 | } else { |
| 186 | # Non-generated packages with sources provided need an __init__.py. |
| 187 | assert(!defined(invoker.sources) || invoker.sources == [] || |
| 188 | filter_include(invoker.sources, [ "*\b__init__.py" ]) != [], |
| 189 | "Python packages must have at least one __init__.py file") |
| 190 | |
| 191 | # Get the directories of the setup files. All must be in the same dir. |
| 192 | _setup_dirs = get_path_info(invoker.setup, "dir") |
| 193 | _setup_dir = _setup_dirs[0] |
| 194 | |
| 195 | foreach(dir, _setup_dirs) { |
| 196 | assert(dir == _setup_dir, |
| 197 | "All files in 'setup' must be in the same directory") |
| 198 | } |
| 199 | |
| 200 | assert(!defined(invoker.strip_prefix), |
| 201 | "'strip_prefix' may only be given if 'generate_setup' is provided") |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | # Process arguments defaults and set defaults. |
| 206 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 207 | _supported_static_analysis_tools = [ |
| 208 | "mypy", |
| 209 | "pylint", |
| 210 | ] |
| 211 | not_needed([ "_supported_static_analysis_tools" ]) |
| 212 | |
| 213 | # Argument: static_analysis (list of tool names or "*"); default = "*" (all) |
| 214 | if (!defined(invoker.static_analysis) || invoker.static_analysis == "*") { |
| 215 | _static_analysis = _supported_static_analysis_tools |
Keir Mierle | e65ddc8 | 2020-12-02 17:59:52 -0800 | [diff] [blame] | 216 | } else { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 217 | _static_analysis = invoker.static_analysis |
| 218 | } |
| 219 | |
| 220 | # TODO(hepler): Remove support for the lint option. |
| 221 | if (defined(invoker.lint)) { |
| 222 | assert(!defined(invoker.static_analysis), |
| 223 | "'lint' is deprecated; use 'static_analysis' instead") |
| 224 | |
| 225 | # Only allow 'lint = false', for backwards compatibility. |
| 226 | assert(invoker.lint == false, "'lint' is deprecated; use 'static_analysis'") |
| 227 | print("WARNING:", |
| 228 | "The 'lint' option for pw_python_package is deprecated.", |
| 229 | "Instead, use 'static_analysis = []' to disable linting.") |
| 230 | _static_analysis = [] |
| 231 | } |
| 232 | |
| 233 | foreach(_tool, _static_analysis) { |
| 234 | assert(_supported_static_analysis_tools + [ _tool ] - [ _tool ] != |
| 235 | _supported_static_analysis_tools, |
| 236 | "'$_tool' is not a supported static analysis tool") |
Keir Mierle | e65ddc8 | 2020-12-02 17:59:52 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 239 | # Argument: sources (list) |
| 240 | _sources = [] |
| 241 | if (defined(invoker.sources)) { |
| 242 | if (_generate_package) { |
| 243 | foreach(source, rebase_path(invoker.sources, _source_root)) { |
| 244 | _sources += [ "$_setup_dir/$source" ] |
| 245 | } |
| 246 | } else { |
| 247 | _sources += invoker.sources |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 250 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 251 | # Argument: tests (list) |
| 252 | _test_sources = [] |
| 253 | if (defined(invoker.tests)) { |
| 254 | if (_generate_package) { |
| 255 | foreach(source, rebase_path(invoker.tests, _source_root)) { |
| 256 | _test_sources += [ "$_setup_dir/$source" ] |
| 257 | } |
| 258 | } else { |
| 259 | _test_sources += invoker.tests |
Wyatt Hepler | d7dc655 | 2020-10-21 16:16:21 -0700 | [diff] [blame] | 260 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 263 | # Argument: setup (list) |
| 264 | _setup_sources = [] |
| 265 | if (defined(invoker.setup)) { |
| 266 | _setup_sources = invoker.setup |
| 267 | } else if (_generate_package) { |
| 268 | _setup_sources = [ "$_setup_dir/setup.py" ] |
| 269 | } |
| 270 | |
| 271 | # Argument: python_test_deps (list) |
| 272 | _python_test_deps = _python_deps # include all deps in test deps |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 273 | if (defined(invoker.python_test_deps)) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 274 | foreach(dep, invoker.python_test_deps) { |
| 275 | _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ] |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | if (_test_sources == []) { |
| 280 | assert(!defined(invoker.python_test_deps), |
| 281 | "python_test_deps was provided, but there are no tests in " + |
| 282 | get_label_info(":$target_name", "label_no_toolchain")) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 283 | not_needed([ "_python_test_deps" ]) |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 286 | _all_py_files = _sources + _test_sources + _setup_sources |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 287 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 288 | # The pw_python_package subtargets are only instantiated in the default |
| 289 | # toolchain. Other toolchains just refer to targets in the default toolchain. |
| 290 | if (current_toolchain == default_toolchain) { |
| 291 | # Declare the main Python package group. This represents the Python files, |
| 292 | # but does not take any actions. GN targets can depend on the package name |
| 293 | # to run when any files in the package change. |
| 294 | if (_generate_package) { |
| 295 | # If this package is generated, mirror the sources to the final directory. |
| 296 | pw_mirror_tree("$target_name._mirror_sources_to_out_dir") { |
| 297 | directory = _setup_dir |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 298 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 299 | sources = [] |
| 300 | if (defined(invoker.sources)) { |
| 301 | sources += invoker.sources |
| 302 | } |
| 303 | if (defined(invoker.tests)) { |
| 304 | sources += invoker.tests |
| 305 | } |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 306 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 307 | source_root = _source_root |
| 308 | public_deps = _python_deps + _other_deps |
Wyatt Hepler | 285636f | 2020-12-15 13:13:11 -0800 | [diff] [blame] | 309 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 310 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 311 | # Depend on the proto's _gen targets (from the default toolchain). |
| 312 | _gen_protos = [] |
| 313 | foreach(proto, _import_protos) { |
| 314 | _gen_protos += |
| 315 | [ get_label_info(proto, "label_no_toolchain") + ".python._gen" ] |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 316 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 317 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 318 | generated_file("$target_name._protos") { |
| 319 | deps = _gen_protos |
| 320 | data_keys = [ "protoc_outputs" ] |
| 321 | outputs = [ "$_setup_dir/protos.txt" ] |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 322 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 323 | |
| 324 | _protos_file = get_target_outputs(":${invoker.target_name}._protos") |
| 325 | |
| 326 | generated_file("$target_name._protos_root") { |
| 327 | deps = _gen_protos |
| 328 | data_keys = [ "root" ] |
| 329 | outputs = [ "$_setup_dir/proto_root.txt" ] |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 330 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 331 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 332 | _root_file = get_target_outputs(":${invoker.target_name}._protos_root") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 333 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 334 | # Get generated_setup scope and write it to disk ask JSON. |
| 335 | _gen_setup = invoker.generate_setup |
| 336 | assert(defined(_gen_setup.name), "'name' is required in generate_package") |
| 337 | assert(!defined(_gen_setup.packages) && !defined(_gen_setup.package_data), |
| 338 | "'packages' and 'package_data' may not be provided " + |
| 339 | "in 'generate_package'") |
| 340 | write_file("$_setup_dir/setup.json", _gen_setup, "json") |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 341 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 342 | # Generate the setup.py, py.typed, and __init__.py files as needed. |
| 343 | action(target_name) { |
| 344 | script = "$dir_pw_build/py/pw_build/generate_python_package.py" |
| 345 | args = [ |
| 346 | "--label", |
| 347 | get_label_info(":$target_name", "label_no_toolchain"), |
| 348 | "--root", |
| 349 | rebase_path(_setup_dir), |
| 350 | "--setup-json", |
| 351 | rebase_path("$_setup_dir/setup.json"), |
| 352 | "--file-list", |
| 353 | rebase_path(_protos_file[0]), |
| 354 | "--file-list-root", |
| 355 | rebase_path(_root_file[0]), |
| 356 | ] + rebase_path(_sources) |
| 357 | |
| 358 | if (defined(invoker._pw_module_as_package) && |
| 359 | invoker._pw_module_as_package) { |
| 360 | args += [ "--module-as-package" ] |
| 361 | } |
| 362 | |
Wyatt Hepler | 0e1f5e4 | 2021-03-16 22:51:57 -0700 | [diff] [blame] | 363 | inputs = [ "$_setup_dir/setup.json" ] |
| 364 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 365 | public_deps = [ |
| 366 | ":$target_name._mirror_sources_to_out_dir", |
| 367 | ":$target_name._protos", |
| 368 | ":$target_name._protos_root", |
| 369 | ] |
| 370 | |
| 371 | foreach(proto, _import_protos) { |
| 372 | _tgt = get_label_info(proto, "label_no_toolchain") |
| 373 | _path = get_label_info("$_tgt($default_toolchain)", "target_gen_dir") |
| 374 | _name = get_label_info(_tgt, "name") |
| 375 | |
| 376 | args += [ |
| 377 | "--proto-library=$_tgt", |
| 378 | "--proto-library-file", |
| 379 | rebase_path("$_path/$_name.proto_library/python_package.txt"), |
| 380 | ] |
| 381 | |
| 382 | public_deps += [ "$_tgt.python._gen($default_toolchain)" ] |
| 383 | } |
| 384 | |
| 385 | outputs = _setup_sources |
| 386 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 387 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 388 | # If the package is not generated, use an input group for the sources. |
| 389 | pw_input_group(target_name) { |
| 390 | inputs = _all_py_files |
| 391 | if (defined(invoker.inputs)) { |
| 392 | inputs += invoker.inputs |
| 393 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 394 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 395 | deps = _python_deps + _other_deps |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 396 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 397 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 398 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 399 | if (_is_package) { |
| 400 | # Install this Python package and its dependencies in the current Python |
| 401 | # environment. |
| 402 | pw_python_action("$target_name.install") { |
| 403 | module = "pip" |
| 404 | public_deps = [] |
| 405 | |
| 406 | args = [ "install" ] |
| 407 | |
| 408 | # For generated packages, reinstall when any files change. For regular |
| 409 | # packages, only reinstall when setup.py changes. |
| 410 | if (_generate_package) { |
| 411 | public_deps += [ ":${invoker.target_name}" ] |
| 412 | } else { |
| 413 | inputs = invoker.setup |
| 414 | |
| 415 | # Install with --editable since the complete package is in source. |
| 416 | args += [ "--editable" ] |
| 417 | } |
| 418 | |
| 419 | args += [ rebase_path(_setup_dir) ] |
| 420 | |
| 421 | stamp = true |
| 422 | |
| 423 | # Parallel pip installations don't work, so serialize pip invocations. |
| 424 | pool = "$dir_pw_build:pip_pool" |
| 425 | |
| 426 | foreach(dep, _python_deps) { |
| 427 | # We need to add a suffix to the target name, but the label is |
| 428 | # formatted as "//path/to:target(toolchain)", so we can't just append |
| 429 | # ".subtarget". Instead, we replace the opening parenthesis of the |
| 430 | # toolchain with ".suffix(". |
| 431 | public_deps += [ string_replace(dep, "(", ".install(") ] |
| 432 | } |
| 433 | } |
| 434 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 435 | # Builds a Python wheel for this package. Records the output directory |
| 436 | # in the pw_python_package_wheels metadata key. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 437 | pw_python_action("$target_name.wheel") { |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 438 | metadata = { |
| 439 | pw_python_package_wheels = [ "$target_out_dir/$target_name" ] |
| 440 | } |
| 441 | |
| 442 | module = "build" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 443 | |
| 444 | args = [ |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 445 | rebase_path(_setup_dir), |
| 446 | "--wheel", |
| 447 | "--no-isolation", |
| 448 | "--outdir", |
| 449 | ] + rebase_path(metadata.pw_python_package_wheels) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 450 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 451 | deps = [ ":${invoker.target_name}" ] |
| 452 | foreach(dep, _python_deps) { |
| 453 | deps += [ string_replace(dep, "(", ".wheel(") ] |
| 454 | } |
| 455 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 456 | stamp = true |
| 457 | } |
| 458 | } else { |
| 459 | # If this is not a package, install or build wheels for its deps only. |
| 460 | group("$target_name.install") { |
| 461 | deps = [] |
| 462 | foreach(dep, _python_deps) { |
| 463 | deps += [ string_replace(dep, "(", ".install(") ] |
| 464 | } |
| 465 | } |
| 466 | group("$target_name.wheel") { |
| 467 | deps = [] |
| 468 | foreach(dep, _python_deps) { |
| 469 | deps += [ string_replace(dep, "(", ".wheel(") ] |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | # Define the static analysis targets for this package. |
| 475 | group("$target_name.lint") { |
| 476 | deps = [ |
| 477 | ":${invoker.target_name}.lint.mypy", |
| 478 | ":${invoker.target_name}.lint.pylint", |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 479 | ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 480 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 481 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 482 | if (_static_analysis != [] || _test_sources != []) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 483 | # All packages to install for either general use or test running. |
| 484 | _test_install_deps = [ ":$target_name.install" ] |
| 485 | foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) { |
| 486 | _test_install_deps += [ string_replace(dep, "(", ".install(") ] |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | # For packages that are not generated, create targets to run mypy and pylint. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 491 | foreach(_tool, _static_analysis) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 492 | # Run lint tools from the setup or target directory so that the tools detect |
| 493 | # config files (e.g. pylintrc or mypy.ini) in that directory. Config files |
| 494 | # may be explicitly specified with the pylintrc or mypy_ini arguments. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 495 | target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 496 | sources = _all_py_files |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 497 | deps = _test_install_deps |
| 498 | python_deps = _python_deps |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 499 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 500 | if (defined(_setup_dir)) { |
| 501 | directory = rebase_path(_setup_dir) |
| 502 | } else { |
| 503 | directory = rebase_path(".") |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 504 | } |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 505 | |
| 506 | _optional_variables = [ |
| 507 | "mypy_ini", |
| 508 | "pylintrc", |
| 509 | ] |
| 510 | forward_variables_from(invoker, _optional_variables) |
| 511 | not_needed(_optional_variables) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 512 | } |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) { |
| 516 | pw_input_group("$target_name.lint.$_unused_tool") { |
| 517 | inputs = [] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 518 | if (defined(invoker.pylintrc)) { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 519 | inputs += [ invoker.pylintrc ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 520 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 521 | if (defined(invoker.mypy_ini)) { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 522 | inputs += [ invoker.mypy_ini ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 523 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 524 | } |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 525 | |
| 526 | # Generated packages with linting disabled never need the whole file list. |
| 527 | not_needed([ "_all_py_files" ]) |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 528 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 529 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 530 | # Create groups with the public target names ($target_name, $target_name.lint, |
| 531 | # $target_name.install, etc.). These are actually wrappers around internal |
| 532 | # Python actions instantiated with the default toolchain. This ensures there |
| 533 | # is only a single copy of each Python action in the build. |
| 534 | # |
| 535 | # The $target_name.tests group is created separately below. |
| 536 | group("$target_name") { |
| 537 | deps = [ ":$target_name($default_toolchain)" ] |
| 538 | } |
| 539 | |
| 540 | foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) { |
| 541 | group("$target_name.$subtarget") { |
| 542 | deps = [ ":${invoker.target_name}.$subtarget($default_toolchain)" ] |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 543 | } |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 544 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 545 | |
| 546 | # Everything Python-related is only instantiated in the default toolchain. |
| 547 | # Silence not-needed warnings except for in the default toolchain. |
| 548 | not_needed("*") |
| 549 | not_needed(invoker, "*") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | # Create a target for each test file. |
| 553 | _test_targets = [] |
| 554 | |
| 555 | foreach(test, _test_sources) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 556 | if (_is_package) { |
| 557 | _name = rebase_path(test, _setup_dir) |
| 558 | } else { |
| 559 | _name = test |
| 560 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 561 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 562 | _test_target = "$target_name.tests." + string_replace(_name, "/", "_") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 563 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 564 | if (current_toolchain == default_toolchain) { |
| 565 | pw_python_action(_test_target) { |
| 566 | script = test |
| 567 | stamp = true |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 568 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 569 | deps = _test_install_deps |
| 570 | |
| 571 | foreach(dep, _python_test_deps) { |
| 572 | deps += [ string_replace(dep, "(", ".tests(") ] |
| 573 | } |
| 574 | } |
| 575 | } else { |
| 576 | # Create a public version of each test target, so tests can be executed as |
| 577 | # //path/to:package.tests.foo.py. |
| 578 | group(_test_target) { |
| 579 | deps = [ ":$_test_target($default_toolchain)" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 583 | _test_targets += [ ":$_test_target" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | group("$target_name.tests") { |
| 587 | deps = _test_targets |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | # Declares a group of Python packages or other Python groups. pw_python_groups |
| 592 | # expose the same set of subtargets as pw_python_package (e.g. |
| 593 | # "$group_name.lint" and "$group_name.tests"), but these apply to all packages |
| 594 | # in deps and their dependencies. |
| 595 | template("pw_python_group") { |
| 596 | if (defined(invoker.python_deps)) { |
| 597 | _python_deps = invoker.python_deps |
| 598 | } else { |
| 599 | _python_deps = [] |
| 600 | } |
| 601 | |
| 602 | group(target_name) { |
| 603 | deps = _python_deps |
| 604 | } |
| 605 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 606 | foreach(subtarget, pw_python_package_subtargets) { |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 607 | group("$target_name.$subtarget") { |
| 608 | deps = [] |
| 609 | foreach(dep, _python_deps) { |
| 610 | # Split out the toolchain to support deps with a toolchain specified. |
| 611 | _target = get_label_info(dep, "label_no_toolchain") |
| 612 | _toolchain = get_label_info(dep, "toolchain") |
| 613 | deps += [ "$_target.$subtarget($_toolchain)" ] |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 618 | |
| 619 | # Declares Python scripts or tests that are not part of a Python package. |
| 620 | # Similar to pw_python_package, but only supports a subset of its features. |
| 621 | # |
| 622 | # pw_python_script accepts the same arguments as pw_python_package, except |
| 623 | # `setup` cannot be provided. |
| 624 | # |
| 625 | # pw_python_script provides the same subtargets as pw_python_package, but |
| 626 | # $target_name.install and $target_name.wheel only affect the python_deps of |
| 627 | # this GN target, not the target itself. |
| 628 | template("pw_python_script") { |
| 629 | _supported_variables = [ |
| 630 | "sources", |
| 631 | "tests", |
| 632 | "python_deps", |
| 633 | "other_deps", |
| 634 | "inputs", |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 635 | "pylintrc", |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 636 | "mypy_ini", |
| 637 | "static_analysis", |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 638 | ] |
| 639 | |
| 640 | pw_python_package(target_name) { |
| 641 | _pw_standalone = true |
| 642 | forward_variables_from(invoker, _supported_variables) |
| 643 | } |
| 644 | } |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 645 | |
| 646 | # Represents a list of Python requirements, as in a requirements.txt. |
| 647 | # |
| 648 | # Args: |
| 649 | # files: One or more requirements.txt files. |
| 650 | # requirements: A list of requirements.txt-style requirements. |
| 651 | template("pw_python_requirements") { |
| 652 | assert(defined(invoker.files) || defined(invoker.requirements), |
| 653 | "pw_python_requirements requires a list of requirements.txt files " + |
| 654 | "in the 'files' arg or requirements in 'requirements'") |
| 655 | |
| 656 | _requirements_files = [] |
| 657 | |
| 658 | if (defined(invoker.files)) { |
| 659 | _requirements_files += invoker.files |
| 660 | } |
| 661 | |
| 662 | if (defined(invoker.requirements)) { |
| 663 | _requirements_file = "$target_gen_dir/$target_name.requirements.txt" |
| 664 | write_file(_requirements_file, invoker.requirements) |
| 665 | _requirements_files += [ _requirements_file ] |
| 666 | } |
| 667 | |
| 668 | # The default target represents the requirements themselves. |
| 669 | pw_input_group(target_name) { |
| 670 | inputs = _requirements_files |
| 671 | } |
| 672 | |
| 673 | # Use the same subtargets as pw_python_package so these targets can be listed |
| 674 | # as python_deps of pw_python_packages. |
| 675 | pw_python_action("$target_name.install") { |
| 676 | inputs = _requirements_files |
| 677 | |
| 678 | module = "pip" |
| 679 | args = [ "install" ] |
| 680 | |
| 681 | foreach(_requirements_file, inputs) { |
| 682 | args += [ |
| 683 | "--requirement", |
| 684 | rebase_path(_requirements_file), |
| 685 | ] |
| 686 | } |
| 687 | |
| 688 | pool = "$dir_pw_build:pip_pool" |
| 689 | stamp = true |
| 690 | } |
| 691 | |
| 692 | # Create stubs for the unused subtargets so that pw_python_requirements can be |
| 693 | # used as python_deps. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 694 | foreach(subtarget, pw_python_package_subtargets - [ "install" ]) { |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 695 | group("$target_name.$subtarget") { |
| 696 | } |
| 697 | } |
| 698 | } |