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