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) { |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 290 | _setup_sources = [ |
| 291 | "$_setup_dir/pyproject.toml", |
| 292 | "$_setup_dir/setup.cfg", |
| 293 | ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | # Argument: python_test_deps (list) |
| 297 | _python_test_deps = _python_deps # include all deps in test deps |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 298 | if (defined(invoker.python_test_deps)) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 299 | foreach(dep, invoker.python_test_deps) { |
| 300 | _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ] |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | if (_test_sources == []) { |
| 305 | assert(!defined(invoker.python_test_deps), |
| 306 | "python_test_deps was provided, but there are no tests in " + |
| 307 | get_label_info(":$target_name", "label_no_toolchain")) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 308 | not_needed([ "_python_test_deps" ]) |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 311 | _all_py_files = |
| 312 | _sources + _test_sources + filter_include(_setup_sources, [ "*.py" ]) |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 313 | |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 314 | # The pw_python_package subtargets are only instantiated in |
| 315 | # pw_build_PYTHON_TOOLCHAIN. Targets in other toolchains just refer to the |
| 316 | # targets in this toolchain. |
| 317 | if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) { |
Anthony DiGirolamo | 211dce4 | 2021-08-12 16:48:43 -0700 | [diff] [blame^] | 318 | # Create the package_metadata.json file. This is used by the |
| 319 | # pw_create_python_source_tree template. |
| 320 | _package_metadata_json_file = |
| 321 | "$target_gen_dir/$target_name/package_metadata.json" |
| 322 | |
| 323 | # Get Python package metadata and write to disk as JSON. |
| 324 | _package_metadata = { |
| 325 | gn_target_name = get_label_info(invoker.target_name, "label_no_toolchain") |
| 326 | |
| 327 | # Get package source files |
| 328 | sources = rebase_path(_sources, root_build_dir) |
| 329 | |
| 330 | # Get setup.cfg, pyproject.toml, or setup.py file |
| 331 | setup_sources = rebase_path(_setup_sources, root_build_dir) |
| 332 | |
| 333 | # Get test source files |
| 334 | tests = rebase_path(_test_sources, root_build_dir) |
| 335 | |
| 336 | # Get package input files (package data) |
| 337 | inputs = [] |
| 338 | if (defined(invoker.inputs)) { |
| 339 | inputs = rebase_path(invoker.inputs, root_build_dir) |
| 340 | } |
| 341 | |
| 342 | # Get generate_setup |
| 343 | if (defined(invoker.generate_setup)) { |
| 344 | generate_setup = invoker.generate_setup |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | # Finally, write out the json |
| 349 | write_file(_package_metadata_json_file, _package_metadata, "json") |
| 350 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 351 | # Declare the main Python package group. This represents the Python files, |
| 352 | # but does not take any actions. GN targets can depend on the package name |
| 353 | # to run when any files in the package change. |
| 354 | if (_generate_package) { |
| 355 | # If this package is generated, mirror the sources to the final directory. |
| 356 | pw_mirror_tree("$target_name._mirror_sources_to_out_dir") { |
| 357 | directory = _setup_dir |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 358 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 359 | sources = [] |
| 360 | if (defined(invoker.sources)) { |
| 361 | sources += invoker.sources |
| 362 | } |
| 363 | if (defined(invoker.tests)) { |
| 364 | sources += invoker.tests |
| 365 | } |
Wyatt Hepler | de20d74 | 2021-06-02 23:34:14 -0700 | [diff] [blame] | 366 | if (defined(invoker.inputs)) { |
| 367 | sources += invoker.inputs |
| 368 | } |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 369 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 370 | source_root = _source_root |
| 371 | public_deps = _python_deps + _other_deps |
Wyatt Hepler | 285636f | 2020-12-15 13:13:11 -0800 | [diff] [blame] | 372 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 373 | |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 374 | # Get generated_setup scope and write it to disk as JSON. |
| 375 | |
| 376 | # Expected setup.cfg structure: |
| 377 | # https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 378 | _gen_setup = invoker.generate_setup |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 379 | assert(defined(_gen_setup.metadata), |
| 380 | "'metadata = {}' is required in generate_package") |
| 381 | |
| 382 | # Get metadata which should contain at least name. |
| 383 | _gen_metadata = { |
| 384 | } |
| 385 | _gen_metadata = _gen_setup.metadata |
| 386 | assert( |
| 387 | defined(_gen_metadata.name), |
| 388 | "metadata = { name = 'package_name' } is required in generate_package") |
| 389 | |
| 390 | # Get options which should not have packages or package_data. |
| 391 | if (defined(_gen_setup.options)) { |
| 392 | _gen_options = { |
| 393 | } |
| 394 | _gen_options = _gen_setup.options |
| 395 | assert(!defined(_gen_options.packages) && |
| 396 | !defined(_gen_options.package_data), |
| 397 | "'packages' and 'package_data' may not be provided " + |
| 398 | "in 'generate_package' options.") |
| 399 | } |
| 400 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 401 | write_file("$_setup_dir/setup.json", _gen_setup, "json") |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 402 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 403 | # Generate the setup.py, py.typed, and __init__.py files as needed. |
| 404 | action(target_name) { |
Anthony DiGirolamo | 211dce4 | 2021-08-12 16:48:43 -0700 | [diff] [blame^] | 405 | metadata = { |
| 406 | pw_python_package_metadata_json = [ _package_metadata_json_file ] |
| 407 | } |
| 408 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 409 | script = "$dir_pw_build/py/pw_build/generate_python_package.py" |
| 410 | args = [ |
| 411 | "--label", |
| 412 | get_label_info(":$target_name", "label_no_toolchain"), |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 413 | "--generated-root", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 414 | rebase_path(_setup_dir, root_build_dir), |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 415 | "--setup-json", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 416 | rebase_path("$_setup_dir/setup.json", root_build_dir), |
| 417 | ] + rebase_path(_sources, root_build_dir) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 418 | |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 419 | # Pass in the .json information files for the imported proto libraries. |
| 420 | foreach(proto, _import_protos) { |
| 421 | _label = get_label_info(proto, "label_no_toolchain") + |
| 422 | ".python($pw_protobuf_compiler_TOOLCHAIN)" |
| 423 | _file = get_label_info(_label, "target_gen_dir") + "/" + |
| 424 | get_label_info(_label, "name") + ".json" |
| 425 | args += [ |
| 426 | "--proto-library", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 427 | rebase_path(_file, root_build_dir), |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 428 | ] |
| 429 | } |
| 430 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 431 | if (defined(invoker._pw_module_as_package) && |
| 432 | invoker._pw_module_as_package) { |
| 433 | args += [ "--module-as-package" ] |
| 434 | } |
| 435 | |
Wyatt Hepler | 0e1f5e4 | 2021-03-16 22:51:57 -0700 | [diff] [blame] | 436 | inputs = [ "$_setup_dir/setup.json" ] |
| 437 | |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 438 | public_deps = [ ":$target_name._mirror_sources_to_out_dir" ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 439 | |
| 440 | outputs = _setup_sources |
| 441 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 442 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 443 | # If the package is not generated, use an input group for the sources. |
| 444 | pw_input_group(target_name) { |
Anthony DiGirolamo | 211dce4 | 2021-08-12 16:48:43 -0700 | [diff] [blame^] | 445 | metadata = { |
| 446 | pw_python_package_metadata_json = [ _package_metadata_json_file ] |
| 447 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 448 | inputs = _all_py_files |
| 449 | if (defined(invoker.inputs)) { |
| 450 | inputs += invoker.inputs |
| 451 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 452 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 453 | deps = _python_deps + _other_deps |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 454 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 455 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 456 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 457 | if (_is_package) { |
| 458 | # Install this Python package and its dependencies in the current Python |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 459 | # environment using pip. |
| 460 | pw_python_action("$target_name._run_pip_install") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 461 | module = "pip" |
| 462 | public_deps = [] |
Joe Ethier | e93ccb1 | 2021-08-20 15:59:53 -0700 | [diff] [blame] | 463 | if (defined(invoker.public_deps)) { |
| 464 | public_deps += invoker.public_deps |
| 465 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 466 | |
| 467 | args = [ "install" ] |
| 468 | |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 469 | inputs = pw_build_PIP_CONSTRAINTS |
| 470 | foreach(_constraints_file, pw_build_PIP_CONSTRAINTS) { |
| 471 | args += [ |
| 472 | "--constraint", |
| 473 | rebase_path(_constraints_file, root_build_dir), |
| 474 | ] |
| 475 | } |
| 476 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 477 | # For generated packages, reinstall when any files change. For regular |
| 478 | # packages, only reinstall when setup.py changes. |
| 479 | if (_generate_package) { |
| 480 | public_deps += [ ":${invoker.target_name}" ] |
| 481 | } else { |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 482 | inputs += invoker.setup |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 483 | |
| 484 | # Install with --editable since the complete package is in source. |
| 485 | args += [ "--editable" ] |
| 486 | } |
| 487 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 488 | args += [ rebase_path(_setup_dir, root_build_dir) ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 489 | |
| 490 | stamp = true |
| 491 | |
| 492 | # Parallel pip installations don't work, so serialize pip invocations. |
Michael Spang | da057f7 | 2021-06-10 00:50:36 -0400 | [diff] [blame] | 493 | pool = "$dir_pw_build/pool:pip($default_toolchain)" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 494 | |
| 495 | foreach(dep, _python_deps) { |
| 496 | # We need to add a suffix to the target name, but the label is |
| 497 | # formatted as "//path/to:target(toolchain)", so we can't just append |
| 498 | # ".subtarget". Instead, we replace the opening parenthesis of the |
| 499 | # toolchain with ".suffix(". |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 500 | public_deps += [ string_replace(dep, "(", "._run_pip_install(") ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 504 | # Builds a Python wheel for this package. Records the output directory |
| 505 | # in the pw_python_package_wheels metadata key. |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 506 | pw_python_action("$target_name._build_wheel") { |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 507 | metadata = { |
| 508 | pw_python_package_wheels = [ "$target_out_dir/$target_name" ] |
| 509 | } |
| 510 | |
| 511 | module = "build" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 512 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 513 | args = |
| 514 | [ |
| 515 | rebase_path(_setup_dir, root_build_dir), |
| 516 | "--wheel", |
| 517 | "--no-isolation", |
| 518 | "--outdir", |
| 519 | ] + rebase_path(metadata.pw_python_package_wheels, root_build_dir) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 520 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 521 | deps = [ ":${invoker.target_name}" ] |
| 522 | foreach(dep, _python_deps) { |
| 523 | deps += [ string_replace(dep, "(", ".wheel(") ] |
| 524 | } |
| 525 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 526 | stamp = true |
| 527 | } |
| 528 | } else { |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 529 | # Stubs for non-package targets. |
| 530 | group("$target_name._run_pip_install") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 531 | } |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 532 | group("$target_name._build_wheel") { |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | # Create the .install and .wheel targets. To limit unnecessary pip |
| 537 | # executions, non-generated packages are only reinstalled when their |
| 538 | # setup.py changes. However, targets that depend on the .install subtarget |
| 539 | # re-run whenever any source files change. |
| 540 | # |
| 541 | # These targets just represent the source files if this isn't a package. |
| 542 | group("$target_name.install") { |
| 543 | public_deps = [ ":${invoker.target_name}" ] |
| 544 | |
| 545 | if (_is_package) { |
| 546 | public_deps += [ ":${invoker.target_name}._run_pip_install" ] |
| 547 | } |
| 548 | |
| 549 | foreach(dep, _python_deps) { |
| 550 | public_deps += [ string_replace(dep, "(", ".install(") ] |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | group("$target_name.wheel") { |
| 555 | public_deps = [ ":${invoker.target_name}.install" ] |
| 556 | |
| 557 | if (_is_package) { |
| 558 | public_deps += [ ":${invoker.target_name}._build_wheel" ] |
| 559 | } |
| 560 | |
| 561 | foreach(dep, _python_deps) { |
| 562 | public_deps += [ string_replace(dep, "(", ".wheel(") ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | # Define the static analysis targets for this package. |
| 567 | group("$target_name.lint") { |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 568 | deps = [] |
| 569 | foreach(_tool, _supported_static_analysis_tools) { |
| 570 | deps += [ ":${invoker.target_name}.lint.$_tool" ] |
| 571 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 572 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 573 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 574 | if (_static_analysis != [] || _test_sources != []) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 575 | # All packages to install for either general use or test running. |
| 576 | _test_install_deps = [ ":$target_name.install" ] |
| 577 | foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) { |
| 578 | _test_install_deps += [ string_replace(dep, "(", ".install(") ] |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | # 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] | 583 | foreach(_tool, _static_analysis) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 584 | # Run lint tools from the setup or target directory so that the tools detect |
| 585 | # config files (e.g. pylintrc or mypy.ini) in that directory. Config files |
| 586 | # may be explicitly specified with the pylintrc or mypy_ini arguments. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 587 | target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 588 | sources = _all_py_files |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 589 | deps = _test_install_deps |
| 590 | python_deps = _python_deps |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 591 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 592 | _optional_variables = [ |
| 593 | "mypy_ini", |
| 594 | "pylintrc", |
| 595 | ] |
| 596 | forward_variables_from(invoker, _optional_variables) |
| 597 | not_needed(_optional_variables) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 598 | } |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) { |
| 602 | pw_input_group("$target_name.lint.$_unused_tool") { |
| 603 | inputs = [] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 604 | if (defined(invoker.pylintrc)) { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 605 | inputs += [ invoker.pylintrc ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 606 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 607 | if (defined(invoker.mypy_ini)) { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 608 | inputs += [ invoker.mypy_ini ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 609 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 610 | } |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 611 | |
| 612 | # Generated packages with linting disabled never need the whole file list. |
| 613 | not_needed([ "_all_py_files" ]) |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 614 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 615 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 616 | # Create groups with the public target names ($target_name, $target_name.lint, |
| 617 | # $target_name.install, etc.). These are actually wrappers around internal |
| 618 | # Python actions instantiated with the default toolchain. This ensures there |
| 619 | # is only a single copy of each Python action in the build. |
| 620 | # |
| 621 | # The $target_name.tests group is created separately below. |
| 622 | group("$target_name") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 623 | deps = [ ":$target_name($pw_build_PYTHON_TOOLCHAIN)" ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) { |
| 627 | group("$target_name.$subtarget") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 628 | deps = |
| 629 | [ ":${invoker.target_name}.$subtarget($pw_build_PYTHON_TOOLCHAIN)" ] |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 630 | } |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 631 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 632 | |
| 633 | # Everything Python-related is only instantiated in the default toolchain. |
| 634 | # Silence not-needed warnings except for in the default toolchain. |
| 635 | not_needed("*") |
| 636 | not_needed(invoker, "*") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | # Create a target for each test file. |
| 640 | _test_targets = [] |
| 641 | |
| 642 | foreach(test, _test_sources) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 643 | if (_is_package) { |
| 644 | _name = rebase_path(test, _setup_dir) |
| 645 | } else { |
| 646 | _name = test |
| 647 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 648 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 649 | _test_target = "$target_name.tests." + string_replace(_name, "/", "_") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 650 | |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 651 | if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 652 | pw_python_action(_test_target) { |
| 653 | script = test |
| 654 | stamp = true |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 655 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 656 | deps = _test_install_deps |
| 657 | |
| 658 | foreach(dep, _python_test_deps) { |
| 659 | deps += [ string_replace(dep, "(", ".tests(") ] |
| 660 | } |
| 661 | } |
| 662 | } else { |
| 663 | # Create a public version of each test target, so tests can be executed as |
| 664 | # //path/to:package.tests.foo.py. |
| 665 | group(_test_target) { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 666 | deps = [ ":$_test_target($pw_build_PYTHON_TOOLCHAIN)" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 670 | _test_targets += [ ":$_test_target" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | group("$target_name.tests") { |
| 674 | deps = _test_targets |
| 675 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 676 | |
| 677 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 678 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | # Declares a group of Python packages or other Python groups. pw_python_groups |
| 682 | # expose the same set of subtargets as pw_python_package (e.g. |
| 683 | # "$group_name.lint" and "$group_name.tests"), but these apply to all packages |
| 684 | # in deps and their dependencies. |
| 685 | template("pw_python_group") { |
| 686 | if (defined(invoker.python_deps)) { |
| 687 | _python_deps = invoker.python_deps |
| 688 | } else { |
| 689 | _python_deps = [] |
Wyatt Hepler | a3ca62a | 2021-05-04 16:21:43 -0700 | [diff] [blame] | 690 | not_needed([ "invoker" ]) # Allow empty groups. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | group(target_name) { |
| 694 | deps = _python_deps |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 695 | |
| 696 | if (defined(invoker.other_deps)) { |
| 697 | deps += invoker.other_deps |
| 698 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 701 | foreach(subtarget, pw_python_package_subtargets) { |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 702 | group("$target_name.$subtarget") { |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 703 | public_deps = [] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 704 | foreach(dep, _python_deps) { |
| 705 | # Split out the toolchain to support deps with a toolchain specified. |
| 706 | _target = get_label_info(dep, "label_no_toolchain") |
| 707 | _toolchain = get_label_info(dep, "toolchain") |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 708 | public_deps += [ "$_target.$subtarget($_toolchain)" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 712 | |
| 713 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 714 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 715 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 716 | |
| 717 | # Declares Python scripts or tests that are not part of a Python package. |
| 718 | # Similar to pw_python_package, but only supports a subset of its features. |
| 719 | # |
| 720 | # pw_python_script accepts the same arguments as pw_python_package, except |
| 721 | # `setup` cannot be provided. |
| 722 | # |
| 723 | # pw_python_script provides the same subtargets as pw_python_package, but |
| 724 | # $target_name.install and $target_name.wheel only affect the python_deps of |
| 725 | # this GN target, not the target itself. |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 726 | # |
| 727 | # pw_python_script allows creating a pw_python_action associated with the |
| 728 | # script. This is provided by passing an 'action' scope to pw_python_script. |
| 729 | # This functions like a normal action, with a few additions: the action uses the |
| 730 | # pw_python_script's python_deps and defaults to using the source file as its |
| 731 | # 'script' argument, if there is only a single source file. |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 732 | template("pw_python_script") { |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 733 | _package_variables = [ |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 734 | "sources", |
| 735 | "tests", |
| 736 | "python_deps", |
| 737 | "other_deps", |
| 738 | "inputs", |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 739 | "pylintrc", |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 740 | "mypy_ini", |
| 741 | "static_analysis", |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 742 | ] |
| 743 | |
| 744 | pw_python_package(target_name) { |
| 745 | _pw_standalone = true |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 746 | forward_variables_from(invoker, _package_variables) |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 747 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 748 | |
| 749 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 750 | } |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 751 | |
| 752 | if (defined(invoker.action)) { |
| 753 | pw_python_action("$target_name.action") { |
| 754 | forward_variables_from(invoker.action, "*", [ "python_deps" ]) |
| 755 | python_deps = [ ":${invoker.target_name}" ] |
| 756 | |
| 757 | if (!defined(script) && !defined(module) && defined(invoker.sources)) { |
| 758 | _sources = invoker.sources |
| 759 | assert(_sources != [] && _sources == [ _sources[0] ], |
| 760 | "'script' must be specified unless there is only one source " + |
| 761 | "in 'sources'") |
| 762 | script = _sources[0] |
| 763 | } |
| 764 | } |
| 765 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 766 | } |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 767 | |
| 768 | # Represents a list of Python requirements, as in a requirements.txt. |
| 769 | # |
| 770 | # Args: |
| 771 | # files: One or more requirements.txt files. |
| 772 | # requirements: A list of requirements.txt-style requirements. |
| 773 | template("pw_python_requirements") { |
| 774 | assert(defined(invoker.files) || defined(invoker.requirements), |
| 775 | "pw_python_requirements requires a list of requirements.txt files " + |
| 776 | "in the 'files' arg or requirements in 'requirements'") |
| 777 | |
| 778 | _requirements_files = [] |
| 779 | |
| 780 | if (defined(invoker.files)) { |
| 781 | _requirements_files += invoker.files |
| 782 | } |
| 783 | |
| 784 | if (defined(invoker.requirements)) { |
| 785 | _requirements_file = "$target_gen_dir/$target_name.requirements.txt" |
| 786 | write_file(_requirements_file, invoker.requirements) |
| 787 | _requirements_files += [ _requirements_file ] |
| 788 | } |
| 789 | |
| 790 | # The default target represents the requirements themselves. |
| 791 | pw_input_group(target_name) { |
| 792 | inputs = _requirements_files |
| 793 | } |
| 794 | |
| 795 | # Use the same subtargets as pw_python_package so these targets can be listed |
| 796 | # as python_deps of pw_python_packages. |
| 797 | pw_python_action("$target_name.install") { |
| 798 | inputs = _requirements_files |
| 799 | |
| 800 | module = "pip" |
| 801 | args = [ "install" ] |
| 802 | |
| 803 | foreach(_requirements_file, inputs) { |
| 804 | args += [ |
| 805 | "--requirement", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 806 | rebase_path(_requirements_file, root_build_dir), |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 807 | ] |
| 808 | } |
| 809 | |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 810 | inputs += pw_build_PIP_CONSTRAINTS |
| 811 | foreach(_constraints_file, pw_build_PIP_CONSTRAINTS) { |
| 812 | args += [ |
| 813 | "--constraint", |
| 814 | rebase_path(_constraints_file, root_build_dir), |
| 815 | ] |
| 816 | } |
| 817 | |
Michael Spang | da057f7 | 2021-06-10 00:50:36 -0400 | [diff] [blame] | 818 | pool = "$dir_pw_build/pool:pip($default_toolchain)" |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 819 | stamp = true |
| 820 | } |
| 821 | |
| 822 | # Create stubs for the unused subtargets so that pw_python_requirements can be |
| 823 | # used as python_deps. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 824 | foreach(subtarget, pw_python_package_subtargets - [ "install" ]) { |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 825 | group("$target_name.$subtarget") { |
| 826 | } |
| 827 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 828 | |
| 829 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 830 | } |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 831 | } |