blob: fccf21263154749a27a11a30445a025a6916a28f [file] [log] [blame]
Wyatt Hepleraf853ea2021-03-09 19:09:54 -08001# Copyright 2021 The Pigweed Authors
Wyatt Heplerb16dfd32020-10-12 08:46:38 -07002#
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
15import("//build_overrides/pigweed.gni")
16
Wyatt Heplerb8e13602020-10-19 11:20:36 -070017import("$dir_pw_build/input_group.gni")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080018import("$dir_pw_build/mirror_tree.gni")
Wyatt Heplerb8e13602020-10-19 11:20:36 -070019import("$dir_pw_build/python_action.gni")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070020
Wyatt Hepler438caa02021-01-15 17:13:11 -080021# Python packages provide the following targets as $target_name.$subtarget.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080022pw_python_package_subtargets = [
Wyatt Hepler438caa02021-01-15 17:13:11 -080023 "tests",
24 "lint",
25 "lint.mypy",
26 "lint.pylint",
27 "install",
28 "wheel",
29]
30
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070031# Defines a Python package. GN Python packages contain several GN targets:
32#
33# - $name - Provides the Python files in the build, but does not take any
34# actions. All subtargets depend on this target.
35# - $name.lint - Runs static analyis tools on the Python code. This is a group
36# of two subtargets:
Wyatt Hepler8ce90132020-12-03 10:57:20 -080037# - $name.lint.mypy - Runs mypy (if enabled).
38# - $name.lint.pylint - Runs pylint (if enabled).
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070039# - $name.tests - Runs all tests for this package.
Rob Mohrfcf60372020-11-04 11:41:36 -080040# - $name.install - Installs the package in a venv.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -080041# - $name.wheel - Builds a Python wheel for the package.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070042#
Wyatt Hepler438caa02021-01-15 17:13:11 -080043# All Python packages are instantiated with the default toolchain, regardless of
44# the current toolchain.
45#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070046# Args:
47# setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg),
48# which must all be in the same directory.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080049# generate_setup: As an alternative to 'setup', generate setup files with the
50# keywords in this scope. 'name' is required.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070051# sources: Python sources files in the package.
52# tests: Test files for this Python package.
53# python_deps: Dependencies on other pw_python_packages in the GN build.
Wyatt Hepler620b3e02021-01-22 09:14:45 -080054# python_test_deps: Test-only pw_python_package dependencies.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070055# other_deps: Dependencies on GN targets that are not pw_python_packages.
56# inputs: Other files to track, such as package_data.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080057# proto_library: A pw_proto_library target to embed in this Python package.
58# generate_setup is required in place of setup if proto_library is used.
Wyatt Hepler8ce90132020-12-03 10:57:20 -080059# lint: If true (default), applies mypy and pylint to the package. If false,
60# does not.
61# pylintrc: Optional path to a pylintrc configuration file to use. If not
62# provided, Pylint's default rcfile search is used. Pylint is executed
63# from the package's setup directory, so pylintrc files in that directory
64# will take precedence over others.
65# mypy_ini: Optional path to a mypy configuration file to use. If not
66# provided, mypy's default configuration file search is used. mypy is
67# executed from the package's setup directory, so mypy.ini files in that
68# directory will take precedence over others.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -080069#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070070template("pw_python_package") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -080071 # The Python targets are always instantiated in the default toolchain. Use
72 # fully qualified labels so that the toolchain is not lost.
73 _other_deps = []
74 if (defined(invoker.other_deps)) {
75 foreach(dep, invoker.other_deps) {
76 _other_deps += [ get_label_info(dep, "label_with_toolchain") ]
77 }
78 }
79
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080080 _python_deps = []
81 if (defined(invoker.python_deps)) {
82 foreach(dep, invoker.python_deps) {
83 _python_deps += [ get_label_info(dep, "label_with_toolchain") ]
84 }
85 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -070086
Wyatt Hepler45af57b2020-10-23 08:05:28 -070087 # pw_python_script uses pw_python_package, but with a limited set of features.
88 # _pw_standalone signals that this target is actually a pw_python_script.
89 _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone)
90
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080091 _generate_package = false
Alexei Frolova4c0aee2020-12-01 13:48:48 -080092
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080093 # Check the generate_setup and import_protos args to determine if this package
94 # is generated.
95 if (_is_package) {
96 assert(defined(invoker.generate_setup) != defined(invoker.setup),
97 "Either 'setup' or 'generate_setup' (but not both) must provided")
98
99 if (defined(invoker.proto_library)) {
100 assert(invoker.proto_library != "", "'proto_library' cannot be empty")
101 assert(defined(invoker.generate_setup),
102 "Python packages that import protos with 'proto_library' must " +
103 "use 'generate_setup' instead of 'setup'")
104
105 _import_protos = [ invoker.proto_library ]
106 } else if (defined(invoker.generate_setup)) {
107 _import_protos = []
108 }
109
110 if (defined(invoker.generate_setup)) {
111 _generate_package = true
112 _setup_dir = "$target_gen_dir/$target_name.generated_python_package"
113
114 if (defined(invoker.strip_prefix)) {
115 _source_root = invoker.strip_prefix
116 } else {
117 _source_root = "."
118 }
119 } else {
120 # Non-generated packages with sources provided need an __init__.py.
121 assert(!defined(invoker.sources) || invoker.sources == [] ||
122 filter_include(invoker.sources, [ "*\b__init__.py" ]) != [],
123 "Python packages must have at least one __init__.py file")
124
125 # Get the directories of the setup files. All must be in the same dir.
126 _setup_dirs = get_path_info(invoker.setup, "dir")
127 _setup_dir = _setup_dirs[0]
128
129 foreach(dir, _setup_dirs) {
130 assert(dir == _setup_dir,
131 "All files in 'setup' must be in the same directory")
132 }
133
134 assert(!defined(invoker.strip_prefix),
135 "'strip_prefix' may only be given if 'generate_setup' is provided")
136 }
137 }
138
139 # Process arguments defaults and set defaults.
140
141 # Argument: lint (bool); default = true.
Keir Mierlee65ddc82020-12-02 17:59:52 -0800142 if (defined(invoker.lint)) {
143 _should_lint = invoker.lint
144 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800145 _should_lint = true
Keir Mierlee65ddc82020-12-02 17:59:52 -0800146 }
147
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800148 # Argument: sources (list)
149 _sources = []
150 if (defined(invoker.sources)) {
151 if (_generate_package) {
152 foreach(source, rebase_path(invoker.sources, _source_root)) {
153 _sources += [ "$_setup_dir/$source" ]
154 }
155 } else {
156 _sources += invoker.sources
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700157 }
158 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700159
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800160 # Argument: tests (list)
161 _test_sources = []
162 if (defined(invoker.tests)) {
163 if (_generate_package) {
164 foreach(source, rebase_path(invoker.tests, _source_root)) {
165 _test_sources += [ "$_setup_dir/$source" ]
166 }
167 } else {
168 _test_sources += invoker.tests
Wyatt Heplerd7dc6552020-10-21 16:16:21 -0700169 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700170 }
171
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800172 # Argument: setup (list)
173 _setup_sources = []
174 if (defined(invoker.setup)) {
175 _setup_sources = invoker.setup
176 } else if (_generate_package) {
177 _setup_sources = [ "$_setup_dir/setup.py" ]
178 }
179
180 # Argument: python_test_deps (list)
181 _python_test_deps = _python_deps # include all deps in test deps
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800182 if (defined(invoker.python_test_deps)) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800183 foreach(dep, invoker.python_test_deps) {
184 _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ]
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800185 }
186 }
187
188 if (_test_sources == []) {
189 assert(!defined(invoker.python_test_deps),
190 "python_test_deps was provided, but there are no tests in " +
191 get_label_info(":$target_name", "label_no_toolchain"))
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800192 not_needed([ "_python_test_deps" ])
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800193 }
194
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800195 _all_py_files = _sources + _test_sources + _setup_sources
Wyatt Hepler438caa02021-01-15 17:13:11 -0800196
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800197 # The pw_python_package subtargets are only instantiated in the default
198 # toolchain. Other toolchains just refer to targets in the default toolchain.
199 if (current_toolchain == default_toolchain) {
200 # Declare the main Python package group. This represents the Python files,
201 # but does not take any actions. GN targets can depend on the package name
202 # to run when any files in the package change.
203 if (_generate_package) {
204 # If this package is generated, mirror the sources to the final directory.
205 pw_mirror_tree("$target_name._mirror_sources_to_out_dir") {
206 directory = _setup_dir
Wyatt Hepler438caa02021-01-15 17:13:11 -0800207
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800208 sources = []
209 if (defined(invoker.sources)) {
210 sources += invoker.sources
211 }
212 if (defined(invoker.tests)) {
213 sources += invoker.tests
214 }
Wyatt Hepler438caa02021-01-15 17:13:11 -0800215
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800216 source_root = _source_root
217 public_deps = _python_deps + _other_deps
Wyatt Hepler285636f2020-12-15 13:13:11 -0800218 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700219
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800220 # Depend on the proto's _gen targets (from the default toolchain).
221 _gen_protos = []
222 foreach(proto, _import_protos) {
223 _gen_protos +=
224 [ get_label_info(proto, "label_no_toolchain") + ".python._gen" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700225 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700226
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800227 generated_file("$target_name._protos") {
228 deps = _gen_protos
229 data_keys = [ "protoc_outputs" ]
230 outputs = [ "$_setup_dir/protos.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700231 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800232
233 _protos_file = get_target_outputs(":${invoker.target_name}._protos")
234
235 generated_file("$target_name._protos_root") {
236 deps = _gen_protos
237 data_keys = [ "root" ]
238 outputs = [ "$_setup_dir/proto_root.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700239 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700240
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800241 _root_file = get_target_outputs(":${invoker.target_name}._protos_root")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700242
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800243 # Get generated_setup scope and write it to disk ask JSON.
244 _gen_setup = invoker.generate_setup
245 assert(defined(_gen_setup.name), "'name' is required in generate_package")
246 assert(!defined(_gen_setup.packages) && !defined(_gen_setup.package_data),
247 "'packages' and 'package_data' may not be provided " +
248 "in 'generate_package'")
249 write_file("$_setup_dir/setup.json", _gen_setup, "json")
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800250
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800251 # Generate the setup.py, py.typed, and __init__.py files as needed.
252 action(target_name) {
253 script = "$dir_pw_build/py/pw_build/generate_python_package.py"
254 args = [
255 "--label",
256 get_label_info(":$target_name", "label_no_toolchain"),
257 "--root",
258 rebase_path(_setup_dir),
259 "--setup-json",
260 rebase_path("$_setup_dir/setup.json"),
261 "--file-list",
262 rebase_path(_protos_file[0]),
263 "--file-list-root",
264 rebase_path(_root_file[0]),
265 ] + rebase_path(_sources)
266
267 if (defined(invoker._pw_module_as_package) &&
268 invoker._pw_module_as_package) {
269 args += [ "--module-as-package" ]
270 }
271
Wyatt Hepler0e1f5e42021-03-16 22:51:57 -0700272 inputs = [ "$_setup_dir/setup.json" ]
273
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800274 public_deps = [
275 ":$target_name._mirror_sources_to_out_dir",
276 ":$target_name._protos",
277 ":$target_name._protos_root",
278 ]
279
280 foreach(proto, _import_protos) {
281 _tgt = get_label_info(proto, "label_no_toolchain")
282 _path = get_label_info("$_tgt($default_toolchain)", "target_gen_dir")
283 _name = get_label_info(_tgt, "name")
284
285 args += [
286 "--proto-library=$_tgt",
287 "--proto-library-file",
288 rebase_path("$_path/$_name.proto_library/python_package.txt"),
289 ]
290
291 public_deps += [ "$_tgt.python._gen($default_toolchain)" ]
292 }
293
294 outputs = _setup_sources
295 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800296 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800297 # If the package is not generated, use an input group for the sources.
298 pw_input_group(target_name) {
299 inputs = _all_py_files
300 if (defined(invoker.inputs)) {
301 inputs += invoker.inputs
302 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800303
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800304 deps = _python_deps + _other_deps
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800305 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700306 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700307
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800308 if (_is_package) {
309 # Install this Python package and its dependencies in the current Python
310 # environment.
311 pw_python_action("$target_name.install") {
312 module = "pip"
313 public_deps = []
314
315 args = [ "install" ]
316
317 # For generated packages, reinstall when any files change. For regular
318 # packages, only reinstall when setup.py changes.
319 if (_generate_package) {
320 public_deps += [ ":${invoker.target_name}" ]
321 } else {
322 inputs = invoker.setup
323
324 # Install with --editable since the complete package is in source.
325 args += [ "--editable" ]
326 }
327
328 args += [ rebase_path(_setup_dir) ]
329
330 stamp = true
331
332 # Parallel pip installations don't work, so serialize pip invocations.
333 pool = "$dir_pw_build:pip_pool"
334
335 foreach(dep, _python_deps) {
336 # We need to add a suffix to the target name, but the label is
337 # formatted as "//path/to:target(toolchain)", so we can't just append
338 # ".subtarget". Instead, we replace the opening parenthesis of the
339 # toolchain with ".suffix(".
340 public_deps += [ string_replace(dep, "(", ".install(") ]
341 }
342 }
343
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800344 # Builds a Python wheel for this package. Records the output directory
345 # in the pw_python_package_wheels metadata key.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800346 pw_python_action("$target_name.wheel") {
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800347 metadata = {
348 pw_python_package_wheels = [ "$target_out_dir/$target_name" ]
349 }
350
351 module = "build"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800352
353 args = [
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800354 rebase_path(_setup_dir),
355 "--wheel",
356 "--no-isolation",
357 "--outdir",
358 ] + rebase_path(metadata.pw_python_package_wheels)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800359
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800360 deps = [ ":${invoker.target_name}" ]
361 foreach(dep, _python_deps) {
362 deps += [ string_replace(dep, "(", ".wheel(") ]
363 }
364
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800365 stamp = true
366 }
367 } else {
368 # If this is not a package, install or build wheels for its deps only.
369 group("$target_name.install") {
370 deps = []
371 foreach(dep, _python_deps) {
372 deps += [ string_replace(dep, "(", ".install(") ]
373 }
374 }
375 group("$target_name.wheel") {
376 deps = []
377 foreach(dep, _python_deps) {
378 deps += [ string_replace(dep, "(", ".wheel(") ]
379 }
380 }
381 }
382
383 # Define the static analysis targets for this package.
384 group("$target_name.lint") {
385 deps = [
386 ":${invoker.target_name}.lint.mypy",
387 ":${invoker.target_name}.lint.pylint",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800388 ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800389 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700390
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800391 if (_should_lint || _test_sources != []) {
392 # All packages to install for either general use or test running.
393 _test_install_deps = [ ":$target_name.install" ]
394 foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) {
395 _test_install_deps += [ string_replace(dep, "(", ".install(") ]
396 }
397 }
398
399 # For packages that are not generated, create targets to run mypy and pylint.
400 if (_should_lint) {
401 # Run lint tools from the setup or target directory so that the tools detect
402 # config files (e.g. pylintrc or mypy.ini) in that directory. Config files
403 # may be explicitly specified with the pylintrc or mypy_ini arguments.
404 if (defined(_setup_dir)) {
405 _lint_directory = rebase_path(_setup_dir)
406 } else {
407 _lint_directory = rebase_path(".")
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800408 }
409
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800410 pw_python_action("$target_name.lint.mypy") {
411 module = "mypy"
412 args = [
413 "--pretty",
414 "--show-error-codes",
415 ]
416
417 if (defined(invoker.mypy_ini)) {
418 args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ]
419 inputs = [ invoker.mypy_ini ]
420 }
421
422 args += rebase_path(_all_py_files)
423
424 # Use this environment variable to force mypy to colorize output.
425 # See https://github.com/python/mypy/issues/7771
426 environment = [ "MYPY_FORCE_COLOR=1" ]
427
428 directory = _lint_directory
429 stamp = true
430
431 deps = _test_install_deps
432
433 foreach(dep, _python_test_deps) {
434 deps += [ string_replace(dep, "(", ".lint.mypy(") ]
435 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800436 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700437
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800438 # Create a target to run pylint on each of the Python files in this
439 # package and its dependencies.
440 pw_python_action_foreach("$target_name.lint.pylint") {
441 module = "pylint"
442 args = [
443 rebase_path(".") + "/{{source_target_relative}}",
444 "--jobs=1",
445 "--output-format=colorized",
446 ]
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800447
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800448 if (defined(invoker.pylintrc)) {
449 args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ]
450 inputs = [ invoker.pylintrc ]
451 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800452
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800453 if (host_os == "win") {
454 # Allow CRLF on Windows, in case Git is set to switch line endings.
455 args += [ "--disable=unexpected-line-ending-format" ]
456 }
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800457
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800458 sources = _all_py_files
459
460 directory = _lint_directory
461 stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed"
462
463 public_deps = _test_install_deps
464
465 foreach(dep, _python_test_deps) {
466 public_deps += [ string_replace(dep, "(", ".lint.pylint(") ]
467 }
468 }
469 } else {
470 pw_input_group("$target_name.lint.mypy") {
471 if (defined(invoker.pylintrc)) {
472 inputs = [ invoker.pylintrc ]
473 }
474 }
475 pw_input_group("$target_name.lint.pylint") {
476 if (defined(invoker.mypy_ini)) {
477 inputs = [ invoker.mypy_ini ]
478 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800479 }
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800480
481 # Generated packages with linting disabled never need the whole file list.
482 not_needed([ "_all_py_files" ])
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700483 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800484 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800485 # Create groups with the public target names ($target_name, $target_name.lint,
486 # $target_name.install, etc.). These are actually wrappers around internal
487 # Python actions instantiated with the default toolchain. This ensures there
488 # is only a single copy of each Python action in the build.
489 #
490 # The $target_name.tests group is created separately below.
491 group("$target_name") {
492 deps = [ ":$target_name($default_toolchain)" ]
493 }
494
495 foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) {
496 group("$target_name.$subtarget") {
497 deps = [ ":${invoker.target_name}.$subtarget($default_toolchain)" ]
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800498 }
Wyatt Heplerb8e13602020-10-19 11:20:36 -0700499 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800500
501 # Everything Python-related is only instantiated in the default toolchain.
502 # Silence not-needed warnings except for in the default toolchain.
503 not_needed("*")
504 not_needed(invoker, "*")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700505 }
506
507 # Create a target for each test file.
508 _test_targets = []
509
510 foreach(test, _test_sources) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800511 if (_is_package) {
512 _name = rebase_path(test, _setup_dir)
513 } else {
514 _name = test
515 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700516
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800517 _test_target = "$target_name.tests." + string_replace(_name, "/", "_")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700518
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800519 if (current_toolchain == default_toolchain) {
520 pw_python_action(_test_target) {
521 script = test
522 stamp = true
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800523
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800524 deps = _test_install_deps
525
526 foreach(dep, _python_test_deps) {
527 deps += [ string_replace(dep, "(", ".tests(") ]
528 }
529 }
530 } else {
531 # Create a public version of each test target, so tests can be executed as
532 # //path/to:package.tests.foo.py.
533 group(_test_target) {
534 deps = [ ":$_test_target($default_toolchain)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700535 }
536 }
537
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800538 _test_targets += [ ":$_test_target" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700539 }
540
541 group("$target_name.tests") {
542 deps = _test_targets
543 }
544}
545
546# Declares a group of Python packages or other Python groups. pw_python_groups
547# expose the same set of subtargets as pw_python_package (e.g.
548# "$group_name.lint" and "$group_name.tests"), but these apply to all packages
549# in deps and their dependencies.
550template("pw_python_group") {
551 if (defined(invoker.python_deps)) {
552 _python_deps = invoker.python_deps
553 } else {
554 _python_deps = []
555 }
556
557 group(target_name) {
558 deps = _python_deps
559 }
560
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800561 foreach(subtarget, pw_python_package_subtargets) {
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700562 group("$target_name.$subtarget") {
563 deps = []
564 foreach(dep, _python_deps) {
565 # Split out the toolchain to support deps with a toolchain specified.
566 _target = get_label_info(dep, "label_no_toolchain")
567 _toolchain = get_label_info(dep, "toolchain")
568 deps += [ "$_target.$subtarget($_toolchain)" ]
569 }
570 }
571 }
572}
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700573
574# Declares Python scripts or tests that are not part of a Python package.
575# Similar to pw_python_package, but only supports a subset of its features.
576#
577# pw_python_script accepts the same arguments as pw_python_package, except
578# `setup` cannot be provided.
579#
580# pw_python_script provides the same subtargets as pw_python_package, but
581# $target_name.install and $target_name.wheel only affect the python_deps of
582# this GN target, not the target itself.
583template("pw_python_script") {
584 _supported_variables = [
585 "sources",
586 "tests",
587 "python_deps",
588 "other_deps",
589 "inputs",
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800590 "pylintrc",
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700591 ]
592
593 pw_python_package(target_name) {
594 _pw_standalone = true
595 forward_variables_from(invoker, _supported_variables)
596 }
597}
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800598
599# Represents a list of Python requirements, as in a requirements.txt.
600#
601# Args:
602# files: One or more requirements.txt files.
603# requirements: A list of requirements.txt-style requirements.
604template("pw_python_requirements") {
605 assert(defined(invoker.files) || defined(invoker.requirements),
606 "pw_python_requirements requires a list of requirements.txt files " +
607 "in the 'files' arg or requirements in 'requirements'")
608
609 _requirements_files = []
610
611 if (defined(invoker.files)) {
612 _requirements_files += invoker.files
613 }
614
615 if (defined(invoker.requirements)) {
616 _requirements_file = "$target_gen_dir/$target_name.requirements.txt"
617 write_file(_requirements_file, invoker.requirements)
618 _requirements_files += [ _requirements_file ]
619 }
620
621 # The default target represents the requirements themselves.
622 pw_input_group(target_name) {
623 inputs = _requirements_files
624 }
625
626 # Use the same subtargets as pw_python_package so these targets can be listed
627 # as python_deps of pw_python_packages.
628 pw_python_action("$target_name.install") {
629 inputs = _requirements_files
630
631 module = "pip"
632 args = [ "install" ]
633
634 foreach(_requirements_file, inputs) {
635 args += [
636 "--requirement",
637 rebase_path(_requirements_file),
638 ]
639 }
640
641 pool = "$dir_pw_build:pip_pool"
642 stamp = true
643 }
644
645 # Create stubs for the unused subtargets so that pw_python_requirements can be
646 # used as python_deps.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800647 foreach(subtarget, pw_python_package_subtargets - [ "install" ]) {
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800648 group("$target_name.$subtarget") {
649 }
650 }
651}