some more build system documentation
[openwrt/svn-archive/archive.git] / docs / build.tex
1 One of the biggest challenges to getting started with embedded devices is that you
2 can't just 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.
11 The process of creating a cross compiler can be tricky, it's not something that's
12 regularly attempted and so there's a certain amount of mystery and black magic
13 associated with it. In many cases when you're dealing with embedded devices you'll
14 be provided with a binary copy of a compiler and basic libraries rather than
15 instructions for creating your own -- it's a time saving step but at the same time
16 often means you'll be using a rather dated set of tools. Likewise, it's also common
17 to be provided with a patched copy of the Linux kernel from the board or chip vendor,
18 but this is also dated and it can be difficult to spot exactly what has been
19 modified to make the kernel run on the embedded platform.
21 \subsection{Building an image}
23 OpenWrt takes a different approach to building a firmware; downloading, patching
24 and compiling everything from scratch, including the cross compiler. To put it
25 in simpler terms, OpenWrt doesn't contain any executables or even sources, it's an
26 automated system for downloading the sources, patching them to work with the given
27 platform and compiling them correctly for that platform. What this means is that
28 just by changing the template, you can change any step in the process.
30 As an example, if a new kernel is released, a simple change to one of the Makefiles
31 will download the latest kernel, patch it to run on the embedded platform and produce
32 a new firmware image -- there's no work to be done trying to track down an unmodified
33 copy of the existing kernel to see what changes had been made, the patches are
34 already provided and the process ends up almost completely transparent. This doesn't
35 just apply to the kernel, but to anything included with OpenWrt -- It's this one
36 simple understated concept which is what allows OpenWrt to stay on the bleeding edge
37 with the latest compilers, latest kernels and latest applications.
39 So let's take a look at OpenWrt and see how this all works.
42 \subsubsection{Download openwrt}
44 This article refers to the "Kamikaze" branch of OpenWrt, which can be downloaded via
45 subversion using the following command:
47 \begin{Verbatim}
48 $ svn co kamikaze
49 \end{Verbatim}
51 Additionally, there's a trac interface on \href{}{}
52 which can be used to monitor svn commits and browse the sources.
55 \subsubsection{The directory structure}
57 There are four key directories in the base:
59 \begin{itemize}
60 \item \texttt{tools}
61 \item \texttt{toolchain}
62 \item \texttt{package}
63 \item \texttt{target}
64 \end{itemize}
66 \texttt{tools} and \texttt{toolchain} refer to common tools which will be
67 used to build the firmware image, the compiler, and the c library.
68 The result of this is three new directories, \texttt{tool\_build}, which is a temporary
69 directory for building the target independent tools, \texttt{toolchain\_build\_\textit{<arch>}}
70 which is used for building the toolchain for a specific architecture, and
71 \texttt{staging\_dir\_\textit{<arch>}} where the resulting toolchain is installed.
72 You won't need to do anything with the toolchain directory unless you intend to
73 add a new version of one of the components above.
75 \begin{itemize}
76 \item \texttt{tool\_build}
77 \item \texttt{toolchain\_build\_\textit{<arch>}}
78 \end{itemize}
80 \texttt{package} is for exactly that -- packages. In an OpenWrt firmware, almost everything
81 is an \texttt{.ipk}, a software package which can be added to the firmware to provide new
82 features or removed to save space. Note that packages are also maintained outside of the main
83 trunk and can be obtained from subversion at the following location:
85 \begin{Verbatim}
86 $ svn co ../packages
87 \end{Verbatim}
89 Those packages can be used to extend the functionality of the build system and need to be
90 symlinked into the main trunk. Once you do that, the packages will show up in the menu for
91 configuration. From kamikaze you would do something like this:
93 \begin{Verbatim}
94 $ ls
95 kamikaze packages
96 $ ln -s packages/net/nmap kamikaze/package/nmap
97 \end{Verbatim}
99 \texttt{target} refers to the embedded platform, this contains items which are specific to
100 a specific embedded platform. Of particular interest here is the "\texttt{target/linux}"
101 directory which is broken down by platform and contains the kernel config and patches
102 to the kernel for a particular platform. There's also the "\texttt{target/image}" directory
103 which describes how to package a firmware for a specific platform.
105 Both the target and package steps will use the directory "\texttt{build\_\textit{<arch>}}"
106 as a temporary directory for compiling. Additionally, anything downloaded by the toolchain,
107 target or package steps will be placed in the "\texttt{dl}" directory.
109 \begin{itemize}
110 \item \texttt{build\_\textit{<arch>}}
111 \item \texttt{dl}
112 \end{itemize}
114 \subsubsection{Building OpenWrt}
116 While the OpenWrt build environment was intended mostly for developers, it also has to be
117 simple enough that an inexperienced end user can easily build his or her own customized firmware.
119 Running the command "\texttt{make menuconfig}" will bring up OpenWrt's configuration menu
120 screen, through this menu you can select which platform you're targeting, which versions of
121 the toolchain you want to use to build and what packages you want to install into the
122 firmware image. Note that it will also check to make sure you have the basic dependencies for it
123 to run correctly. If that fails, you will need to install some more tools in your local environment
124 before you can begin.
126 Similar to the linux kernel config, almost every option has three choices,
127 \texttt{y/m/n} which are represented as follows:
129 \begin{itemize}
130 \item{\texttt{<*>} (pressing y)} \\
131 This will be included in the firmware image
132 \item{\texttt{<M>} (pressing m)} \\
133 This will be compiled but not included (for later install)
134 \item{\texttt{< >} (pressing n)} \\
135 This will not be compiled
136 \end{itemize}
138 After you've finished with the menu configuration, exit and when prompted, save your
139 configuration changes. To begin compiling the firmware, type "\texttt{make}". By default
140 OpenWrt will only display a high level overview of the compile process and not each individual
141 command.
143 \subsubsection{Example:}
145 \begin{Verbatim}
146 make[2] toolchain/install
147 make[3] -C toolchain install
148 make[2] target/compile
149 make[3] -C target compile
150 make[4] -C target/utils prepare
152 [...]
153 \end{Verbatim}
155 This makes it easier to monitor which step it's actually compiling and reduces the amount
156 of noise caused by the compile output. To see the full output, run the command
157 "\texttt{make V=99}".
159 During the build process, buildroot will download all sources to the "\texttt{dl}"
160 directory and will start patching and compiling them in the "\texttt{build\_\textit{<arch>}}"
161 directory. When finished, the resulting firmware will be in the "\texttt{bin}" directory
162 and packages will be in the "\texttt{bin/packages}" directory.
165 \subsection{Creating packages}
167 One of the things that we've attempted to do with OpenWrt's template system is make it
168 incredibly easy to port software to OpenWrt. If you look at a typical package directory
169 in OpenWrt you'll find two things:
171 \begin{itemize}
172 \item \texttt{package/\textit{<name>}/Makefile}
173 \item \texttt{package/\textit{<name>}/patches}
174 \end{itemize}
176 The patches directory is optional and typically contains bug fixes or optimizations to
177 reduce the size of the executable. The package makefile is the important item, provides
178 the steps actually needed to download and compile the package.
180 Looking at one of the package makefiles, you'd hardly recognize it as a makefile.
181 Through what can only be described as blatant disregard and abuse of the traditional
182 make format, the makefile has been transformed into an object oriented template which
183 simplifies the entire ordeal.
185 Here for example, is \texttt{package/bridge/Makefile}:
187 \begin{Verbatim}[frame=single,numbers=left]
188 #
189 # Copyright (C) 2006
190 #
191 # This is free software, licensed under the GNU General Public License v2.
192 # See /LICENSE for more information.
193 #
194 # $Id: Makefile 5624 2006-11-23 00:29:07Z nbd $
196 include $(TOPDIR)/
198 PKG_NAME:=bridge
199 PKG_VERSION:=1.0.6
202 PKG_SOURCE:=bridge-utils-$(PKG_VERSION).tar.gz
203 PKG_SOURCE_URL:=@SF/bridge
204 PKG_MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd
205 PKG_CAT:=zcat
207 PKG_BUILD_DIR:=$(BUILD_DIR)/bridge-utils-$(PKG_VERSION)
209 include $(INCLUDE_DIR)/
211 define Package/bridge
212 SECTION:=net
213 CATEGORY:=Base system
214 TITLE:=Ethernet bridging configuration utility
216 Manage ethernet bridging: a way to connect networks together to \\\
217 form a larger network.
218 URL:=
219 endef
221 define Build/Configure
222 $(call Build/Configure/Default, \
223 --with-linux-headers="$(LINUX_DIR)" \
224 )
225 endef
227 define Package/bridge/install
228 $(INSTALL_DIR) $(1)/usr/sbin
229 $(INSTALL_BIN) $(PKG_BUILD_DIR)/brctl/brctl $(1)/usr/sbin/
230 endef
232 $(eval $(call BuildPackage,bridge))
233 \end{Verbatim}
235 As you can see, there's not much work to be done; everything is hidden in other makefiles
236 and abstracted to the point where you only need to specify a few variables.
238 \begin{itemize}
239 \item \texttt{PKG\_NAME} \\
240 The name of the package, as seen via menuconfig and ipkg
241 \item \texttt{PKG\_VERSION} \\
242 The upstream version number that we're downloading
243 \item \texttt{PKG\_RELEASE} \\
244 The version of this package Makefile
245 \item \texttt{PKG\_SOURCE} \\
246 The filename of the original sources
247 \item \texttt{PKG\_SOURCE\_URL} \\
248 Where to download the sources from
249 \item \texttt{PKG\_MD5SUM} \\
250 A checksum to validate the download
251 \item \texttt{PKG\_CAT} \\
252 How to decompress the sources (zcat, bzcat, unzip)
253 \item \texttt{PKG\_BUILD\_DIR} \\
254 Where to compile the package
255 \end{itemize}
257 The \texttt{PKG\_*} variables define where to download the package from;
258 \texttt{@SF} is a special keyword for downloading packages from sourceforge.
259 The md5sum is used to verify the package was downloaded correctly and
260 \texttt{PKG\_BUILD\_DIR} defines where to find the package after the sources are
261 uncompressed into \texttt{\$(BUILD\_DIR)}.
263 At the bottom of the file is where the real magic happens, "BuildPackage" is a macro
264 set up by the earlier include statements. BuildPackage only takes one argument directly --
265 the name of the package to be built, in this case "\texttt{bridge}". All other information
266 is taken from the define blocks. This is a way of providing a level of verbosity, it's
267 inherently clear what the contents of the \texttt{description} template in
268 \texttt{Package/bridge} is, which wouldn't be the case if we passed this information
269 directly as the Nth argument to \texttt{BuildPackage}.
271 \texttt{BuildPackage} uses the following defines:
273 \textbf{\texttt{Package/\textit{<name>}}:} \\
274 \texttt{\textit{<name>}} matches the argument passed to buildroot, this describes
275 the package the menuconfig and ipkg entries. Within \texttt{Package/\textit{<name>}}
276 you can define the following variables:
278 \begin{itemize}
279 \item \texttt{SECTION} \\
280 The type of package (currently unused)
281 \item \texttt{CATEGORY} \\
282 Which menu it appears in menuconfig
283 \item \texttt{TITLE} \\
284 A short description of the package
285 \item \texttt{URL} \\
286 Where to find the original software
287 \item \texttt{MAINTAINER} (optional) \\
288 Who to contact concerning the package
289 \item \texttt{DEPENDS} (optional) \\
290 Which packages must be built/installed before this package
291 \end{itemize}
293 \textbf{\texttt{Package/\textit{<name>}/conffiles} (optional):} \\
294 A list of config files installed by this package, one file per line.
296 \textbf{\texttt{Build/Prepare} (optional):} \\
297 A set of commands to unpack and patch the sources. You may safely leave this
298 undefined.
300 \textbf{\texttt{Build/Configure} (optional):} \\
301 You can leave this undefined if the source doesn't use configure or has a
302 normal config script, otherwise you can put your own commands here or use
303 "\texttt{\$(call Build/Configure/Default,\textit{<args>})}" as above to
304 pass in additional arguments for a standard configure script.
306 \textbf{\texttt{Build/Compile} (optional):} \\
307 How to compile the source; in most cases you should leave this undefined.
309 \textbf{\texttt{Package/\textit{<name>}/install}:} \\
310 A set of commands to copy files out of the compiled source and into the ipkg
311 which is represented by the \texttt{\$(1)} directory. Note that there are currently
312 3 defined install macros:
313 \begin{itemize}
314 \item \texttt{INSTALL\_DIR} \\
315 install -d -m0755
316 \item \texttt{INSTALL\_BIN} \\
317 install -m0755
318 \item \texttt{INSTALL\_DATA} \\
319 install -m0644
320 \end{itemize}
322 The reason that some of the defines are prefixed by "\texttt{Package/\textit{<name>}}"
323 and others are simply "\texttt{Build}" is because of the possibility of generating
324 multiple packages from a single source. OpenWrt works under the assumption of one
325 source per package Makefile, but you can split that source into as many packages as
326 desired. Since you only need to compile the sources once, there's one global set of
327 "\texttt{Build}" defines, but you can add as many "Package/<name>" defines as you want
328 by adding extra calls to \texttt{BuildPackage} -- see the dropbear package for an example.
330 After you've created your \texttt{package/\textit{<name>}/Makefile}, the new package
331 will automatically show in the menu the next time you run "make menuconfig" and if selected
332 will be built automatically the next time "\texttt{make}" is run.
334 \subsubsection{Troubleshooting}
336 If you find your package doesn't show up in menuconfig, try the following command to
337 see if you get the correct description:
339 \begin{Verbatim}
340 TOPDIR=$PWD make -C package/<name> DUMP=1 V=99
341 \end{Verbatim}
343 If you're just having trouble getting your package to compile, there's a few
344 shortcuts you can take. Instead of waiting for make to get to your package, you can
345 run one of the following:
347 \begin{itemize}
348 \item \texttt{make package/\textit{<name>}-clean V=99}
349 \item \texttt{make package/\textit{<name>}-install V=99}
350 \end{itemize}
352 Another nice trick is that if the source directory under \texttt{build\_\textit{<arch>}}
353 is newer than the package directory, it won't clobber it by unpacking the sources again.
354 If you were working on a patch you could simply edit the sources under the
355 \texttt{build\_\textit{<arch>}/\textit{<source>}} directory and run the install command above,
356 when satisfied, copy the patched sources elsewhere and diff them with the unpatched
357 sources. A warning though - if you go modify anything under \texttt{package/\textit{<name>}}
358 it will remove the old sources and unpack a fresh copy.
360 Other useful targets include:
362 \begin{itemize}
363 \item \texttt{make package/\textit{<name>}-prepare V=99}
364 \item \texttt{make package/\textit{<name>}-compile V=99}
365 \item \texttt{make package/\textit{<name>}-configure V=99}
366 \end{itemize}