blob: bdde00f26835b65feece5cc5938d8c1da0a78081 [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 Heplerc2ce5242021-03-17 08:42:14 -070031# Internal template that runs Mypy.
32template("_pw_python_static_analysis_mypy") {
33 pw_python_action(target_name) {
34 module = "mypy"
35 args = [
36 "--pretty",
37 "--show-error-codes",
38 ]
39
40 if (defined(invoker.mypy_ini)) {
41 args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ]
42 inputs = [ invoker.mypy_ini ]
43 }
44
45 args += rebase_path(invoker.sources)
46
47 # Use this environment variable to force mypy to colorize output.
48 # See https://github.com/python/mypy/issues/7771
49 environment = [ "MYPY_FORCE_COLOR=1" ]
50
51 directory = invoker.directory
52 stamp = true
53
54 deps = invoker.deps
55
56 foreach(dep, invoker.python_deps) {
57 deps += [ string_replace(dep, "(", ".lint.mypy(") ]
58 }
59 }
60}
61
62# Internal template that runs Pylint.
63template("_pw_python_static_analysis_pylint") {
64 # Create a target to run pylint on each of the Python files in this
65 # package and its dependencies.
66 pw_python_action_foreach(target_name) {
67 module = "pylint"
68 args = [
69 rebase_path(".") + "/{{source_target_relative}}",
70 "--jobs=1",
71 "--output-format=colorized",
72 ]
73
74 if (defined(invoker.pylintrc)) {
75 args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ]
76 inputs = [ invoker.pylintrc ]
77 }
78
79 if (host_os == "win") {
80 # Allow CRLF on Windows, in case Git is set to switch line endings.
81 args += [ "--disable=unexpected-line-ending-format" ]
82 }
83
84 sources = invoker.sources
85 directory = invoker.directory
86
87 stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed"
88
89 public_deps = invoker.deps
90
91 foreach(dep, invoker.python_deps) {
92 public_deps += [ string_replace(dep, "(", ".lint.pylint(") ]
93 }
94 }
95}
96
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070097# Defines a Python package. GN Python packages contain several GN targets:
98#
99# - $name - Provides the Python files in the build, but does not take any
100# actions. All subtargets depend on this target.
101# - $name.lint - Runs static analyis tools on the Python code. This is a group
102# of two subtargets:
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800103# - $name.lint.mypy - Runs mypy (if enabled).
104# - $name.lint.pylint - Runs pylint (if enabled).
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700105# - $name.tests - Runs all tests for this package.
Rob Mohrfcf60372020-11-04 11:41:36 -0800106# - $name.install - Installs the package in a venv.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800107# - $name.wheel - Builds a Python wheel for the package.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700108#
Wyatt Hepler438caa02021-01-15 17:13:11 -0800109# All Python packages are instantiated with the default toolchain, regardless of
110# the current toolchain.
111#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700112# Args:
113# setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg),
114# which must all be in the same directory.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800115# generate_setup: As an alternative to 'setup', generate setup files with the
116# keywords in this scope. 'name' is required.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700117# sources: Python sources files in the package.
118# tests: Test files for this Python package.
119# python_deps: Dependencies on other pw_python_packages in the GN build.
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800120# python_test_deps: Test-only pw_python_package dependencies.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700121# other_deps: Dependencies on GN targets that are not pw_python_packages.
122# inputs: Other files to track, such as package_data.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800123# proto_library: A pw_proto_library target to embed in this Python package.
124# generate_setup is required in place of setup if proto_library is used.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700125# static_analysis: List of static analysis tools to run; "*" (default) runs
126# all tools. The supported tools are "mypy" and "pylint".
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800127# pylintrc: Optional path to a pylintrc configuration file to use. If not
128# provided, Pylint's default rcfile search is used. Pylint is executed
129# from the package's setup directory, so pylintrc files in that directory
130# will take precedence over others.
131# mypy_ini: Optional path to a mypy configuration file to use. If not
132# provided, mypy's default configuration file search is used. mypy is
133# executed from the package's setup directory, so mypy.ini files in that
134# directory will take precedence over others.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800135#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700136template("pw_python_package") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800137 # The Python targets are always instantiated in the default toolchain. Use
138 # fully qualified labels so that the toolchain is not lost.
139 _other_deps = []
140 if (defined(invoker.other_deps)) {
141 foreach(dep, invoker.other_deps) {
142 _other_deps += [ get_label_info(dep, "label_with_toolchain") ]
143 }
144 }
145
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800146 _python_deps = []
147 if (defined(invoker.python_deps)) {
148 foreach(dep, invoker.python_deps) {
149 _python_deps += [ get_label_info(dep, "label_with_toolchain") ]
150 }
151 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700152
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700153 # pw_python_script uses pw_python_package, but with a limited set of features.
154 # _pw_standalone signals that this target is actually a pw_python_script.
155 _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone)
156
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800157 _generate_package = false
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800158
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800159 # Check the generate_setup and import_protos args to determine if this package
160 # is generated.
161 if (_is_package) {
162 assert(defined(invoker.generate_setup) != defined(invoker.setup),
163 "Either 'setup' or 'generate_setup' (but not both) must provided")
164
165 if (defined(invoker.proto_library)) {
166 assert(invoker.proto_library != "", "'proto_library' cannot be empty")
167 assert(defined(invoker.generate_setup),
168 "Python packages that import protos with 'proto_library' must " +
169 "use 'generate_setup' instead of 'setup'")
170
171 _import_protos = [ invoker.proto_library ]
172 } else if (defined(invoker.generate_setup)) {
173 _import_protos = []
174 }
175
176 if (defined(invoker.generate_setup)) {
177 _generate_package = true
178 _setup_dir = "$target_gen_dir/$target_name.generated_python_package"
179
180 if (defined(invoker.strip_prefix)) {
181 _source_root = invoker.strip_prefix
182 } else {
183 _source_root = "."
184 }
185 } else {
186 # Non-generated packages with sources provided need an __init__.py.
187 assert(!defined(invoker.sources) || invoker.sources == [] ||
188 filter_include(invoker.sources, [ "*\b__init__.py" ]) != [],
189 "Python packages must have at least one __init__.py file")
190
191 # Get the directories of the setup files. All must be in the same dir.
192 _setup_dirs = get_path_info(invoker.setup, "dir")
193 _setup_dir = _setup_dirs[0]
194
195 foreach(dir, _setup_dirs) {
196 assert(dir == _setup_dir,
197 "All files in 'setup' must be in the same directory")
198 }
199
200 assert(!defined(invoker.strip_prefix),
201 "'strip_prefix' may only be given if 'generate_setup' is provided")
202 }
203 }
204
205 # Process arguments defaults and set defaults.
206
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700207 _supported_static_analysis_tools = [
208 "mypy",
209 "pylint",
210 ]
211 not_needed([ "_supported_static_analysis_tools" ])
212
213 # Argument: static_analysis (list of tool names or "*"); default = "*" (all)
214 if (!defined(invoker.static_analysis) || invoker.static_analysis == "*") {
215 _static_analysis = _supported_static_analysis_tools
Keir Mierlee65ddc82020-12-02 17:59:52 -0800216 } else {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700217 _static_analysis = invoker.static_analysis
218 }
219
220 # TODO(hepler): Remove support for the lint option.
221 if (defined(invoker.lint)) {
222 assert(!defined(invoker.static_analysis),
223 "'lint' is deprecated; use 'static_analysis' instead")
224
225 # Only allow 'lint = false', for backwards compatibility.
226 assert(invoker.lint == false, "'lint' is deprecated; use 'static_analysis'")
227 print("WARNING:",
228 "The 'lint' option for pw_python_package is deprecated.",
229 "Instead, use 'static_analysis = []' to disable linting.")
230 _static_analysis = []
231 }
232
233 foreach(_tool, _static_analysis) {
234 assert(_supported_static_analysis_tools + [ _tool ] - [ _tool ] !=
235 _supported_static_analysis_tools,
236 "'$_tool' is not a supported static analysis tool")
Keir Mierlee65ddc82020-12-02 17:59:52 -0800237 }
238
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800239 # Argument: sources (list)
240 _sources = []
241 if (defined(invoker.sources)) {
242 if (_generate_package) {
243 foreach(source, rebase_path(invoker.sources, _source_root)) {
244 _sources += [ "$_setup_dir/$source" ]
245 }
246 } else {
247 _sources += invoker.sources
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700248 }
249 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700250
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800251 # Argument: tests (list)
252 _test_sources = []
253 if (defined(invoker.tests)) {
254 if (_generate_package) {
255 foreach(source, rebase_path(invoker.tests, _source_root)) {
256 _test_sources += [ "$_setup_dir/$source" ]
257 }
258 } else {
259 _test_sources += invoker.tests
Wyatt Heplerd7dc6552020-10-21 16:16:21 -0700260 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700261 }
262
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800263 # Argument: setup (list)
264 _setup_sources = []
265 if (defined(invoker.setup)) {
266 _setup_sources = invoker.setup
267 } else if (_generate_package) {
268 _setup_sources = [ "$_setup_dir/setup.py" ]
269 }
270
271 # Argument: python_test_deps (list)
272 _python_test_deps = _python_deps # include all deps in test deps
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800273 if (defined(invoker.python_test_deps)) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800274 foreach(dep, invoker.python_test_deps) {
275 _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ]
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800276 }
277 }
278
279 if (_test_sources == []) {
280 assert(!defined(invoker.python_test_deps),
281 "python_test_deps was provided, but there are no tests in " +
282 get_label_info(":$target_name", "label_no_toolchain"))
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800283 not_needed([ "_python_test_deps" ])
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800284 }
285
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800286 _all_py_files = _sources + _test_sources + _setup_sources
Wyatt Hepler438caa02021-01-15 17:13:11 -0800287
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800288 # The pw_python_package subtargets are only instantiated in the default
289 # toolchain. Other toolchains just refer to targets in the default toolchain.
290 if (current_toolchain == default_toolchain) {
291 # Declare the main Python package group. This represents the Python files,
292 # but does not take any actions. GN targets can depend on the package name
293 # to run when any files in the package change.
294 if (_generate_package) {
295 # If this package is generated, mirror the sources to the final directory.
296 pw_mirror_tree("$target_name._mirror_sources_to_out_dir") {
297 directory = _setup_dir
Wyatt Hepler438caa02021-01-15 17:13:11 -0800298
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800299 sources = []
300 if (defined(invoker.sources)) {
301 sources += invoker.sources
302 }
303 if (defined(invoker.tests)) {
304 sources += invoker.tests
305 }
Wyatt Hepler438caa02021-01-15 17:13:11 -0800306
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800307 source_root = _source_root
308 public_deps = _python_deps + _other_deps
Wyatt Hepler285636f2020-12-15 13:13:11 -0800309 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700310
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800311 # Depend on the proto's _gen targets (from the default toolchain).
312 _gen_protos = []
313 foreach(proto, _import_protos) {
314 _gen_protos +=
315 [ get_label_info(proto, "label_no_toolchain") + ".python._gen" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700316 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700317
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800318 generated_file("$target_name._protos") {
319 deps = _gen_protos
320 data_keys = [ "protoc_outputs" ]
321 outputs = [ "$_setup_dir/protos.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700322 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800323
324 _protos_file = get_target_outputs(":${invoker.target_name}._protos")
325
326 generated_file("$target_name._protos_root") {
327 deps = _gen_protos
328 data_keys = [ "root" ]
329 outputs = [ "$_setup_dir/proto_root.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700330 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700331
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800332 _root_file = get_target_outputs(":${invoker.target_name}._protos_root")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700333
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800334 # Get generated_setup scope and write it to disk ask JSON.
335 _gen_setup = invoker.generate_setup
336 assert(defined(_gen_setup.name), "'name' is required in generate_package")
337 assert(!defined(_gen_setup.packages) && !defined(_gen_setup.package_data),
338 "'packages' and 'package_data' may not be provided " +
339 "in 'generate_package'")
340 write_file("$_setup_dir/setup.json", _gen_setup, "json")
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800341
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800342 # Generate the setup.py, py.typed, and __init__.py files as needed.
343 action(target_name) {
344 script = "$dir_pw_build/py/pw_build/generate_python_package.py"
345 args = [
346 "--label",
347 get_label_info(":$target_name", "label_no_toolchain"),
348 "--root",
349 rebase_path(_setup_dir),
350 "--setup-json",
351 rebase_path("$_setup_dir/setup.json"),
352 "--file-list",
353 rebase_path(_protos_file[0]),
354 "--file-list-root",
355 rebase_path(_root_file[0]),
356 ] + rebase_path(_sources)
357
358 if (defined(invoker._pw_module_as_package) &&
359 invoker._pw_module_as_package) {
360 args += [ "--module-as-package" ]
361 }
362
Wyatt Hepler0e1f5e42021-03-16 22:51:57 -0700363 inputs = [ "$_setup_dir/setup.json" ]
364
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800365 public_deps = [
366 ":$target_name._mirror_sources_to_out_dir",
367 ":$target_name._protos",
368 ":$target_name._protos_root",
369 ]
370
371 foreach(proto, _import_protos) {
372 _tgt = get_label_info(proto, "label_no_toolchain")
373 _path = get_label_info("$_tgt($default_toolchain)", "target_gen_dir")
374 _name = get_label_info(_tgt, "name")
375
376 args += [
377 "--proto-library=$_tgt",
378 "--proto-library-file",
379 rebase_path("$_path/$_name.proto_library/python_package.txt"),
380 ]
381
382 public_deps += [ "$_tgt.python._gen($default_toolchain)" ]
383 }
384
385 outputs = _setup_sources
386 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800387 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800388 # If the package is not generated, use an input group for the sources.
389 pw_input_group(target_name) {
390 inputs = _all_py_files
391 if (defined(invoker.inputs)) {
392 inputs += invoker.inputs
393 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800394
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800395 deps = _python_deps + _other_deps
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800396 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700397 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700398
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800399 if (_is_package) {
400 # Install this Python package and its dependencies in the current Python
401 # environment.
402 pw_python_action("$target_name.install") {
403 module = "pip"
404 public_deps = []
405
406 args = [ "install" ]
407
408 # For generated packages, reinstall when any files change. For regular
409 # packages, only reinstall when setup.py changes.
410 if (_generate_package) {
411 public_deps += [ ":${invoker.target_name}" ]
412 } else {
413 inputs = invoker.setup
414
415 # Install with --editable since the complete package is in source.
416 args += [ "--editable" ]
417 }
418
419 args += [ rebase_path(_setup_dir) ]
420
421 stamp = true
422
423 # Parallel pip installations don't work, so serialize pip invocations.
424 pool = "$dir_pw_build:pip_pool"
425
426 foreach(dep, _python_deps) {
427 # We need to add a suffix to the target name, but the label is
428 # formatted as "//path/to:target(toolchain)", so we can't just append
429 # ".subtarget". Instead, we replace the opening parenthesis of the
430 # toolchain with ".suffix(".
431 public_deps += [ string_replace(dep, "(", ".install(") ]
432 }
433 }
434
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800435 # Builds a Python wheel for this package. Records the output directory
436 # in the pw_python_package_wheels metadata key.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800437 pw_python_action("$target_name.wheel") {
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800438 metadata = {
439 pw_python_package_wheels = [ "$target_out_dir/$target_name" ]
440 }
441
442 module = "build"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800443
444 args = [
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800445 rebase_path(_setup_dir),
446 "--wheel",
447 "--no-isolation",
448 "--outdir",
449 ] + rebase_path(metadata.pw_python_package_wheels)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800450
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800451 deps = [ ":${invoker.target_name}" ]
452 foreach(dep, _python_deps) {
453 deps += [ string_replace(dep, "(", ".wheel(") ]
454 }
455
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800456 stamp = true
457 }
458 } else {
459 # If this is not a package, install or build wheels for its deps only.
460 group("$target_name.install") {
461 deps = []
462 foreach(dep, _python_deps) {
463 deps += [ string_replace(dep, "(", ".install(") ]
464 }
465 }
466 group("$target_name.wheel") {
467 deps = []
468 foreach(dep, _python_deps) {
469 deps += [ string_replace(dep, "(", ".wheel(") ]
470 }
471 }
472 }
473
474 # Define the static analysis targets for this package.
475 group("$target_name.lint") {
476 deps = [
477 ":${invoker.target_name}.lint.mypy",
478 ":${invoker.target_name}.lint.pylint",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800479 ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800480 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700481
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700482 if (_static_analysis != [] || _test_sources != []) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800483 # All packages to install for either general use or test running.
484 _test_install_deps = [ ":$target_name.install" ]
485 foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) {
486 _test_install_deps += [ string_replace(dep, "(", ".install(") ]
487 }
488 }
489
490 # For packages that are not generated, create targets to run mypy and pylint.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700491 foreach(_tool, _static_analysis) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800492 # Run lint tools from the setup or target directory so that the tools detect
493 # config files (e.g. pylintrc or mypy.ini) in that directory. Config files
494 # may be explicitly specified with the pylintrc or mypy_ini arguments.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700495 target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800496 sources = _all_py_files
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700497 deps = _test_install_deps
498 python_deps = _python_deps
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800499
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700500 if (defined(_setup_dir)) {
501 directory = rebase_path(_setup_dir)
502 } else {
503 directory = rebase_path(".")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800504 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700505
506 _optional_variables = [
507 "mypy_ini",
508 "pylintrc",
509 ]
510 forward_variables_from(invoker, _optional_variables)
511 not_needed(_optional_variables)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800512 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700513 }
514
515 foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) {
516 pw_input_group("$target_name.lint.$_unused_tool") {
517 inputs = []
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800518 if (defined(invoker.pylintrc)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700519 inputs += [ invoker.pylintrc ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800520 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800521 if (defined(invoker.mypy_ini)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700522 inputs += [ invoker.mypy_ini ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800523 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800524 }
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800525
526 # Generated packages with linting disabled never need the whole file list.
527 not_needed([ "_all_py_files" ])
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700528 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800529 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800530 # Create groups with the public target names ($target_name, $target_name.lint,
531 # $target_name.install, etc.). These are actually wrappers around internal
532 # Python actions instantiated with the default toolchain. This ensures there
533 # is only a single copy of each Python action in the build.
534 #
535 # The $target_name.tests group is created separately below.
536 group("$target_name") {
537 deps = [ ":$target_name($default_toolchain)" ]
538 }
539
540 foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) {
541 group("$target_name.$subtarget") {
542 deps = [ ":${invoker.target_name}.$subtarget($default_toolchain)" ]
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800543 }
Wyatt Heplerb8e13602020-10-19 11:20:36 -0700544 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800545
546 # Everything Python-related is only instantiated in the default toolchain.
547 # Silence not-needed warnings except for in the default toolchain.
548 not_needed("*")
549 not_needed(invoker, "*")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700550 }
551
552 # Create a target for each test file.
553 _test_targets = []
554
555 foreach(test, _test_sources) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800556 if (_is_package) {
557 _name = rebase_path(test, _setup_dir)
558 } else {
559 _name = test
560 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700561
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800562 _test_target = "$target_name.tests." + string_replace(_name, "/", "_")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700563
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800564 if (current_toolchain == default_toolchain) {
565 pw_python_action(_test_target) {
566 script = test
567 stamp = true
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800568
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800569 deps = _test_install_deps
570
571 foreach(dep, _python_test_deps) {
572 deps += [ string_replace(dep, "(", ".tests(") ]
573 }
574 }
575 } else {
576 # Create a public version of each test target, so tests can be executed as
577 # //path/to:package.tests.foo.py.
578 group(_test_target) {
579 deps = [ ":$_test_target($default_toolchain)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700580 }
581 }
582
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800583 _test_targets += [ ":$_test_target" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700584 }
585
586 group("$target_name.tests") {
587 deps = _test_targets
588 }
589}
590
591# Declares a group of Python packages or other Python groups. pw_python_groups
592# expose the same set of subtargets as pw_python_package (e.g.
593# "$group_name.lint" and "$group_name.tests"), but these apply to all packages
594# in deps and their dependencies.
595template("pw_python_group") {
596 if (defined(invoker.python_deps)) {
597 _python_deps = invoker.python_deps
598 } else {
599 _python_deps = []
600 }
601
602 group(target_name) {
603 deps = _python_deps
604 }
605
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800606 foreach(subtarget, pw_python_package_subtargets) {
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700607 group("$target_name.$subtarget") {
608 deps = []
609 foreach(dep, _python_deps) {
610 # Split out the toolchain to support deps with a toolchain specified.
611 _target = get_label_info(dep, "label_no_toolchain")
612 _toolchain = get_label_info(dep, "toolchain")
613 deps += [ "$_target.$subtarget($_toolchain)" ]
614 }
615 }
616 }
617}
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700618
619# Declares Python scripts or tests that are not part of a Python package.
620# Similar to pw_python_package, but only supports a subset of its features.
621#
622# pw_python_script accepts the same arguments as pw_python_package, except
623# `setup` cannot be provided.
624#
625# pw_python_script provides the same subtargets as pw_python_package, but
626# $target_name.install and $target_name.wheel only affect the python_deps of
627# this GN target, not the target itself.
628template("pw_python_script") {
629 _supported_variables = [
630 "sources",
631 "tests",
632 "python_deps",
633 "other_deps",
634 "inputs",
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800635 "pylintrc",
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700636 "mypy_ini",
637 "static_analysis",
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700638 ]
639
640 pw_python_package(target_name) {
641 _pw_standalone = true
642 forward_variables_from(invoker, _supported_variables)
643 }
644}
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800645
646# Represents a list of Python requirements, as in a requirements.txt.
647#
648# Args:
649# files: One or more requirements.txt files.
650# requirements: A list of requirements.txt-style requirements.
651template("pw_python_requirements") {
652 assert(defined(invoker.files) || defined(invoker.requirements),
653 "pw_python_requirements requires a list of requirements.txt files " +
654 "in the 'files' arg or requirements in 'requirements'")
655
656 _requirements_files = []
657
658 if (defined(invoker.files)) {
659 _requirements_files += invoker.files
660 }
661
662 if (defined(invoker.requirements)) {
663 _requirements_file = "$target_gen_dir/$target_name.requirements.txt"
664 write_file(_requirements_file, invoker.requirements)
665 _requirements_files += [ _requirements_file ]
666 }
667
668 # The default target represents the requirements themselves.
669 pw_input_group(target_name) {
670 inputs = _requirements_files
671 }
672
673 # Use the same subtargets as pw_python_package so these targets can be listed
674 # as python_deps of pw_python_packages.
675 pw_python_action("$target_name.install") {
676 inputs = _requirements_files
677
678 module = "pip"
679 args = [ "install" ]
680
681 foreach(_requirements_file, inputs) {
682 args += [
683 "--requirement",
684 rebase_path(_requirements_file),
685 ]
686 }
687
688 pool = "$dir_pw_build:pip_pool"
689 stamp = true
690 }
691
692 # Create stubs for the unused subtargets so that pw_python_requirements can be
693 # used as python_deps.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800694 foreach(subtarget, pw_python_package_subtargets - [ "install" ]) {
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800695 group("$target_name.$subtarget") {
696 }
697 }
698}