|
| 1 | +# Functions reference |
| 2 | + |
| 3 | +This document lists all the functions defined in the [`/cmake`](../blob/cmake_dev/cmake) folder in this project. |
| 4 | +It would be a good idea to add this folder to the "CMake search path" in your project : |
| 5 | + |
| 6 | +```cmake |
| 7 | +list(APPEND CMAKE_MODULE_PATH ${CORE_PATH}/cmake) |
| 8 | +``` |
| 9 | +(done by default in the [quickstart template](./Quickstart-guide#Quickstart-script).) |
| 10 | + |
| 11 | +All the functions are defined in a CMake module of the same name. Therefore, you have to include said module before calling the function: |
| 12 | + |
| 13 | +```cmake |
| 14 | +include(foo) |
| 15 | +foo() |
| 16 | +``` |
| 17 | + |
| 18 | +(Of course, this does _not_ apply to the CMake built-in functions as described in [Introduction to CMake](./Introduction-to-CMake#CMake-language). These can be called directly.) |
| 19 | + |
| 20 | +# Functions emulating the Arduino behavior |
| 21 | + |
| 22 | +## build_sketch |
| 23 | + |
| 24 | +__Syntax:__ |
| 25 | + |
| 26 | +```cmake |
| 27 | +build_sketch(TARGET <targetName> |
| 28 | + SOURCES <sourcefile...> |
| 29 | + [DEPENDS <dependency...>] |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +This is the main function that encapsulates most of the automation of Arduino. It should only be called _after_ `overall_settings` and `set_board`. |
| 34 | + |
| 35 | +- `targetName` is the name of the [binary target](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#binary-executables) that will be created as a result of this function; this is a handle that may be reused in later calls, to e.g., `insights()`. |
| 36 | +- `sourcefile...` is a list of sources files to be compiled. This does not comprise header files (`.h`). |
| 37 | + You are strongly advised to hard-code the list of files directly in the CMakeLists.txt. Using [wildcards](https://cmake.org/cmake/help/latest/command/file.html#glob) is possible, but not guaranteed to work in all cases. |
| 38 | +- the optional `dependencies` are targets, usually Arduino libraries such as created by `external_library()`, that the sketch depdends on. |
| 39 | + The CMake build model makes it awkward at best to automatically handle dependencies the way Arduino does, so they have to be specified explicitely. |
| 40 | + |
| 41 | +What this function does is the following: |
| 42 | + |
| 43 | +- Include the parts of Arduino_Core_STM32 needed for the project (core + right variant); |
| 44 | +- Preprocess the sources: turn the `.ino` files to `.cpp`; |
| 45 | +- Create the binary executable target; |
| 46 | +- Bind the target to its dependencies (core + "DEPENDS"); |
| 47 | +- Add post-build hooks to: |
| 48 | + - Display the size report message ("Sketch uses xxx bytes of program storage space. Maximum is yyy bytes..."; |
| 49 | + - Convert the binary to `.bin` and `.hex`. |
| 50 | + |
| 51 | +## external_library |
| 52 | + |
| 53 | +__Syntax:__ |
| 54 | + |
| 55 | +```cmake |
| 56 | +external_library(PATH <path_to_lib> |
| 57 | + [DEPENDS <dependency...>] |
| 58 | + [FORCE] |
| 59 | +) |
| 60 | +``` |
| 61 | + |
| 62 | +This function is used to add a third-party library in the build description. It takes as arguments: |
| 63 | +- the path to the library to parse; |
| 64 | +- as with `build_sketch()`, an optional list of dependencies of the library; |
| 65 | +- optionally, the `FORCE` keyword; read below about it. |
| 66 | + |
| 67 | +Most third-party libraries do not ship with a CMakeLists.txt. |
| 68 | +Therefore, one needs to be generated before that library can be used in a CMake build. |
| 69 | +This function wraps the generation of the CMakeLists.txt and the subsequent `add_subdirectory()` to actually add the library to the build. |
| 70 | + |
| 71 | +If no CMakeLists.txt is found, it is autogenerated by a Python script after reading `library.properties`. |
| 72 | +The `FORCE` keyword lets you override this behavior and overwrite the CMakeLists.txt unconditionally. |
| 73 | + |
| 74 | +In the default case, the autogenerated CMakeLists.txt will create for the library __a target with the same name as the folder name__. |
| 75 | +You can then use this target as a dependency of another library or of your sketch. |
| 76 | + |
| 77 | +Note: libraries built into Arduino_Core_STM32 are handled automatically; they do not need this function and can be used out-of-the-box. |
| 78 | +Such libraries include EEPROM, Servo, SPI, Mouse, or Wire. |
| 79 | +Libraries that are shipped with Arduino IDE (SD, Ethernet, ...) are not covered by this convenience. |
| 80 | + |
| 81 | +## insights |
| 82 | + |
| 83 | +__Syntax:__ |
| 84 | + |
| 85 | +```cmake |
| 86 | +insights(TARGET <targetName> |
| 87 | + [DIRECT_INCLUDES] |
| 88 | + [TRANSITIVE_INCLUDES] |
| 89 | + [SYMBOLS] |
| 90 | + [ARCHIVES] |
| 91 | + [LOGIC_STRUCTURE] |
| 92 | +) |
| 93 | +``` |
| 94 | + |
| 95 | +This function lets you produce insights about the build process. |
| 96 | +It uses Graphviz to generate SVG files in order to better understand what happens throughout the build: configuration, compilation, linking. |
| 97 | +The mandatory TARGET argument is the name of the element to analyze, usually your sketch target (crated with `build_sketch()`). |
| 98 | +Each insight keyword is optional; even when provided, they are not built by default, you have to explicitely generate them with the build tool (e.g., `ninja symbols.svg`). |
| 99 | + |
| 100 | +| Keyword | Generated file | Description | |
| 101 | +| :------ | :------------- | :---------- | |
| 102 | +| DIRECT_INCLUDES | direct_includes.svg | Shows which source files include which | |
| 103 | +| TRANSITIVE_INCLUDES | transitive_includes.svg | Shows which source files include which, both directly and indirectly (chained `#include`) | |
| 104 | +| SYMBOLS | symbols.svg | Shows which symbol pull which other(s), at link-time | |
| 105 | +| ARCHIVES | archives.svg | Shows which archive pull which other(s), at link-time | |
| 106 | +| LOGIC_STRUCTURE | logicstructure.svg | Shows the targets involved in the project, and the dependencies they share. This is a wrapper around a built-in feature of CMake, read about details [here](https://cmake.org/cmake/help/latest/module/CMakeGraphVizOptions.html). | |
| 107 | + |
| 108 | +Note: Enabling some of these graphs can hinder performances much (in the order of +50% build time). |
| 109 | + |
| 110 | +## overall_settings |
| 111 | +__Syntax:__ |
| 112 | + |
| 113 | +```cmake |
| 114 | +overall_settings( |
| 115 | + [STANDARD_LIBC] |
| 116 | + [PRINTF_FLOAT] |
| 117 | + [SCANF_FLOAT] |
| 118 | + [DEBUG_SYMBOLS] |
| 119 | + [LTO] |
| 120 | + [NO_RELATIVE_MACRO] |
| 121 | + [UNDEF_NDEBUG] |
| 122 | + [CORE_CALLBACK] |
| 123 | + [OPTIMIZATION <optLevel>] |
| 124 | + [BUILD_OPT <path_to_build.opt>] |
| 125 | + [DISABLE_HAL_MODULES <modules...>] |
| 126 | +) |
| 127 | +``` |
| 128 | + |
| 129 | +This function replaces, with additions, some of the options of the menu bar of Arduino IDE. |
| 130 | +All the keywords are optional; the default behavior is selected on fallback. The order of the keywords does not matter. |
| 131 | + |
| 132 | +Keywords that take no argument: |
| 133 | +- STANDARD_LIBC: switch to Newlib Standard as the runtime library, instead of Newlib Nano; |
| 134 | +- PRINTF_FLOAT: enable `%f` in `printf` (Newlib Nano only); |
| 135 | +- SCANF_FLOAT: enable `%f` in `scanf` (Newlib Nano only); |
| 136 | +- DEBUG_SYMBOLS: add `-g` to GCC (for debug); |
| 137 | +- LTO: enable Link-Time Optimizations (`-flto`); |
| 138 | +- NO_RELATIVE_MACRO: make `__FILE__` yield absolute paths; the default is the oppisite, for size and security reasons; |
| 139 | +- UNDEF_NDEBUG: do not define the `NDEBUG` macro; use this keyword for debug builds; |
| 140 | +- CORE_CALLBACK: define the `CORE_CALLBACK` macro; read about use cases [here](https://github.com/stm32duino/wiki/wiki/API#core-callback). |
| 141 | + |
| 142 | +Keywords that take a single argument: |
| 143 | +- OPTIMIZATION: takes an optimization level (one of `0123gs`); this will then be passed to GCC's as a `-O` flag; |
| 144 | +- BUILD_OPT: add the specified file to GCC's command line, as a `@file`. The file is usually named "build.opt", hence the keyword name. Note: with Arduino IDE, the file is taken by default into account; with CMake, it is not, only this keyword triggers this. |
| 145 | + |
| 146 | +Keyword that takes several arguments: |
| 147 | +- DISABLE_HAL_MODULES: lets you disable any unused HAL modules as an optimization. |
| 148 | + Read about it [on the wiki](https://github.com/stm32duino/wiki/wiki/HAL-configuration). |
| 149 | + The supported HAL modules are: |
| 150 | + - ADC |
| 151 | + - I2C |
| 152 | + - RTC |
| 153 | + - SPI |
| 154 | + - TIM |
| 155 | + - DAC |
| 156 | + - EXTI |
| 157 | + - ETH |
| 158 | + - SD |
| 159 | + - QSPI |
| 160 | + Disabling them all (when possible) yields a significant additional size gain. |
| 161 | + |
| 162 | +## set_board |
| 163 | + |
| 164 | +__Syntax:__ |
| 165 | + |
| 166 | +```cmake |
| 167 | +set_board(<boardName> |
| 168 | + [SERIAL <serialSetting>] |
| 169 | + [USB <usbSetting>] |
| 170 | + [XUSB <usbSpeed>] |
| 171 | + [VIRTIO <virtioSetting>] |
| 172 | +) |
| 173 | +``` |
| 174 | + |
| 175 | +This function replaces the options of the menu bar of Arduino IDE that are related to board-specific features. |
| 176 | +It manages to stay up-to-date with what Arduino IDE would offer by parsing `board.txt`/`platform.txt` as needed, |
| 177 | +and caches the result in a CMake-readable database. |
| 178 | + |
| 179 | +The first argument to this function must be the board codename, as used in boards.txt. |
| 180 | +E.g., "GENERIC_L010RBTX", not "Generic L010RBTx" (the latter being what the IDE displays). |
| 181 | +Then come any number of the four keywords, in any order. As with `overall_settings`, default values are used on fallback. |
| 182 | +Depending on your board, not all features may be meaningful. Note: the values for each keyword are case-sensitive! |
| 183 | +- SERIAL: Serial support. Usually one of `generic`, `disabled`, `none` (standing for "Enabled (no generic 'Serial')"); |
| 184 | +- USB: USB support. Usually one of `none`, `CDCgen`, `CDC`, `HID`; |
| 185 | +- XUSB: USB speed. Usually one of `FS`, `HS`, `HSFS`; |
| 186 | +- VIRTIO: virtIO support. Usually one of `disable`, `generic`, `enabled`. |
0 commit comments