blob: 11338cdd87d44e1aa48bf9036fc7eab1e8a0c55d [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.
34- ``lint`` - If true (default), applies Mypy and Pylint to the package. If
35 false, does not.
36- ``pylintrc`` - Optional path to a pylintrc configuration file to use. If not
37 provided, Pylint's default rcfile search is used. Pylint is executed
38 from the package's setup directory, so pylintrc files in that directory
39 will take precedence over others.
40- ``mypy_ini`` - Optional path to a mypy configuration file to use. If not
41 provided, mypy's default configuration file search is used. mypy is
42 executed from the package's setup directory, so mypy.ini files in that
43 directory will take precedence over others.
44
45Example
46-------
47This is an example Python package declaration for a ``pw_my_module`` module.
48
49.. code-block::
50
51 import("//build_overrides/pigweed.gni")
52
53 import("$dir_pw_build/python.gni")
54
55 pw_python_package("py") {
56 setup = [ "setup.py" ]
57 sources = [
58 "pw_my_module/__init__.py",
59 "pw_my_module/alfa.py",
60 "pw_my_module/bravo.py",
61 "pw_my_module/charlie.py",
62 ]
63 tests = [
64 "alfa_test.py",
65 "charlie_test.py",
66 ]
67 python_deps = [
68 "$dir_pw_status/py",
69 ":some_protos.python",
70 ]
71 python_test_deps = [ "$dir_pw_build/py" ]
72 pylintrc = "$dir_pigweed/.pylintrc"
73 }
74
75pw_python_script
76================
77A ``pw_python_script`` represents a set of standalone Python scripts and/or
78tests. These files support all of the arguments of ``pw_python_package`` except
79those ``setup``. These targets can be installed, but this only installs their
80dependencies.
81
82pw_python_group
83===============
84Represents a group of ``pw_python_package`` and ``pw_python_script`` targets.
85These targets do not add any files. Their subtargets simply forward to those of
86their dependencies.
87
88pw_python_wheels
89================
90Builds and collects Python wheels for one or more ``pw_python_package`` targets.
91A package's ``.wheel`` subtarget builds the wheel for just that package.
92``pw_python_package`` collects wheels from all of its transitive dependencies
93and collects them in a specified directory.
94
95pw_python_requirements
96======================
97Represents a set of local and PyPI requirements, with no associated source
98files. These targets serve the role of a ``requirements.txt`` file.