blob: b13b189805a6d62bba95f664f53ff42ff6083f7a [file] [log] [blame]
Galanakis, Minos41f85972019-09-30 15:56:40 +01001##################
2Build instructions
3##################
Anton Komlev0dbe8f12022-06-17 16:48:12 +01004
Anton Komlev4b1c05c2023-10-06 20:22:58 +01005.. warning::
6 The build process was changed a lot in Q3 2023 and included into the release after v1.9.
7 For building instructions for early versions please refer to the documentation of respective
8 versions.
9
10As you know from the :doc:`introduction </introduction/readme>` TF-M implements
11:term:`SPE` with a set of secure services.
12TF-M application as :term:`NSPE` client uses those services through isolation boundary via
13:term:`PSA-FF-M` API.
14Both SPE and NSPE are separate binaries and built independently.
Kevin Peng304e2682023-11-06 14:10:50 +080015SPE and NSPE binaries are combined and signed making the final image for downloading onto targets
16when building NSPE.
17
18.. Note::
19 This document describes the process of building a single SPE alone.
20 Refer to :doc:`Building Tests </building/tests_build_instruction>` on how to build TF-M
21 regression tests and PSA Arch tests to verify TF-M.
Anton Komlev4b1c05c2023-10-06 20:22:58 +010022
23TF-M uses `CMake <https://cmake.org/overview/>`__ **v3.15** or higher.
24Before starting please make sure you have all required software installed and
25configured as explained in the
Anton Komlev3356ba32022-03-31 22:02:11 +010026:doc:`TF-M getting started </getting_started/tfm_getting_started>`.
Anton Komlev4b1c05c2023-10-06 20:22:58 +010027
28.. contents:: Contents
29 :depth: 2
30 :local:
31
32The additional building materials you can find in the following links:TF-M source folder
Anton Komlev91281f02022-04-22 09:24:20 +010033
34.. toctree::
35 :maxdepth: 1
36
Kevin Peng304e2682023-11-06 14:10:50 +080037 Build Tests <tests_build_instruction>
Anton Komlev4b1c05c2023-10-06 20:22:58 +010038 Run TF-M tests and applications <run_tfm_examples_on_arm_platforms>
Anton Komlev0dbe8f12022-06-17 16:48:12 +010039 Building the documentation <documentation_generation>
40 IAR toolchain <tfm_build_instruction_iar>
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020041
Anton Komlev4b1c05c2023-10-06 20:22:58 +010042.. _Building SPE:
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020043
Anton Komlev4b1c05c2023-10-06 20:22:58 +010044*******************
45Building TF-M (SPE)
46*******************
47This build generates the SPE binary and artifacts, necessary for :ref:`Building NSPE`.
Raef Coles1ecb8132020-10-02 15:11:24 +010048
David Wangd6fd6af2021-06-21 17:17:26 +080049.. _Getting the source-code:
50
Anton Komlev4b1c05c2023-10-06 20:22:58 +010051Getting the source code
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020052=======================
53.. code-block:: bash
54
Leonardo Sandovald7f72d52020-07-28 18:02:34 -050055 cd <base folder>
Minos Galanakis0f39fa52020-06-08 16:53:53 +010056 git clone https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git
Anton Komlevb8e3af02020-08-28 10:23:57 +010057
Anton Komlev4b1c05c2023-10-06 20:22:58 +010058In this documentation, the cloned ``trusted-firmware-m`` repository will be referenced as
59``<TF-M source dir>``.
60Additionally, TF-M depends on several external projects, handled by CMake automatically but
61you can alter that behaviour using :ref:`Dependency management`.
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020062
Minos Galanakisac6b06c2020-03-19 12:57:02 +000063.. Note::
Anton Komlevb8e3af02020-08-28 10:23:57 +010064
Minos Galanakis3b740a12020-10-15 11:10:26 +010065 - For building with Armclang compiler version 6.10.0+, please follow the note
Anton Komlev3356ba32022-03-31 22:02:11 +010066 in :doc:`TF-M getting started </getting_started/tfm_getting_started>`.
Anton Komlevb8e3af02020-08-28 10:23:57 +010067 - For building with the IAR toolchain, please see the notes in
Minos Galanakis3b740a12020-10-15 11:10:26 +010068 :doc:`IAR software requirements <tfm_build_instruction_iar>`
Ken Liu6792e042023-11-13 14:48:00 +080069 - Please use "/" instead of "\\" for paths when running CMAKE commands under
70 Windows Command Prompt.
TTornblomb9e5ed02020-02-13 16:53:15 +010071
Anton Komlev4b1c05c2023-10-06 20:22:58 +010072Configuring
73===========
74TF-M has many config options for configuring and fine-tuning. Please check the
75:ref:`tf-m_configuration` section for the details. The **base** (default) configuration
76contains only essential components such as SPM and HW platform support hence the
77only mandatory argument to TF-M build is a platform name, provided via
78CMake command-line option ``-DTFM_PLATFORM=<platform name>``, it can be:
Anton Komlevb8e3af02020-08-28 10:23:57 +010079
Anton Komlev4b1c05c2023-10-06 20:22:58 +010080- A relative path under ``<TF-M source dir>/platform/ext/target``, for example ``arm/mps2/an521``.
81- An absolute path of target platform, mainly used for out-of-tree platform build.
82- A basename of the target platform folder, for example ``an521``.
Anton Komlevb8e3af02020-08-28 10:23:57 +010083
Anton Komlev4b1c05c2023-10-06 20:22:58 +010084Essential Directories
85---------------------
86There are 3 essential directories used by CMake for building TF-M:
87
88- Source code directory ``<TF-M source dir>``
89- Build directory ``<Build Dir>`` - the location of all intermediate files required to produce
90 a build target.
91- Install directory ``<Artifact Dir>`` - the location of the build output files.
92
93Note::
94 It's recommended to use absolute paths for all directories. Relative paths may not fully work.
95
96.. _Toolchains:
97
98Toolchains
99----------
100TF-M supports 3 toolchains for cross-compiling and building the project binaries:
101
102- GNU - **default**
103- ArmClang
104- IAR
105
106Each toolchain has a configuration file for the compiler and linker.
107Use ``TFM_TOOLCHAIN_FILE`` option to provide a path to a preferred toolchain file or the toolchain
108file name.
109The default **toolchain_GNUARM.cmake** is selected by `config_base.cmake`
110file if the option is omitted.
111
112.. _Build type:
Anton Komlevb8e3af02020-08-28 10:23:57 +0100113
Anton Komlevb8e3af02020-08-28 10:23:57 +0100114Build type
115----------
Dávid Házie50f0a92022-07-06 14:46:01 +0200116By default, a MinSizeRel configuration is built. Alternate build types can be
Chris Brand80266ba2022-01-05 09:14:52 -0800117specified with the ``CMAKE_BUILD_TYPE`` variable. The possible
Anton Komlevb8e3af02020-08-28 10:23:57 +0100118types are:
119
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100120- Debug
121- RelWithDebInfo
122- Release
123- MinSizeRel - **default**
Anton Komlevb8e3af02020-08-28 10:23:57 +0100124
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100125Debug symbols are added by default to all builds, but can be removed from
126*Release* and *MinSizeRel* builds by setting ``TFM_DEBUG_SYMBOLS`` to `OFF`.
Sebastian Bøeb73f1762021-10-28 14:23:13 +0200127
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100128*RelWithDebInfo*, *Release* and *MinSizeRel* all have different
129optimizations turned on and hence will produce smaller, faster code than
130*Debug*. *MinSizeRel* will produce the smallest code and hence is often
131a good idea on RAM or flash-constrained systems.
Anton Komlevb8e3af02020-08-28 10:23:57 +0100132
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100133.. _Output files:
134
135Output files
136------------
137In a successful build, a set of files will be created in the ``<Artifact Dir>``.
138By default, it is ``<Build Dir>\api_ns`` subfolder but you can redirect the
139output to any location using ``CMAKE_INSTALL_PREFIX`` option. It can be an
140absolute path or relative to your current directory. For the contents of the
141artifact directory please refer to :ref:`Artifacts structure`.
142
143Other build parameters
Anton Komlevb8e3af02020-08-28 10:23:57 +0100144----------------------
Anton Komlev2cd91532022-11-17 13:36:55 +0000145The full list of default options is in ``config/config_base.cmake`` and
146explained in :ref:`tfm_cmake_configuration`. Several important options
147are listed below.
Anton Komlevb8e3af02020-08-28 10:23:57 +0100148
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800149+----------------------------+------------------------------------------+---------------+
150| Parameter | Description | Default value |
151+============================+==========================================+===============+
152| BL2 | Build level 2 secure bootloader. | ON |
153+----------------------------+------------------------------------------+---------------+
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800154| PROJECT_CONFIG_HEADER_FILE | User defined header file for TF-M config | |
155+----------------------------+------------------------------------------+---------------+
156| TFM_ISOLATION_LEVEL | Set TFM isolation level. | 1 |
157+----------------------------+------------------------------------------+---------------+
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100158| TFM_PROFILE | See :ref:`tf-m_profiles`. | |
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800159+----------------------------+------------------------------------------+---------------+
160
161Project Config Header File
162--------------------------
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100163CMake variable ``PROJECT_CONFIG_HEADER_FILE`` can be set by a user the full path to a
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800164configuration header file, which is used to fine-tune component options. The detailed reference
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100165for the project config header file is in :ref:`Header_configuration`.
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800166
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100167Building binaries
168=================
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800169
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100170The command below shows a general template for building TF-M as a typical CMake project:
Anton Komlevb8e3af02020-08-28 10:23:57 +0100171
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200172.. code-block:: bash
173
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100174 cmake -S <TF-M source dir> -B <Build Dir> -DTFM_PLATFORM=<platform>
175 cmake --build <Build Dir> -- install
Anton Komlevb8e3af02020-08-28 10:23:57 +0100176
177.. Note::
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100178 It is recommended to clean up the build directory before re-build if the config
179 header file is updated. CMake is unable to automatically recognize the
180 dependency when the header file is defined as a macro.
Anton Komlevb8e3af02020-08-28 10:23:57 +0100181
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100182Building default configuration for an521
183----------------------------------------
184
185.. code-block:: bash
186
187 cd <TF-M source dir>
188 cmake -S . -B build -DTFM_PLATFORM=arm/mps2/an521
189 cmake --build build -- install
190
191The command above is intended to do:
192 - take TF-M sources in the current ``.`` folder
193 - build SPE in the ``build`` folder
194 - for **an521** platform
195 - using GNU toolchain *by default*. Use ``-DTFM_TOOLCHAIN_FILE=<toolchain file>``
196 for alternatives as described in :ref:`Toolchains`
197 - install output files in ``build/api_ns`` folder *by default*. You can specify
198 a different directory using ``-DCMAKE_INSTALL_PREFIX=<Artifact dir>``
199 as described in :ref:`Output files`
200
201.. Note::
Anton Komlevb8e3af02020-08-28 10:23:57 +0100202 It is recommended to build each different build configuration in a separate
203 build directory.
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200204
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100205CMake can generate code for many native build systems. TF-M is tested with
206``Unix Makefiles`` (default) and ``Ninja``. The ``-G`` option can specify
207alternative generators. For example for building with Ninja in the Debug
208:ref:`Build type` using ArmClang :ref:`Toolchains` you can use the following:
Leonardo Sandoval49a558c2021-02-03 17:04:22 -0600209
210.. code-block:: bash
Subhasish Ghoshbc849302021-04-08 20:21:20 +0100211
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100212 cd <TF-M source dir>
213 cmake -S . -B build -DTFM_PLATFORM=arm/mps2/an521 -GNinja -DTFM_TOOLCHAIN_FILE=toolchain_ARMCLANG.cmake -DCMAKE_BUILD_TYPE=Debug
214 cmake --build build -- install
Leonardo Sandoval49a558c2021-02-03 17:04:22 -0600215
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100216.. _Dependency management:
Anton Komlevb8e3af02020-08-28 10:23:57 +0100217
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100218Dependency management
219=====================
220
221The TF-M build system will fetch all dependencies by default with appropriate
222versions and store them inside the build tree. In this case, the build tree
223location is ``<build_dir>/lib/ext``.
224
225If you have local copies already and wish to avoid having the libraries
226downloaded every time the build directory is deleted, then the following
227variables can be set to the paths to the root directories of the local repos.
228This will disable the automatic downloading for that dependencies and speed up
229development iterations or allow usage of a dependency version different from the
230current one.
231Additionally, these path variables can be set in ``localrepos.cmake``
232file which will be included in a build if it exists.
233This file is ignored in TF-M git settings.
234
235The following table lists the commonly used repos. For others, you can refer to ``lib/ext``.
236
237+----------------+---------------------+-----------------------------------------------------+
238| Dependency | Cmake variable | Git repo URL |
239+================+=====================+=====================================================+
240| Mbed Crypto | MBEDCRYPTO_PATH | https://github.com/ARMmbed/mbedtls |
241+----------------+---------------------+-----------------------------------------------------+
242| MCUboot | MCUBOOT_PATH | https://github.com/mcu-tools/mcuboot |
243+----------------+---------------------+-----------------------------------------------------+
244| QCBOR | QCBOR_PATH | https://github.com/laurencelundblade/QCBOR.git |
245+----------------+---------------------+-----------------------------------------------------+
246
247The recommended versions of the dependencies are listed in ``config/config_base.cmake``.
248
249.. Note::
250
251 - Some repositories might need patches to allow building it as a part of TF-M.
252 While these patches are being upstreamed they are stored in a
253 dependency folder under ``lib/ext/``.
254 In order to use local repositories those patches shall be applied to original source.
255 An alternative is to copy out the auto-downloaded repos under the ``<build_dir>/lib/ext``.
256 They have been applied with patches and can be used directly.
257
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100258Example: building TF-M with local Mbed Crypto repo
259--------------------------------------------------
260
261Preparing a local repository consists of 2 steps: cloning and patching.
262This is only required to be done once. For dependencies without ``.patch``
263files in their ``lib/ext`` directory the only required step is
264cloning the repo and checking out the correct branch.
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200265
266.. code-block:: bash
267
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100268 cd <Mbed Crypto base folder>
269 git clone https://github.com/ARMmbed/mbedtls
270 cd mbedtls
271 git checkout <MBEDCRYPTO_VERSION from <TF-M source dir>/config_base.cmake>
272 git apply <TF-M source dir>/lib/ext/mbedcrypo/*.patch
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200273
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100274.. Note::
275
276 ``<Mbed Crypto base folder>`` does not need to have any fixed position related
277 to the TF-M repo so alternative method to get prepared dependency repos is to
278 let TF-M download it once and then copy them out of the ``build/lib/ext`` folder.
279
280Now build TF-M binaries
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200281
282.. code-block:: bash
283
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100284 cd <TF-M source dir>
285 cmake -S . -B build -DTFM_PLATFORM=arm/mps2/an521 -DMBEDCRYPTO_PATH=<Mbed Crypto base folder>/mbedtls
286 cmake --build build -- install
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200287
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100288.. _Building NSPE:
289
290***************************
291Building Application (NSPE)
292***************************
293
294As a result of :ref:`Building SPE` you will get a set of :ref:`Output files` in
295``<Artifact Dir>`` required for building TF-M application. Essentially, SPE
296exports a binary and a set of C source files for PSA interface and platform.
297Please note that NSPE and SPE are independent projects and can be built using
298different toolchains and toolchain options.
299
300.. _Artifacts structure:
301
302SPE artifacts structure
Kevin Peng23a583c2021-09-08 22:33:33 +0800303=======================
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100304
305SPE components prepared and installed for NSPE usage in ``<Artifact Dir>``
306will have the following structure:
307
308.. code-block:: none
309
310 <Artifact Dir>
311 ├── bin
312 ├── cmake
313 ├── config
314 ├── image_signing
315 ├── interface
316 ├── platform
317 └── CMakeLists.txt
318
319With certain configurations, additional folders may also be installed.
320These folders have the following content:
321
322- **bin** - binary images of SPE, Bootloader(optional) and combined.
323- **cmake** - CMake scripts like SPE configuration and :ref:`NSPE toolchains`.
324- **config** - Configuration files
325- **image_signing** - binary image signing tool and keys.
326- **interface** - PSA interface exposed by SPE.
327- **platform** - source code for a selected hardware platform.
328- **CMakeLists.txt** - CMake script for the artifacts integration in NSPE.
329
Kevin Peng304e2682023-11-06 14:10:50 +0800330The content of ``<Artifact Dir>`` is a prepared directory for integration with CMake projects.
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100331
332.. _NSPE toolchains:
333
334NSPE toolchains
335===============
336
337SPE prepares and exports CMake toolchain files for building NSPE in all
338supported :ref:`Toolchains` in ``<Artifact Dir>/cmake`` folder.
339Toolchain used to build NSPE can be different from what is used to build SPE.
340
Anton Komlev4b1c05c2023-10-06 20:22:58 +0100341Basic SPE integration
342=====================
Kevin Peng304e2682023-11-06 14:10:50 +0800343Refer to the
344`example <https://git.trustedfirmware.org/TF-M/tf-m-extras.git/tree/examples/tf-m-example-ns-app>`__
345of TF-M applications in **tf-m-extras** repository.
Raef Coles1ecb8132020-10-02 15:11:24 +0100346
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200347--------------
348
Xinyu Zhang5f9ffeb2023-03-31 11:09:45 +0800349*Copyright (c) 2017-2023, Arm Limited. All rights reserved.*
Chris Brand80266ba2022-01-05 09:14:52 -0800350*Copyright (c) 2022, Cypress Semiconductor Corporation. All rights reserved.*