Skip to content

Commit e88101a

Browse files
muditsharmaa-armAnton-TF
authored andcommitted
Python: Introduce python configuration with pyproject.toml
`pyproject.toml` was introduced with PEP 518 as an effort to standardize packaging and dependency management in python projects. This commit sets up `pyproject.toml` with following: 1. Basic project information (Name, author, etc) 2. List of runtime dependencies for building TF-M 3. List of optional dependencies (for Docs) for TF-M 5. Minimum python version requirement for TF-M This commit also includes: 1. `uv.lock` file that aids with reproducibility and consistency of python build/dev environment required for building the project. 2. Update docs to use `pyproject.toml` instead of `requirements.txt` 3. Docs for use UV to manage different python versions 4. Docs for writing python scripts and modules and including capturing related metadata in pyproject.toml. We also add `.venv` and `trusted_firmware_m.egg-info/` in `.gitignore`. These folders contain data related to virtual environment and setuptools respectively. Change-Id: I4b3e7dfab41690dc7a079074d4da891250e616ce Signed-off-by: Mudit Sharma <mudit.sharma@arm.com>
1 parent 5199406 commit e88101a

File tree

8 files changed

+1340
-26
lines changed

8 files changed

+1340
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ localrepos.cmake
2424

2525
# Python venv
2626
venv
27+
.venv
28+
29+
trusted_firmware_m.egg-info/

.readthedocs.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#-------------------------------------------------------------------------------
2-
#
3-
# Copyright (c) 2023, Arm Limited. All rights reserved.
2+
# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
43
#
54
# SPDX-License-Identifier: BSD-3-Clause
65
#
@@ -19,6 +18,14 @@ build:
1918
python: "3.11"
2019
apt_packages:
2120
- plantuml
21+
jobs:
22+
create_environment:
23+
- asdf plugin add uv
24+
- asdf install uv latest
25+
- asdf global uv latest
26+
- uv venv
27+
install:
28+
- uv pip install ".[docs]"
2229

2330
# Build documentation in the "docs/" directory with Sphinx
2431
sphinx:
@@ -28,8 +35,3 @@ sphinx:
2835
formats:
2936
- pdf
3037
- htmlzip
31-
32-
# Configuration of the Python environment
33-
python:
34-
install:
35-
- requirements: docs/requirements.txt

docs/building/documentation_generation.rst

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Two documents are available for generation:
99

1010
The documentation build is independent from building the binary artifacts.
1111

12+
.. _build-environment-setup:
13+
1214
******************************
1315
Tools and building environment
1416
******************************
@@ -19,7 +21,7 @@ These tools are used to generate TF-M documentation:
1921
- Graphviz dot v2.38.0 or later
2022
- PlantUML v1.2018.11 or later
2123
- Java runtime environment v1.8 or later (for running PlantUML)
22-
- Sphinx and other python modules, listed in ``docs/requirements.txt``
24+
- Optionally, `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>`
2325

2426
Additionally, for PDFs format:
2527

@@ -49,10 +51,18 @@ To prepare your building environment execute the following steps:
4951
# For PDF generation
5052
sudo apt-get install -y doxygen-latex librsvg2-bin
5153
52-
# Install the required Python modules
53-
pip3 install --upgrade pip
54-
cd <TF-M base folder>
55-
pip3 install -r docs/requirements.txt
54+
# Setup python venv for the project
55+
python3 -m venv .venv
56+
57+
# NOTE: If your system python install version is <3.10 you can use `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>` to setup your .venv
58+
uv venv --python 3.12
59+
60+
source .venv/bin/activate
61+
pip install ".[docs]"
62+
63+
64+
# NOTE: If you've used `uv` to setup your `.venv`, prepend the `pip` commands with `uv`
65+
uv pip install ".[docs]"
5666
5767
Set the environment variables:
5868

@@ -82,23 +92,39 @@ To prepare your building environment execute the following steps:
8292
set PATH=$PATH;<ARM_DS_PATH>\sw\java\bin
8393
8494
# Install the required Python modules
85-
pip3 install --upgrade pip
86-
cd trusted-firmware-m
87-
pip3 install -r docs\requirements.txt
95+
96+
# Setup python venv for the project
97+
python3 -m venv .venv
98+
99+
# NOTE: If your system python install version is <3.10 you can use `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>` to setup your .venv
100+
uv venv --python 3.12
101+
102+
source .venv/bin/activate
103+
pip install ".[docs]"
104+
# If uv is installed
105+
uv pip install ".[docs]"
106+
107+
# NOTE: If you've used `uv` to setup your `.venv`, prepend the `pip` commands with `uv`
108+
uv pip sync pylock.toml
88109
89110
***************************
90111
Build TF-M Reference Manual
91112
***************************
92113

93114
The Reference Manual will be generated in the ``build_docs/reference_manual``.
94115

