Balint Dobszay | 637f473 | 2019-11-13 12:48:00 +0100 | [diff] [blame] | 1 | TF-A CMake buildsystem |
| 2 | ====================== |
| 3 | |
| 4 | :Author: Balint Dobszay |
| 5 | :Organization: Arm Limited |
| 6 | :Contact: Balint Dobszay <balint.dobszay@arm.com> |
| 7 | :Status: Accepted |
| 8 | |
| 9 | .. contents:: Table of Contents |
| 10 | |
| 11 | Abstract |
| 12 | -------- |
| 13 | This document presents a proposal for a new buildsystem for TF-A using CMake, |
Olivier Deprez | aac64b0 | 2024-03-20 09:41:29 +0100 | [diff] [blame^] | 14 | and as part of this a reusable CMake framework for embedded projects. |
Balint Dobszay | 637f473 | 2019-11-13 12:48:00 +0100 | [diff] [blame] | 15 | |
| 16 | Introduction |
| 17 | ------------ |
| 18 | The current Makefile based buildsystem of TF-A has become complicated and hard |
| 19 | to maintain, there is a need for a new, more flexible solution. The proposal is |
| 20 | to use CMake language for the new buildsystem. The main reasons of this decision |
| 21 | are the following: |
| 22 | |
| 23 | * It is a well-established, mature tool, widely accepted by open-source |
| 24 | projects. |
| 25 | * TF-M is already using CMake, reducing fragmentation for tf.org projects can be |
| 26 | beneficial. |
| 27 | * CMake has various advantages over Make, e.g.: |
| 28 | |
| 29 | * Host and target system agnostic project. |
| 30 | * CMake project is scalable, supports project modularization. |
| 31 | * Supports software integration. |
| 32 | * Out-of-the-box support for integration with several tools (e.g. project |
| 33 | generation for various IDEs, integration with cppcheck, etc). |
| 34 | |
| 35 | Of course there are drawbacks too: |
| 36 | |
| 37 | * Language is problematic (e.g. variable scope). |
| 38 | * Not embedded approach. |
| 39 | |
| 40 | To overcome these and other problems, we need to create workarounds for some |
| 41 | tasks, wrap CMake functions, etc. Since this functionality can be useful in |
| 42 | other embedded projects too, it is beneficial to collect the new code into a |
| 43 | reusable framework and store this in a separate repository. The following |
| 44 | diagram provides an overview of the framework structure: |
| 45 | |
| 46 | |Framework structure| |
| 47 | |
| 48 | Main features |
| 49 | ------------- |
| 50 | |
| 51 | Structured configuration description |
| 52 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 53 | In the current Makefile system the build configuration description, validation, |
| 54 | processing, and the target creation, source file description are mixed and |
| 55 | spread across several files. One of the goals of the framework is to organize |
| 56 | this. |
| 57 | |
| 58 | The framework provides a solution to describe the input build parameters, flags, |
| 59 | macros, etc. in a structured way. It contains two utilities for this purpose: |
| 60 | |
| 61 | * Map: simple key-value pair implementation. |
| 62 | * Group: collection of related maps. |
| 63 | |
| 64 | The related parameters shall be packed into a group (or "setting group"). The |
| 65 | setting groups shall be defined and filled with content in config files. |
| 66 | Currently the config files are created and edited manually, but later a |
| 67 | configuration management tool (e.g. Kconfig) shall be used to generate these |
| 68 | files. Therefore, the framework does not contain parameter validation and |
| 69 | conflict checking, these shall be handled by the configuration tool. |
| 70 | |
| 71 | Target description |
| 72 | ^^^^^^^^^^^^^^^^^^ |
| 73 | The framework provides an API called STGT ('simple target') to describe the |
| 74 | targets, i.e. what is the build output, what source files are used, what |
| 75 | libraries are linked, etc. The API wraps the CMake target functions, and also |
| 76 | extends the built-in functionality, it can use the setting groups described in |
| 77 | the previous section. A group can be applied onto a target, i.e. a collection of |
| 78 | macros, flags, etc. can be applied onto the given output executable/library. |
| 79 | This provides a more granular way than the current Makefile system where most of |
| 80 | these are global and applied onto each target. |
| 81 | |
| 82 | Compiler abstraction |
| 83 | ^^^^^^^^^^^^^^^^^^^^ |
| 84 | Apart from the built-in CMake usage of the compiler, there are some common tasks |
| 85 | that CMake does not solve (e.g. preprocessing a file). For these tasks the |
| 86 | framework uses wrapper functions instead of direct calls to the compiler. This |
| 87 | way it is not tied to one specific compiler. |
| 88 | |
| 89 | External tools |
| 90 | ^^^^^^^^^^^^^^ |
| 91 | In the TF-A buildsystem some external tools are used, e.g. fiptool for image |
| 92 | generation or dtc for device tree compilation. These tools have to be found |
| 93 | and/or built by the framework. For this, the CMake find_package functionality is |
| 94 | used, any other necessary tools can be added later. |
| 95 | |
| 96 | Workflow |
| 97 | -------- |
| 98 | The following diagram demonstrates the development workflow using the framework: |
| 99 | |
| 100 | |Framework workflow| |
| 101 | |
| 102 | The process can be split into two main phases: |
| 103 | |
| 104 | In the provisioning phase, first we have to obtain the necessary resources, i.e. |
| 105 | clone the code repository and other dependencies. Next we have to do the |
| 106 | configuration, preferably using a config tool like KConfig. |
| 107 | |
| 108 | In the development phase first we run CMake, which will generate the buildsystem |
| 109 | using the selected generator backend (currently only the Makefile generator is |
| 110 | supported). After this we run the selected build tool which in turn calls the |
| 111 | compiler, linker, packaging tool, etc. Finally we can run and debug the output |
| 112 | executables. |
| 113 | |
| 114 | Usually during development only the steps in this second phase have to be |
| 115 | repeated, while the provisioning phase needs to be done only once (or rarely). |
| 116 | |
| 117 | Example |
| 118 | ------- |
| 119 | This is a short example for the basic framework usage. |
| 120 | |
| 121 | First, we create a setting group called *mem_conf* and fill it with several |
| 122 | parameters. It is worth noting the difference between *CONFIG* and *DEFINE* |
| 123 | types: the former is only a CMake domain option, the latter is only a C language |
| 124 | macro. |
| 125 | |
| 126 | Next, we create a target called *fw1* and add the *mem_conf* setting group to |
| 127 | it. This means that all source and header files used by the target will have all |
| 128 | the parameters declared in the setting group. Then we set the target type to |
| 129 | executable, and add some source files. Since the target has the parameters from |
| 130 | the settings group, we can use it for conditionally adding source files. E.g. |
| 131 | *dram_controller.c* will only be added if MEM_TYPE equals dram. |
| 132 | |
| 133 | .. code-block:: cmake |
| 134 | |
| 135 | group_new(NAME mem_conf) |
| 136 | group_add(NAME mem_conf TYPE DEFINE KEY MEM_SIZE VAL 1024) |
| 137 | group_add(NAME mem_conf TYPE CONFIG DEFINE KEY MEM_TYPE VAL dram) |
| 138 | group_add(NAME mem_conf TYPE CFLAG KEY -Os) |
| 139 | |
| 140 | stgt_create(NAME fw1) |
| 141 | stgt_add_setting(NAME fw1 GROUPS mem_conf) |
| 142 | stgt_set_target(NAME fw1 TYPE exe) |
| 143 | |
| 144 | stgt_add_src(NAME fw1 SRC |
| 145 | ${CMAKE_SOURCE_DIR}/main.c |
| 146 | ) |
| 147 | |
| 148 | stgt_add_src_cond(NAME fw1 KEY MEM_TYPE VAL dram SRC |
| 149 | ${CMAKE_SOURCE_DIR}/dram_controller.c |
| 150 | ) |
| 151 | |
| 152 | .. |Framework structure| image:: |
| 153 | ../resources/diagrams/cmake_framework_structure.png |
| 154 | :width: 75 % |
| 155 | |
| 156 | .. |Framework workflow| image:: |
| 157 | ../resources/diagrams/cmake_framework_workflow.png |
| 158 | |
| 159 | -------------- |
| 160 | |
Olivier Deprez | aac64b0 | 2024-03-20 09:41:29 +0100 | [diff] [blame^] | 161 | *Copyright (c) 2019-2024, Arm Limited and Contributors. All rights reserved.* |