blob: 520b333f8bc87fc04753701bd4f82c867c873047 [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
Wyatt Hepleraf853ea2021-03-09 19:09:54 -080078
79.. _module-pw_build-python-wheels:
80
81Collecting Python wheels for distribution
82-----------------------------------------
83The ``.wheel`` subtarget generates a wheel (``.whl``) for the Python package.
84Wheels for a package and its transitive dependencies can be collected by
85traversing the ``pw_python_package_wheels`` `GN metadata
86<https://gn.googlesource.com/gn/+/master/docs/reference.md#var_metadata>`_ key,
87which lists the output directory for each wheel.
88
89The ``pw_mirror_tree`` template can be used to collect wheels in an output
90directory:
91
92.. code-block::
93
94 import("$dir_pw_build/mirror_tree.gni")
95
96 pw_mirror_tree("my_wheels") {
97 path_data_keys = [ "pw_python_package_wheels" ]
98 deps = [ ":python_packages" ]
99 directory = "$root_out_dir/the_wheels"
100 }
101
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800102pw_python_script
103================
104A ``pw_python_script`` represents a set of standalone Python scripts and/or
105tests. These files support all of the arguments of ``pw_python_package`` except
106those ``setup``. These targets can be installed, but this only installs their
107dependencies.
108
109pw_python_group
110===============
111Represents a group of ``pw_python_package`` and ``pw_python_script`` targets.
112These targets do not add any files. Their subtargets simply forward to those of
113their dependencies.
114
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800115pw_python_requirements
116======================
117Represents a set of local and PyPI requirements, with no associated source
118files. These targets serve the role of a ``requirements.txt`` file.