116+
.. Note::
117+
Please make sure to follow :ref:`Tools and building environment <build-environment-setup>`
118+
before proceeding
119+
95120
.. tabs::
96121

97122
.. group-tab:: Linux
98123

99124
.. code-block:: bash
100125
101126
cd <TF-M base folder>
127+
source .venv/bin/activate
102128
cmake -S docs -B build_docs
103129
cmake --build build_docs -- tfm_docs_refman_html tfm_docs_refman_pdf
104130
@@ -107,6 +133,10 @@ The Reference Manual will be generated in the ``build_docs/reference_manual``.
107133
.. code-block:: bash
108134
109135
cd <TF-M base folder>
136+
source .venv/bin/activate
137+
pip install ".[docs]"
138+
# If uv is installed
139+
uv pip install ".[docs]"
110140
cmake -S docs -B build_docs -G"Unix Makefiles"
111141
cmake --build build_docs -- tfm_docs_refman_html tfm_docs_refman_pdf
112142
@@ -123,6 +153,7 @@ The User Manual will be available under the directory ``build_docs/user_guide``.
123153
.. code-block:: bash
124154
125155
cd <TF-M base folder>
156+
source .venv/bin/activate
126157
cmake -S docs -B build_docs
127158
cmake --build build_docs -- tfm_docs_userguide_html tfm_docs_userguide_pdf
128159
@@ -131,6 +162,7 @@ The User Manual will be available under the directory ``build_docs/user_guide``.
131162
.. code-block:: bash
132163
133164
cd <TF-M base folder>
165+
source .venv/bin/activate
134166
cmake -S docs -B build_docs -G"Unix Makefiles"
135167
cmake --build build_docs -- tfm_docs_userguide_html tfm_docs_userguide_pdf
136168
@@ -150,6 +182,7 @@ The direct build will build both user_guide and reference_manual.
150182
151183
# Build the documentation from build_docs directory
152184
cd <TF-M base folder>
185+
source .venv/bin/activate
153186
mkdir build_docs
154187
cp docs/conf.py build_docs/conf.py
155188
cd build_docs

docs/contributing/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Contribution Guidelines
1313
Documentation <doc_guidelines>
1414
Design proposal <tfm_design_proposal_guideline>
1515
MISRA-C <standards/misra>
16+
Python scripts and modules <python_scripting>
1617

1718
--------------
1819

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
##########################
2+
Python Scripts and Modules
3+
##########################
4+
5+
Overview
6+
--------
7+
8+
This document describes the guidelines for adding Python scripts and modules to TF-M. TF-M supports
9+
multiple platforms, each potentially requiring platform-specific scripts or modules. Scripts and
10+
modules are organized as `Namespace Packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages/>`_.
11+
12+
Adding Platform-Specific Modules
13+
--------------------------------
14+
15+
Suppose a module is located at
16+
``path/to/module/``
17+
and its package name is ``foo``:
18+
19+
- Add the module path to ``pyproject.toml``:
20+
21+
.. code-block:: toml
22+
23+
[tool.setuptools.package-dir]
24+
foo = "path/to/module/"
25+
26+
- Register the package in ``pyproject.toml``:
27+
28+
.. code-block:: toml
29+
30+
[tool.setuptools]
31+
packages = ["foo"]
32+
33+
- Install the module:
34+
35+
.. code-block:: bash
36+
37+
pip install .
38+
39+
Or, for editable installation:
40+
41+
.. code-block:: bash
42+
43+
source .venv/bin/activate
44+
pip install -e .
45+
46+
- Import the installed module in other Python scripts/modules:
47+
48+
.. code-block:: python
49+
50+
import foo.<MODULE_NAME> as <MODULE_SHORT_NAME>
51+
52+
Adding Platform-Specific Scripts
53+
-------------------------------
54+
55+
- Every script should define a ``main()`` entry point.
56+
- Add the script’s entry point to ``pyproject.toml``. The script executable name must be prefixed
57+
with the platform name.
58+
59+
For example, for a script ``bar`` in package ``foo``:
60+
61+
- Register the script in ``pyproject.toml``:
62+
63+
.. code-block:: toml
64+
65+
[project.scripts]
66+
foo_bar = "foo.bar:main"
67+
68+
- Install scripts in a virtual environment:
69+
70+
.. code-block:: bash
71+
# Subsequent edits made to scripts after install **WONT** be reflected immediately and will need to be installed again
72+
source .venv/bin/activate
73+
pip install .
74+
75+
Or, for editable installation:
76+
77+
.. code-block:: bash
78+
# Edits made to the scripts will be reflected immediately
79+
source .venv/bin/activate
80+
pip install -e .
81+
82+
- Run the script by calling the executable:
83+
84+
.. code-block:: bash
85+
86+
(.venv) ± foo_bar
87+
foo_bar help text
88+
89+
--------------
90+
91+
*SPDX-License-Identifier: BSD-3-Clause*
92+
93+
*SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors*

