blob: b7bf71ac07988db13886747d06aac91421edb424 [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 Heplerf6f74f42021-04-13 19:26:20 -070020import("$dir_pw_protobuf_compiler/toolchain.gni")
21
22declare_args() {
23 # Python tasks, such as running tests and Pylint, are done in a single GN
24 # toolchain to avoid unnecessary duplication in the build.
25 pw_build_PYTHON_TOOLCHAIN = "$dir_pw_build/python_toolchain:python"
26}
Wyatt Heplerb16dfd32020-10-12 08:46:38 -070027
Wyatt Hepler438caa02021-01-15 17:13:11 -080028# Python packages provide the following targets as $target_name.$subtarget.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080029pw_python_package_subtargets = [
Wyatt Hepler438caa02021-01-15 17:13:11 -080030 "tests",
31 "lint",
32 "lint.mypy",
33 "lint.pylint",
34 "install",
35 "wheel",
Wyatt Hepler446f24c2021-03-24 09:29:35 -070036
37 # Internal targets that directly depend on one another.
38 "_run_pip_install",
39 "_build_wheel",
Wyatt Hepler438caa02021-01-15 17:13:11 -080040]
41
Wyatt Heplerb85cda42021-04-07 13:13:15 -070042# Create aliases for subsargets when the target name matches the directory name.
43# This allows //foo:foo.tests to be accessed as //foo:tests, for example.
44template("_pw_create_aliases_if_name_matches_directory") {
45 not_needed([ "invoker" ])
46
47 if (get_label_info(":$target_name", "name") ==
48 get_path_info(get_label_info(":$target_name", "dir"), "name")) {
49 foreach(subtarget, pw_python_package_subtargets) {
50 group(subtarget) {
51 public_deps = [ ":${invoker.target_name}.$subtarget" ]
52 }
53 }
54 }
55}
56
Wyatt Heplerc2ce5242021-03-17 08:42:14 -070057# Internal template that runs Mypy.
58template("_pw_python_static_analysis_mypy") {
59 pw_python_action(target_name) {
60 module = "mypy"
61 args = [
62 "--pretty",
63 "--show-error-codes",
64 ]
65
66 if (defined(invoker.mypy_ini)) {
67 args += [ "--config-file=" + rebase_path(invoker.mypy_ini) ]
68 inputs = [ invoker.mypy_ini ]
69 }
70
71 args += rebase_path(invoker.sources)
72
73 # Use this environment variable to force mypy to colorize output.
74 # See https://github.com/python/mypy/issues/7771
75 environment = [ "MYPY_FORCE_COLOR=1" ]
76
77 directory = invoker.directory
78 stamp = true
79
80 deps = invoker.deps
81
82 foreach(dep, invoker.python_deps) {
83 deps += [ string_replace(dep, "(", ".lint.mypy(") ]
84 }
85 }
86}
87
88# Internal template that runs Pylint.
89template("_pw_python_static_analysis_pylint") {
90 # Create a target to run pylint on each of the Python files in this
91 # package and its dependencies.
92 pw_python_action_foreach(target_name) {
93 module = "pylint"
94 args = [
95 rebase_path(".") + "/{{source_target_relative}}",
96 "--jobs=1",
97 "--output-format=colorized",
98 ]
99
100 if (defined(invoker.pylintrc)) {
101 args += [ "--rcfile=" + rebase_path(invoker.pylintrc) ]
102 inputs = [ invoker.pylintrc ]
103 }
104
105 if (host_os == "win") {
106 # Allow CRLF on Windows, in case Git is set to switch line endings.
107 args += [ "--disable=unexpected-line-ending-format" ]
108 }
109
110 sources = invoker.sources
111 directory = invoker.directory
112
113 stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed"
114
115 public_deps = invoker.deps
116
117 foreach(dep, invoker.python_deps) {
118 public_deps += [ string_replace(dep, "(", ".lint.pylint(") ]
119 }
120 }
121}
122
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700123# Defines a Python package. GN Python packages contain several GN targets:
124#
125# - $name - Provides the Python files in the build, but does not take any
126# actions. All subtargets depend on this target.
127# - $name.lint - Runs static analyis tools on the Python code. This is a group
128# of two subtargets:
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800129# - $name.lint.mypy - Runs mypy (if enabled).
130# - $name.lint.pylint - Runs pylint (if enabled).
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700131# - $name.tests - Runs all tests for this package.
Rob Mohrfcf60372020-11-04 11:41:36 -0800132# - $name.install - Installs the package in a venv.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800133# - $name.wheel - Builds a Python wheel for the package.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700134#
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700135# All Python packages are instantiated with in pw_build_PYTHON_TOOLCHAIN,
136# regardless of the current toolchain. This prevents Python-specific work, like
137# running Pylint, from occurring multiple times in a build.
Wyatt Hepler438caa02021-01-15 17:13:11 -0800138#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700139# Args:
140# setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg),
141# which must all be in the same directory.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800142# generate_setup: As an alternative to 'setup', generate setup files with the
143# keywords in this scope. 'name' is required.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700144# sources: Python sources files in the package.
145# tests: Test files for this Python package.
146# python_deps: Dependencies on other pw_python_packages in the GN build.
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800147# python_test_deps: Test-only pw_python_package dependencies.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700148# other_deps: Dependencies on GN targets that are not pw_python_packages.
149# inputs: Other files to track, such as package_data.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800150# proto_library: A pw_proto_library target to embed in this Python package.
151# generate_setup is required in place of setup if proto_library is used.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700152# static_analysis: List of static analysis tools to run; "*" (default) runs
153# all tools. The supported tools are "mypy" and "pylint".
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800154# pylintrc: Optional path to a pylintrc configuration file to use. If not
155# provided, Pylint's default rcfile search is used. Pylint is executed
156# from the package's setup directory, so pylintrc files in that directory
157# will take precedence over others.
158# mypy_ini: Optional path to a mypy configuration file to use. If not
159# provided, mypy's default configuration file search is used. mypy is
160# executed from the package's setup directory, so mypy.ini files in that
161# directory will take precedence over others.
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800162#
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700163template("pw_python_package") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700164 # The Python targets are always instantiated in pw_build_PYTHON_TOOLCHAIN. Use
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800165 # fully qualified labels so that the toolchain is not lost.
166 _other_deps = []
167 if (defined(invoker.other_deps)) {
168 foreach(dep, invoker.other_deps) {
169 _other_deps += [ get_label_info(dep, "label_with_toolchain") ]
170 }
171 }
172
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800173 _python_deps = []
174 if (defined(invoker.python_deps)) {
175 foreach(dep, invoker.python_deps) {
176 _python_deps += [ get_label_info(dep, "label_with_toolchain") ]
177 }
178 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700179
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700180 # pw_python_script uses pw_python_package, but with a limited set of features.
181 # _pw_standalone signals that this target is actually a pw_python_script.
182 _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone)
183
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800184 _generate_package = false
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800185
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800186 # Check the generate_setup and import_protos args to determine if this package
187 # is generated.
188 if (_is_package) {
189 assert(defined(invoker.generate_setup) != defined(invoker.setup),
190 "Either 'setup' or 'generate_setup' (but not both) must provided")
191
192 if (defined(invoker.proto_library)) {
193 assert(invoker.proto_library != "", "'proto_library' cannot be empty")
194 assert(defined(invoker.generate_setup),
195 "Python packages that import protos with 'proto_library' must " +
196 "use 'generate_setup' instead of 'setup'")
197
198 _import_protos = [ invoker.proto_library ]
Wyatt Heplercf184e82021-05-11 18:15:35 -0700199
200 # Depend on the dependencies of the proto library.
201 _proto = get_label_info(invoker.proto_library, "label_no_toolchain")
202 _toolchain = get_label_info(invoker.proto_library, "toolchain")
203 _python_deps += [ "$_proto.python._deps($_toolchain)" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800204 } else if (defined(invoker.generate_setup)) {
205 _import_protos = []
206 }
207
208 if (defined(invoker.generate_setup)) {
209 _generate_package = true
210 _setup_dir = "$target_gen_dir/$target_name.generated_python_package"
211
212 if (defined(invoker.strip_prefix)) {
213 _source_root = invoker.strip_prefix
214 } else {
215 _source_root = "."
216 }
217 } else {
218 # Non-generated packages with sources provided need an __init__.py.
219 assert(!defined(invoker.sources) || invoker.sources == [] ||
220 filter_include(invoker.sources, [ "*\b__init__.py" ]) != [],
221 "Python packages must have at least one __init__.py file")
222
223 # Get the directories of the setup files. All must be in the same dir.
224 _setup_dirs = get_path_info(invoker.setup, "dir")
225 _setup_dir = _setup_dirs[0]
226
227 foreach(dir, _setup_dirs) {
228 assert(dir == _setup_dir,
229 "All files in 'setup' must be in the same directory")
230 }
231
232 assert(!defined(invoker.strip_prefix),
233 "'strip_prefix' may only be given if 'generate_setup' is provided")
234 }
235 }
236
237 # Process arguments defaults and set defaults.
238
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700239 _supported_static_analysis_tools = [
240 "mypy",
241 "pylint",
242 ]
243 not_needed([ "_supported_static_analysis_tools" ])
244
245 # Argument: static_analysis (list of tool names or "*"); default = "*" (all)
246 if (!defined(invoker.static_analysis) || invoker.static_analysis == "*") {
247 _static_analysis = _supported_static_analysis_tools
Keir Mierlee65ddc82020-12-02 17:59:52 -0800248 } else {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700249 _static_analysis = invoker.static_analysis
250 }
251
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700252 foreach(_tool, _static_analysis) {
253 assert(_supported_static_analysis_tools + [ _tool ] - [ _tool ] !=
254 _supported_static_analysis_tools,
255 "'$_tool' is not a supported static analysis tool")
Keir Mierlee65ddc82020-12-02 17:59:52 -0800256 }
257
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800258 # Argument: sources (list)
259 _sources = []
260 if (defined(invoker.sources)) {
261 if (_generate_package) {
262 foreach(source, rebase_path(invoker.sources, _source_root)) {
263 _sources += [ "$_setup_dir/$source" ]
264 }
265 } else {
266 _sources += invoker.sources
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700267 }
268 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700269
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800270 # Argument: tests (list)
271 _test_sources = []
272 if (defined(invoker.tests)) {
273 if (_generate_package) {
274 foreach(source, rebase_path(invoker.tests, _source_root)) {
275 _test_sources += [ "$_setup_dir/$source" ]
276 }
277 } else {
278 _test_sources += invoker.tests
Wyatt Heplerd7dc6552020-10-21 16:16:21 -0700279 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700280 }
281
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800282 # Argument: setup (list)
283 _setup_sources = []
284 if (defined(invoker.setup)) {
285 _setup_sources = invoker.setup
286 } else if (_generate_package) {
287 _setup_sources = [ "$_setup_dir/setup.py" ]
288 }
289
290 # Argument: python_test_deps (list)
291 _python_test_deps = _python_deps # include all deps in test deps
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800292 if (defined(invoker.python_test_deps)) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800293 foreach(dep, invoker.python_test_deps) {
294 _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ]
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800295 }
296 }
297
298 if (_test_sources == []) {
299 assert(!defined(invoker.python_test_deps),
300 "python_test_deps was provided, but there are no tests in " +
301 get_label_info(":$target_name", "label_no_toolchain"))
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800302 not_needed([ "_python_test_deps" ])
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800303 }
304
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800305 _all_py_files = _sources + _test_sources + _setup_sources
Wyatt Hepler438caa02021-01-15 17:13:11 -0800306
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700307 # The pw_python_package subtargets are only instantiated in
308 # pw_build_PYTHON_TOOLCHAIN. Targets in other toolchains just refer to the
309 # targets in this toolchain.
310 if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800311 # Declare the main Python package group. This represents the Python files,
312 # but does not take any actions. GN targets can depend on the package name
313 # to run when any files in the package change.
314 if (_generate_package) {
315 # If this package is generated, mirror the sources to the final directory.
316 pw_mirror_tree("$target_name._mirror_sources_to_out_dir") {
317 directory = _setup_dir
Wyatt Hepler438caa02021-01-15 17:13:11 -0800318
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800319 sources = []
320 if (defined(invoker.sources)) {
321 sources += invoker.sources
322 }
323 if (defined(invoker.tests)) {
324 sources += invoker.tests
325 }
Wyatt Heplerde20d742021-06-02 23:34:14 -0700326 if (defined(invoker.inputs)) {
327 sources += invoker.inputs
328 }
Wyatt Hepler438caa02021-01-15 17:13:11 -0800329
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800330 source_root = _source_root
331 public_deps = _python_deps + _other_deps
Wyatt Hepler285636f2020-12-15 13:13:11 -0800332 }
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"),
Wyatt Heplercf184e82021-05-11 18:15:35 -0700348 "--generated-root",
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800349 rebase_path(_setup_dir),
350 "--setup-json",
351 rebase_path("$_setup_dir/setup.json"),
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800352 ] + rebase_path(_sources)
353
Wyatt Heplercf184e82021-05-11 18:15:35 -0700354 # Pass in the .json information files for the imported proto libraries.
355 foreach(proto, _import_protos) {
356 _label = get_label_info(proto, "label_no_toolchain") +
357 ".python($pw_protobuf_compiler_TOOLCHAIN)"
358 _file = get_label_info(_label, "target_gen_dir") + "/" +
359 get_label_info(_label, "name") + ".json"
360 args += [
361 "--proto-library",
362 rebase_path(_file),
363 ]
364 }
365
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800366 if (defined(invoker._pw_module_as_package) &&
367 invoker._pw_module_as_package) {
368 args += [ "--module-as-package" ]
369 }
370
Wyatt Hepler0e1f5e42021-03-16 22:51:57 -0700371 inputs = [ "$_setup_dir/setup.json" ]
372
Wyatt Heplercf184e82021-05-11 18:15:35 -0700373 public_deps = [ ":$target_name._mirror_sources_to_out_dir" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800374
375 outputs = _setup_sources
376 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800377 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800378 # If the package is not generated, use an input group for the sources.
379 pw_input_group(target_name) {
380 inputs = _all_py_files
381 if (defined(invoker.inputs)) {
382 inputs += invoker.inputs
383 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800384
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800385 deps = _python_deps + _other_deps
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800386 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700387 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700388
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800389 if (_is_package) {
390 # Install this Python package and its dependencies in the current Python
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700391 # environment using pip.
392 pw_python_action("$target_name._run_pip_install") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800393 module = "pip"
394 public_deps = []
395
396 args = [ "install" ]
397
398 # For generated packages, reinstall when any files change. For regular
399 # packages, only reinstall when setup.py changes.
400 if (_generate_package) {
401 public_deps += [ ":${invoker.target_name}" ]
402 } else {
403 inputs = invoker.setup
404
405 # Install with --editable since the complete package is in source.
406 args += [ "--editable" ]
407 }
408
409 args += [ rebase_path(_setup_dir) ]
410
411 stamp = true
412
413 # Parallel pip installations don't work, so serialize pip invocations.
414 pool = "$dir_pw_build:pip_pool"
415
416 foreach(dep, _python_deps) {
417 # We need to add a suffix to the target name, but the label is
418 # formatted as "//path/to:target(toolchain)", so we can't just append
419 # ".subtarget". Instead, we replace the opening parenthesis of the
420 # toolchain with ".suffix(".
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700421 public_deps += [ string_replace(dep, "(", "._run_pip_install(") ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800422 }
423 }
424
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800425 # Builds a Python wheel for this package. Records the output directory
426 # in the pw_python_package_wheels metadata key.
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700427 pw_python_action("$target_name._build_wheel") {
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800428 metadata = {
429 pw_python_package_wheels = [ "$target_out_dir/$target_name" ]
430 }
431
432 module = "build"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800433
434 args = [
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800435 rebase_path(_setup_dir),
436 "--wheel",
437 "--no-isolation",
438 "--outdir",
439 ] + rebase_path(metadata.pw_python_package_wheels)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800440
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800441 deps = [ ":${invoker.target_name}" ]
442 foreach(dep, _python_deps) {
443 deps += [ string_replace(dep, "(", ".wheel(") ]
444 }
445
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800446 stamp = true
447 }
448 } else {
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700449 # Stubs for non-package targets.
450 group("$target_name._run_pip_install") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800451 }
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700452 group("$target_name._build_wheel") {
453 }
454 }
455
456 # Create the .install and .wheel targets. To limit unnecessary pip
457 # executions, non-generated packages are only reinstalled when their
458 # setup.py changes. However, targets that depend on the .install subtarget
459 # re-run whenever any source files change.
460 #
461 # These targets just represent the source files if this isn't a package.
462 group("$target_name.install") {
463 public_deps = [ ":${invoker.target_name}" ]
464
465 if (_is_package) {
466 public_deps += [ ":${invoker.target_name}._run_pip_install" ]
467 }
468
469 foreach(dep, _python_deps) {
470 public_deps += [ string_replace(dep, "(", ".install(") ]
471 }
472 }
473
474 group("$target_name.wheel") {
475 public_deps = [ ":${invoker.target_name}.install" ]
476
477 if (_is_package) {
478 public_deps += [ ":${invoker.target_name}._build_wheel" ]
479 }
480
481 foreach(dep, _python_deps) {
482 public_deps += [ string_replace(dep, "(", ".wheel(") ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800483 }
484 }
485
486 # Define the static analysis targets for this package.
487 group("$target_name.lint") {
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700488 deps = []
489 foreach(_tool, _supported_static_analysis_tools) {
490 deps += [ ":${invoker.target_name}.lint.$_tool" ]
491 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800492 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700493
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700494 if (_static_analysis != [] || _test_sources != []) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800495 # All packages to install for either general use or test running.
496 _test_install_deps = [ ":$target_name.install" ]
497 foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) {
498 _test_install_deps += [ string_replace(dep, "(", ".install(") ]
499 }
500 }
501
502 # For packages that are not generated, create targets to run mypy and pylint.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700503 foreach(_tool, _static_analysis) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800504 # Run lint tools from the setup or target directory so that the tools detect
505 # config files (e.g. pylintrc or mypy.ini) in that directory. Config files
506 # may be explicitly specified with the pylintrc or mypy_ini arguments.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700507 target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800508 sources = _all_py_files
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700509 deps = _test_install_deps
510 python_deps = _python_deps
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800511
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700512 if (defined(_setup_dir)) {
513 directory = rebase_path(_setup_dir)
514 } else {
515 directory = rebase_path(".")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800516 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700517
518 _optional_variables = [
519 "mypy_ini",
520 "pylintrc",
521 ]
522 forward_variables_from(invoker, _optional_variables)
523 not_needed(_optional_variables)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800524 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700525 }
526
527 foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) {
528 pw_input_group("$target_name.lint.$_unused_tool") {
529 inputs = []
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800530 if (defined(invoker.pylintrc)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700531 inputs += [ invoker.pylintrc ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800532 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800533 if (defined(invoker.mypy_ini)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700534 inputs += [ invoker.mypy_ini ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800535 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800536 }
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800537
538 # Generated packages with linting disabled never need the whole file list.
539 not_needed([ "_all_py_files" ])
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700540 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800541 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800542 # Create groups with the public target names ($target_name, $target_name.lint,
543 # $target_name.install, etc.). These are actually wrappers around internal
544 # Python actions instantiated with the default toolchain. This ensures there
545 # is only a single copy of each Python action in the build.
546 #
547 # The $target_name.tests group is created separately below.
548 group("$target_name") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700549 deps = [ ":$target_name($pw_build_PYTHON_TOOLCHAIN)" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800550 }
551
552 foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) {
553 group("$target_name.$subtarget") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700554 deps =
555 [ ":${invoker.target_name}.$subtarget($pw_build_PYTHON_TOOLCHAIN)" ]
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800556 }
Wyatt Heplerb8e13602020-10-19 11:20:36 -0700557 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800558
559 # Everything Python-related is only instantiated in the default toolchain.
560 # Silence not-needed warnings except for in the default toolchain.
561 not_needed("*")
562 not_needed(invoker, "*")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700563 }
564
565 # Create a target for each test file.
566 _test_targets = []
567
568 foreach(test, _test_sources) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800569 if (_is_package) {
570 _name = rebase_path(test, _setup_dir)
571 } else {
572 _name = test
573 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700574
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800575 _test_target = "$target_name.tests." + string_replace(_name, "/", "_")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700576
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700577 if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800578 pw_python_action(_test_target) {
579 script = test
580 stamp = true
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800581
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800582 deps = _test_install_deps
583
584 foreach(dep, _python_test_deps) {
585 deps += [ string_replace(dep, "(", ".tests(") ]
586 }
587 }
588 } else {
589 # Create a public version of each test target, so tests can be executed as
590 # //path/to:package.tests.foo.py.
591 group(_test_target) {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700592 deps = [ ":$_test_target($pw_build_PYTHON_TOOLCHAIN)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700593 }
594 }
595
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800596 _test_targets += [ ":$_test_target" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700597 }
598
599 group("$target_name.tests") {
600 deps = _test_targets
601 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700602
603 _pw_create_aliases_if_name_matches_directory(target_name) {
604 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700605}
606
607# Declares a group of Python packages or other Python groups. pw_python_groups
608# expose the same set of subtargets as pw_python_package (e.g.
609# "$group_name.lint" and "$group_name.tests"), but these apply to all packages
610# in deps and their dependencies.
611template("pw_python_group") {
612 if (defined(invoker.python_deps)) {
613 _python_deps = invoker.python_deps
614 } else {
615 _python_deps = []
Wyatt Heplera3ca62a2021-05-04 16:21:43 -0700616 not_needed([ "invoker" ]) # Allow empty groups.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700617 }
618
619 group(target_name) {
620 deps = _python_deps
Wyatt Heplercf184e82021-05-11 18:15:35 -0700621
622 if (defined(invoker.other_deps)) {
623 deps += invoker.other_deps
624 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700625 }
626
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800627 foreach(subtarget, pw_python_package_subtargets) {
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700628 group("$target_name.$subtarget") {
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700629 public_deps = []
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700630 foreach(dep, _python_deps) {
631 # Split out the toolchain to support deps with a toolchain specified.
632 _target = get_label_info(dep, "label_no_toolchain")
633 _toolchain = get_label_info(dep, "toolchain")
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700634 public_deps += [ "$_target.$subtarget($_toolchain)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700635 }
636 }
637 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700638
639 _pw_create_aliases_if_name_matches_directory(target_name) {
640 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700641}
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700642
643# Declares Python scripts or tests that are not part of a Python package.
644# Similar to pw_python_package, but only supports a subset of its features.
645#
646# pw_python_script accepts the same arguments as pw_python_package, except
647# `setup` cannot be provided.
648#
649# pw_python_script provides the same subtargets as pw_python_package, but
650# $target_name.install and $target_name.wheel only affect the python_deps of
651# this GN target, not the target itself.
Wyatt Hepler473efe02021-05-04 14:15:16 -0700652#
653# pw_python_script allows creating a pw_python_action associated with the
654# script. This is provided by passing an 'action' scope to pw_python_script.
655# This functions like a normal action, with a few additions: the action uses the
656# pw_python_script's python_deps and defaults to using the source file as its
657# 'script' argument, if there is only a single source file.
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700658template("pw_python_script") {
Wyatt Hepler473efe02021-05-04 14:15:16 -0700659 _package_variables = [
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700660 "sources",
661 "tests",
662 "python_deps",
663 "other_deps",
664 "inputs",
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800665 "pylintrc",
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700666 "mypy_ini",
667 "static_analysis",
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700668 ]
669
670 pw_python_package(target_name) {
671 _pw_standalone = true
Wyatt Hepler473efe02021-05-04 14:15:16 -0700672 forward_variables_from(invoker, _package_variables)
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700673 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700674
675 _pw_create_aliases_if_name_matches_directory(target_name) {
676 }
Wyatt Hepler473efe02021-05-04 14:15:16 -0700677
678 if (defined(invoker.action)) {
679 pw_python_action("$target_name.action") {
680 forward_variables_from(invoker.action, "*", [ "python_deps" ])
681 python_deps = [ ":${invoker.target_name}" ]
682
683 if (!defined(script) && !defined(module) && defined(invoker.sources)) {
684 _sources = invoker.sources
685 assert(_sources != [] && _sources == [ _sources[0] ],
686 "'script' must be specified unless there is only one source " +
687 "in 'sources'")
688 script = _sources[0]
689 }
690 }
691 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700692}
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800693
694# Represents a list of Python requirements, as in a requirements.txt.
695#
696# Args:
697# files: One or more requirements.txt files.
698# requirements: A list of requirements.txt-style requirements.
699template("pw_python_requirements") {
700 assert(defined(invoker.files) || defined(invoker.requirements),
701 "pw_python_requirements requires a list of requirements.txt files " +
702 "in the 'files' arg or requirements in 'requirements'")
703
704 _requirements_files = []
705
706 if (defined(invoker.files)) {
707 _requirements_files += invoker.files
708 }
709
710 if (defined(invoker.requirements)) {
711 _requirements_file = "$target_gen_dir/$target_name.requirements.txt"
712 write_file(_requirements_file, invoker.requirements)
713 _requirements_files += [ _requirements_file ]
714 }
715
716 # The default target represents the requirements themselves.
717 pw_input_group(target_name) {
718 inputs = _requirements_files
719 }
720
721 # Use the same subtargets as pw_python_package so these targets can be listed
722 # as python_deps of pw_python_packages.
723 pw_python_action("$target_name.install") {
724 inputs = _requirements_files
725
726 module = "pip"
727 args = [ "install" ]
728
729 foreach(_requirements_file, inputs) {
730 args += [
731 "--requirement",
732 rebase_path(_requirements_file),
733 ]
734 }
735
736 pool = "$dir_pw_build:pip_pool"
737 stamp = true
738 }
739
740 # Create stubs for the unused subtargets so that pw_python_requirements can be
741 # used as python_deps.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800742 foreach(subtarget, pw_python_package_subtargets - [ "install" ]) {
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800743 group("$target_name.$subtarget") {
744 }
745 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700746
747 _pw_create_aliases_if_name_matches_directory(target_name) {
748 }
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800749}