blob: 2b1ce229f29d6c756edf2092c529fee85ff94a45 [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
272 public_deps = [
273 ":$target_name._mirror_sources_to_out_dir",
274 ":$target_name._protos",
275 ":$target_name._protos_root",
276 ]
277
278 foreach(proto, _import_protos) {
279 _tgt = get_label_info(proto, "label_no_toolchain")
280 _path = get_label_info("$_tgt($default_toolchain)", "target_gen_dir")
281 _name = get_label_info(_tgt, "name")
282
283 args += [
284 "--proto-library=$_tgt",
285 "--proto-library-file",
286 rebase_path("$_path/$_name.proto_library/python_package.txt"),
287 ]
288
289 public_deps += [ "$_tgt.python._gen($default_toolchain)" ]
290 }
291
292 outputs = _setup_sources
293 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800294 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800295 # If the package is not generated, use an input group for the sources.
296 pw_input_group(target_name) {
297 inputs = _all_py_files
298 if (defined(invoker.inputs)) {
299 inputs += invoker.inputs
300 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800301
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800302 deps = _python_deps + _other_deps
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800303 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700304 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700305
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800306 if (_is_package) {
307 # Install this Python package and its dependencies in the current Python
308 # environment.
309 pw_python_action("$target_name.install") {
310 module = "pip"
311 public_deps = []
312
313 args = [ "install" ]
314
315 # For generated packages, reinstall when any files change. For regular
316 # packages, only reinstall when setup.py changes.
317 if (_generate_package) {
318 public_deps += [ ":${invoker.target_name}" ]
319 } else {
320 inputs = invoker.setup
321
322 # Install with --editable since the complete package is in source.
323 args += [ "--editable" ]
324 }
325
326 args += [ rebase_path(_setup_dir) ]
327
328 stamp = true
329
330 # Parallel pip installations don't work, so serialize pip invocations.
331 pool = "$dir_pw_build:pip_pool"
332
333 foreach(dep, _python_deps) {
334 # We need to add a suffix to the target name, but the label is
335 # formatted as "//path/to:target(toolchain)", so we can't just append
336 # ".subtarget". Instead, we replace the opening parenthesis of the
337 # toolchain with ".suffix(".
338 public_deps += [ string_replace(dep, "(", ".install(") ]
339 }
340 }
341
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800342 # Builds a Python wheel for this package. Records the output directory
343 # in the pw_python_package_wheels metadata key.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800344 pw_python_action("$target_name.wheel") {
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800345 metadata = {
346 pw_python_package_wheels = [ "$target_out_dir/$target_name" ]
347 }
348
349 module = "build"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800350
351 args = [
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800352 rebase_path(_setup_dir),
353 "--wheel",
354 "--no-isolation",
355 "--outdir",
356 ] + rebase_path(metadata.pw_python_package_wheels)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800357
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800358 deps = [ ":${invoker.target_name}" ]
359 foreach(dep, _python_deps) {
360 deps += [ string_replace(dep, "(", ".wheel(") ]
361 }
362
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800363 stamp = true
364 }
365 } else {
366 # If this is not a package, install or build wheels for its deps only.
367 group("$target_name.install") {
368 deps = []
369 foreach(dep, _python_deps) {
370 deps += [ string_replace(dep, "(", ".install(") ]
371 }
372 }
373 group("$target_name.wheel") {
374 deps = []
375 foreach(dep, _python_deps) {
376 deps += [ string_replace(dep, "(", ".wheel(") ]
377 }
378 }
379 }
380
381 # Define the static analysis targets for this package.
382 group("$target_name.lint") {
383 deps = [
384 ":${invoker.target_name}.lint.mypy",
385 ":${invoker.target_name}.lint.pylint",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800386 ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800387 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700388
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800389 if (_should_lint || _test_sources != []) {
390 # All packages to install for either general use or test running.
391 _test_install_deps = [ ":$target_name.install" ]
392 foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) {
393 _test_install_deps += [ string_replace(dep, "(", ".install(") ]
394 }
395 }
396
397 # For packages that are not generated, create targets to run mypy and pylint.
398 if (_should_lint) {
399 # Run lint tools from the setup or target directory so that the tools detect
400 # config files (e.g. pylintrc or mypy.ini) in that directory. Config files
401 # may be explicitly specified with the pylintrc or mypy_ini arguments.
402 if (defined(_setup_dir)) {
403 _lint_directory = rebase_path(_setup_dir)
404 } else {
405 _lint_directory = rebase_path(".")
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800406 }
407
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800408 pw_python_action("$target_name.lint.mypy") {
409 module = "mypy"
410 args = [
411 "--pretty",
412 "--show-error-codes",
413 ]
414
415 if (defined(invoker.mypy_ini)) {
416 args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ]
417 inputs = [ invoker.mypy_ini ]
418 }
419
420 args += rebase_path(_all_py_files)
421
422 # Use this environment variable to force mypy to colorize output.
423 # See https://github.com/python/mypy/issues/7771
424 environment = [ "MYPY_FORCE_COLOR=1" ]
425
426 directory = _lint_directory
427 stamp = true
428
429 deps = _test_install_deps
430
431 foreach(dep, _python_test_deps) {
432 deps += [ string_replace(dep, "(", ".lint.mypy(") ]
433 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800434 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700435
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800436 # Create a target to run pylint on each of the Python files in this
437 # package and its dependencies.
438 pw_python_action_foreach("$target_name.lint.pylint") {
439 module = "pylint"
440 args = [
441 rebase_path(".") + "/{{source_target_relative}}",
442 "--jobs=1",
443 "--output-format=colorized",
444 ]
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800445
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800446 if (defined(invoker.pylintrc)) {
447 args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ]
448 inputs = [ invoker.pylintrc ]
449 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800450
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800451 if (host_os == "win") {
452 # Allow CRLF on Windows, in case Git is set to switch line endings.
453 args += [ "--disable=unexpected-line-ending-format" ]
454 }
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800455
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800456 sources = _all_py_files
457
458 directory = _lint_directory
459 stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed"
460
461 public_deps = _test_install_deps
462
463 foreach(dep, _python_test_deps) {
464 public_deps += [ string_replace(dep, "(", ".lint.pylint(") ]
465 }
466 }
467 } else {
468 pw_input_group("$target_name.lint.mypy") {
469 if (defined(invoker.pylintrc)) {
470 inputs = [ invoker.pylintrc ]
471 }
472 }
473 pw_input_group("$target_name.lint.pylint") {
474 if (defined(invoker.mypy_ini)) {
475 inputs = [ invoker.mypy_ini ]
476 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800477 }
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800478
479 # Generated packages with linting disabled never need the whole file list.
480 not_needed([ "_all_py_files" ])
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700481 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800482 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800483 # Create groups with the public target names ($target_name, $target_name.lint,
484 # $target_name.install, etc.). These are actually wrappers around internal
485 # Python actions instantiated with the default toolchain. This ensures there
486 # is only a single copy of each Python action in the build.
487 #
488 # The $target_name.tests group is created separately below.
489 group("$target_name") {
490 deps = [ ":$target_name($default_toolchain)" ]
491 }
492
493 foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) {
494 group("$target_name.$subtarget") {
495 deps = [ ":${invoker.target_name}.$subtarget($default_toolchain)" ]
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800496 }
Wyatt Heplerb8e13602020-10-19 11:20:36 -0700497 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800498
499 # Everything Python-related is only instantiated in the default toolchain.
500 # Silence not-needed warnings except for in the default toolchain.
501 not_needed("*")
502 not_needed(invoker, "*")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700503 }
504
505 # Create a target for each test file.
506 _test_targets = []
507
508 foreach(test, _test_sources) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800509 if (_is_package) {
510 _name = rebase_path(test, _setup_dir)
511 } else {
512 _name = test
513 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700514
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800515 _test_target = "$target_name.tests." + string_replace(_name, "/", "_")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700516
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800517 if (current_toolchain == default_toolchain) {
518 pw_python_action(_test_target) {
519 script = test
520 stamp = true
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800521
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800522 deps = _test_install_deps
523
524 foreach(dep, _python_test_deps) {
525 deps += [ string_replace(dep, "(", ".tests(") ]
526 }
527 }
528 } else {
529 # Create a public version of each test target, so tests can be executed as
530 # //path/to:package.tests.foo.py.
531 group(_test_target) {
532 deps = [ ":$_test_target($default_toolchain)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700533 }
534 }
535
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800536 _test_targets += [ ":$_test_target" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700537 }
538
539 group("$target_name.tests") {
540 deps = _test_targets
541 }
542}
543
544# Declares a group of Python packages or other Python groups. pw_python_groups
545# expose the same set of subtargets as pw_python_package (e.g.
546# "$group_name.lint" and "$group_name.tests"), but these apply to all packages
547# in deps and their dependencies.
548template("pw_python_group") {
549 if (defined(invoker.python_deps)) {
550 _python_deps = invoker.python_deps
551 } else {
552 _python_deps = []
553 }
554
555 group(target_name) {
556 deps = _python_deps
557 }
558
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800559 foreach(subtarget, pw_python_package_subtargets) {
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700560 group("$target_name.$subtarget") {
561 deps = []
562 foreach(dep, _python_deps) {
563 # Split out the toolchain to support deps with a toolchain specified.
564 _target = get_label_info(dep, "label_no_toolchain")
565 _toolchain = get_label_info(dep, "toolchain")
566 deps += [ "$_target.$subtarget($_toolchain)" ]
567 }
568 }
569 }
570}
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700571
572# Declares Python scripts or tests that are not part of a Python package.
573# Similar to pw_python_package, but only supports a subset of its features.
574#
575# pw_python_script accepts the same arguments as pw_python_package, except
576# `setup` cannot be provided.
577#
578# pw_python_script provides the same subtargets as pw_python_package, but
579# $target_name.install and $target_name.wheel only affect the python_deps of
580# this GN target, not the target itself.
581template("pw_python_script") {
582 _supported_variables = [
583 "sources",
584 "tests",
585 "python_deps",
586 "other_deps",
587 "inputs",
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800588 "pylintrc",
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700589 ]
590
591 pw_python_package(target_name) {
592 _pw_standalone = true
593 forward_variables_from(invoker, _supported_variables)
594 }
595}
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800596
597# Represents a list of Python requirements, as in a requirements.txt.
598#
599# Args:
600# files: One or more requirements.txt files.
601# requirements: A list of requirements.txt-style requirements.
602template("pw_python_requirements") {
603 assert(defined(invoker.files) || defined(invoker.requirements),
604 "pw_python_requirements requires a list of requirements.txt files " +
605 "in the 'files' arg or requirements in 'requirements'")
606
607 _requirements_files = []
608
609 if (defined(invoker.files)) {
610 _requirements_files += invoker.files
611 }
612
613 if (defined(invoker.requirements)) {
614 _requirements_file = "$target_gen_dir/$target_name.requirements.txt"
615 write_file(_requirements_file, invoker.requirements)
616 _requirements_files += [ _requirements_file ]
617 }
618
619 # The default target represents the requirements themselves.
620 pw_input_group(target_name) {
621 inputs = _requirements_files
622 }
623
624 # Use the same subtargets as pw_python_package so these targets can be listed
625 # as python_deps of pw_python_packages.
626 pw_python_action("$target_name.install") {
627 inputs = _requirements_files
628
629 module = "pip"
630 args = [ "install" ]
631
632 foreach(_requirements_file, inputs) {
633 args += [
634 "--requirement",
635 rebase_path(_requirements_file),
636 ]
637 }
638
639 pool = "$dir_pw_build:pip_pool"
640 stamp = true
641 }
642
643 # Create stubs for the unused subtargets so that pw_python_requirements can be
644 # used as python_deps.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800645 foreach(subtarget, pw_python_package_subtargets - [ "install" ]) {
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800646 group("$target_name.$subtarget") {
647 }
648 }
649}