move the lzma decompressor out of squashfs-lzma patch and into a new patch (i want...
[openwrt.git] / docs / build.tex
1 One of the biggest challenges to getting started with embedded devices is that you
2 cannot 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 would not 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 most of the time 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
11 The process of creating a cross compiler can be tricky, it is not something that is
12 regularly attempted and so there is a certain amount of mystery and black magic
13 associated with it. In many cases when you are dealing with embedded devices you will
14 be provided with a binary copy of a compiler and basic libraries rather than
15 instructions for creating your own -- it is a time saving step but at the same time
16 often means you will be using a rather dated set of tools. Likewise, it is 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.
20
21 \subsection{Building an image}
22
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 does not contain any executables or even sources, it is 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.
29
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 is 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 does not
35 just apply to the kernel, but to anything included with OpenWrt -- It is 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.
38
39 So let's take a look at OpenWrt and see how this all works.
40
41
42 \subsubsection{Download OpenWrt}
43
44 This article refers to the "Kamikaze" branch of OpenWrt, which can be downloaded via
45 subversion using the following command:
46
47 \begin{Verbatim}
48 $ svn checkout https://svn.openwrt.org/openwrt/trunk kamikaze
49 \end{Verbatim}
50
51 Additionally, there is a trac interface on \href{https://dev.openwrt.org/}{https://dev.openwrt.org/}
52 which can be used to monitor svn commits and browse the source repository.
53
54
55 \subsubsection{The directory structure}
56
57 There are four key directories in the base:
58
59 \begin{itemize}
60 \item \texttt{tools}
61 \item \texttt{toolchain}
62 \item \texttt{package}
63 \item \texttt{target}
64 \end{itemize}
65
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 will not need to do anything with the toolchain directory unless you intend to
73 add a new version of one of the components above.
74
75 \begin{itemize}
76 \item \texttt{tool\_build}
77 \item \texttt{toolchain\_build\_\textit{<arch>}}
78 \end{itemize}
79
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:
84
85 \begin{Verbatim}
86 $ svn checkout https://svn.openwrt.org/openwrt/packages packages
87 \end{Verbatim}
88
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:
92
93 \begin{Verbatim}
94 $ ls
95 kamikaze packages
96 $ ln -s packages/net/nmap kamikaze/package/nmap
97 \end{Verbatim}
98
99 To include all packages, issue the following command:
100
101 \begin{Verbatim}
102 $ ln -s packages/*/* kamikaze/package/
103 \end{Verbatim}
104
105 \texttt{target} refers to the embedded platform, this contains items which are specific to
106 a specific embedded platform. Of particular interest here is the "\texttt{target/linux}"
107 directory which is broken down by platform \textit{<arch>} and contains the patches to the
108 kernel, profile config, for a particular platform. There's also the "\texttt{target/image}" directory
109 which describes how to package a firmware for a specific platform.
110
111 Both the target and package steps will use the directory "\texttt{build\_\textit{<arch>}}"
112 as a temporary directory for compiling. Additionally, anything downloaded by the toolchain,
113 target or package steps will be placed in the "\texttt{dl}" directory.
114
115 \begin{itemize}
116 \item \texttt{build\_\textit{<arch>}}
117 \item \texttt{dl}
118 \end{itemize}
119
120 \subsubsection{Building OpenWrt}
121
122 While the OpenWrt build environment was intended mostly for developers, it also has to be
123 simple enough that an inexperienced end user can easily build his or her own customized firmware.
124
125 Running the command "\texttt{make menuconfig}" will bring up OpenWrt's configuration menu
126 screen, through this menu you can select which platform you're targeting, which versions of
127 the toolchain you want to use to build and what packages you want to install into the
128 firmware image. Note that it will also check to make sure you have the basic dependencies for it
129 to run correctly. If that fails, you will need to install some more tools in your local environment
130 before you can begin.
131
132 Similar to the linux kernel config, almost every option has three choices,
133 \texttt{y/m/n} which are represented as follows:
134
135 \begin{itemize}
136 \item{\texttt{<*>} (pressing y)} \\
137 This will be included in the firmware image
138 \item{\texttt{<M>} (pressing m)} \\
139 This will be compiled but not included (for later install)
140 \item{\texttt{< >} (pressing n)} \\
141 This will not be compiled
142 \end{itemize}
143
144 After you've finished with the menu configuration, exit and when prompted, save your
145 configuration changes.
146
147 If you want, you can also modify the kernel config for the selected target system.
148 simply run "\texttt{make kernel\_menuconfig}" and the build system will unpack the kernel sources
149 (if necessary), run menuconfig inside of the kernel tree, and then copy the kernel config
150 to \texttt{target/linux/\textit{<platform>}/config} so that it is preserved over
151 "\texttt{make clean}" calls.
152
153 To begin compiling the firmware, type "\texttt{make}". By default
154 OpenWrt will only display a high level overview of the compile process and not each individual
155 command.
156
157 \subsubsection{Example:}
158
159 \begin{Verbatim}
160 make[2] toolchain/install
161 make[3] -C toolchain install
162 make[2] target/compile
163 make[3] -C target compile
164 make[4] -C target/utils prepare
165
166 [...]
167 \end{Verbatim}
168
169 This makes it easier to monitor which step it's actually compiling and reduces the amount
170 of noise caused by the compile output. To see the full output, run the command
171 "\texttt{make V=99}".
172
173 During the build process, buildroot will download all sources to the "\texttt{dl}"
174 directory and will start patching and compiling them in the "\texttt{build\_\textit{<arch>}}"
175 directory. When finished, the resulting firmware will be in the "\texttt{bin}" directory
176 and packages will be in the "\texttt{bin/packages}" directory.
177
178
179 \subsection{Creating packages}
180
181 One of the things that we've attempted to do with OpenWrt's template system is make it
182 incredibly easy to port software to OpenWrt. If you look at a typical package directory
183 in OpenWrt you'll find two things:
184
185 \begin{itemize}
186 \item \texttt{package/\textit{<name>}/Makefile}
187 \item \texttt{package/\textit{<name>}/patches}
188 \item \texttt{package/\textit{<name>}/files}
189 \end{itemize}
190
191 The patches directory is optional and typically contains bug fixes or optimizations to
192 reduce the size of the executable. The package makefile is the important item, provides
193 the steps actually needed to download and compile the package.
194
195 The files directory is also optional and typicall contains package specific startup scripts or default configuration files that can be used out of the box with OpenWrt.
196
197 Looking at one of the package makefiles, you'd hardly recognize it as a makefile.
198 Through what can only be described as blatant disregard and abuse of the traditional
199 make format, the makefile has been transformed into an object oriented template which
200 simplifies the entire ordeal.
201
202 Here for example, is \texttt{package/bridge/Makefile}:
203
204 \begin{Verbatim}[frame=single,numbers=left]
205 #
206 # Copyright (C) 2006 OpenWrt.org
207 #
208 # This is free software, licensed under the GNU General Public License v2.
209 # See /LICENSE for more information.
210 #
211 # $Id: Makefile 5624 2006-11-23 00:29:07Z nbd $
212
213 include $(TOPDIR)/rules.mk
214
215 PKG_NAME:=bridge
216 PKG_VERSION:=1.0.6
217 PKG_RELEASE:=1
218
219 PKG_SOURCE:=bridge-utils-$(PKG_VERSION).tar.gz
220 PKG_SOURCE_URL:=@SF/bridge
221 PKG_MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd
222 PKG_CAT:=zcat
223
224 PKG_BUILD_DIR:=$(BUILD_DIR)/bridge-utils-$(PKG_VERSION)
225
226 include $(INCLUDE_DIR)/package.mk
227
228 define Package/bridge
229 SECTION:=net
230 CATEGORY:=Base system
231 TITLE:=Ethernet bridging configuration utility
232 DESCRIPTION:=\
233 Manage ethernet bridging: a way to connect networks together to \\\
234 form a larger network.
235 URL:=http://bridge.sourceforge.net/
236 endef
237
238 define Build/Configure
239 $(call Build/Configure/Default, \
240 --with-linux-headers="$(LINUX_DIR)" \
241 )
242 endef
243
244 define Package/bridge/install
245 $(INSTALL_DIR) $(1)/usr/sbin
246 $(INSTALL_BIN) $(PKG_BUILD_DIR)/brctl/brctl $(1)/usr/sbin/
247 endef
248
249 $(eval $(call BuildPackage,bridge))
250 \end{Verbatim}
251
252 As you can see, there's not much work to be done; everything is hidden in other makefiles
253 and abstracted to the point where you only need to specify a few variables.
254
255 \begin{itemize}
256 \item \texttt{PKG\_NAME} \\
257 The name of the package, as seen via menuconfig and ipkg
258 \item \texttt{PKG\_VERSION} \\
259 The upstream version number that we are downloading
260 \item \texttt{PKG\_RELEASE} \\
261 The version of this package Makefile
262 \item \texttt{PKG\_SOURCE} \\
263 The filename of the original sources
264 \item \texttt{PKG\_SOURCE\_URL} \\
265 Where to download the sources from (no trailing slash), you can add multiple download sources by separating them with a \\ and a carriage return.
266 \item \texttt{PKG\_MD5SUM} \\
267 A checksum to validate the download
268 \item \texttt{PKG\_CAT} \\
269 How to decompress the sources (zcat, bzcat, unzip)
270 \item \texttt{PKG\_BUILD\_DIR} \\
271 Where to compile the package
272 \end{itemize}
273
274 The \texttt{PKG\_*} variables define where to download the package from;
275 \texttt{@SF} is a special keyword for downloading packages from sourceforge. There is also
276 another keyword of \texttt{@GNU} for grabbing GNU source releases. If any of the above mentionned download source fails, the OpenWrt mirrors will be used as source.
277
278 The md5sum (if present) is used to verify the package was downloaded correctly and
279 \texttt{PKG\_BUILD\_DIR} defines where to find the package after the sources are
280 uncompressed into \texttt{\$(BUILD\_DIR)}.
281
282 At the bottom of the file is where the real magic happens, "BuildPackage" is a macro
283 set up by the earlier include statements. BuildPackage only takes one argument directly --
284 the name of the package to be built, in this case "\texttt{bridge}". All other information
285 is taken from the define blocks. This is a way of providing a level of verbosity, it's
286 inherently clear what the contents of the \texttt{description} template in
287 \texttt{Package/bridge} is, which wouldn't be the case if we passed this information
288 directly as the Nth argument to \texttt{BuildPackage}.
289
290 \texttt{BuildPackage} uses the following defines:
291
292 \textbf{\texttt{Package/\textit{<name>}}:} \\
293 \texttt{\textit{<name>}} matches the argument passed to buildroot, this describes
294 the package the menuconfig and ipkg entries. Within \texttt{Package/\textit{<name>}}
295 you can define the following variables:
296
297 \begin{itemize}
298 \item \texttt{SECTION} \\
299 The type of package (currently unused)
300 \item \texttt{CATEGORY} \\
301 Which menu it appears in menuconfig: Network, Sound, Utilities, Multimedia ...
302 \item \texttt{TITLE} \\
303 A short description of the package
304 \item \texttt{URL} \\
305 Where to find the original software
306 \item \texttt{MAINTAINER} (optional) \\
307 Who to contact concerning the package
308 \item \texttt{DEPENDS} (optional) \\
309 Which packages must be built/installed before this package. To reference a dependency defined in the
310 same Makefile, use \textit{<dependency name>}. If defined as an external package, use
311 \textit{+<dependency name>}. For a kernel version dependency use: \textit{@LINUX\_2\_<minor version>}
312 \end{itemize}
313
314 \textbf{\texttt{Package/\textit{<name>}/conffiles} (optional):} \\
315 A list of config files installed by this package, one file per line.
316
317 \textbf{\texttt{Build/Prepare} (optional):} \\
318 A set of commands to unpack and patch the sources. You may safely leave this
319 undefined.
320
321 \textbf{\texttt{Build/Configure} (optional):} \\
322 You can leave this undefined if the source doesn't use configure or has a
323 normal config script, otherwise you can put your own commands here or use
324 "\texttt{\$(call Build/Configure/Default,\textit{<first list of arguments, second list>})}" as above to
325 pass in additional arguments for a standard configure script. The first list of arguments will be passed
326 to the configure script like that: \texttt{--arg 1} \texttt{--arg 2}. The second list contains arguments that should be
327 defined before running the configure script such as autoconf or compiler specific variables.
328
329 To make it easier to modify the configure command line, you can either extend or completely override the following variables:
330 \begin{itemize}
331 \item \texttt{CONFIGURE\_ARGS} \\
332 Contains all command line arguments (format: \texttt{--arg 1} \texttt{--arg 2})
333 \item \texttt{CONFIGURE\_VARS} \\
334 Contains all environment variables that are passed to ./configure (format: \texttt{NAME="value"})
335 \end{itemize}
336
337 \textbf{\texttt{Build/Compile} (optional):} \\
338 How to compile the source; in most cases you should leave this undefined.
339
340 As with \texttt{Build/Configure} there are two variables that allow you to override
341 the make command line environment variables and flags:
342 \begin{itemize}
343 \item \texttt{MAKE\_FLAGS} \\
344 Contains all command line arguments (typically variable overrides like \texttt{NAME="value"}
345 \item \texttt{MAKE\_VARS} \\
346 Contains all environment variables that are passed to the make command
347 \end{itemize}
348
349 \textbf{\texttt{Package/\textit{<name>}/install}:} \\
350 A set of commands to copy files out of the compiled source and into the ipkg
351 which is represented by the \texttt{\$(1)} directory. Note that there are currently
352 4 defined install macros:
353 \begin{itemize}
354 \item \texttt{INSTALL\_DIR} \\
355 install -d -m0755
356 \item \texttt{INSTALL\_BIN} \\
357 install -m0755
358 \item \texttt{INSTALL\_DATA} \\
359 install -m0644
360 \item \texttt{INSTALL\_CONF} \\
361 install -m0600
362 \end{itemize}
363
364 The reason that some of the defines are prefixed by "\texttt{Package/\textit{<name>}}"
365 and others are simply "\texttt{Build}" is because of the possibility of generating
366 multiple packages from a single source. OpenWrt works under the assumption of one
367 source per package Makefile, but you can split that source into as many packages as
368 desired. Since you only need to compile the sources once, there's one global set of
369 "\texttt{Build}" defines, but you can add as many "Package/<name>" defines as you want
370 by adding extra calls to \texttt{BuildPackage} -- see the dropbear package for an example.
371
372 After you have created your \texttt{package/\textit{<name>}/Makefile}, the new package
373 will automatically show in the menu the next time you run "make menuconfig" and if selected
374 will be built automatically the next time "\texttt{make}" is run.
375
376 \subsection{Creating kernel modules packages}
377
378 The OpenWrt distribution makes the distinction between two kind of kernel modules, those coming along with the mainline kernel, and the others available as a separate project. We will see later that a common template is used for both of them.
379
380 For kernel modules that are part of the mainline kernel source, the makefiles are located in \textit{package/kernel/modules/*.mk} and they appear under the section "Kernel modules"
381
382 For external kernel modules, you can add them to the build system just like if they were software packages by defining a KernelPackage section in the package makefile.
383
384 Here for instance the Makefile for the I2C subsytem kernel modules :
385
386 \begin{Verbatim}[frame=single,numbers=left]
387 #
388 # Copyright (C) 2006 OpenWrt.org
389 #
390 # This is free software, licensed under the GNU General Public License v2.
391 # See /LICENSE for more information.
392 #
393 # $Id $
394
395 I2CMENU:=I2C Bus
396
397 define KernelPackage/i2c-core
398 TITLE:=I2C support
399 DESCRIPTION:=Kernel modules for i2c support
400 SUBMENU:=$(I2CMENU)
401 KCONFIG:=$(CONFIG_I2C_CORE) \
402 $(CONFIG_I2C_DEV)
403 FILES:=$(MODULES_DIR)/kernel/drivers/i2c/*.$(LINUX_KMOD_SUFFIX)
404 AUTOLOAD:=$(call AutoLoad,50,i2c-core i2c-dev)
405 endef
406 $(eval $(call KernelPackage,i2c-core))
407 \end{Verbatim}
408
409 To group kernel modules under a common description in menuconfig, you might want to define a \textit{<description>MENU} variable on top of the kernel modules makefile.
410
411 \begin{itemize}
412 \item \texttt{TITLE} \\
413 The name of the module as seen via menuconfig
414 \item \texttt{DESCRIPTION} \\
415 The description as seen via help in menuconfig
416 \item \texttt{SUBMENU} \\
417 The sub menu under which this package will be seen
418 \item \texttt{KCONFIG} \\
419 Kernel configuration option dependency. For external modules, remove it.
420 \item \texttt{FILES} \\
421 Files you want to inlude to this kernel module package, separate with spaces.
422 \item \texttt{AUTOLOAD} \\
423 Modules that will be loaded automatically on boot, the order you write them is the order they would be loaded.
424 \end{itemize}
425
426 After you have created your \texttt{package/kernel/modules/\textit{<name>}.mk}, the new kernel modules package
427 will automatically show in the menu under "Kernel modules" next time you run "make menuconfig" and if selected
428 will be built automatically the next time "\texttt{make}" is run.
429
430 \subsection{Conventions}
431
432 There are a couple conventions to follow regarding packages:
433
434 \begin{itemize}
435 \item \texttt{files}
436 \begin{enumerate}
437 \item configuration files follow the convention \\
438 \texttt{\textit{<name>}.conf}
439 \item init files follow the convention \\
440 \texttt{\textit{<name>}.init}
441 \end{enumerate}
442 \item \texttt{patches}
443 \begin{enumerate}
444 \item patches are numerically prefixed and named related to what they do
445 \end{enumerate}
446 \end{itemize}
447
448 \subsection{Troubleshooting}
449
450 If you find your package doesn't show up in menuconfig, try the following command to
451 see if you get the correct description:
452
453 \begin{Verbatim}
454 TOPDIR=$PWD make -C package/<name> DUMP=1 V=99
455 \end{Verbatim}
456
457 If you're just having trouble getting your package to compile, there's a few
458 shortcuts you can take. Instead of waiting for make to get to your package, you can
459 run one of the following:
460
461 \begin{itemize}
462 \item \texttt{make package/\textit{<name>}-clean V=99}
463 \item \texttt{make package/\textit{<name>}-install V=99}
464 \end{itemize}
465
466 Another nice trick is that if the source directory under \texttt{build\_\textit{<arch>}}
467 is newer than the package directory, it won't clobber it by unpacking the sources again.
468 If you were working on a patch you could simply edit the sources under the
469 \texttt{build\_\textit{<arch>}/\textit{<source>}} directory and run the install command above,
470 when satisfied, copy the patched sources elsewhere and diff them with the unpatched
471 sources. A warning though - if you go modify anything under \texttt{package/\textit{<name>}}
472 it will remove the old sources and unpack a fresh copy.
473
474 Other useful targets include:
475
476 \begin{itemize}
477 \item \texttt{make package/\textit{<name>}-prepare V=99}
478 \item \texttt{make package/\textit{<name>}-compile V=99}
479 \item \texttt{make package/\textit{<name>}-configure V=99}
480 \end{itemize}
481
This page took 0.071806 seconds and 5 git commands to generate.