blob: 7157746deb09ef1e4308f9e2eaeb5e85f87d6ffe [file] [log] [blame]
Wyatt Heplerb3ea9802021-02-23 09:46:09 -08001.. _module-pw_build-python:
2
3-------------------
4Python GN templates
5-------------------
6The Python build is implemented with GN templates defined in
7``pw_build/python.gni``. That file contains the complete usage documentation.
8
9.. seealso:: :ref:`docs-python-build`
10
11pw_python_package
12=================
13The main Python template is ``pw_python_package``. Each ``pw_python_package``
14target represents a Python package. As described in
15:ref:`module-pw_build-python-target`, each ``pw_python_package`` expands to
16several subtargets. In summary, these are:
17
18- ``<name>`` - Represents the files themselves
19- ``<name>.lint`` - Runs static analysis
20- ``<name>.tests`` - Runs all tests for this package
21- ``<name>.install`` - Installs the package
22- ``<name>.wheel`` - Builds a Python wheel
23
24Arguments
25---------
26- ``setup`` - List of setup file paths (setup.py or pyproject.toml & setup.cfg),
27 which must all be in the same directory.
28- ``sources`` - Python sources files in the package.
29- ``tests`` - Test files for this Python package.
30- ``python_deps`` - Dependencies on other pw_python_packages in the GN build.
31- ``python_test_deps`` - Test-only pw_python_package dependencies.
32- ``other_deps`` - Dependencies on GN targets that are not pw_python_packages.
33- ``inputs`` - Other files to track, such as package_data.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080034- ``proto_library`` - A pw_proto_library target to embed in this Python package.
35 generate_setup is required in place of setup if proto_library is used. See
36 :ref:`module-pw_protobuf_compiler-add-to-python-package`.
Wyatt Heplerb3ea9802021-02-23 09:46:09 -080037- ``lint`` - If true (default), applies Mypy and Pylint to the package. If
38 false, does not.
39- ``pylintrc`` - Optional path to a pylintrc configuration file to use. If not
40 provided, Pylint's default rcfile search is used. Pylint is executed
41 from the package's setup directory, so pylintrc files in that directory
42 will take precedence over others.
43- ``mypy_ini`` - Optional path to a mypy configuration file to use. If not
44 provided, mypy's default configuration file search is used. mypy is
45 executed from the package's setup directory, so mypy.ini files in that
46 directory will take precedence over others.
47
48Example
49-------
50This is an example Python package declaration for a ``pw_my_module`` module.
51
52.. code-block::
53
54 import("//build_overrides/pigweed.gni")
55
56 import("$dir_pw_build/python.gni")
57
58 pw_python_package("py") {
59 setup = [ "setup.py" ]
60 sources = [
61 "pw_my_module/__init__.py",
62 "pw_my_module/alfa.py",
63 "pw_my_module/bravo.py",
64 "pw_my_module/charlie.py",
65 ]
66 tests = [
67 "alfa_test.py",
68 "charlie_test.py",
69 ]
70 python_deps = [
71 "$dir_pw_status/py",
72 ":some_protos.python",
73 ]
74 python_test_deps = [ "$dir_pw_build/py" ]
75 pylintrc = "$dir_pigweed/.pylintrc"
76 }
77
78pw_python_script
79================
80A ``pw_python_script`` represents a set of standalone Python scripts and/or
81tests. These files support all of the arguments of ``pw_python_package`` except
82those ``setup``. These targets can be installed, but this only installs their
83dependencies.
84
85pw_python_group
86===============
87Represents a group of ``pw_python_package`` and ``pw_python_script`` targets.
88These targets do not add any files. Their subtargets simply forward to those of
89their dependencies.
90
91pw_python_wheels
92================
93Builds and collects Python wheels for one or more ``pw_python_package`` targets.
94A package's ``.wheel`` subtarget builds the wheel for just that package.
95``pw_python_package`` collects wheels from all of its transitive dependencies
96and collects them in a specified directory.
97
98pw_python_requirements
99======================
100Represents a set of local and PyPI requirements, with no associated source
101files. These targets serve the role of a ``requirements.txt`` file.