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 | |
Wyatt Hepler | 0e1f5e4 | 2021-03-16 22:51:57 -0700 | [diff] [blame^] | 272 | inputs = [ "$_setup_dir/setup.json" ] |
| 273 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 274 | public_deps = [ |
| 275 | ":$target_name._mirror_sources_to_out_dir", |
| 276 | ":$target_name._protos", |
| 277 | ":$target_name._protos_root", |
| 278 | ] |
| 279 | |
| 280 | foreach(proto, _import_protos) { |
| 281 | _tgt = get_label_info(proto, "label_no_toolchain") |
| 282 | _path = get_label_info("$_tgt($default_toolchain)", "target_gen_dir") |
| 283 | _name = get_label_info(_tgt, "name") |
| 284 | |
| 285 | args += [ |
| 286 | "--proto-library=$_tgt", |
| 287 | "--proto-library-file", |
| 288 | rebase_path("$_path/$_name.proto_library/python_package.txt"), |
| 289 | ] |
| 290 | |
| 291 | public_deps += [ "$_tgt.python._gen($default_toolchain)" ] |
| 292 | } |
| 293 | |
| 294 | outputs = _setup_sources |
| 295 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 296 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 297 | # If the package is not generated, use an input group for the sources. |
| 298 | pw_input_group(target_name) { |
| 299 | inputs = _all_py_files |
| 300 | if (defined(invoker.inputs)) { |
| 301 | inputs += invoker.inputs |
| 302 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 303 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 304 | deps = _python_deps + _other_deps |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 305 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 306 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 307 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 308 | if (_is_package) { |
| 309 | # Install this Python package and its dependencies in the current Python |
| 310 | # environment. |
| 311 | pw_python_action("$target_name.install") { |
| 312 | module = "pip" |
| 313 | public_deps = [] |
| 314 | |
| 315 | args = [ "install" ] |
| 316 | |
| 317 | # For generated packages, reinstall when any files change. For regular |
| 318 | # packages, only reinstall when setup.py changes. |
| 319 | if (_generate_package) { |
| 320 | public_deps += [ ":${invoker.target_name}" ] |
| 321 | } else { |
| 322 | inputs = invoker.setup |
| 323 | |
| 324 | # Install with --editable since the complete package is in source. |
| 325 | args += [ "--editable" ] |
| 326 | } |
| 327 | |
| 328 | args += [ rebase_path(_setup_dir) ] |
| 329 | |
| 330 | stamp = true |
| 331 | |
| 332 | # Parallel pip installations don't work, so serialize pip invocations. |
| 333 | pool = "$dir_pw_build:pip_pool" |
| 334 | |
| 335 | foreach(dep, _python_deps) { |
| 336 | # We need to add a suffix to the target name, but the label is |
| 337 | # formatted as "//path/to:target(toolchain)", so we can't just append |
| 338 | # ".subtarget". Instead, we replace the opening parenthesis of the |
| 339 | # toolchain with ".suffix(". |
| 340 | public_deps += [ string_replace(dep, "(", ".install(") ] |
| 341 | } |
| 342 | } |
| 343 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 344 | # Builds a Python wheel for this package. Records the output directory |
| 345 | # in the pw_python_package_wheels metadata key. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 346 | pw_python_action("$target_name.wheel") { |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 347 | metadata = { |
| 348 | pw_python_package_wheels = [ "$target_out_dir/$target_name" ] |
| 349 | } |
| 350 | |
| 351 | module = "build" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 352 | |
| 353 | args = [ |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 354 | rebase_path(_setup_dir), |
| 355 | "--wheel", |
| 356 | "--no-isolation", |
| 357 | "--outdir", |
| 358 | ] + rebase_path(metadata.pw_python_package_wheels) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 359 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 360 | deps = [ ":${invoker.target_name}" ] |
| 361 | foreach(dep, _python_deps) { |
| 362 | deps += [ string_replace(dep, "(", ".wheel(") ] |
| 363 | } |
| 364 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 365 | stamp = true |
| 366 | } |
| 367 | } else { |
| 368 | # If this is not a package, install or build wheels for its deps only. |
| 369 | group("$target_name.install") { |
| 370 | deps = [] |
| 371 | foreach(dep, _python_deps) { |
| 372 | deps += [ string_replace(dep, "(", ".install(") ] |
| 373 | } |
| 374 | } |
| 375 | group("$target_name.wheel") { |
| 376 | deps = [] |
| 377 | foreach(dep, _python_deps) { |
| 378 | deps += [ string_replace(dep, "(", ".wheel(") ] |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | # Define the static analysis targets for this package. |
| 384 | group("$target_name.lint") { |
| 385 | deps = [ |
| 386 | ":${invoker.target_name}.lint.mypy", |
| 387 | ":${invoker.target_name}.lint.pylint", |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 388 | ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 389 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 390 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 391 | if (_should_lint || _test_sources != []) { |
| 392 | # All packages to install for either general use or test running. |
| 393 | _test_install_deps = [ ":$target_name.install" ] |
| 394 | foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) { |
| 395 | _test_install_deps += [ string_replace(dep, "(", ".install(") ] |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | # For packages that are not generated, create targets to run mypy and pylint. |
| 400 | if (_should_lint) { |
| 401 | # Run lint tools from the setup or target directory so that the tools detect |
| 402 | # config files (e.g. pylintrc or mypy.ini) in that directory. Config files |
| 403 | # may be explicitly specified with the pylintrc or mypy_ini arguments. |
| 404 | if (defined(_setup_dir)) { |
| 405 | _lint_directory = rebase_path(_setup_dir) |
| 406 | } else { |
| 407 | _lint_directory = rebase_path(".") |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 410 | pw_python_action("$target_name.lint.mypy") { |
| 411 | module = "mypy" |
| 412 | args = [ |
| 413 | "--pretty", |
| 414 | "--show-error-codes", |
| 415 | ] |
| 416 | |
| 417 | if (defined(invoker.mypy_ini)) { |
| 418 | args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ] |
| 419 | inputs = [ invoker.mypy_ini ] |
| 420 | } |
| 421 | |
| 422 | args += rebase_path(_all_py_files) |
| 423 | |
| 424 | # Use this environment variable to force mypy to colorize output. |
| 425 | # See https://github.com/python/mypy/issues/7771 |
| 426 | environment = [ "MYPY_FORCE_COLOR=1" ] |
| 427 | |
| 428 | directory = _lint_directory |
| 429 | stamp = true |
| 430 | |
| 431 | deps = _test_install_deps |
| 432 | |
| 433 | foreach(dep, _python_test_deps) { |
| 434 | deps += [ string_replace(dep, "(", ".lint.mypy(") ] |
| 435 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 436 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 437 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 438 | # Create a target to run pylint on each of the Python files in this |
| 439 | # package and its dependencies. |
| 440 | pw_python_action_foreach("$target_name.lint.pylint") { |
| 441 | module = "pylint" |
| 442 | args = [ |
| 443 | rebase_path(".") + "/{{source_target_relative}}", |
| 444 | "--jobs=1", |
| 445 | "--output-format=colorized", |
| 446 | ] |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 447 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 448 | if (defined(invoker.pylintrc)) { |
| 449 | args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ] |
| 450 | inputs = [ invoker.pylintrc ] |
| 451 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 452 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 453 | if (host_os == "win") { |
| 454 | # Allow CRLF on Windows, in case Git is set to switch line endings. |
| 455 | args += [ "--disable=unexpected-line-ending-format" ] |
| 456 | } |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 457 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 458 | sources = _all_py_files |
| 459 | |
| 460 | directory = _lint_directory |
| 461 | stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed" |
| 462 | |
| 463 | public_deps = _test_install_deps |
| 464 | |
| 465 | foreach(dep, _python_test_deps) { |
| 466 | public_deps += [ string_replace(dep, "(", ".lint.pylint(") ] |
| 467 | } |
| 468 | } |
| 469 | } else { |
| 470 | pw_input_group("$target_name.lint.mypy") { |
| 471 | if (defined(invoker.pylintrc)) { |
| 472 | inputs = [ invoker.pylintrc ] |
| 473 | } |
| 474 | } |
| 475 | pw_input_group("$target_name.lint.pylint") { |
| 476 | if (defined(invoker.mypy_ini)) { |
| 477 | inputs = [ invoker.mypy_ini ] |
| 478 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 479 | } |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 480 | |
| 481 | # Generated packages with linting disabled never need the whole file list. |
| 482 | not_needed([ "_all_py_files" ]) |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 483 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 484 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 485 | # Create groups with the public target names ($target_name, $target_name.lint, |
| 486 | # $target_name.install, etc.). These are actually wrappers around internal |
| 487 | # Python actions instantiated with the default toolchain. This ensures there |
| 488 | # is only a single copy of each Python action in the build. |
| 489 | # |
| 490 | # The $target_name.tests group is created separately below. |
| 491 | group("$target_name") { |
| 492 | deps = [ ":$target_name($default_toolchain)" ] |
| 493 | } |
| 494 | |
| 495 | foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) { |
| 496 | group("$target_name.$subtarget") { |
| 497 | deps = [ ":${invoker.target_name}.$subtarget($default_toolchain)" ] |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 498 | } |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 499 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 500 | |
| 501 | # Everything Python-related is only instantiated in the default toolchain. |
| 502 | # Silence not-needed warnings except for in the default toolchain. |
| 503 | not_needed("*") |
| 504 | not_needed(invoker, "*") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | # Create a target for each test file. |
| 508 | _test_targets = [] |
| 509 | |
| 510 | foreach(test, _test_sources) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 511 | if (_is_package) { |
| 512 | _name = rebase_path(test, _setup_dir) |
| 513 | } else { |
| 514 | _name = test |
| 515 | } |
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 | _test_target = "$target_name.tests." + string_replace(_name, "/", "_") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 518 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 519 | if (current_toolchain == default_toolchain) { |
| 520 | pw_python_action(_test_target) { |
| 521 | script = test |
| 522 | stamp = true |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 523 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 524 | deps = _test_install_deps |
| 525 | |
| 526 | foreach(dep, _python_test_deps) { |
| 527 | deps += [ string_replace(dep, "(", ".tests(") ] |
| 528 | } |
| 529 | } |
| 530 | } else { |
| 531 | # Create a public version of each test target, so tests can be executed as |
| 532 | # //path/to:package.tests.foo.py. |
| 533 | group(_test_target) { |
| 534 | deps = [ ":$_test_target($default_toolchain)" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 538 | _test_targets += [ ":$_test_target" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | group("$target_name.tests") { |
| 542 | deps = _test_targets |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | # Declares a group of Python packages or other Python groups. pw_python_groups |
| 547 | # expose the same set of subtargets as pw_python_package (e.g. |
| 548 | # "$group_name.lint" and "$group_name.tests"), but these apply to all packages |
| 549 | # in deps and their dependencies. |
| 550 | template("pw_python_group") { |
| 551 | if (defined(invoker.python_deps)) { |
| 552 | _python_deps = invoker.python_deps |
| 553 | } else { |
| 554 | _python_deps = [] |
| 555 | } |
| 556 | |
| 557 | group(target_name) { |
| 558 | deps = _python_deps |
| 559 | } |
| 560 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 561 | foreach(subtarget, pw_python_package_subtargets) { |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 562 | group("$target_name.$subtarget") { |
| 563 | deps = [] |
| 564 | foreach(dep, _python_deps) { |
| 565 | # Split out the toolchain to support deps with a toolchain specified. |
| 566 | _target = get_label_info(dep, "label_no_toolchain") |
| 567 | _toolchain = get_label_info(dep, "toolchain") |
| 568 | deps += [ "$_target.$subtarget($_toolchain)" ] |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 573 | |
| 574 | # Declares Python scripts or tests that are not part of a Python package. |
| 575 | # Similar to pw_python_package, but only supports a subset of its features. |
| 576 | # |
| 577 | # pw_python_script accepts the same arguments as pw_python_package, except |
| 578 | # `setup` cannot be provided. |
| 579 | # |
| 580 | # pw_python_script provides the same subtargets as pw_python_package, but |
| 581 | # $target_name.install and $target_name.wheel only affect the python_deps of |
| 582 | # this GN target, not the target itself. |
| 583 | template("pw_python_script") { |
| 584 | _supported_variables = [ |
| 585 | "sources", |
| 586 | "tests", |
| 587 | "python_deps", |
| 588 | "other_deps", |
| 589 | "inputs", |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 590 | "pylintrc", |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 591 | ] |
| 592 | |
| 593 | pw_python_package(target_name) { |
| 594 | _pw_standalone = true |
| 595 | forward_variables_from(invoker, _supported_variables) |
| 596 | } |
| 597 | } |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 598 | |
| 599 | # Represents a list of Python requirements, as in a requirements.txt. |
| 600 | # |
| 601 | # Args: |
| 602 | # files: One or more requirements.txt files. |
| 603 | # requirements: A list of requirements.txt-style requirements. |
| 604 | template("pw_python_requirements") { |
| 605 | assert(defined(invoker.files) || defined(invoker.requirements), |
| 606 | "pw_python_requirements requires a list of requirements.txt files " + |
| 607 | "in the 'files' arg or requirements in 'requirements'") |
| 608 | |
| 609 | _requirements_files = [] |
| 610 | |
| 611 | if (defined(invoker.files)) { |
| 612 | _requirements_files += invoker.files |
| 613 | } |
| 614 | |
| 615 | if (defined(invoker.requirements)) { |
| 616 | _requirements_file = "$target_gen_dir/$target_name.requirements.txt" |
| 617 | write_file(_requirements_file, invoker.requirements) |
| 618 | _requirements_files += [ _requirements_file ] |
| 619 | } |
| 620 | |
| 621 | # The default target represents the requirements themselves. |
| 622 | pw_input_group(target_name) { |
| 623 | inputs = _requirements_files |
| 624 | } |
| 625 | |
| 626 | # Use the same subtargets as pw_python_package so these targets can be listed |
| 627 | # as python_deps of pw_python_packages. |
| 628 | pw_python_action("$target_name.install") { |
| 629 | inputs = _requirements_files |
| 630 | |
| 631 | module = "pip" |
| 632 | args = [ "install" ] |
| 633 | |
| 634 | foreach(_requirements_file, inputs) { |
| 635 | args += [ |
| 636 | "--requirement", |
| 637 | rebase_path(_requirements_file), |
| 638 | ] |
| 639 | } |
| 640 | |
| 641 | pool = "$dir_pw_build:pip_pool" |
| 642 | stamp = true |
| 643 | } |
| 644 | |
| 645 | # Create stubs for the unused subtargets so that pw_python_requirements can be |
| 646 | # used as python_deps. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 647 | foreach(subtarget, pw_python_package_subtargets - [ "install" ]) { |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 648 | group("$target_name.$subtarget") { |
| 649 | } |
| 650 | } |
| 651 | } |