build.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. One of the biggest challenges to getting started with embedded devices is that you
  2. just can't install a copy of Linux and expect to be able to compile a firmware.
  3. Even if you did remember to install a compiler and every development tool offered,
  4. you still wouldn't have the basic set of tools needed to produce a firmware image.
  5. The embedded device represents an entirely new hardware platform, which is
  6. incompatible with the hardware on your development machine, so in a process called
  7. cross compiling you need to produce a new compiler capable of generating code for
  8. your embedded platform, and then use it to compile a basic Linux distribution to
  9. run on your device.
  10. The process of creating a cross compiler can be tricky, it's not something that's
  11. regularly attempted and so the there's a certain amount of mystery and black magic
  12. associated with it. In many cases when you're dealing with embedded devices you'll
  13. be provided with a binary copy of a compiler and basic libraries rather than
  14. instructions for creating your own -- it's a time saving step but at the same time
  15. often means you'll be using a rather dated set. Likewise, it's also common to be
  16. provided with a patched copy of the Linux kernel from the board or chip vendor,
  17. but this is also dated and it can be difficult to spot exactly what has been
  18. changed to make the kernel run on the embedded platform.
  19. \subsection{Building an image}
  20. OpenWrt takes a different approach to building a firmware, downloading, patching
  21. and compiling everything from scratch, including the cross compiler. Or to put it
  22. in simpler terms, OpenWrt doesn't contain any executables or even sources, it's an
  23. automated system for downloading the sources, patching them to work with the given
  24. platform and compiling them correctly for the platform. What this means is that
  25. just by changing the template, you can change any step in the process.
  26. As an example, if a new kernel is released, a simple change to one of the Makefiles
  27. will download the latest kernel, patch it to run on the embedded platform and produce
  28. a new firmware image -- there's no work to be done trying to track down an unmodified
  29. copy of the existing kernel to see what changes had been made, the patches are
  30. already provided and the process ends up almost completely transparent. This doesn't
  31. just apply to the kernel, but to anything included with OpenWrt -- It's this one
  32. simple understated concept which is what allows OpenWrt to stay on the bleeding edge
  33. with the latest compilers, latest kernels and latest applications.
  34. So let's take a look at OpenWrt and see how this all works
  35. \subsubsection{Download openwrt}
  36. This article refers to the "Kamikaze" branch of OpenWrt, which can be downloaded via
  37. subversion using the following command:
  38. \begin{Verbatim}
  39. svn co https://svn.openwrt.org/openwrt/trunk kamikaze
  40. \end{Verbatim}
  41. Additionally, there's a trac interface on \href{https://dev.openwrt.org/}{https://dev.openwrt.org/}
  42. which can be used to monitor svn commits and browse the sources.
  43. \subsubsection{The directory structure}
  44. There are four key directories in the base:
  45. \begin{itemize}
  46. \item tools
  47. \item toolchain
  48. \item package
  49. \item target
  50. \end{itemize}
  51. \texttt{tools} and \texttt{toolchain} refer to common tools which will be
  52. used to build the firmware image and the compiler and c library.
  53. The result of this is three new directories, \texttt{tool\_build}, which is a temporary
  54. directory for building the target independent tools, \texttt{toolchain\_build\_\textit{<arch>}}
  55. which is used for building the toolchain for a specific architecture, and
  56. \texttt{staging\_dir\_\textit{<arch>}} where the resulting toolchain is installed.
  57. You won't need to do anything with the toolchain directory unless you intend to
  58. add a new version of one of the components above.
  59. \texttt{package} is for exactly that -- packages. In an OpenWrt firmware, almost everything
  60. is an \texttt{.ipk}, a software package which can be added to the firmware to provide new
  61. features or removed to save space.
  62. \texttt{target} refers to the embedded platform, this contains items which are specific to
  63. a specific embedded platform. Of particular interest here is the "\texttt{target/linux}"
  64. directory which is broken down by platform and contains the kernel config and patches
  65. to the kernel for a particular platform. There's also the "\texttt{target/image}" directory
  66. which describes how to package a firmware for a specific platform.
  67. Both the target and package steps will use the directory "\texttt{build\_\textit{<arch>}}"
  68. as a temporary directory for compiling. Additionally, anything downloaded by the toolchain,
  69. target or package steps will be placed in the "\texttt{dl}" directory.
  70. \subsubsection{Building OpenWrt}
  71. While the OpenWrt build environment was intended mostly for developers, it also has to be
  72. simple enough that an inexperienced end user can easily build his or her own customized firmware.
  73. Running the command "\texttt{make menuconfig}" will bring up OpenWrt's configuration menu
  74. screen, through this menu you can select which platform you're targeting, which versions of
  75. the toolchain you want to use to build and what packages you want to install into the
  76. firmware image. Similar to the linux kernel config, almost every option has three choices,
  77. \texttt{y/m/n} which are represented as follows:
  78. \begin{itemize}
  79. \item{\texttt{<*>} (pressing y)} \\
  80. This will be included in the firmware image
  81. \item{\texttt{<M>} (pressing m)} \\
  82. This will be compiled but not included (for later install)
  83. \item{\texttt{< >} (pressing n)} \\
  84. This will not be compiled
  85. \end{itemize}
  86. After you've finished with the menu configuration, exit and when prompted, save your
  87. configuration changes. To begin compiling the firmware, type "\texttt{make}". By default
  88. OpenWrt will only display a high level overview of the compile process and not each individual
  89. command.
  90. \subsubsection{Example:}
  91. \begin{Verbatim}
  92. make[2] toolchain/install
  93. make[3] -C toolchain install
  94. make[2] target/compile
  95. make[3] -C target compile
  96. make[4] -C target/utils prepare
  97. [...]
  98. \end{Verbatim}
  99. This makes it easier to monitor which step it's actually compiling and reduces the amount
  100. of noise caused by the compile output. To see the full output, run the command
  101. "\texttt{make V=99}".
  102. During the build process, buildroot will download all sources to the "\texttt{dl}"
  103. directory and will start patching and compiling them in the "\texttt{build\_\textit{<arch>}}"
  104. directory. When finished, the resulting firmware will be in the "\texttt{bin}" directory
  105. and packages will be in the "\texttt{bin/packages}" directory.
  106. \subsection{Creating packages}
  107. One of the things that we've attempted to do with OpenWrt's template system is make it
  108. incredibly easy to port software to OpenWrt. If you look at a typical package directory
  109. in OpenWrt you'll find two things:
  110. \begin{itemize}
  111. \item \texttt{package/\textit{<name>}/Makefile}
  112. \item \texttt{package/\textit{<name>}/patches}
  113. \end{itemize}
  114. The patches directory is optional and typically contains bug fixes or optimizations to
  115. reduce the size of the executable. The package makefile is the important item, provides
  116. the steps actually needed to download and compile the package.
  117. Looking at one of the package makefiles, you'd hardly recognize it as a makefile.
  118. Through what can only be described as blatant disregard and abuse of the traditional
  119. make format, the makefile has been transformed into an object oriented template which
  120. simplifies the entire ordeal.
  121. Here for example, is \texttt{package/bridge/Makefile}:
  122. \begin{Verbatim}[frame=single,numbers=left]
  123. include $(TOPDIR)/rules.mk
  124. PKG_NAME:=bridge
  125. PKG_VERSION:=1.0.6
  126. PKG_RELEASE:=1
  127. PKG_BUILD_DIR:=$(BUILD_DIR)/bridge-utils-$(PKG_VERSION)
  128. PKG_SOURCE:=bridge-utils-$(PKG_VERSION).tar.gz
  129. PKG_SOURCE_URL:=@SF/bridge
  130. PKG_MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd
  131. PKG_CAT:=zcat
  132. include $(INCLUDE_DIR)/package.mk
  133. define Package/bridge
  134. SECTION:=base
  135. CATEGORY:=Network
  136. DEFAULT:=y
  137. TITLE:=Ethernet bridging configuration utility
  138. URL:=http://bridge.sourceforge.net/
  139. endef
  140. define Package/bridge/description
  141. Ethernet bridging configuration utility
  142. Manage ethernet bridging; a way to connect networks together
  143. to form a larger network.
  144. endef
  145. define Build/Configure
  146. $(call Build/Configure/Default, \
  147. --with-linux-headers=$(LINUX_DIR))
  148. endef
  149. define Package/bridge/install
  150. install -m0755 -d $(1)/usr/sbin
  151. install -m0755 $(PKG_BUILD_DIR)/brctl/brctl \
  152. $(1)/usr/sbin/
  153. endef
  154. $(eval $(call BuildPackage,bridge))
  155. \end{Verbatim}
  156. As you can see, there's not much work to be done; everything is hidden in other makefiles
  157. and abstracted to the point where you only need to specify a few variables.
  158. \begin{itemize}
  159. \item \texttt{PKG\_NAME} \\
  160. The name of the package, as seen via menuconfig and ipkg
  161. \item \texttt{PKG\_VERSION} \\
  162. The upstream version number that we're downloading
  163. \item \texttt{PKG\_RELEASE} \\
  164. The version of this package Makefile
  165. \item \texttt{PKG\_BUILD\_DIR} \\
  166. Where to compile the package
  167. \item \texttt{PKG\_SOURCE} \\
  168. The filename of the original sources
  169. \item \texttt{PKG\_SOURCE\_URL} \\
  170. Where to download the sources from
  171. \item \texttt{PKG\_MD5SUM} \\
  172. A checksum to validate the download
  173. \item \texttt{PKG\_CAT} \\
  174. How to decompress the sources (zcat, bzcat, unzip)
  175. \end{itemize}
  176. The \texttt{PKG\_*} variables define where to download the package from;
  177. \texttt{@SF} is a special keyword for downloading packages from sourceforge.
  178. The md5sum is used to verify the package was downloaded correctly and
  179. \texttt{PKG\_BUILD\_DIR} defines where to find the package after the sources are
  180. uncompressed into \texttt{\$(BUILD\_DIR)}.
  181. At the bottom of the file is where the real magic happens, "BuildPackage" is a macro
  182. setup by the earlier include statements. BuildPackage only takes one argument directly --
  183. the name of the package to be built, in this case "\texttt{bridge}". All other information
  184. is taken from the define blocks. This is a way of providing a level of verbosity, it's
  185. inherently clear what the contents of the \texttt{description} template in
  186. \texttt{Package/bridge} is, which wouldn't be the case if we passed this information
  187. directly as the Nth argument to \texttt{BuildPackage}.
  188. \texttt{BuildPackage} uses the following defines:
  189. \textbf{\texttt{Package/\textit{<name>}}:} \\
  190. \texttt{\textit{<name>}} matches the argument passed to buildroot, this describes
  191. the package the menuconfig and ipkg entries. Within \texttt{Package/\textit{<name>}}
  192. you can define the following variables:
  193. \begin{itemize}
  194. \item \texttt{SECTION} \\
  195. The type of package (currently unused)
  196. \item \texttt{CATEGORY} \\
  197. Which menu it appears in menuconfig
  198. \item \texttt{TITLE} \\
  199. A short description of the package
  200. \item \texttt{URL} \\
  201. Where to find the original software
  202. \item \texttt{MAINTAINER} (optional) \\
  203. Who to contact concerning the package
  204. \item \texttt{DEPENDS} (optional) \\
  205. Which packages must be built/installed before this package
  206. \end{itemize}
  207. \textbf{\texttt{Package/\textit{<name>}/conffiles} (optional):} \\
  208. A list of config files installed by this package, one file per line.
  209. \textbf{\texttt{Build/Prepare} (optional):} \\
  210. A set of commands to unpack and patch the sources. You may safely leave this
  211. undefined.
  212. \textbf{\texttt{Build/Configure} (optional):} \\
  213. You can leave this undefined if the source doesn't use configure or has a
  214. normal config script, otherwise you can put your own commands here or use
  215. "\texttt{\$(call Build/Configure/Default,\textit{<args>})}" as above to
  216. pass in additional arguments for a standard configure script.
  217. \textbf{\texttt{Build/Compile} (optional):} \\
  218. How to compile the source; in most cases you should leave this undefined.
  219. \textbf{\texttt{Package/\textit{<name>}/install}:} \\
  220. A set of commands to copy files out of the compiled source and into the ipkg
  221. which is represented by the \texttt{\$(1)} directory.
  222. The reason that some of the defines are prefixed by "\texttt{Package/\textit{<name>}}"
  223. and others are simply "\texttt{Build}" is because of the possibility of generating
  224. multiple packages from a single source. OpenWrt works under the assumption of one
  225. source per package makefile, but you can split that source into as many packages as
  226. desired. Since you only need to compile the sources once, there's one global set of
  227. "\texttt{Build}" defines, but you can add as many "Package/<name>" defines as you want
  228. by adding extra calls to \texttt{BuildPackage} -- see the dropbear package for an example.
  229. After you've created your \texttt{package/\textit{<name>}/Makefile}, the new package
  230. will automatically show in the menu the next time you run "make menuconfig" and if selected
  231. will be built automatically the next time "\texttt{make}" is run.
  232. \subsubsection{Troubleshooting}
  233. If you find your package doesn't show up in menuconfig, try the following command to
  234. see if you get the correct description:
  235. \begin{Verbatim}
  236. TOPDIR=$PWD make -C package/<name> DUMP=1 V=99
  237. \end{Verbatim}
  238. If you're just having trouble getting your package to compile, there's a few
  239. shortcuts you can take. Instead of waiting for make to get to your package, you can
  240. run one of the following:
  241. \begin{itemize}
  242. \item \texttt{make package/\textit{<name>}-clean V=99}
  243. \item \texttt{make package/\textit{<name>}-install V=99}
  244. \end{itemize}
  245. Another nice trick is that if the source directory under \texttt{build\_\textit{<arch>}}
  246. is newer than the package directory, it won't clobber it by unpacking the sources again.
  247. If you were working on a patch you could simply edit the sources under the
  248. \texttt{build\_\textit{<arch>}/\textit{<source>}} directory and run the install command above,
  249. when satisfied, copy the patched sources elsewhere and diff them with the unpatched
  250. sources. A warning though - if you go modify anything under \texttt{package/\textit{<name>}}
  251. it will remove the old sources and unpack a fresh copy.