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