blob: de56f91472023483003d16556ff98609f4fced18 [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 ]
199 } else if (defined(invoker.generate_setup)) {
200 _import_protos = []
201 }
202
203 if (defined(invoker.generate_setup)) {
204 _generate_package = true
205 _setup_dir = "$target_gen_dir/$target_name.generated_python_package"
206
207 if (defined(invoker.strip_prefix)) {
208 _source_root = invoker.strip_prefix
209 } else {
210 _source_root = "."
211 }
212 } else {
213 # Non-generated packages with sources provided need an __init__.py.
214 assert(!defined(invoker.sources) || invoker.sources == [] ||
215 filter_include(invoker.sources, [ "*\b__init__.py" ]) != [],
216 "Python packages must have at least one __init__.py file")
217
218 # Get the directories of the setup files. All must be in the same dir.
219 _setup_dirs = get_path_info(invoker.setup, "dir")
220 _setup_dir = _setup_dirs[0]
221
222 foreach(dir, _setup_dirs) {
223 assert(dir == _setup_dir,
224 "All files in 'setup' must be in the same directory")
225 }
226
227 assert(!defined(invoker.strip_prefix),
228 "'strip_prefix' may only be given if 'generate_setup' is provided")
229 }
230 }
231
232 # Process arguments defaults and set defaults.
233
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700234 _supported_static_analysis_tools = [
235 "mypy",
236 "pylint",
237 ]
238 not_needed([ "_supported_static_analysis_tools" ])
239
240 # Argument: static_analysis (list of tool names or "*"); default = "*" (all)
241 if (!defined(invoker.static_analysis) || invoker.static_analysis == "*") {
242 _static_analysis = _supported_static_analysis_tools
Keir Mierlee65ddc82020-12-02 17:59:52 -0800243 } else {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700244 _static_analysis = invoker.static_analysis
245 }
246
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700247 foreach(_tool, _static_analysis) {
248 assert(_supported_static_analysis_tools + [ _tool ] - [ _tool ] !=
249 _supported_static_analysis_tools,
250 "'$_tool' is not a supported static analysis tool")
Keir Mierlee65ddc82020-12-02 17:59:52 -0800251 }
252
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800253 # Argument: sources (list)
254 _sources = []
255 if (defined(invoker.sources)) {
256 if (_generate_package) {
257 foreach(source, rebase_path(invoker.sources, _source_root)) {
258 _sources += [ "$_setup_dir/$source" ]
259 }
260 } else {
261 _sources += invoker.sources
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700262 }
263 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700264
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800265 # Argument: tests (list)
266 _test_sources = []
267 if (defined(invoker.tests)) {
268 if (_generate_package) {
269 foreach(source, rebase_path(invoker.tests, _source_root)) {
270 _test_sources += [ "$_setup_dir/$source" ]
271 }
272 } else {
273 _test_sources += invoker.tests
Wyatt Heplerd7dc6552020-10-21 16:16:21 -0700274 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700275 }
276
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800277 # Argument: setup (list)
278 _setup_sources = []
279 if (defined(invoker.setup)) {
280 _setup_sources = invoker.setup
281 } else if (_generate_package) {
282 _setup_sources = [ "$_setup_dir/setup.py" ]
283 }
284
285 # Argument: python_test_deps (list)
286 _python_test_deps = _python_deps # include all deps in test deps
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800287 if (defined(invoker.python_test_deps)) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800288 foreach(dep, invoker.python_test_deps) {
289 _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ]
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800290 }
291 }
292
293 if (_test_sources == []) {
294 assert(!defined(invoker.python_test_deps),
295 "python_test_deps was provided, but there are no tests in " +
296 get_label_info(":$target_name", "label_no_toolchain"))
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800297 not_needed([ "_python_test_deps" ])
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800298 }
299
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800300 _all_py_files = _sources + _test_sources + _setup_sources
Wyatt Hepler438caa02021-01-15 17:13:11 -0800301
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700302 # The pw_python_package subtargets are only instantiated in
303 # pw_build_PYTHON_TOOLCHAIN. Targets in other toolchains just refer to the
304 # targets in this toolchain.
305 if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800306 # Declare the main Python package group. This represents the Python files,
307 # but does not take any actions. GN targets can depend on the package name
308 # to run when any files in the package change.
309 if (_generate_package) {
310 # If this package is generated, mirror the sources to the final directory.
311 pw_mirror_tree("$target_name._mirror_sources_to_out_dir") {
312 directory = _setup_dir
Wyatt Hepler438caa02021-01-15 17:13:11 -0800313
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800314 sources = []
315 if (defined(invoker.sources)) {
316 sources += invoker.sources
317 }
318 if (defined(invoker.tests)) {
319 sources += invoker.tests
320 }
Wyatt Hepler438caa02021-01-15 17:13:11 -0800321
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800322 source_root = _source_root
323 public_deps = _python_deps + _other_deps
Wyatt Hepler285636f2020-12-15 13:13:11 -0800324 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700325
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800326 # Depend on the proto's _gen targets (from the default toolchain).
327 _gen_protos = []
328 foreach(proto, _import_protos) {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700329 _gen_protos += [ get_label_info(proto, "label_no_toolchain") +
330 ".python._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700331 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700332
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800333 generated_file("$target_name._protos") {
334 deps = _gen_protos
335 data_keys = [ "protoc_outputs" ]
336 outputs = [ "$_setup_dir/protos.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700337 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800338
339 _protos_file = get_target_outputs(":${invoker.target_name}._protos")
340
341 generated_file("$target_name._protos_root") {
342 deps = _gen_protos
343 data_keys = [ "root" ]
344 outputs = [ "$_setup_dir/proto_root.txt" ]
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700345 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700346
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800347 _root_file = get_target_outputs(":${invoker.target_name}._protos_root")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700348
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800349 # Get generated_setup scope and write it to disk ask JSON.
350 _gen_setup = invoker.generate_setup
351 assert(defined(_gen_setup.name), "'name' is required in generate_package")
352 assert(!defined(_gen_setup.packages) && !defined(_gen_setup.package_data),
353 "'packages' and 'package_data' may not be provided " +
354 "in 'generate_package'")
355 write_file("$_setup_dir/setup.json", _gen_setup, "json")
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800356
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800357 # Generate the setup.py, py.typed, and __init__.py files as needed.
358 action(target_name) {
359 script = "$dir_pw_build/py/pw_build/generate_python_package.py"
360 args = [
361 "--label",
362 get_label_info(":$target_name", "label_no_toolchain"),
363 "--root",
364 rebase_path(_setup_dir),
365 "--setup-json",
366 rebase_path("$_setup_dir/setup.json"),
367 "--file-list",
368 rebase_path(_protos_file[0]),
369 "--file-list-root",
370 rebase_path(_root_file[0]),
371 ] + rebase_path(_sources)
372
373 if (defined(invoker._pw_module_as_package) &&
374 invoker._pw_module_as_package) {
375 args += [ "--module-as-package" ]
376 }
377
Wyatt Hepler0e1f5e42021-03-16 22:51:57 -0700378 inputs = [ "$_setup_dir/setup.json" ]
379
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800380 public_deps = [
381 ":$target_name._mirror_sources_to_out_dir",
382 ":$target_name._protos",
383 ":$target_name._protos_root",
384 ]
385
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700386 # Each pw_proto_library generates a file that indicates which Python
387 # package it is nested in, if any. Locate those files.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800388 foreach(proto, _import_protos) {
389 _tgt = get_label_info(proto, "label_no_toolchain")
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700390 _path = get_label_info("$_tgt($pw_protobuf_compiler_TOOLCHAIN)",
391 "target_gen_dir")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800392 _name = get_label_info(_tgt, "name")
393
394 args += [
395 "--proto-library=$_tgt",
396 "--proto-library-file",
397 rebase_path("$_path/$_name.proto_library/python_package.txt"),
398 ]
399
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700400 public_deps +=
401 [ "$_tgt.python._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800402 }
403
404 outputs = _setup_sources
405 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800406 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800407 # If the package is not generated, use an input group for the sources.
408 pw_input_group(target_name) {
409 inputs = _all_py_files
410 if (defined(invoker.inputs)) {
411 inputs += invoker.inputs
412 }
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800413
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800414 deps = _python_deps + _other_deps
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800415 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700416 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700417
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800418 if (_is_package) {
419 # Install this Python package and its dependencies in the current Python
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700420 # environment using pip.
421 pw_python_action("$target_name._run_pip_install") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800422 module = "pip"
423 public_deps = []
424
425 args = [ "install" ]
426
427 # For generated packages, reinstall when any files change. For regular
428 # packages, only reinstall when setup.py changes.
429 if (_generate_package) {
430 public_deps += [ ":${invoker.target_name}" ]
431 } else {
432 inputs = invoker.setup
433
434 # Install with --editable since the complete package is in source.
435 args += [ "--editable" ]
436 }
437
438 args += [ rebase_path(_setup_dir) ]
439
440 stamp = true
441
442 # Parallel pip installations don't work, so serialize pip invocations.
443 pool = "$dir_pw_build:pip_pool"
444
445 foreach(dep, _python_deps) {
446 # We need to add a suffix to the target name, but the label is
447 # formatted as "//path/to:target(toolchain)", so we can't just append
448 # ".subtarget". Instead, we replace the opening parenthesis of the
449 # toolchain with ".suffix(".
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700450 public_deps += [ string_replace(dep, "(", "._run_pip_install(") ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800451 }
452 }
453
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800454 # Builds a Python wheel for this package. Records the output directory
455 # in the pw_python_package_wheels metadata key.
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700456 pw_python_action("$target_name._build_wheel") {
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800457 metadata = {
458 pw_python_package_wheels = [ "$target_out_dir/$target_name" ]
459 }
460
461 module = "build"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800462
463 args = [
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800464 rebase_path(_setup_dir),
465 "--wheel",
466 "--no-isolation",
467 "--outdir",
468 ] + rebase_path(metadata.pw_python_package_wheels)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800469
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800470 deps = [ ":${invoker.target_name}" ]
471 foreach(dep, _python_deps) {
472 deps += [ string_replace(dep, "(", ".wheel(") ]
473 }
474
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800475 stamp = true
476 }
477 } else {
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700478 # Stubs for non-package targets.
479 group("$target_name._run_pip_install") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800480 }
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700481 group("$target_name._build_wheel") {
482 }
483 }
484
485 # Create the .install and .wheel targets. To limit unnecessary pip
486 # executions, non-generated packages are only reinstalled when their
487 # setup.py changes. However, targets that depend on the .install subtarget
488 # re-run whenever any source files change.
489 #
490 # These targets just represent the source files if this isn't a package.
491 group("$target_name.install") {
492 public_deps = [ ":${invoker.target_name}" ]
493
494 if (_is_package) {
495 public_deps += [ ":${invoker.target_name}._run_pip_install" ]
496 }
497
498 foreach(dep, _python_deps) {
499 public_deps += [ string_replace(dep, "(", ".install(") ]
500 }
501 }
502
503 group("$target_name.wheel") {
504 public_deps = [ ":${invoker.target_name}.install" ]
505
506 if (_is_package) {
507 public_deps += [ ":${invoker.target_name}._build_wheel" ]
508 }
509
510 foreach(dep, _python_deps) {
511 public_deps += [ string_replace(dep, "(", ".wheel(") ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800512 }
513 }
514
515 # Define the static analysis targets for this package.
516 group("$target_name.lint") {
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700517 deps = []
518 foreach(_tool, _supported_static_analysis_tools) {
519 deps += [ ":${invoker.target_name}.lint.$_tool" ]
520 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800521 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700522
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700523 if (_static_analysis != [] || _test_sources != []) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800524 # All packages to install for either general use or test running.
525 _test_install_deps = [ ":$target_name.install" ]
526 foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) {
527 _test_install_deps += [ string_replace(dep, "(", ".install(") ]
528 }
529 }
530
531 # For packages that are not generated, create targets to run mypy and pylint.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700532 foreach(_tool, _static_analysis) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800533 # Run lint tools from the setup or target directory so that the tools detect
534 # config files (e.g. pylintrc or mypy.ini) in that directory. Config files
535 # may be explicitly specified with the pylintrc or mypy_ini arguments.
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700536 target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800537 sources = _all_py_files
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700538 deps = _test_install_deps
539 python_deps = _python_deps
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800540
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700541 if (defined(_setup_dir)) {
542 directory = rebase_path(_setup_dir)
543 } else {
544 directory = rebase_path(".")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800545 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700546
547 _optional_variables = [
548 "mypy_ini",
549 "pylintrc",
550 ]
551 forward_variables_from(invoker, _optional_variables)
552 not_needed(_optional_variables)
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800553 }
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700554 }
555
556 foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) {
557 pw_input_group("$target_name.lint.$_unused_tool") {
558 inputs = []
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800559 if (defined(invoker.pylintrc)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700560 inputs += [ invoker.pylintrc ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800561 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800562 if (defined(invoker.mypy_ini)) {
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700563 inputs += [ invoker.mypy_ini ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800564 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800565 }
Wyatt Hepleraf853ea2021-03-09 19:09:54 -0800566
567 # Generated packages with linting disabled never need the whole file list.
568 not_needed([ "_all_py_files" ])
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700569 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800570 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800571 # Create groups with the public target names ($target_name, $target_name.lint,
572 # $target_name.install, etc.). These are actually wrappers around internal
573 # Python actions instantiated with the default toolchain. This ensures there
574 # is only a single copy of each Python action in the build.
575 #
576 # The $target_name.tests group is created separately below.
577 group("$target_name") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700578 deps = [ ":$target_name($pw_build_PYTHON_TOOLCHAIN)" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800579 }
580
581 foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) {
582 group("$target_name.$subtarget") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700583 deps =
584 [ ":${invoker.target_name}.$subtarget($pw_build_PYTHON_TOOLCHAIN)" ]
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800585 }
Wyatt Heplerb8e13602020-10-19 11:20:36 -0700586 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800587
588 # Everything Python-related is only instantiated in the default toolchain.
589 # Silence not-needed warnings except for in the default toolchain.
590 not_needed("*")
591 not_needed(invoker, "*")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700592 }
593
594 # Create a target for each test file.
595 _test_targets = []
596
597 foreach(test, _test_sources) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800598 if (_is_package) {
599 _name = rebase_path(test, _setup_dir)
600 } else {
601 _name = test
602 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700603
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800604 _test_target = "$target_name.tests." + string_replace(_name, "/", "_")
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700605
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700606 if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800607 pw_python_action(_test_target) {
608 script = test
609 stamp = true
Wyatt Hepler620b3e02021-01-22 09:14:45 -0800610
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800611 deps = _test_install_deps
612
613 foreach(dep, _python_test_deps) {
614 deps += [ string_replace(dep, "(", ".tests(") ]
615 }
616 }
617 } else {
618 # Create a public version of each test target, so tests can be executed as
619 # //path/to:package.tests.foo.py.
620 group(_test_target) {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700621 deps = [ ":$_test_target($pw_build_PYTHON_TOOLCHAIN)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700622 }
623 }
624
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800625 _test_targets += [ ":$_test_target" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700626 }
627
628 group("$target_name.tests") {
629 deps = _test_targets
630 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700631
632 _pw_create_aliases_if_name_matches_directory(target_name) {
633 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700634}
635
636# Declares a group of Python packages or other Python groups. pw_python_groups
637# expose the same set of subtargets as pw_python_package (e.g.
638# "$group_name.lint" and "$group_name.tests"), but these apply to all packages
639# in deps and their dependencies.
640template("pw_python_group") {
641 if (defined(invoker.python_deps)) {
642 _python_deps = invoker.python_deps
643 } else {
644 _python_deps = []
Wyatt Heplera3ca62a2021-05-04 16:21:43 -0700645 not_needed([ "invoker" ]) # Allow empty groups.
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700646 }
647
648 group(target_name) {
649 deps = _python_deps
650 }
651
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800652 foreach(subtarget, pw_python_package_subtargets) {
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700653 group("$target_name.$subtarget") {
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700654 public_deps = []
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700655 foreach(dep, _python_deps) {
656 # Split out the toolchain to support deps with a toolchain specified.
657 _target = get_label_info(dep, "label_no_toolchain")
658 _toolchain = get_label_info(dep, "toolchain")
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700659 public_deps += [ "$_target.$subtarget($_toolchain)" ]
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700660 }
661 }
662 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700663
664 _pw_create_aliases_if_name_matches_directory(target_name) {
665 }
Wyatt Heplerb16dfd32020-10-12 08:46:38 -0700666}
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700667
668# Declares Python scripts or tests that are not part of a Python package.
669# Similar to pw_python_package, but only supports a subset of its features.
670#
671# pw_python_script accepts the same arguments as pw_python_package, except
672# `setup` cannot be provided.
673#
674# pw_python_script provides the same subtargets as pw_python_package, but
675# $target_name.install and $target_name.wheel only affect the python_deps of
676# this GN target, not the target itself.
Wyatt Hepler473efe02021-05-04 14:15:16 -0700677#
678# pw_python_script allows creating a pw_python_action associated with the
679# script. This is provided by passing an 'action' scope to pw_python_script.
680# This functions like a normal action, with a few additions: the action uses the
681# pw_python_script's python_deps and defaults to using the source file as its
682# 'script' argument, if there is only a single source file.
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700683template("pw_python_script") {
Wyatt Hepler473efe02021-05-04 14:15:16 -0700684 _package_variables = [
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700685 "sources",
686 "tests",
687 "python_deps",
688 "other_deps",
689 "inputs",
Wyatt Hepler8ce90132020-12-03 10:57:20 -0800690 "pylintrc",
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700691 "mypy_ini",
692 "static_analysis",
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700693 ]
694
695 pw_python_package(target_name) {
696 _pw_standalone = true
Wyatt Hepler473efe02021-05-04 14:15:16 -0700697 forward_variables_from(invoker, _package_variables)
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700698 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700699
700 _pw_create_aliases_if_name_matches_directory(target_name) {
701 }
Wyatt Hepler473efe02021-05-04 14:15:16 -0700702
703 if (defined(invoker.action)) {
704 pw_python_action("$target_name.action") {
705 forward_variables_from(invoker.action, "*", [ "python_deps" ])
706 python_deps = [ ":${invoker.target_name}" ]
707
708 if (!defined(script) && !defined(module) && defined(invoker.sources)) {
709 _sources = invoker.sources
710 assert(_sources != [] && _sources == [ _sources[0] ],
711 "'script' must be specified unless there is only one source " +
712 "in 'sources'")
713 script = _sources[0]
714 }
715 }
716 }
Wyatt Hepler45af57b2020-10-23 08:05:28 -0700717}
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800718
719# Represents a list of Python requirements, as in a requirements.txt.
720#
721# Args:
722# files: One or more requirements.txt files.
723# requirements: A list of requirements.txt-style requirements.
724template("pw_python_requirements") {
725 assert(defined(invoker.files) || defined(invoker.requirements),
726 "pw_python_requirements requires a list of requirements.txt files " +
727 "in the 'files' arg or requirements in 'requirements'")
728
729 _requirements_files = []
730
731 if (defined(invoker.files)) {
732 _requirements_files += invoker.files
733 }
734
735 if (defined(invoker.requirements)) {
736 _requirements_file = "$target_gen_dir/$target_name.requirements.txt"
737 write_file(_requirements_file, invoker.requirements)
738 _requirements_files += [ _requirements_file ]
739 }
740
741 # The default target represents the requirements themselves.
742 pw_input_group(target_name) {
743 inputs = _requirements_files
744 }
745
746 # Use the same subtargets as pw_python_package so these targets can be listed
747 # as python_deps of pw_python_packages.
748 pw_python_action("$target_name.install") {
749 inputs = _requirements_files
750
751 module = "pip"
752 args = [ "install" ]
753
754 foreach(_requirements_file, inputs) {
755 args += [
756 "--requirement",
757 rebase_path(_requirements_file),
758 ]
759 }
760
761 pool = "$dir_pw_build:pip_pool"
762 stamp = true
763 }
764
765 # Create stubs for the unused subtargets so that pw_python_requirements can be
766 # used as python_deps.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800767 foreach(subtarget, pw_python_package_subtargets - [ "install" ]) {
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800768 group("$target_name.$subtarget") {
769 }
770 }
Wyatt Heplerb85cda42021-04-07 13:13:15 -0700771
772 _pw_create_aliases_if_name_matches_directory(target_name) {
773 }
Wyatt Hepler4d1e6aa2021-01-14 08:39:42 -0800774}