174601490SBalint DobszayTF-A CMake buildsystem 274601490SBalint Dobszay====================== 374601490SBalint Dobszay 474601490SBalint Dobszay:Author: Balint Dobszay 574601490SBalint Dobszay:Organization: Arm Limited 674601490SBalint Dobszay:Contact: Balint Dobszay <balint.dobszay@arm.com> 774601490SBalint Dobszay:Status: Accepted 874601490SBalint Dobszay 974601490SBalint Dobszay.. contents:: Table of Contents 1074601490SBalint Dobszay 1174601490SBalint DobszayAbstract 1274601490SBalint Dobszay-------- 1374601490SBalint DobszayThis document presents a proposal for a new buildsystem for TF-A using CMake, 14*ed3525e6SOlivier Deprezand as part of this a reusable CMake framework for embedded projects. 1574601490SBalint Dobszay 1674601490SBalint DobszayIntroduction 1774601490SBalint Dobszay------------ 1874601490SBalint DobszayThe current Makefile based buildsystem of TF-A has become complicated and hard 1974601490SBalint Dobszayto maintain, there is a need for a new, more flexible solution. The proposal is 2074601490SBalint Dobszayto use CMake language for the new buildsystem. The main reasons of this decision 2174601490SBalint Dobszayare the following: 2274601490SBalint Dobszay 2374601490SBalint Dobszay* It is a well-established, mature tool, widely accepted by open-source 2474601490SBalint Dobszay projects. 2574601490SBalint Dobszay* TF-M is already using CMake, reducing fragmentation for tf.org projects can be 2674601490SBalint Dobszay beneficial. 2774601490SBalint Dobszay* CMake has various advantages over Make, e.g.: 2874601490SBalint Dobszay 2974601490SBalint Dobszay * Host and target system agnostic project. 3074601490SBalint Dobszay * CMake project is scalable, supports project modularization. 3174601490SBalint Dobszay * Supports software integration. 3274601490SBalint Dobszay * Out-of-the-box support for integration with several tools (e.g. project 3374601490SBalint Dobszay generation for various IDEs, integration with cppcheck, etc). 3474601490SBalint Dobszay 3574601490SBalint DobszayOf course there are drawbacks too: 3674601490SBalint Dobszay 3774601490SBalint Dobszay* Language is problematic (e.g. variable scope). 3874601490SBalint Dobszay* Not embedded approach. 3974601490SBalint Dobszay 4074601490SBalint DobszayTo overcome these and other problems, we need to create workarounds for some 4174601490SBalint Dobszaytasks, wrap CMake functions, etc. Since this functionality can be useful in 4274601490SBalint Dobszayother embedded projects too, it is beneficial to collect the new code into a 4374601490SBalint Dobszayreusable framework and store this in a separate repository. The following 4474601490SBalint Dobszaydiagram provides an overview of the framework structure: 4574601490SBalint Dobszay 4674601490SBalint Dobszay|Framework structure| 4774601490SBalint Dobszay 4874601490SBalint DobszayMain features 4974601490SBalint Dobszay------------- 5074601490SBalint Dobszay 5174601490SBalint DobszayStructured configuration description 5274601490SBalint Dobszay^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 5374601490SBalint DobszayIn the current Makefile system the build configuration description, validation, 5474601490SBalint Dobszayprocessing, and the target creation, source file description are mixed and 5574601490SBalint Dobszayspread across several files. One of the goals of the framework is to organize 5674601490SBalint Dobszaythis. 5774601490SBalint Dobszay 5874601490SBalint DobszayThe framework provides a solution to describe the input build parameters, flags, 5974601490SBalint Dobszaymacros, etc. in a structured way. It contains two utilities for this purpose: 6074601490SBalint Dobszay 6174601490SBalint Dobszay* Map: simple key-value pair implementation. 6274601490SBalint Dobszay* Group: collection of related maps. 6374601490SBalint Dobszay 6474601490SBalint DobszayThe related parameters shall be packed into a group (or "setting group"). The 6574601490SBalint Dobszaysetting groups shall be defined and filled with content in config files. 6674601490SBalint DobszayCurrently the config files are created and edited manually, but later a 6774601490SBalint Dobszayconfiguration management tool (e.g. Kconfig) shall be used to generate these 6874601490SBalint Dobszayfiles. Therefore, the framework does not contain parameter validation and 6974601490SBalint Dobszayconflict checking, these shall be handled by the configuration tool. 7074601490SBalint Dobszay 7174601490SBalint DobszayTarget description 7274601490SBalint Dobszay^^^^^^^^^^^^^^^^^^ 7374601490SBalint DobszayThe framework provides an API called STGT ('simple target') to describe the 7474601490SBalint Dobszaytargets, i.e. what is the build output, what source files are used, what 7574601490SBalint Dobszaylibraries are linked, etc. The API wraps the CMake target functions, and also 7674601490SBalint Dobszayextends the built-in functionality, it can use the setting groups described in 7774601490SBalint Dobszaythe previous section. A group can be applied onto a target, i.e. a collection of 7874601490SBalint Dobszaymacros, flags, etc. can be applied onto the given output executable/library. 7974601490SBalint DobszayThis provides a more granular way than the current Makefile system where most of 8074601490SBalint Dobszaythese are global and applied onto each target. 8174601490SBalint Dobszay 8274601490SBalint DobszayCompiler abstraction 8374601490SBalint Dobszay^^^^^^^^^^^^^^^^^^^^ 8474601490SBalint DobszayApart from the built-in CMake usage of the compiler, there are some common tasks 8574601490SBalint Dobszaythat CMake does not solve (e.g. preprocessing a file). For these tasks the 8674601490SBalint Dobszayframework uses wrapper functions instead of direct calls to the compiler. This 8774601490SBalint Dobszayway it is not tied to one specific compiler. 8874601490SBalint Dobszay 8974601490SBalint DobszayExternal tools 9074601490SBalint Dobszay^^^^^^^^^^^^^^ 9174601490SBalint DobszayIn the TF-A buildsystem some external tools are used, e.g. fiptool for image 9274601490SBalint Dobszaygeneration or dtc for device tree compilation. These tools have to be found 9374601490SBalint Dobszayand/or built by the framework. For this, the CMake find_package functionality is 9474601490SBalint Dobszayused, any other necessary tools can be added later. 9574601490SBalint Dobszay 9674601490SBalint DobszayWorkflow 9774601490SBalint Dobszay-------- 9874601490SBalint DobszayThe following diagram demonstrates the development workflow using the framework: 9974601490SBalint Dobszay 10074601490SBalint Dobszay|Framework workflow| 10174601490SBalint Dobszay 10274601490SBalint DobszayThe process can be split into two main phases: 10374601490SBalint Dobszay 10474601490SBalint DobszayIn the provisioning phase, first we have to obtain the necessary resources, i.e. 10574601490SBalint Dobszayclone the code repository and other dependencies. Next we have to do the 10674601490SBalint Dobszayconfiguration, preferably using a config tool like KConfig. 10774601490SBalint Dobszay 10874601490SBalint DobszayIn the development phase first we run CMake, which will generate the buildsystem 10974601490SBalint Dobszayusing the selected generator backend (currently only the Makefile generator is 11074601490SBalint Dobszaysupported). After this we run the selected build tool which in turn calls the 11174601490SBalint Dobszaycompiler, linker, packaging tool, etc. Finally we can run and debug the output 11274601490SBalint Dobszayexecutables. 11374601490SBalint Dobszay 11474601490SBalint DobszayUsually during development only the steps in this second phase have to be 11574601490SBalint Dobszayrepeated, while the provisioning phase needs to be done only once (or rarely). 11674601490SBalint Dobszay 11774601490SBalint DobszayExample 11874601490SBalint Dobszay------- 11974601490SBalint DobszayThis is a short example for the basic framework usage. 12074601490SBalint Dobszay 12174601490SBalint DobszayFirst, we create a setting group called *mem_conf* and fill it with several 12274601490SBalint Dobszayparameters. It is worth noting the difference between *CONFIG* and *DEFINE* 12374601490SBalint Dobszaytypes: the former is only a CMake domain option, the latter is only a C language 12474601490SBalint Dobszaymacro. 12574601490SBalint Dobszay 12674601490SBalint DobszayNext, we create a target called *fw1* and add the *mem_conf* setting group to 12774601490SBalint Dobszayit. This means that all source and header files used by the target will have all 12874601490SBalint Dobszaythe parameters declared in the setting group. Then we set the target type to 12974601490SBalint Dobszayexecutable, and add some source files. Since the target has the parameters from 13074601490SBalint Dobszaythe settings group, we can use it for conditionally adding source files. E.g. 13174601490SBalint Dobszay*dram_controller.c* will only be added if MEM_TYPE equals dram. 13274601490SBalint Dobszay 13374601490SBalint Dobszay.. code-block:: cmake 13474601490SBalint Dobszay 13574601490SBalint Dobszay group_new(NAME mem_conf) 13674601490SBalint Dobszay group_add(NAME mem_conf TYPE DEFINE KEY MEM_SIZE VAL 1024) 13774601490SBalint Dobszay group_add(NAME mem_conf TYPE CONFIG DEFINE KEY MEM_TYPE VAL dram) 13874601490SBalint Dobszay group_add(NAME mem_conf TYPE CFLAG KEY -Os) 13974601490SBalint Dobszay 14074601490SBalint Dobszay stgt_create(NAME fw1) 14174601490SBalint Dobszay stgt_add_setting(NAME fw1 GROUPS mem_conf) 14274601490SBalint Dobszay stgt_set_target(NAME fw1 TYPE exe) 14374601490SBalint Dobszay 14474601490SBalint Dobszay stgt_add_src(NAME fw1 SRC 14574601490SBalint Dobszay ${CMAKE_SOURCE_DIR}/main.c 14674601490SBalint Dobszay ) 14774601490SBalint Dobszay 14874601490SBalint Dobszay stgt_add_src_cond(NAME fw1 KEY MEM_TYPE VAL dram SRC 14974601490SBalint Dobszay ${CMAKE_SOURCE_DIR}/dram_controller.c 15074601490SBalint Dobszay ) 15174601490SBalint Dobszay 15274601490SBalint Dobszay.. |Framework structure| image:: 15374601490SBalint Dobszay ../resources/diagrams/cmake_framework_structure.png 15474601490SBalint Dobszay :width: 75 % 15574601490SBalint Dobszay 15674601490SBalint Dobszay.. |Framework workflow| image:: 15774601490SBalint Dobszay ../resources/diagrams/cmake_framework_workflow.png 15874601490SBalint Dobszay 15974601490SBalint Dobszay-------------- 16074601490SBalint Dobszay 161*ed3525e6SOlivier Deprez*Copyright (c) 2019-2024, Arm Limited and Contributors. All rights reserved.* 162