docs/getting_started/tfm_getting_started.rst

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,24 @@ dependencies.
119119
120120
git clone https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git
121121
122-
2. TF-M's ``tools/requirements.txt`` file declares additional Python
123-
dependencies. Install them with ``pip3``:
122+
2. TF-M recommends installing dependencies in a venv
124123

125124
.. code-block:: bash
126125
127-
pip3 install --upgrade pip
128-
cd trusted-firmware-m
129-
pip3 install -r tools/requirements.txt
126+
# Setup python venv for the project
127+
python3 -m venv .venv
128+
129+
# NOTE: If your system python install version is <3.10 you can use `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>` to setup your .venv
130+
uv venv --python 3.12
131+
132+
source .venv/bin/activate
133+
# `-e` installs modules and scripts in editable/development mode
134+
pip install -e .
135+
136+
# NOTE: If you've used `uv` to setup your `.venv`, prepend the `pip` commands with `uv`
137+
# `-e` installs modules and scripts in editable/development mode
138+
uv pip install -e .
139+
130140
131141
.. group-tab:: Windows
132142

@@ -136,13 +146,23 @@ dependencies.
136146
137147
git clone https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git
138148
139-
2. TF-M's ``tools/requirements.txt`` file declares additional Python
140-
dependencies. Install them with ``pip3``:
149+
2. TF-M recommends installing dependencies in a venv
141150

142151
.. code-block:: bash
143152
144-
cd trusted-firmware-m
145-
pip3 install -r tools\requirements.txt
153+
# Setup python venv for the project
154+
python3 -m venv .venv
155+
156+
# NOTE: If your system python install version is <3.10 you can use `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>` to setup your .venv
157+
uv venv --python 3.12
158+
159+
source .venv/bin/activate
160+
# `-e` installs modules and scripts in editable/development mode
161+
pip install -e .
162+
163+
# NOTE: If you've used `uv` to setup your `.venv`, prepend the `pip` commands with `uv`
164+
# `-e` installs modules and scripts in editable/development mode
165+
uv pip install -e .
146166
147167
###################
148168
Install a toolchain
@@ -434,8 +454,8 @@ To build the TF-M firmware the following tools are needed:
434454
- CMake version 3.21 or later
435455
- Git
436456
- gmake, aka GNU Make
437-
- Python v3.x
438-
- a set of python modules listed in ``tools/requirements.txt``
457+
- Python >=v3.11
458+
- [Optionally] `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>`
439459

440460
****************
441461
Dependency chain

pyproject.toml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[project]
2+
name = "trusted-firmware-m"
3+
description = "Trusted Firmware-M (TF-M) Python dependencies."
4+
authors = [{ name = "Arm Ltd." }]
5+
license = "BSD-3-Clause"
6+
readme = "readme.rst"
7+
version = "2.2.0"
8+
9+
requires-python = ">=3.10"
10+
11+
dependencies = [
12+
"pyhsslms (>=2.0.0,<2.1.0)",
13+
"cbor2 (>=5.6.5,<6.0.0)",
14+
"click (>=8.1.8,<9.0.0)",
15+
"cryptography (>=36.0.1)",
16+
"pyasn1 (>=0.6.1,<0.7.0)",
17+
"imgtool (>=2.1.0,<3.0.0)",
18+
"jinja2 (>=3.1.6,<4.0.0)",
19+
"jsonschema (>=4.23.0,<5.0.0)",
20+
"pyyaml (>=6.0.2,<7.0.0)",
21+
"ecdsa (>=0.19.1,<0.20.0)",
22+
"kconfiglib (>=14.1.0,<15.0.0)",
23+
"networkx (>=3.1,<4.0)",
24+
"pyelftools (>=0.32,<0.33)",
25+
"rich (>=13.9.4,<14.0.0)",
26+
"libclang (>=18.1.1,<19.0.0)",
27+
]
28+
29+
[project.optional-dependencies]
30+
docs = [
31+
"sphinx==5.0.0",
32+
"sphinx-rtd-theme>=1.0.0",
33+
"sphinxcontrib-plantuml==0.30",
34+
"sphinxcontrib-svg2pdfconverter==1.3.0",
35+
"sphinx-tabs==3.4.7",
36+
"jinja2==3.1.6",
37+
"graphviz==0.20.3",
38+
"latex==0.7.0",
39+
"m2r2<0.3.3.post2",
40+
"setuptools==78.1.0"
41+
]
42+
43+
[tool.setuptools]
44+
package-dir = {"" = "tools/modules"}
45+
46+
[tool.setuptools.packages.find]
47+
where = ["tools/modules"]
48+
49+
[build-system]
50+
requires = ["setuptools"]
51+
build-backend = "setuptools.build_meta"

0 commit comments

Comments
 (0)