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