blob: 550f5921495421578eaf6ff16f951bae67686ce4 [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",
Wyatt Hepler446f24c2021-03-24 09:29:35 -070029
30 # Internal targets that directly depend on one another.
31 "_run_pip_install",
32 "_build_wheel",
Wyatt Hepler438caa02021-01-15 17:13:11 -080033]
34
Wyatt Heplerc2ce5242021-03-17 08:42:14 -070035# Internal template that runs Mypy.
36template("_pw_python_static_analysis_mypy") {
37 pw_python_action(target_name) {
38 module = "mypy"
39 args = [
40 "--pretty",
41 "--show-error-codes",
42 ]
43
44 if (defined(invoker.mypy_ini)) {
45 args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ]
46 inputs = [ invoker.mypy_ini ]
47 }
48
49 args += rebase_path(invoker.sources)
50
51 # Use this environment variable to force mypy to colorize output.
52 # See https://github.com/python/mypy/issues/7771
53 environment = [ "MYPY_FORCE_COLOR=1" ]
54
55 directory = invoker.directory
56 stamp = true
57
58 deps = invoker.deps
59
60 foreach(dep, invoker.python_deps) {
61 deps += [ string_replace(dep, "(", ".lint.mypy(") ]
62 }
63 }
64}
65
66# Internal template that runs Pylint.
67template("_pw_python_static_analysis_pylint") {
68 # Create a target to run pylint on each of the Python files in this
69 # package and its dependencies.
70 pw_python_action_foreach(target_name) {
71 module = "pylint"
72 args = [
73 rebase_path(".") + "/{{source_target_relative}}",
74 "--jobs=1",
75 "--output-format=colorized",
76 ]
77
78 if (defined(invoker.pylintrc)) {
79 args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ]
80 inputs = [ invoker.pylintrc ]
81 }
82
83 if (host_os == "win") {
84 # Allow CRLF on Windows, in case Git is set to switch line endings.
85 args += [ "--disable=unexpected-line-ending-format" ]
86 }
87
88 sources = invoker.sources
89 directory = invoker.directory
90
91 stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed"
92
93 public_deps = invoker.deps
94
95 foreach(dep, invoker.python_deps) {
96 public_deps += [ string_replace(dep, "(", ".lint.pylint(") ]
97 }
98 }
99}
100
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700101# Defines a Python package. GN Python packages contain several GN targets:
102#
103# - $name - Provides the Python files in the build, but does not take any
104# actions. All subtargets depend on this target.
105# - $name.lint - Runs static analyis tools on the Python code. This is a group
106# of two subtargets:
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800107# - $name.lint.mypy - Runs mypy (if enabled).
108# - $name.lint.pylint - Runs pylint (if enabled).
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700109# - $name.tests - Runs all tests for this package.
Rob Mohrfcf60372020-11-04 11:41:36 -0800110# - $name.install - Installs the package in a venv.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800111# - $name.wheel - Builds a Python wheel for the package.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700112#
Wyatt Hepler438caa02021-01-15 17:13:11 -0800113# All Python packages are instantiated with the default toolchain, regardless of
114# the current toolchain.
115#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700116# Args:
117# setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg),
118# which must all be in the same directory.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800119# generate_setup: As an alternative to 'setup', generate setup files with the
120# keywords in this scope. 'name' is required.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700121# sources: Python sources files in the package.
122# tests: Test files for this Python package.
123# python_deps: Dependencies on other pw_python_packages in the GN build.
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800124# python_test_deps: Test-only pw_python_package dependencies.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700125# other_deps: Dependencies on GN targets that are not pw_python_packages.
126# inputs: Other files to track, such as package_data.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800127# proto_library: A pw_proto_library target to embed in this Python package.
128# generate_setup is required in place of setup if proto_library is used.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700129# static_analysis: List of static analysis tools to run; "*" (default) runs
130# all tools. The supported tools are "mypy" and "pylint".
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800131# pylintrc: Optional path to a pylintrc configuration file to use. If not
132# provided, Pylint's default rcfile search is used. Pylint is executed
133# from the package's setup directory, so pylintrc files in that directory
134# will take precedence over others.
135# mypy_ini: Optional path to a mypy configuration file to use. If not
136# provided, mypy's default configuration file search is used. mypy is
137# executed from the package's setup directory, so mypy.ini files in that
138# directory will take precedence over others.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800139#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700140template("pw_python_package") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800141 # The Python targets are always instantiated in the default toolchain. Use
142 # fully qualified labels so that the toolchain is not lost.
143 _other_deps = []
144 if (defined(invoker.other_deps)) {
145 foreach(dep, invoker.other_deps) {
146 _other_deps += [ get_label_info(dep, "label_with_toolchain") ]
147 }
148 }
149
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800150 _python_deps = []
151 if (defined(invoker.python_deps)) {
152 foreach(dep, invoker.python_deps) {
153 _python_deps += [ get_label_info(dep, "label_with_toolchain") ]
154 }
155 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700156
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700157 # pw_python_script uses pw_python_package, but with a limited set of features.
158 # _pw_standalone signals that this target is actually a pw_python_script.
159 _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone)
160
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800161 _generate_package = false
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800162
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800163 # Check the generate_setup and import_protos args to determine if this package
164 # is generated.
165 if (_is_package) {
166 assert(defined(invoker.generate_setup) != defined(invoker.setup),
167 "Either 'setup' or 'generate_setup' (but not both) must provided")
168
169 if (defined(invoker.proto_library)) {
170 assert(invoker.proto_library != "", "'proto_library' cannot be empty")
171 assert(defined(invoker.generate_setup),
172 "Python packages that import protos with 'proto_library' must " +
173 "use 'generate_setup' instead of 'setup'")
174
175 _import_protos = [ invoker.proto_library ]
176 } else if (defined(invoker.generate_setup)) {
177 _import_protos = []
178 }
179
180 if (defined(invoker.generate_setup)) {
181 _generate_package = true
182 _setup_dir = "$target_gen_dir/$target_name.generated_python_package"
183
184 if (defined(invoker.strip_prefix)) {
185 _source_root = invoker.strip_prefix
186 } else {
187 _source_root = "."
188 }
189 } else {
190 # Non-generated packages with sources provided need an __init__.py.
191 assert(!defined(invoker.sources) || invoker.sources == [] ||
192 filter_include(invoker.sources, [ "*\b__init__.py" ]) != [],
193 "Python packages must have at least one __init__.py file")
194
195 # Get the directories of the setup files. All must be in the same dir.
196 _setup_dirs = get_path_info(invoker.setup, "dir")
197 _setup_dir = _setup_dirs[0]
198
199 foreach(dir, _setup_dirs) {
200 assert(dir == _setup_dir,
201 "All files in 'setup' must be in the same directory")
202 }
203
204 assert(!defined(invoker.strip_prefix),
205 "'strip_prefix' may only be given if 'generate_setup' is provided")
206 }
207 }
208
209 # Process arguments defaults and set defaults.
210
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700211 _supported_static_analysis_tools = [
212 "mypy",
213 "pylint",
214 ]
215 not_needed([ "_supported_static_analysis_tools" ])
216
217 # Argument: static_analysis (list of tool names or "*"); default = "*" (all)
218 if (!defined(invoker.static_analysis) || invoker.static_analysis == "*") {
219 _static_analysis = _supported_static_analysis_tools
Keir Mierlee65ddc82020-12-02 17:59:52 -0800220 } else {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700221 _static_analysis = invoker.static_analysis
222 }
223
224 # TODO(hepler): Remove support for the lint option.
225 if (defined(invoker.lint)) {
226 assert(!defined(invoker.static_analysis),
227 "'lint' is deprecated; use 'static_analysis' instead")
228
229 # Only allow 'lint = false', for backwards compatibility.
230 assert(invoker.lint == false, "'lint' is deprecated; use 'static_analysis'")
231 print("WARNING:",
232 "The 'lint' option for pw_python_package is deprecated.",
233 "Instead, use 'static_analysis = []' to disable linting.")
234 _static_analysis = []
235 }
236
237 foreach(_tool, _static_analysis) {
238 assert(_supported_static_analysis_tools + [ _tool ] - [ _tool ] !=
239 _supported_static_analysis_tools,
240 "'$_tool' is not a supported static analysis tool")
Keir Mierlee65ddc82020-12-02 17:59:52 -0800241 }
242
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800243 # Argument: sources (list)
244 _sources = []
245 if (defined(invoker.sources)) {
246 if (_generate_package) {
247 foreach(source, rebase_path(invoker.sources, _source_root)) {
248 _sources += [ "$_setup_dir/$source" ]
249 }
250 } else {
251 _sources += invoker.sources
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700252 }
253 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700254
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800255 # Argument: tests (list)
256 _test_sources = []
257 if (defined(invoker.tests)) {
258 if (_generate_package) {
259 foreach(source, rebase_path(invoker.tests, _source_root)) {
260 _test_sources += [ "$_setup_dir/$source" ]
261 }
262 } else {
263 _test_sources += invoker.tests
Wyatt Heplerd7dc6552020-10-21 16:16:21 -0700264 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700265 }
266
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800267 # Argument: setup (list)
268 _setup_sources = []
269 if (defined(invoker.setup)) {
270 _setup_sources = invoker.setup
271 } else if (_generate_package) {
272 _setup_sources = [ "$_setup_dir/setup.py" ]
273 }
274
275 # Argument: python_test_deps (list)
276 _python_test_deps = _python_deps # include all deps in test deps
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800277 if (defined(invoker.python_test_deps)) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800278 foreach(dep, invoker.python_test_deps) {
279 _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ]
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800280 }
281 }
282
283 if (_test_sources == []) {
284 assert(!defined(invoker.python_test_deps),
285 "python_test_deps was provided, but there are no tests in " +
286 get_label_info(":$target_name", "label_no_toolchain"))
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800287 not_needed([ "_python_test_deps" ])
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800288 }
289
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800290 _all_py_files = _sources + _test_sources + _setup_sources
Wyatt Hepler438caa02021-01-15 17:13:11 -0800291
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800292 # The pw_python_package subtargets are only instantiated in the default
293 # toolchain. Other toolchains just refer to targets in the default toolchain.
294 if (current_toolchain == default_toolchain) {
295 # Declare the main Python package group. This represents the Python files,
296 # but does not take any actions. GN targets can depend on the package name
297 # to run when any files in the package change.
298 if (_generate_package) {
299 # If this package is generated, mirror the sources to the final directory.
300 pw_mirror_tree("$target_name._mirror_sources_to_out_dir") {
301 directory = _setup_dir
Wyatt Hepler438caa02021-01-15 17:13:11 -0800302
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800303 sources = []
304 if (defined(invoker.sources)) {
305 sources += invoker.sources
306 }
307 if (defined(invoker.tests)) {
308 sources += invoker.tests
309 }
Wyatt Hepler438caa02021-01-15 17:13:11 -0800310
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800311 source_root = _source_root
312 public_deps = _python_deps + _other_deps
Wyatt Hepler285636f2020-12-15 13:13:11 -0800313 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700314
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800315 # Depend on the proto's _gen targets (from the default toolchain).
316 _gen_protos = []
317 foreach(proto, _import_protos) {
318 _gen_protos +=
319 [ get_label_info(proto, "label_no_toolchain") + ".python._gen" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700320 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700321
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800322 generated_file("$target_name._protos") {
323 deps = _gen_protos
324 data_keys = [ "protoc_outputs" ]
325 outputs = [ "$_setup_dir/protos.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700326 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800327
328 _protos_file = get_target_outputs(":${invoker.target_name}._protos")
329
330 generated_file("$target_name._protos_root") {
331 deps = _gen_protos
332 data_keys = [ "root" ]
333 outputs = [ "$_setup_dir/proto_root.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700334 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700335
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800336 _root_file = get_target_outputs(":${invoker.target_name}._protos_root")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700337
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800338 # Get generated_setup scope and write it to disk ask JSON.
339 _gen_setup = invoker.generate_setup
340 assert(defined(_gen_setup.name), "'name' is required in generate_package")
341 assert(!defined(_gen_setup.packages) && !defined(_gen_setup.package_data),
342 "'packages' and 'package_data' may not be provided " +
343 "in 'generate_package'")
344 write_file("$_setup_dir/setup.json", _gen_setup, "json")
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800345
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800346 # Generate the setup.py, py.typed, and __init__.py files as needed.
347 action(target_name) {
348 script = "$dir_pw_build/py/pw_build/generate_python_package.py"
349 args = [
350 "--label",
351 get_label_info(":$target_name", "label_no_toolchain"),
352 "--root",
353 rebase_path(_setup_dir),
354 "--setup-json",
355 rebase_path("$_setup_dir/setup.json"),
356 "--file-list",
357 rebase_path(_protos_file[0]),
358 "--file-list-root",
359 rebase_path(_root_file[0]),
360 ] + rebase_path(_sources)
361
362 if (defined(invoker._pw_module_as_package) &&
363 invoker._pw_module_as_package) {
364 args += [ "--module-as-package" ]
365 }
366
Wyatt Hepler0e1f5e42021-03-16 22:51:57 -0700367 inputs = [ "$_setup_dir/setup.json" ]
368
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800369 public_deps = [
370 ":$target_name._mirror_sources_to_out_dir",
371 ":$target_name._protos",
372 ":$target_name._protos_root",
373 ]
374
375 foreach(proto, _import_protos) {
376 _tgt = get_label_info(proto, "label_no_toolchain")
377 _path = get_label_info("$_tgt($default_toolchain)", "target_gen_dir")
378 _name = get_label_info(_tgt, "name")
379
380 args += [
381 "--proto-library=$_tgt",
382 "--proto-library-file",
383 rebase_path("$_path/$_name.proto_library/python_package.txt"),
384 ]
385
386 public_deps += [ "$_tgt.python._gen($default_toolchain)" ]
387 }
388
389 outputs = _setup_sources
390 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800391 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800392 # If the package is not generated, use an input group for the sources.
393 pw_input_group(target_name) {
394 inputs = _all_py_files
395 if (defined(invoker.inputs)) {
396 inputs += invoker.inputs
397 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800398
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800399 deps = _python_deps + _other_deps
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800400 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700401 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700402
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800403 if (_is_package) {
404 # Install this Python package and its dependencies in the current Python
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700405 # environment using pip.
406 pw_python_action("$target_name._run_pip_install") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800407 module = "pip"
408 public_deps = []
409
410 args = [ "install" ]
411
412 # For generated packages, reinstall when any files change. For regular
413 # packages, only reinstall when setup.py changes.
414 if (_generate_package) {
415 public_deps += [ ":${invoker.target_name}" ]
416 } else {
417 inputs = invoker.setup
418
419 # Install with --editable since the complete package is in source.
420 args += [ "--editable" ]
421 }
422
423 args += [ rebase_path(_setup_dir) ]
424
425 stamp = true
426
427 # Parallel pip installations don't work, so serialize pip invocations.
428 pool = "$dir_pw_build:pip_pool"
429
430 foreach(dep, _python_deps) {
431 # We need to add a suffix to the target name, but the label is
432 # formatted as "//path/to:target(toolchain)", so we can't just append
433 # ".subtarget". Instead, we replace the opening parenthesis of the
434 # toolchain with ".suffix(".
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700435 public_deps += [ string_replace(dep, "(", "._run_pip_install(") ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800436 }
437 }
438
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800439 # Builds a Python wheel for this package. Records the output directory
440 # in the pw_python_package_wheels metadata key.
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700441 pw_python_action("$target_name._build_wheel") {
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800442 metadata = {
443 pw_python_package_wheels = [ "$target_out_dir/$target_name" ]
444 }
445
446 module = "build"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800447
448 args = [
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800449 rebase_path(_setup_dir),
450 "--wheel",
451 "--no-isolation",
452 "--outdir",
453 ] + rebase_path(metadata.pw_python_package_wheels)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800454
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800455 deps = [ ":${invoker.target_name}" ]
456 foreach(dep, _python_deps) {
457 deps += [ string_replace(dep, "(", ".wheel(") ]
458 }
459
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800460 stamp = true
461 }
462 } else {
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700463 # Stubs for non-package targets.
464 group("$target_name._run_pip_install") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800465 }
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700466 group("$target_name._build_wheel") {
467 }
468 }
469
470 # Create the .install and .wheel targets. To limit unnecessary pip
471 # executions, non-generated packages are only reinstalled when their
472 # setup.py changes. However, targets that depend on the .install subtarget
473 # re-run whenever any source files change.
474 #
475 # These targets just represent the source files if this isn't a package.
476 group("$target_name.install") {
477 public_deps = [ ":${invoker.target_name}" ]
478
479 if (_is_package) {
480 public_deps += [ ":${invoker.target_name}._run_pip_install" ]
481 }
482
483 foreach(dep, _python_deps) {
484 public_deps += [ string_replace(dep, "(", ".install(") ]
485 }
486 }
487
488 group("$target_name.wheel") {
489 public_deps = [ ":${invoker.target_name}.install" ]
490
491 if (_is_package) {
492 public_deps += [ ":${invoker.target_name}._build_wheel" ]
493 }
494
495 foreach(dep, _python_deps) {
496 public_deps += [ string_replace(dep, "(", ".wheel(") ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800497 }
498 }
499
500 # Define the static analysis targets for this package.
501 group("$target_name.lint") {
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700502 deps = []
503 foreach(_tool, _supported_static_analysis_tools) {
504 deps += [ ":${invoker.target_name}.lint.$_tool" ]
505 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800506 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700507
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700508 if (_static_analysis != [] || _test_sources != []) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800509 # All packages to install for either general use or test running.
510 _test_install_deps = [ ":$target_name.install" ]
511 foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) {
512 _test_install_deps += [ string_replace(dep, "(", ".install(") ]
513 }
514 }
515
516 # For packages that are not generated, create targets to run mypy and pylint.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700517 foreach(_tool, _static_analysis) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800518 # Run lint tools from the setup or target directory so that the tools detect
519 # config files (e.g. pylintrc or mypy.ini) in that directory. Config files
520 # may be explicitly specified with the pylintrc or mypy_ini arguments.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700521 target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800522 sources = _all_py_files
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700523 deps = _test_install_deps
524 python_deps = _python_deps
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800525
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700526 if (defined(_setup_dir)) {
527 directory = rebase_path(_setup_dir)
528 } else {
529 directory = rebase_path(".")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800530 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700531
532 _optional_variables = [
533 "mypy_ini",
534 "pylintrc",
535 ]
536 forward_variables_from(invoker, _optional_variables)
537 not_needed(_optional_variables)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800538 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700539 }
540
541 foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) {
542 pw_input_group("$target_name.lint.$_unused_tool") {
543 inputs = []
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800544 if (defined(invoker.pylintrc)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700545 inputs += [ invoker.pylintrc ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800546 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800547 if (defined(invoker.mypy_ini)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700548 inputs += [ invoker.mypy_ini ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800549 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800550 }
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800551
552 # Generated packages with linting disabled never need the whole file list.
553 not_needed([ "_all_py_files" ])
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700554 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800555 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800556 # Create groups with the public target names ($target_name, $target_name.lint,
557 # $target_name.install, etc.). These are actually wrappers around internal
558 # Python actions instantiated with the default toolchain. This ensures there
559 # is only a single copy of each Python action in the build.
560 #
561 # The $target_name.tests group is created separately below.
562 group("$target_name") {
563 deps = [ ":$target_name($default_toolchain)" ]
564 }
565
566 foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) {
567 group("$target_name.$subtarget") {
568 deps = [ ":${invoker.target_name}.$subtarget($default_toolchain)" ]
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800569 }
Wyatt Heplerb8e13602020-10-19 11:20:36 -0700570 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800571
572 # Everything Python-related is only instantiated in the default toolchain.
573 # Silence not-needed warnings except for in the default toolchain.
574 not_needed("*")
575 not_needed(invoker, "*")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700576 }
577
578 # Create a target for each test file.
579 _test_targets = []
580
581 foreach(test, _test_sources) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800582 if (_is_package) {
583 _name = rebase_path(test, _setup_dir)
584 } else {
585 _name = test
586 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700587
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800588 _test_target = "$target_name.tests." + string_replace(_name, "/", "_")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700589
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800590 if (current_toolchain == default_toolchain) {
591 pw_python_action(_test_target) {
592 script = test
593 stamp = true
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800594
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800595 deps = _test_install_deps
596
597 foreach(dep, _python_test_deps) {
598 deps += [ string_replace(dep, "(", ".tests(") ]
599 }
600 }
601 } else {
602 # Create a public version of each test target, so tests can be executed as
603 # //path/to:package.tests.foo.py.
604 group(_test_target) {
605 deps = [ ":$_test_target($default_toolchain)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700606 }
607 }
608
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800609 _test_targets += [ ":$_test_target" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700610 }
611
612 group("$target_name.tests") {
613 deps = _test_targets
614 }
615}
616
617# Declares a group of Python packages or other Python groups. pw_python_groups
618# expose the same set of subtargets as pw_python_package (e.g.
619# "$group_name.lint" and "$group_name.tests"), but these apply to all packages
620# in deps and their dependencies.
621template("pw_python_group") {
622 if (defined(invoker.python_deps)) {
623 _python_deps = invoker.python_deps
624 } else {
625 _python_deps = []
626 }
627
628 group(target_name) {
629 deps = _python_deps
630 }
631
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800632 foreach(subtarget, pw_python_package_subtargets) {
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700633 group("$target_name.$subtarget") {
634 deps = []
635 foreach(dep, _python_deps) {
636 # Split out the toolchain to support deps with a toolchain specified.
637 _target = get_label_info(dep, "label_no_toolchain")
638 _toolchain = get_label_info(dep, "toolchain")
639 deps += [ "$_target.$subtarget($_toolchain)" ]
640 }
641 }
642 }
643}
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700644
645# Declares Python scripts or tests that are not part of a Python package.
646# Similar to pw_python_package, but only supports a subset of its features.
647#
648# pw_python_script accepts the same arguments as pw_python_package, except
649# `setup` cannot be provided.
650#
651# pw_python_script provides the same subtargets as pw_python_package, but
652# $target_name.install and $target_name.wheel only affect the python_deps of
653# this GN target, not the target itself.
654template("pw_python_script") {
655 _supported_variables = [
656 "sources",
657 "tests",
658 "python_deps",
659 "other_deps",
660 "inputs",
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800661 "pylintrc",
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700662 "mypy_ini",
663 "static_analysis",
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700664 ]
665
666 pw_python_package(target_name) {
667 _pw_standalone = true
668 forward_variables_from(invoker, _supported_variables)
669 }
670}
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800671
672# Represents a list of Python requirements, as in a requirements.txt.
673#
674# Args:
675# files: One or more requirements.txt files.
676# requirements: A list of requirements.txt-style requirements.
677template("pw_python_requirements") {
678 assert(defined(invoker.files) || defined(invoker.requirements),
679 "pw_python_requirements requires a list of requirements.txt files " +
680 "in the 'files' arg or requirements in 'requirements'")
681
682 _requirements_files = []
683
684 if (defined(invoker.files)) {
685 _requirements_files += invoker.files
686 }
687
688 if (defined(invoker.requirements)) {
689 _requirements_file = "$target_gen_dir/$target_name.requirements.txt"
690 write_file(_requirements_file, invoker.requirements)
691 _requirements_files += [ _requirements_file ]
692 }
693
694 # The default target represents the requirements themselves.
695 pw_input_group(target_name) {
696 inputs = _requirements_files
697 }
698
699 # Use the same subtargets as pw_python_package so these targets can be listed
700 # as python_deps of pw_python_packages.
701 pw_python_action("$target_name.install") {
702 inputs = _requirements_files
703
704 module = "pip"
705 args = [ "install" ]
706
707 foreach(_requirements_file, inputs) {
708 args += [
709 "--requirement",
710 rebase_path(_requirements_file),
711 ]
712 }
713
714 pool = "$dir_pw_build:pip_pool"
715 stamp = true
716 }
717
718 # Create stubs for the unused subtargets so that pw_python_requirements can be
719 # used as python_deps.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800720 foreach(subtarget, pw_python_package_subtargets - [ "install" ]) {
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800721 group("$target_name.$subtarget") {
722 }
723 }
724}