Wyatt Hepler | b3ea980 | 2021-02-23 09:46:09 -0800 | [diff] [blame] | 1 | .. _module-pw_build-python: |
| 2 | |
| 3 | ------------------- |
| 4 | Python GN templates |
| 5 | ------------------- |
| 6 | The 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 | |
| 11 | pw_python_package |
| 12 | ================= |
| 13 | The main Python template is ``pw_python_package``. Each ``pw_python_package`` |
| 14 | target represents a Python package. As described in |
| 15 | :ref:`module-pw_build-python-target`, each ``pw_python_package`` expands to |
| 16 | several 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 | |
| 24 | Arguments |
| 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 Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 34 | - ``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 Hepler | b3ea980 | 2021-02-23 09:46:09 -0800 | [diff] [blame] | 37 | - ``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 | |
| 48 | Example |
| 49 | ------- |
| 50 | This 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 | |
| 78 | pw_python_script |
| 79 | ================ |
| 80 | A ``pw_python_script`` represents a set of standalone Python scripts and/or |
| 81 | tests. These files support all of the arguments of ``pw_python_package`` except |
| 82 | those ``setup``. These targets can be installed, but this only installs their |
| 83 | dependencies. |
| 84 | |
| 85 | pw_python_group |
| 86 | =============== |
| 87 | Represents a group of ``pw_python_package`` and ``pw_python_script`` targets. |
| 88 | These targets do not add any files. Their subtargets simply forward to those of |
| 89 | their dependencies. |
| 90 | |
| 91 | pw_python_wheels |
| 92 | ================ |
| 93 | Builds and collects Python wheels for one or more ``pw_python_package`` targets. |
| 94 | A package's ``.wheel`` subtarget builds the wheel for just that package. |
| 95 | ``pw_python_package`` collects wheels from all of its transitive dependencies |
| 96 | and collects them in a specified directory. |
| 97 | |
| 98 | pw_python_requirements |
| 99 | ====================== |
| 100 | Represents a set of local and PyPI requirements, with no associated source |
| 101 | files. These targets serve the role of a ``requirements.txt`` file. |