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