1*84e33947SAndroid Build Coastguard Worker# CHRE Framework Build System 2*84e33947SAndroid Build Coastguard Worker 3*84e33947SAndroid Build Coastguard Worker[TOC] 4*84e33947SAndroid Build Coastguard Worker 5*84e33947SAndroid Build Coastguard WorkerThe CHRE build system is based on Make, and uses a set of Makefiles that allow 6*84e33947SAndroid Build Coastguard Workerbuilding the CHRE framework for a variety of hardware and software architectures 7*84e33947SAndroid Build Coastguard Workerand variants (e.g. different combinations of CPU and OS/underlying system). It 8*84e33947SAndroid Build Coastguard Workeris also flexible to different build toolchains (though LLVM/Clang or GCC are 9*84e33947SAndroid Build Coastguard Workerrecommended), by abstracting out key operations to a few Make or environment 10*84e33947SAndroid Build Coastguard Workervariables. While the CHRE framework source code may be integrated into another 11*84e33947SAndroid Build Coastguard Workerbuild system, it can be beneficial to leverage the existing build system to 12*84e33947SAndroid Build Coastguard Workerreduce maintenance burden when the CHRE framework is updated. Additionally, it 13*84e33947SAndroid Build Coastguard Workershould be possible to build nanoapps from the CHRE build system (to have 14*84e33947SAndroid Build Coastguard Workercommonality across devices), and the CHRE framework build shares configuration 15*84e33947SAndroid Build Coastguard Workerwith the nanoapp build. 16*84e33947SAndroid Build Coastguard Worker 17*84e33947SAndroid Build Coastguard WorkerBy default, the output of the build is linked into both a static archive 18*84e33947SAndroid Build Coastguard Worker`libchre.a` and a shared library `libchre.so`, though generally only one of the 19*84e33947SAndroid Build Coastguard Workertwo is used, depending on the target device details. 20*84e33947SAndroid Build Coastguard Worker 21*84e33947SAndroid Build Coastguard Worker## Design 22*84e33947SAndroid Build Coastguard Worker 23*84e33947SAndroid Build Coastguard WorkerThe CHRE build system was originally designed around the philosophy that a 24*84e33947SAndroid Build Coastguard Workervanilla invocation of `make` or `make all` should build any given nanoapp for 25*84e33947SAndroid Build Coastguard Workerall targets. This allows for efficient use of the job scheduler in the Make 26*84e33947SAndroid Build Coastguard Workerbuild system for multi-threaded builds and also promotes a level of separation 27*84e33947SAndroid Build Coastguard Workerof concerns between targets (this is not enforced by any Make language 28*84e33947SAndroid Build Coastguard Workerconstruct, merely convention). In practice, the CHRE build system is rarely used 29*84e33947SAndroid Build Coastguard Workerto build multiple targets with one invocation of Make. However, the design goal 30*84e33947SAndroid Build Coastguard Workeris carried forward for the variant support and separation of concerns between 31*84e33947SAndroid Build Coastguard Workertargets. 32*84e33947SAndroid Build Coastguard Worker 33*84e33947SAndroid Build Coastguard WorkerAll variant-specific compiler and linker flags are held under variables that 34*84e33947SAndroid Build Coastguard Workeronly apply to their specific target. This encourages developers to avoid leaking 35*84e33947SAndroid Build Coastguard Workerbuild details between targets. This is important because not all compiler or 36*84e33947SAndroid Build Coastguard Workerlinker flags are compatible with all toolchains. For example: if a target uses a 37*84e33947SAndroid Build Coastguard Workercompiler that does not support `-Wdouble-promotion`, but this were to be 38*84e33947SAndroid Build Coastguard Workerenforced for all builds, then by definition this target would not compatible 39*84e33947SAndroid Build Coastguard Workerwith the CHRE build. The goal is for the CHRE build to be as flexible as 40*84e33947SAndroid Build Coastguard Workerpossible. 41*84e33947SAndroid Build Coastguard Worker 42*84e33947SAndroid Build Coastguard Worker### Build Template 43*84e33947SAndroid Build Coastguard Worker 44*84e33947SAndroid Build Coastguard WorkerThe CHRE build system is implemented using template meta-programming techniques. 45*84e33947SAndroid Build Coastguard WorkerA build template is used to create Make rules for tasks that are common to all 46*84e33947SAndroid Build Coastguard Workertargets. This includes compiling C/C++/assembly sources, linking, nanoapp header 47*84e33947SAndroid Build Coastguard Workergeneration, etc. The rationale behind this approach is to reduce boilerplate 48*84e33947SAndroid Build Coastguard Workerwhen adding support for a new build target. 49*84e33947SAndroid Build Coastguard Worker 50*84e33947SAndroid Build Coastguard WorkerThe build template is located at `build/build_template.mk`, and is documented 51*84e33947SAndroid Build Coastguard Workerwith all the variables used to generate build targets, like `TARGET_CFLAGS`. 52*84e33947SAndroid Build Coastguard Worker 53*84e33947SAndroid Build Coastguard Worker## Build Targets (Variants) 54*84e33947SAndroid Build Coastguard Worker 55*84e33947SAndroid Build Coastguard WorkerCompiling the framework for different devices is done by specifying the build 56*84e33947SAndroid Build Coastguard Workertarget when invoking `make`. Conventionally, build targets consist of three 57*84e33947SAndroid Build Coastguard Workerparts: (software) vendor, architecture and variant and follow the 58*84e33947SAndroid Build Coastguard Worker`<vendor>_<arch>_<variant>` pattern. A “vendor” is typically the company that 59*84e33947SAndroid Build Coastguard Workercreated the CHRE implementation, which may bring with it some details related to 60*84e33947SAndroid Build Coastguard Workernanoapp compatibility, for example the Nanoapp Support Library from the same 61*84e33947SAndroid Build Coastguard Workervendor may be required. The “arch” field refers to the Instruction Set 62*84e33947SAndroid Build Coastguard WorkerArchitecture (ISA) and related compiler configuration to create a binary for the 63*84e33947SAndroid Build Coastguard Workertarget processor. The “variant” is primarily related to the underlying platform 64*84e33947SAndroid Build Coastguard Workersoftware that the CHRE framework builds on top of, such as the combination of 65*84e33947SAndroid Build Coastguard Workeroperating system and other software needed to select the appropriate combination 66*84e33947SAndroid Build Coastguard Workerof code in the `platform/` folder, but can also define other attributes of the 67*84e33947SAndroid Build Coastguard Workerbuild, such as the target memory region for the binary. If a vendor, 68*84e33947SAndroid Build Coastguard Workerarchitecture, or variant consist of multiple words or components, then they 69*84e33947SAndroid Build Coastguard Workershould be separated by a hyphen and not an underscore. 70*84e33947SAndroid Build Coastguard Worker 71*84e33947SAndroid Build Coastguard WorkerFor example, if we assume that a fictional company named Aperture developed its 72*84e33947SAndroid Build Coastguard Workerown CHRE framework implementation, targeting a CPU family called Potato, and a 73*84e33947SAndroid Build Coastguard Workercollection of platform software called GladOS/Cake, then a suitable build target 74*84e33947SAndroid Build Coastguard Workername would be `aperture_potato_glados-cake`. 75*84e33947SAndroid Build Coastguard Worker 76*84e33947SAndroid Build Coastguard WorkerThe build target may optionally have `_debug` appended, which is a common suffix 77*84e33947SAndroid Build Coastguard Workerwhich enables `-g` and any additional target-specific debug flags. 78*84e33947SAndroid Build Coastguard Worker 79*84e33947SAndroid Build Coastguard Worker### Creating a New Build Target 80*84e33947SAndroid Build Coastguard Worker 81*84e33947SAndroid Build Coastguard Worker#### Architecture Support 82*84e33947SAndroid Build Coastguard Worker 83*84e33947SAndroid Build Coastguard WorkerThe architecture-specific portion of the build deals with mainly the build 84*84e33947SAndroid Build Coastguard Workertoolchain, and its associated flags. 85*84e33947SAndroid Build Coastguard Worker 86*84e33947SAndroid Build Coastguard WorkerIt is easiest to check if the architecture is currently listed in `build/arch`, 87*84e33947SAndroid Build Coastguard Workerand if it is, _Hooray! You're (almost) done_. It is still worthwhile to quickly 88*84e33947SAndroid Build Coastguard Workerread through to know how the build is layered. 89*84e33947SAndroid Build Coastguard Worker 90*84e33947SAndroid Build Coastguard WorkerCHRE expects the build toolchain to be exported via Makefile variables, 91*84e33947SAndroid Build Coastguard Workerspecifically the compiler (`TARGET_CC`), archiver (`TARGET_AR`), and the linker 92*84e33947SAndroid Build Coastguard Worker(`TARGET_LD`). Architecture specific compiler and linker flags are passed in via 93*84e33947SAndroid Build Coastguard Workerthe `TARGET_CFLAGS` and `TARGET_LDFLAGS` respectively. Additional 94*84e33947SAndroid Build Coastguard Workerarchitecture-specific configuration is possible - refer to existing files under 95*84e33947SAndroid Build Coastguard Worker`build/arch` and `build/build_template.mk` for details. 96*84e33947SAndroid Build Coastguard Worker 97*84e33947SAndroid Build Coastguard Worker#### Build Target Makefile 98*84e33947SAndroid Build Coastguard Worker 99*84e33947SAndroid Build Coastguard WorkerMakefiles for each build target can be found at 100*84e33947SAndroid Build Coastguard Worker`build/variant/<target_name>.mk`. These files are included at the end of the 101*84e33947SAndroid Build Coastguard Workertop-level Makefile, and has the responsibility of collecting arguments for the 102*84e33947SAndroid Build Coastguard Workerbuild template and invoking it to instantiate build rules. This involves doing 103*84e33947SAndroid Build Coastguard Workersteps including (not an exhaustive listing): 104*84e33947SAndroid Build Coastguard Worker 105*84e33947SAndroid Build Coastguard Worker* Setting the target name and platform ID 106*84e33947SAndroid Build Coastguard Worker 107*84e33947SAndroid Build Coastguard Worker* Configuring (if needed) and including the apporpriate `build/arch/*.mk` file 108*84e33947SAndroid Build Coastguard Worker 109*84e33947SAndroid Build Coastguard Worker* Collecting sources and flags specific to the platform into 110*84e33947SAndroid Build Coastguard Worker `TARGET_VARIANT_SRCS` and `TARGET_CFLAGS` 111*84e33947SAndroid Build Coastguard Worker 112*84e33947SAndroid Build Coastguard Worker* Including `build/build_template.mk` to instantiate the build targets - this 113*84e33947SAndroid Build Coastguard Worker must be the last step, as the make targets cannot be modified once generated 114*84e33947SAndroid Build Coastguard Worker 115*84e33947SAndroid Build Coastguard WorkerRefer to existing files under `build/variant` for examples. 116*84e33947SAndroid Build Coastguard Worker 117*84e33947SAndroid Build Coastguard Worker## Device Variants 118*84e33947SAndroid Build Coastguard Worker 119*84e33947SAndroid Build Coastguard WorkerWhile the build target is primarily concerned with configuring the CHRE build 120*84e33947SAndroid Build Coastguard Workerfor a particular chipset, the same chipset can appear in multiple device 121*84e33947SAndroid Build Coastguard Workermodels/SKUs, potentially with different peripheral hardware, targeted levels of 122*84e33947SAndroid Build Coastguard Workerfeature support, etc. Additionally, a device/chip vendor may wish to provide 123*84e33947SAndroid Build Coastguard Workeradditional build customization outside of the Makefiles contained in the 124*84e33947SAndroid Build Coastguard Workersystem/chre project. The build system supports configuration at this level via 125*84e33947SAndroid Build Coastguard Workerthe device variant makefile, typically named `variant.mk`, which is injected 126*84e33947SAndroid Build Coastguard Workerinto the build by setting the `CHRE_VARIANT_MK_INCLUDES` environment variable 127*84e33947SAndroid Build Coastguard Workerwhen invoking the top-level Makefile. Refer to the file 128*84e33947SAndroid Build Coastguard Worker`variant/android/variant.mk` for an example. 129*84e33947SAndroid Build Coastguard Worker 130*84e33947SAndroid Build Coastguard Worker## Platform Sources 131*84e33947SAndroid Build Coastguard Worker 132*84e33947SAndroid Build Coastguard WorkerThe file at `platform/platform.mk` lists sources and flags needed to compile the 133*84e33947SAndroid Build Coastguard WorkerCHRE framework for each supported platform. These must be added to Make 134*84e33947SAndroid Build Coastguard Workervariables prefixed with the platform name (for example, `SIM_SRCS` for platform 135*84e33947SAndroid Build Coastguard Workersources used with the simulator build target), and not `COMMON_SRCS` or other 136*84e33947SAndroid Build Coastguard Workercommon variables, to avoid affecting other build targets. 137*84e33947SAndroid Build Coastguard Worker 138*84e33947SAndroid Build Coastguard Worker## Build Artifacts 139*84e33947SAndroid Build Coastguard Worker 140*84e33947SAndroid Build Coastguard WorkerAt the end of a successful build, the following are generated in the `out` 141*84e33947SAndroid Build Coastguard Workerdirectory: 142*84e33947SAndroid Build Coastguard Worker 143*84e33947SAndroid Build Coastguard Worker* `<build_target>/libchre.so` and `libchre.a`: the resulting CHRE framework 144*84e33947SAndroid Build Coastguard Worker binary, built as a dynamic/static library 145*84e33947SAndroid Build Coastguard Worker 146*84e33947SAndroid Build Coastguard Worker* `<build_target>_objs/`: Directory with object files and other intermediates 147*84e33947SAndroid Build Coastguard Worker 148*84e33947SAndroid Build Coastguard Worker* Depending on the build target, additional intermediates (e.g. `nanopb_gen` for 149*84e33947SAndroid Build Coastguard Worker files generated for use with NanoPB) 150