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