build_system.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. Build System
  2. ------------
  3. This document explains the Espressif IoT Development Framework build system and the
  4. concept of "components"
  5. Read this document if you want to know how to organise a new ESP-IDF project.
  6. We recommend using the esp-idf-template_ project as a starting point for your project.
  7. Overview
  8. ========
  9. An ESP-IDF project can be seen as an almagation of a number of components.
  10. For example, for a webserver that shows the current humidity, we would
  11. have:
  12. - The ESP32 base libraries (libc, rom bindings etc)
  13. - The WiFi drivers
  14. - A TCP/IP stack
  15. - The FreeRTOS operating system
  16. - A webserver
  17. - A driver for an humidity sensor
  18. - Main code tying it all together
  19. ESP-IDF makes these components explicit and configurable. To do that, when a project
  20. is compiled, the build environment will look up all the components in the
  21. ESP-IDF directories, the project directories and optionally custom other component
  22. directories. It then allows the user to configure compile-time options using
  23. a friendly text-based menu system to customize the ESP-IDF as well as other components
  24. to the requirements of the project. After the components are customized, the
  25. build process will compile everything into an output file, which can then be uploaded
  26. into a board in a way that can also be defined by components.
  27. A project in this sense is defined as a directory under which all the files required
  28. to build it live, excluding the ESP-IDF files and the toolchain. A simple project
  29. tree might look like this::
  30. - myProject/ - build/
  31. - components/ - component1/ - component.mk
  32. - Kconfig
  33. - src1.c
  34. - component2/ - component.mk
  35. - Kconfig
  36. - src1.c
  37. - main/ - src1.c
  38. - src2.c
  39. - Makefile
  40. As we can see, a project consists of a components/ subdirectory containing its
  41. components as well as one or more directories containing the project-specific
  42. sources; by default a single directory called 'main' is assumed. The project
  43. directory will also have a Makefile where the projects name as well as optionally
  44. other options are defined. After compilation, the project directory will contain
  45. a 'build'-directory containing all of the objects, libraries and other generated
  46. files as well as the final binary.
  47. Components also have a custom makefile - ``component.mk``. This contains various definititions
  48. influencing the build process of the component as well as the project it's used
  49. in. Components may also include a Kconfig file defining the compile-time options that are
  50. settable by means of the menu system.
  51. Project Makefile variables that can be set by the programmer::
  52. PROJECT_NAME: Mandatory. Name for the project
  53. BUILD_DIR_BASE: Set the directory where all objects/libraries/binaries end up in.
  54. Defaults to $(PROJECT_PATH)/build
  55. COMPONENT_DIRS: Search path for components. Defaults to the component/ directories
  56. in the ESP-IDF path and the project path.
  57. COMPONENTS: A list of component names. Defaults to all the component found in the
  58. COMPONENT_DIRS directory
  59. EXTRA_COMPONENT_DIRS: Defaults to unset. Use this to add directories to the default
  60. COMPONENT_DIRS.
  61. SRCDIRS: Directories under the project dir containing project-specific sources.
  62. Defaults to 'main'. These are treated as 'lite' components: they do not have
  63. include directories that are passed to the compilation pass of all components and
  64. they do not have a Kconfig option.
  65. Component-specific component.mk variables that can be set by the programmer::
  66. COMPONENT_ADD_INCLUDEDIRS: Relative path to include directories to be added to
  67. the entire project. If an include directory is only needed to compile this
  68. specific component, don't add it here.
  69. COMPONENT_PRIV_INCLUDEDIRS: Relative path to include directories that are only used
  70. when compiling this specific component.
  71. COMPONENT_DEPENDS: Names of any components that need to be compiled before this component.
  72. COMPONENT_ADD_LDFLAGS: LD flags to add for the entire project. Defaults to -l$(COMPONENT_NAME).
  73. Add libraries etc in the current directory as $(abspath libwhatever.a)
  74. COMPONENT_EXTRA_INCLUDES: Any extra include paths used when compiling the component's
  75. source files. These will be prefixed with '-I' and passed to the compiler.
  76. Similar to COMPONENT_PRIV_INCLUDEDIRS, but these paths are passed as-is instead of
  77. expanded relative to the component directory.
  78. COMPONENT_SRCDIRS: Relative directories to look in for sources. Defaults to '.', the current
  79. directory (the root of the component) only. Use this to specify any subdirectories. Note
  80. that specifying this overwrites the default action of compiling everything in the
  81. components root dir; to keep this behaviour please also add '.' as a directory in this
  82. list.
  83. COMPONENT_OBJS: Object files to compile. Defaults to the .o variants of all .c and .S files
  84. that are found in COMPONENT_SRCDIRS.
  85. COMPONENT_EXTRA_CLEAN: Files that are generated using rules in the components Makefile
  86. that also need to be cleaned
  87. COMPONENT_BUILDRECIPE: Recipe to build the component. Optional. Defaults to building all
  88. COMPONENT_OBJS and linking them into lib(componentname).a
  89. COMPONENT_CLEANRECIPE: Recipe to clean the component. Optional. Defaults to removing
  90. all built objects and libraries.
  91. COMPONENT_BUILD_DIR: Equals the cwd of the component build, which is the build dir
  92. of the component (where all the .o etc files should be created).
  93. These variables are already set early on in the Makefile and the values in it will
  94. be usable in component or project Makefiles::
  95. CC, LD, AR, OBJCOPY: Xtensa gcc tools
  96. HOSTCC, HOSTLD etc: Host gcc tools
  97. LDFLAGS, CFLAGS: Set to usable values as defined in ESP-IDF Makefile
  98. PROJECT_NAME: Name of the project, as set in project makefile
  99. PROJECT_PATH: Path to the root of the project folder
  100. COMPONENTS: Name of the components to be included
  101. CONFIG_*: All values set by 'make menuconfig' have corresponding Makefile variables.
  102. Inside your component's component.mk makefile, you can override or add to these variables
  103. as necessary. The changes are isolated from other components (see Makefile.projbuild below
  104. if you want to share these changes with all other components.)
  105. For components, there also are these defines::
  106. COMPONENT_PATH: Absolute path to the root of the source tree of the component we're
  107. compiling
  108. COMPONENT_LIBRARY: The full path to the static library the components compilation pass
  109. is supposed to generate
  110. Make Process
  111. ------------
  112. The Make process is always invoked from the project directory by the
  113. user; invoking it anywhere else gives an error. This is what happens if
  114. we build a binary:
  115. The Makefile first determines how it was included. It figures out
  116. various paths as well as the components available to it. It will also
  117. collect the ldflags and includes that the components specify they need.
  118. It does this by running a dummy make on the components with a "get_variable"
  119. target that will output these values.
  120. The Makefile will then create targets to build the lib*.a libraries of
  121. all components and make the elf target depend on this. The main Makefile
  122. invokes Make on the componen.mk of each components inside a sub-mke: this way
  123. the components have full freedom to do whatever is necessary to build
  124. the library without influencing other components. By default, the
  125. component.mk includes the utility makefile $(IDF_PATH)/make/component_common.mk.
  126. This provides default targets and configurations that will work
  127. out-of-the-box for most projects.
  128. KConfig
  129. -------
  130. Each component can also have a Kconfig file, alongside the component.mk, that contains
  131. details to add to "menuconfig" for this component.
  132. Makefile.projbuild
  133. ------------------
  134. For components that have parts that need to be evaluated in the top-level
  135. project context, you can create a file called Makefile.projbuild in the
  136. component root directory. These files is included into the project's
  137. top-level Makefile.
  138. For example, if your component needs to add to CFLAGS for the entire
  139. project (not just for its own source files) then you can set
  140. ``CFLAGS +=`` in Makefile.projbuild. Note that this isn't necessary for
  141. adding include directories to the project, you can set
  142. ``COMPONENT_ADD_INCLUDEDIRS`` (see above) in the component.mk.
  143. KConfig.projbuild
  144. -----------------
  145. There's an equivalent to Makefile.projbuild for KConfig: if you want to include
  146. options at the top-level, not inside the 'components' submenu then create a Kconfig.projbuild and
  147. it will be included in the main menu of menuconfig.
  148. Take good care when (re)defining stuff here: because it's included with all the other
  149. .projbuild files, it's possible to overwrite variables or re-declare targets defined in
  150. the ESP-IDF makefile/Kconfig and other .projbuild files. It's generally better to just
  151. create a KConfig file, if you can.
  152. Writing Component Makefiles
  153. ---------------------------
  154. A component consists of a directory which doubles as the name for the
  155. component: a component named 'httpd' lives in a directory called 'httpd'
  156. Because components usually live under the project directory (although
  157. they can also reside in an other folder), the path to this may be
  158. something like /home/myuser/projects/myprojects/components/httpd .
  159. Components can have any name (unique to the project) but the name
  160. cannot contain spaces (esp-idf does not support spaces in paths).
  161. One of the things that most components will have is a component.mk makefile,
  162. containing instructions on how to build the component. Because the
  163. build environment tries to set reasonable defaults that will work most
  164. of the time, component.mk can be very small.
  165. Simplest component.mk
  166. =====================
  167. At the minimum, component.mk will just include the ESP-IDF component "common" makefile,
  168. which adds common component functionality::
  169. include $(IDF_PATH)/make/component_common.mk
  170. This will take all the .c and .S files in the component root and compile
  171. them into object files, finally linking them into a library.
  172. Adding source directories
  173. =========================
  174. By default, subdirectories are ignored. If your project has sources in subdirectories
  175. instead of in the root of the component then you can tell that to the build
  176. system by setting COMPONENT_SRCDIRS::
  177. COMPONENT_SRCDIRS := src1 src2
  178. include $(IDF_PATH)/make/component_common.mk
  179. This will compile all source files in the src1/ and src2/ subdirectories
  180. instead.
  181. Specifying source files
  182. =======================
  183. The standard component.mk logic adds all .S and .c files in the source
  184. directories as sources to be compiled unconditionally. It is possible
  185. to circumvent that logic and hardcode the objects to be compiled by
  186. manually setting the COMPONENT_OBJS variable to the name of the
  187. objects that need to be generated::
  188. COMPONENT_OBJS := file1.o file2.o thing/filea.o thing/fileb.o anotherthing/main.o
  189. include $(IDF_PATH)/make/component_common.mk
  190. Adding conditional configuration
  191. ================================
  192. The configuration system can be used to conditionally compile some files
  193. dependending on the options selected in ``make menuconfig``:
  194. Kconfig::
  195. config FOO_ENABLE_BAR
  196. bool "Enable the BAR feature."
  197. help
  198. This enables the BAR feature of the FOO component.
  199. Makefile::
  200. COMPONENT_OBJS := foo_a.o foo_b.o $(if $(CONFIG_FOO_ENABLE_BAR),foo_bar.o foo_bar_interface.o)
  201. include $(IDF_PATH)/make/component_common.mk
  202. Source Code Generation
  203. ======================
  204. Some components will have a situation where a source file isn't supplied
  205. with the component itself but has to be generated from another file. Say
  206. our component has a header file that consists of the converted binary
  207. data of a BMP file, converted using a hypothetical tool called bmp2h. The
  208. header file is then included in as C source file called graphics_lib.c::
  209. COMPONENT_EXTRA_CLEAN := logo.h
  210. graphics_lib.o: logo.h
  211. logo.h: $(COMPONENT_PATH)/logo.bmp
  212. bmp2h -i $^ -o $@
  213. include $(IDF_PATH)/make/component_common.mk
  214. In this example, graphics_lib.o and logo.h will be generated in the
  215. current directory (the build directory) while logo.bmp comes with the
  216. component and resides under the component path. Because logo.h is a
  217. generated file, it needs to be cleaned when make clean is called which
  218. why it is added to the COMPONENT_EXTRA_CLEAN variable.
  219. Cosmetic Improvements
  220. =====================
  221. The above example will work just fine, but there's one last cosmetic
  222. improvement that can be done. The make system tries to make the make
  223. process somewhat easier on the eyes by hiding the commands (unless you
  224. run make with the V=1 switch) and this does not do that yet. Here's an
  225. improved version that will output in the same style as the rest of the
  226. make process::
  227. COMPONENT_EXTRA_CLEAN := test_tjpgd_logo.h
  228. graphics_lib.o: logo.h
  229. logo.h: $(COMPONENT_PATH)/logo.bmp
  230. $(summary) BMP2H $@
  231. $(Q) bmp2h -i $^ -o $@
  232. include $(IDF_PATH)/make/component_common.mk
  233. Fully Overriding The Component Makefile
  234. ---------------------------------------
  235. Obviously, there are cases where all these recipes are insufficient for a
  236. certain component, for example when the component is basically a wrapper
  237. around another third-party component not originally intended to be
  238. compiled under this build system. In that case, it's possible to forego
  239. the build system entirely by setting COMPONENT_OWNBUILDTARGET and
  240. possibly COMPONENT_OWNCLEANTARGET and defining your own build- and clean
  241. target. The build target can do anything as long as it creates
  242. $(COMPONENT_LIBRARY) for the main file to link into the project binary,
  243. and even that is not necessary: if the COMPONENT_ADD_LDFLAGS variable
  244. is set, the component can instruct the linker to do anything else as well.
  245. .. _esp-idf-template: https://github.com/espressif/esp-idf-template