PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/reference/buildsystem.md

https://gitlab.com/b_perraudin_catie/yotta
Markdown | 275 lines | 204 code | 71 blank | 0 comment | 0 complexity | e6d732806f397bcf23a6f2276b10f066 MD5 | raw file
  1. ---
  2. layout: default
  3. title: yotta Configuration System Reference
  4. section: reference/buildsystem
  5. ---
  6. # Build System Reference
  7. yotta defines the way that software builds in order to make it easier for
  8. separate modules to work together. It has both a simple automatic build system
  9. that will build modules based on the source files discovered in the `source`
  10. directory, and the capability to support building modules of any complexity
  11. using [CMake](http://cmake.org).
  12. ## <a href="#info" name="info">#</a> Information Available To Builds
  13. yotta makes some useful information available to the modules being compiled, which
  14. can be embedded in the built binary or otherwise used at compile time. The
  15. majority of this information is defined by yotta's [configuration
  16. system](/reference/config.html), but some other information is also available.
  17. ### Information Always Available
  18. The name of the library being built by the current module is available as
  19. `YOTTA_MODULE_NAME`, as if it were defined:
  20. ```
  21. #define YOTTA_MODULE_NAME modulename-unquoted
  22. ```
  23. No header needs to be included for this definition to be available.
  24. Use the [preprocessor stringification
  25. trick](https://gcc.gnu.org/onlinedocs/cpp/Stringification.html) to get the
  26. module name as a string, if desired. Note that this definition is **not**
  27. currently available when compiling tests, and there are other circumstances
  28. where using custom CMake can make it unavailable.
  29. ### Information Available in the Build Info Header
  30. If the yotta build information header is included, then you can also access
  31. other information. Note that this header changes with every build (as it
  32. includes a build timestamp and unique ID), so do not include it unnecessarily.
  33. To include the yotta build information header:
  34. ```C
  35. #include YOTTA_BUILD_INFO_HEADER
  36. ```
  37. This header defines the following:
  38. ```C
  39. #define YOTTA_BUILD_YEAR 2015 // the current year, UTC
  40. #define YOTTA_BUILD_MONTH 9 // the current month, 1-12, UTC
  41. #define YOTTA_BUILD_DAY 16 // the current day of the month, 1-31, UTC
  42. #define YOTTA_BUILD_HOUR 18 // UTC hour 0-23
  43. #define YOTTA_BUILD_MINUTE 16 // UTC minute 0-59
  44. #define YOTTA_BUILD_SECOND 47 // UTC second 0-61 (1)
  45. #define YOTTA_BUILD_UUID 1f37f267-f31b-48c0-bfdd-2a7a5449817b // a uuid representing the build (2)
  46. ```
  47. If yotta finds a mercurial or git repository in the module or application being
  48. built, then the following will also be defined in this header:
  49. ```C
  50. #define YOTTA_BUILD_VCS_ID 0123456789abcdef // git or mercurial hash, variable length up to 40 characters
  51. #define YOTTA_BUILD_VCS_CLEAN 1 // 1 if there were no uncommitted changes, else 0
  52. #define YOTTA_BUILD_VCS_DESCRIPTION v0.5-57-gad36348 // git describe or mercurial equivalent
  53. ```
  54. Corresponding definitions for all of the build information are always available
  55. in CMake without including any files.
  56. Notes:
  57. * (1) Leap seconds will not currently occur in the _SECOND value, but they may do
  58. in future, so allow for the possibility of values up to and including 61 here.
  59. * (2) The build UUID changes every time that yotta build is invoked, even if the
  60. build would otherwise be identical.
  61. ## <a href="#automatic" name="automatic">#</a> Automatic Build System
  62. yotta will automatically build the contents of the `source` and `test`
  63. subdirectories of a software module. If you wan to exclude files from being
  64. picked up by this build system then you can add them to a
  65. [`.yotta_ignore`](/reference/ignore.html) file placed at the top of the module.
  66. Any files in the source directory, and any of its subdirectories, will be
  67. compiled into a single static library (for normal modules), or into the
  68. application (for an executable module).
  69. Any source files at the top-level of the test directory will be compiled into
  70. separate test executables, and the (recursive) contents of any subdirectories
  71. will each be compiled into a single test executable. You can use the
  72. `yotta test` subcommand to build and run these tests.
  73. Files in any other directories are normally ignored by yotta. By convention,
  74. public header files that a module exposes are placed in a subdirectory with the
  75. same name as the module, and then should be included as:
  76. ```C
  77. #include "modulename/headername.h"
  78. ```
  79. ## <a href="#custom-cmake" name="custom-cmake">#</a> Using Custom CMake to Control The Build
  80. To override yotta's default build rules for the `source` and `test`
  81. directories, place your own CMakeLists.txt file in these directories. yotta
  82. will also ensure that any CMakeLists.txt file in any other top-level
  83. subdirectory of your module is included in the build. The testing with yotta
  84. guide explains how to make yotta aware of any tests you add manually so that
  85. `yotta test` can run them.
  86. yotta will also respect a CMakeLists.txt file at the top-level of your module.
  87. If this file is detected, then yotta will not automatically generate any build
  88. rules. This is useful if you're adding yotta support to a module with an
  89. existing build system, especially if the build system already uses CMake.
  90. To ensure that yotta can automatically link your module to other modules, make
  91. sure you define exactly one library with the same name as your module. A custom
  92. build system may also define other build artifacts. In this case take care to
  93. ensure that you name them in a way that minimizes the likelihood of name
  94. collisions with other modules.
  95. ### <a href="#cmakelists" name="cmakelists">#</a> Places CMake Rules Can be Defined
  96. There are various places you can define CMake rules to control the build, these
  97. each have different effects:
  98. * **`./CMakeLists.txt` (in the module root)**: if you define a
  99. `CMakeLists.txt` file in the root of your module or executable, then yotta
  100. completely delegates building your module to this file.
  101. * **`./source/CMakeLists.txt`**: defining a `CMakeLists.txt` file in the
  102. source directory replaces yotta's default build rules for your library or
  103. executable, but yotta will still generate default rules for yout test
  104. directory (if any).
  105. * **`./source/<anything>.cmake`**: any .cmake files found in the source
  106. directory will be included at the *end* of the yotta-generated build rules
  107. for the source directory. If you want to make a very simple modification
  108. (such as definining a new preprocessor macro that your module needs), then
  109. this is the best way to do it.
  110. * **`./test/CMakeLists.txt`**: defining a `CMakeLists.txt` file in the
  111. test directory replaces yotta's default build rules for your tests. yotta
  112. will build your library or executable from the contents of the source
  113. directory as normal.
  114. * **`./<anything>/CMakeLists.txt`**: Any subdirectory with a `CMakeLists.txt`
  115. file will be included in the build (unless it is ignored in the
  116. .yotta_ignore file). There aren't very many good reasons to do this.
  117. ### <a href="#cmake-examples" name="cmake-examples">#</a> Custom CMake Examples
  118. All the following examples are using standard [CMake](http://cmake.org) syntax.
  119. For documentation on the commands used, pleas see the [CMake
  120. docs](https://cmake.org/documentation/).
  121. General tips for writing CMake:
  122. * Always wrap expanded `"${VARIABLES}"` in quotes (or expand them inside a
  123. quoted string), if they are unquoted then any spaces in the expanded
  124. variable will cause it to be split into separate arguments
  125. * Where possible, avoid overriding yotta's generated CMakeLists.txt, and use
  126. the automatically included `.cmake` files to modify what yotta defined
  127. instead.
  128. #### <a href="#generating-files" name="generating-files">#</a> Generating Files
  129. If you have a script `./scripts/munge.py <input> <output>` that you want to run on an input file
  130. `./resources/input.data` to generate an output C file to include in the build,
  131. you can use a custom CMake file like this:
  132. `./source/CMakeLists.txt`
  133. ```cmake
  134. # construct the output path for our generated file (in the build directory, so
  135. # that it gets removed by `yotta clean`):
  136. set(MYMODULE_GENERATED_FILES ${CMAKE_BINARY_DIR}/generated/mymodule)
  137. # ensure that the directory for the generated file exists:
  138. file(MAKE_DIRECTORY "${MYMODULE_GENERATED_FILES}")
  139. # save the paths to the script, input and output files, for convenience:
  140. set(MYMODULE_MUNGE_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/../scripts/munge.py")
  141. set(MYMODULE_MUNGE_INPUT "${CMAKE_CURRENT_LIST_DIR}/../resources/input.data")
  142. set(MYMODULE_MUNGE_OUTPUT "${MYMODULE_GENERATED_FILES}/generated.c")
  143. # define the command to generate this file
  144. add_custom_command(
  145. OUTPUT "${MYMODULE_GENERATED_FILES}/generated.c"
  146. DEPENDS "${MYMODULE_MUNGE_SCRIPT}"
  147. "${MYMODULE_MUNGE_INPUT}"
  148. COMMAND python "${MYMODULE_MUNGE_SCRIPT}" "${MYMODULE_MUNGE_INPUT}" "${MYMODULE_MUNGE_OUTPUT}"
  149. COMMENT "Munging input into generated.c"
  150. )
  151. # define the library for this module, using the generated file:
  152. add_library(mymodule # your module must create a library with its own name
  153. sourcefile1.c
  154. sourcefile2.c
  155. "${MYMODULE_GENERATED_FILES}/generated.c"
  156. )
  157. # link against the module's dependencies
  158. target_link_libraries(mymodule
  159. mydependency
  160. myotherdependency
  161. )
  162. ```
  163. Note that as we're replacing the yotta-generated CMakeLists for the source
  164. directory, you need to make sure you're still linking against all of your
  165. module's dependencies
  166. #### <a href="#changing-flags" name="changing-flags">#</a> Changing the Compilation Flags for a module
  167. You can use a `.cmake` file to change the link flags of an existing target
  168. without having to redefine the automatically generated build rules. For
  169. example, if your module is called `mymodule`, you could add this:
  170. `./source/override_flags.cmake`:
  171. ```CMake
  172. # add -funroll loops to the compile commands used for the sources in this
  173. # module... loops deserve some fun too!
  174. set_target_properties(mymodule COMPILE_FLAGS "-funroll-loops")
  175. ```
  176. Note that here "mymodule" is the name of the static library that your module is
  177. generating (the CMake "target" that it defines  nothing to do with the yotta
  178. target). By convention all yotta modules produce a static library with the same
  179. name as the module
  180. For documentation on the other things that you can set with
  181. set_target_properties, including preprocessor definitions, see the [CMake
  182. docs](https://cmake.org/cmake/help/v3.0/command/set_target_properties.html).
  183. #### <a href="#linking-external" name="linking-external">#</a> Linking an External Library
  184. `./source/link_foo.cmake`:
  185. ```CMake
  186. # link ./precompiled/foo.a into mymodule, in addition to its yotta
  187. # dependencies:
  188. target_link_libraries(mymodule "${CMAKE_CURRENT_LIST_DIR}/../precompiled/foo.a")
  189. ```
  190. ### <a href="#cmake-definitions" name="cmake-definitions">#</a> Definitions Available in CMake Lists
  191. yotta makes all the definitions which are available to the preprocessor
  192. available to CMake scripts (including, for example, the path to the build
  193. information header, and the definitions derived from the config information).
  194. In addition, several other definitions are available, including:
  195. * `YOTTA_CONFIG_MERGED_JSON_FILE`: This expands to the full path of a file
  196. where the current yotta config information has been written. If you want to
  197. use the config information for advanced pre-build steps (such as including
  198. portions of it in the executable in a parseable form), then this is the file
  199. you should read the config information from.
  200. ## <a href="#build-products" name="build-products">#</a> Build Products
  201. Everything that yotta generates during the build is created within the `build`
  202. subdirectory. Within this directory build products are further divided by the
  203. name of the [target](tutorial/targets.html) being built. This makes it safe to
  204. switch between building for different targets without cleaning.