#! /bin/sh # Output a system dependent set of variables, describing how to set the # run time search path of shared libraries in an executable. # # Copyright 1996-2007 Free Software Foundation, Inc. # Taken from GNU libtool, 2001 # Originally by Gordon Matzigkeit , 1996 # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # The first argument passed to this file is the canonical host specification, # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM # or # CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM # The environment variables CC, GCC, LDFLAGS, LD, with_gnu_ld # should be set by the caller. # # The set of defined variables is at the end of this script. # Known limitations: # - On IRIX 6.5 with CC="cc", the run time search patch must not be longer # than 256 bytes, otherwise the compiler driver will dump core. The only # known workaround is to choose shorter directory names for the build # directory and/or the installation directory. # All known linkers require a `.a' archive for static linking (except MSVC, # which needs '.lib'). libext=a shrext=.so host="$1" host_cpu=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'` host_vendor=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'` host_os=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'` # Code taken from libtool.m4's _LT_CC_BASENAME. for cc_temp in $CC""; do case $cc_temp in compile | *[\\/]compile | ccache | *[\\/]ccache ) ;; distcc | *[\\/]distcc | purify | *[\\/]purify ) ;; \-*) ;; *) break;; esac done cc_basename=`echo "$cc_temp" | sed -e 's%^.*/%%'` # Code taken from libtool.m4's AC_LIBTOOL_PROG_COMPILER_PIC. wl= if test "$GCC" = yes; then wl='-Wl,' else case "$host_os" in aix*) wl='-Wl,' ;; darwin*) case $cc_basename in xlc*) wl='-Wl,' ;; esac ;; mingw* | cygwin* | pw32* | os2*) ;; hpux9* | hpux10* | hpux11*) wl='-Wl,' ;; irix5* | irix6* | nonstopux*) wl='-Wl,' ;; newsos6) ;; linux* | k*bsd*-gnu) case $cc_basename in icc* | ecc*) wl='-Wl,' ;; pgcc | pgf77 | pgf90) wl='-Wl,' ;; ccc*) wl='-Wl,' ;; como) wl='-lopt=' ;; *) case `$CC -V 2>&1 | sed 5q` in *Sun\ C*) wl='-Wl,' ;; esac ;; esac ;; osf3* | osf4* | osf5*) wl='-Wl,' ;; rdos*) ;; solaris*) wl='-Wl,' ;; sunos4*) wl='-Qoption ld ' ;; sysv4 | sysv4.2uw2* | sysv4.3*) wl='-Wl,' ;; sysv4*MP*) ;; sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) wl='-Wl,' ;; unicos*) wl='-Wl,' ;; uts4*) ;; esac fi # Code taken from libtool.m4's AC_LIBTOOL_PROG_LD_SHLIBS. hardcode_libdir_flag_spec= hardcode_libdir_separator= hardcode_direct=no hardcode_minus_L=no case "$host_os" in cygwin* | mingw* | pw32*) # FIXME: the MSVC++ port hasn't been tested in a loooong time # When not using gcc, we currently assume that we are using # Microsoft Visual C++. if test "$GCC" != yes; then with_gnu_ld=no fi ;; interix*) # we just hope/assume this is gcc and not c89 (= MSVC++) with_gnu_ld=yes ;; openbsd*) with_gnu_ld=no ;; esac ld_shlibs=yes if test "$with_gnu_ld" = yes; then # Set some defaults for GNU ld with shared library support. These # are reset later if shared libraries are not supported. Putting them # here allows them to be overridden if necessary. # Unlike libtool, we use -rpath here, not --rpath, since the documented # option of GNU ld is called -rpath, not --rpath. hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' case "$host_os" in aix3* | aix4* | aix5*) # On AIX/PPC, the GNU linker is very broken if test "$host_cpu" != ia64; then ld_shlibs=no fi ;; amigaos*) hardcode_libdir_flag_spec='-L$libdir' hardcode_minus_L=yes # Samuel A. Falvo II reports # that the semantics of dynamic libraries on AmigaOS, at least up # to version 4, is to share data among multiple programs linked # with the same dynamic library. Since this doesn't match the # behavior of shared libraries on other platforms, we cannot use # them. ld_shlibs=no ;; beos*) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; cygwin* | mingw* | pw32*) # hardcode_libdir_flag_spec is actually meaningless, as there is # no search path for DLLs. hardcode_libdir_flag_spec='-L$libdir' if $LD --help 2>&1 | grep 'auto-import' > /dev/null; then : else ld_shlibs=no fi ;; interix[3-9]*) hardcode_direct=no hardcode_libdir_flag_spec='${wl}-rpath,$libdir' ;; gnu* | linux* | k*bsd*-gnu) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; netbsd*) ;; solaris*) if $LD -v 2>&1 | grep 'BFD 2\.8' > /dev/null; then ld_shlibs=no elif $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*) case `$LD -v 2>&1` in *\ [01].* | *\ 2.[0-9].* | *\ 2.1[0-5].*) ld_shlibs=no ;; *) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then hardcode_libdir_flag_spec='`test -z "$SCOABSPATH" && echo ${wl}-rpath,$libdir`' else ld_shlibs=no fi ;; esac ;; sunos4*) hardcode_direct=yes ;; *) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; esac if test "$ld_shlibs" = no; then hardcode_libdir_flag_spec= fi else case "$host_os" in aix3*) # Note: this linker hardcodes the directories in LIBPATH if there # are no directories specified by -L. hardcode_minus_L=yes if test "$GCC" = yes; then # Neither direct hardcoding nor static linking is supported with a # broken collect2. hardcode_direct=unsupported fi ;; aix4* | aix5*) if test "$host_cpu" = ia64; then # On IA64, the linker does run time linking by default, so we don't # have to do anything special. aix_use_runtimelinking=no else aix_use_runtimelinking=no # Test if we are trying to use run time linking or normal # AIX style linking. If -brtl is somewhere in LDFLAGS, we # need to do runtime linking. case $host_os in aix4.[23]|aix4.[23].*|aix5*) for ld_flag in $LDFLAGS; do if (test $ld_flag = "-brtl" || test $ld_flag = "-Wl,-brtl"); then aix_use_runtimelinking=yes break fi done ;; esac fi hardcode_direct=yes hardcode_libdir_separator=':' if test "$GCC" = yes; then case $host_os in aix4.[012]|aix4.[012].*) collect2name=`${CC} -print-prog-name=collect2` if test -f "$collect2name" && \ strings "$collect2name" | grep resolve_lib_name >/dev/null then # We have reworked collect2 : else # We have old collect2 hardcode_direct=unsupported hardcode_minus_L=yes hardcode_libdir_flag_spec='-L$libdir' hardcode_libdir_separator= fi ;; esac fi # Begin _LT_AC_SYS_LIBPATH_AIX. echo 'int main () { return 0; }' > conftest.c ${CC} ${LDFLAGS} conftest.c -o conftest aix_libpath=`dump -H conftest 2>/dev/null | sed -n -e '/Import File Strings/,/^$/ { /^0/ { s/^0 *\(.*\)$/\1/; p; } }'` if test -z "$aix_libpath"; then aix_libpath=`dump -HX64 conftest 2>/dev/null | sed -n -e '/Import File Strings/,/^$/ { /^0/ { s/^0 *\(.*\)$/\1/; p; } }'` fi if test -z "$aix_libpath"; then aix_libpath="/usr/lib:/lib" fi rm -f conftest.c conftest # End _LT_AC_SYS_LIBPATH_AIX. if test "$aix_use_runtimelinking" = yes; then hardcode_libdir_flag_spec='${wl}-blibpath:$libdir:'"$aix_libpath" else if test "$host_cpu" = ia64; then hardcode_libdir_flag_spec='${wl}-R $libdir:/usr/lib:/lib' else hardcode_libdir_flag_spec='${wl}-blibpath:$libdir:'"$aix_libpath" fi fi ;; amigaos*) hardcode_libdir_flag_spec='-L$libdir' hardcode_minus_L=yes # see comment about different semantics on the GNU ld section ld_shlibs=no ;; bsdi[45]*) ;; cygwin* | mingw* | pw32*) # When not using gcc, we currently assume that we are using # Microsoft Visual C++. # hardcode_libdir_flag_spec is actually meaningless, as there is # no search path for DLLs. hardcode_libdir_flag_spec=' ' libext=lib ;; darwin* | rhapsody*) hardcode_direct=no if test "$GCC" = yes ; then : else case $cc_basename in xlc*) ;; *) ld_shlibs=no ;; esac fi ;; dgux*) hardcode_libdir_flag_spec='-L$libdir' ;; freebsd1*) ld_shlibs=no ;; freebsd2.2*) hardcode_libdir_flag_spec='-R$libdir' hardcode_direct=yes ;; freebsd2*) hardcode_direct=yes hardcode_minus_L=yes ;; freebsd* | dragonfly*) hardcode_libdir_flag_spec='-R$libdir' hardcode_direct=yes ;; hpux9*) hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' hardcode_libdir_separator=: hardcode_direct=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. hardcode_minus_L=yes ;; hpux10*) if test "$with_gnu_ld" = no; then hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' hardcode_libdir_separator=: hardcode_direct=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. hardcode_minus_L=yes fi ;; hpux11*) if test "$with_gnu_ld" = no; then hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' hardcode_libdir_separator=: case $host_cpu in hppa*64*|ia64*) hardcode_direct=no ;; *) hardcode_direct=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. hardcode_minus_L=yes ;; esac fi ;; irix5* | irix6* | nonstopux*) hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' hardcode_libdir_separator=: ;; netbsd*) hardcode_libdir_flag_spec='-R$libdir' hardcode_direct=yes ;; newsos6) hardcode_direct=yes hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' hardcode_libdir_separator=: ;; openbsd*) if test -f /usr/libexec/ld.so; then hardcode_direct=yes if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then hardcode_libdir_flag_spec='${wl}-rpath,$libdir' else case "$host_os" in openbsd[01].* | openbsd2.[0-7] | openbsd2.[0-7].*) hardcode_libdir_flag_spec='-R$libdir' ;; *) hardcode_libdir_flag_spec='${wl}-rpath,$libdir' ;; esac fi else ld_shlibs=no fi ;; os2*) hardcode_libdir_flag_spec='-L$libdir' hardcode_minus_L=yes ;; osf3*) hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' hardcode_libdir_separator=: ;; osf4* | osf5*) if test "$GCC" = yes; then hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' else # Both cc and cxx compiler support -rpath directly hardcode_libdir_flag_spec='-rpath $libdir' fi hardcode_libdir_separator=: ;; solaris*) hardcode_libdir_flag_spec='-R$libdir' ;; sunos4*) hardcode_libdir_flag_spec='-L$libdir' hardcode_direct=yes hardcode_minus_L=yes ;; sysv4) case $host_vendor in sni) hardcode_direct=yes # is this really true??? ;; siemens) hardcode_direct=no ;; motorola) hardcode_direct=no #Motorola manual says yes, but my tests say they lie ;; esac ;; sysv4.3*) ;; sysv4*MP*) if test -d /usr/nec; then ld_shlibs=yes fi ;; sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[01].[10]* | unixware7* | sco3.2v5.0.[024]*) ;; sysv5* | sco3.2v5* | sco5v6*) hardcode_libdir_flag_spec='`test -z "$SCOABSPATH" && echo ${wl}-R,$libdir`' hardcode_libdir_separator=':' ;; uts4*) hardcode_libdir_flag_spec='-L$libdir' ;; *) ld_shlibs=no ;; esac fi # Check dynamic linker characteristics # Code taken from libtool.m4's AC_LIBTOOL_SYS_DYNAMIC_LINKER. # Unlike libtool.m4, here we don't care about _all_ names of the library, but # only about the one the linker finds when passed -lNAME. This is the last # element of library_names_spec in libtool.m4, or possibly two of them if the # linker has special search rules. library_names_spec= # the last element of library_names_spec in libtool.m4 libname_spec='lib$name' case "$host_os" in aix3*) library_names_spec='$libname.a' ;; aix4* | aix5*) library_names_spec='$libname$shrext' ;; amigaos*) library_names_spec='$libname.a' ;; beos*) library_names_spec='$libname$shrext' ;; bsdi[45]*) library_names_spec='$libname$shrext' ;; cygwin* | mingw* | pw32*) shrext=.dll library_names_spec='$libname.dll.a $libname.lib' ;; darwin* | rhapsody*) shrext=.dylib library_names_spec='$libname$shrext' ;; dgux*) library_names_spec='$libname$shrext' ;; freebsd1*) ;; freebsd* | dragonfly*) case "$host_os" in freebsd[123]*) library_names_spec='$libname$shrext$versuffix' ;; *) library_names_spec='$libname$shrext' ;; esac ;; gnu*) library_names_spec='$libname$shrext' ;; hpux9* | hpux10* | hpux11*) case $host_cpu in ia64*) shrext=.so ;; hppa*64*) shrext=.sl ;; *) shrext=.sl ;; esac library_names_spec='$libname$shrext' ;; interix[3-9]*) library_names_spec='$libname$shrext' ;; irix5* | irix6* | nonstopux*) library_names_spec='$libname$shrext' case "$host_os" in irix5* | nonstopux*) libsuff= shlibsuff= ;; *) case $LD in *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ") libsuff= shlibsuff= ;; *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ") libsuff=32 shlibsuff=N32 ;; *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ") libsuff=64 shlibsuff=64 ;; *) libsuff= shlibsuff= ;; esac ;; esac ;; linux*oldld* | linux*aout* | linux*coff*) ;; linux* | k*bsd*-gnu) library_names_spec='$libname$shrext' ;; knetbsd*-gnu) library_names_spec='$libname$shrext' ;; netbsd*) library_names_spec='$libname$shrext' ;; newsos6) library_names_spec='$libname$shrext' ;; nto-qnx*) library_names_spec='$libname$shrext' ;; openbsd*) library_names_spec='$libname$shrext$versuffix' ;; os2*) libname_spec='$name' shrext=.dll library_names_spec='$libname.a' ;; osf3* | osf4* | osf5*) library_names_spec='$libname$shrext' ;; rdos*) ;; solaris*) library_names_spec='$libname$shrext' ;; sunos4*) library_names_spec='$libname$shrext$versuffix' ;; sysv4 | sysv4.3*) library_names_spec='$libname$shrext' ;; sysv4*MP*) library_names_spec='$libname$shrext' ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) library_names_spec='$libname$shrext' ;; uts4*) library_names_spec='$libname$shrext' ;; esac sed_quote_subst='s/\(["`$\\]\)/\\\1/g' escaped_wl=`echo "X$wl" | sed -e 's/^X//' -e "$sed_quote_subst"` shlibext=`echo "$shrext" | sed -e 's,^\.,,'` escaped_libname_spec=`echo "X$libname_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` escaped_library_names_spec=`echo "X$library_names_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` escaped_hardcode_libdir_flag_spec=`echo "X$hardcode_libdir_flag_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` LC_ALL=C sed -e 's/^\([a-zA-Z0-9_]*\)=/acl_cv_\1=/' <
One of the biggest challenges to getting started with embedded devices is that you
cannot just install a copy of Linux and expect to be able to compile a firmware.
Even if you did remember to install a compiler and every development tool offered,
you still would not have the basic set of tools needed to produce a firmware image.
The embedded device represents an entirely new hardware platform, which is
most of the time incompatible with the hardware on your development machine, so in a process called
cross compiling you need to produce a new compiler capable of generating code for
your embedded platform, and then use it to compile a basic Linux distribution to
run on your device.

The process of creating a cross compiler can be tricky, it is not something that is
regularly attempted and so there is a certain amount of mystery and black magic
associated with it. In many cases when you are dealing with embedded devices you will
be provided with a binary copy of a compiler and basic libraries rather than
instructions for creating your own -- it is a time saving step but at the same time
often means you will be using a rather dated set of tools. Likewise, it is also common
to be provided with a patched copy of the Linux kernel from the board or chip vendor,
but this is also dated and it can be difficult to spot exactly what has been
modified to make the kernel run on the embedded platform.

\subsection{Building an image}

OpenWrt takes a different approach to building a firmware; downloading, patching
and compiling everything from scratch, including the cross compiler. To put it
in simpler terms, OpenWrt does not contain any executables or even sources, it is an
automated system for downloading the sources, patching them to work with the given
platform and compiling them correctly for that platform. What this means is that
just by changing the template, you can change any step in the process.

As an example, if a new kernel is released, a simple change to one of the Makefiles
will download the latest kernel, patch it to run on the embedded platform and produce
a new firmware image -- there is no work to be done trying to track down an unmodified
copy of the existing kernel to see what changes had been made, the patches are
already provided and the process ends up almost completely transparent. This does not
just apply to the kernel, but to anything included with OpenWrt -- It is this one
simple understated concept which is what allows OpenWrt to stay on the bleeding edge
with the latest compilers, latest kernels and latest applications.

So let's take a look at OpenWrt and see how this all works.


\subsubsection{Download openwrt}

This article refers to the "Kamikaze" branch of OpenWrt, which can be downloaded via
subversion using the following command:

\begin{Verbatim}
$ svn co https://svn.openwrt.org/openwrt/trunk kamikaze
\end{Verbatim}

Additionally, ther is a trac interface on \href{https://dev.openwrt.org/}{https://dev.openwrt.org/}
which can be used to monitor svn commits and browse the sources.


\subsubsection{The directory structure}

There are four key directories in the base:

\begin{itemize}
    \item \texttt{tools}
    \item \texttt{toolchain}
    \item \texttt{package}
    \item \texttt{target}
\end{itemize}

\texttt{tools} and \texttt{toolchain} refer to common tools which will be
used to build the firmware image, the compiler, and the C library.
The result of this is three new directories, \texttt{tool\_build}, which is a temporary
directory for building the target independent tools, \texttt{toolchain\_build\_\textit{<arch>}}
which is used for building the toolchain for a specific architecture, and
\texttt{staging\_dir\_\textit{<arch>}} where the resulting toolchain is installed.
You will not need to do anything with the toolchain directory unless you intend to
add a new version of one of the components above.

\begin{itemize}
    \item \texttt{tool\_build}
    \item \texttt{toolchain\_build\_\textit{<arch>}}
\end{itemize}

\texttt{package} is for exactly that -- packages. In an OpenWrt firmware, almost everything
is an \texttt{.ipk}, a software package which can be added to the firmware to provide new
features or removed to save space. Note that packages are also maintained outside of the main
trunk and can be obtained from subversion at the following location:

\begin{Verbatim}
$ svn co https://svn.openwrt.org/openwrt/packages ../packages
\end{Verbatim}

Those packages can be used to extend the functionality of the build system and need to be
symlinked into the main trunk. Once you do that, the packages will show up in the menu for
configuration. From kamikaze you would do something like this:

\begin{Verbatim}
$ ls
kamikaze  packages
$ ln -s packages/net/nmap kamikaze/package/nmap
\end{Verbatim}

To include all packages, issue the following command:

\begin{Verbatim}
$ ln -s packages/*/* kamikaze/package/
\end{Verbatim}


\texttt{target} refers to the embedded platform, this contains items which are specific to
a specific embedded platform. Of particular interest here is the "\texttt{target/linux}"
directory which is broken down by platform and contains the kernel config and patches
to the kernel for a particular platform. There's also the "\texttt{target/image}" directory
which describes how to package a firmware for a specific platform.

Both the target and package steps will use the directory "\texttt{build\_\textit{<arch>}}"
as a temporary directory for compiling. Additionally, anything downloaded by the toolchain,
target or package steps will be placed in the "\texttt{dl}" directory.

\begin{itemize}
    \item \texttt{build\_\textit{<arch>}}
    \item \texttt{dl}
\end{itemize}

\subsubsection{Building OpenWrt}

While the OpenWrt build environment was intended mostly for developers, it also has to be
simple enough that an inexperienced end user can easily build his or her own customized firmware.

Running the command "\texttt{make menuconfig}" will bring up OpenWrt's configuration menu
screen, through this menu you can select which platform you're targeting, which versions of
the toolchain you want to use to build and what packages you want to install into the
firmware image. Note that it will also check to make sure you have the basic dependencies for it
to run correctly.  If that fails, you will need to install some more tools in your local environment
before you can begin.

Similar to the linux kernel config, almost every option has three choices,
\texttt{y/m/n} which are represented as follows:

\begin{itemize}
    \item{\texttt{<*>} (pressing y)} \\
        This will be included in the firmware image
    \item{\texttt{<M>} (pressing m)} \\
        This will be compiled but not included (for later install)
    \item{\texttt{< >} (pressing n)} \\
        This will not be compiled
\end{itemize}

After you've finished with the menu configuration, exit and when prompted, save your
configuration changes.

If you want, you can also modify the kernel config for the selected target system.
simply run "\texttt{make kernel\_menuconfig}" and the build system will unpack the kernel sources
(if necessary), run menuconfig inside of the kernel tree, and then copy the kernel config
to \texttt{target/linux/\textit{<platform>}/config} so that it is preserved over
"\texttt{make clean}" calls.

To begin compiling the firmware, type "\texttt{make}". By default
OpenWrt will only display a high level overview of the compile process and not each individual
command.

\subsubsection{Example:}

\begin{Verbatim}
make[2] toolchain/install
make[3] -C toolchain install
make[2] target/compile
make[3] -C target compile
make[4] -C target/utils prepare

[...]
\end{Verbatim}

This makes it easier to monitor which step it's actually compiling and reduces the amount
of noise caused by the compile output. To see the full output, run the command
"\texttt{make V=99}".

During the build process, buildroot will download all sources to the "\texttt{dl}"
directory and will start patching and compiling them in the "\texttt{build\_\textit{<arch>}}"
directory. When finished, the resulting firmware will be in the "\texttt{bin}" directory
and packages will be in the "\texttt{bin/packages}" directory.


\subsection{Creating packages}

One of the things that we've attempted to do with OpenWrt's template system is make it
incredibly easy to port software to OpenWrt. If you look at a typical package directory
in OpenWrt you'll find two things:

\begin{itemize}
    \item \texttt{package/\textit{<name>}/Makefile}
    \item \texttt{package/\textit{<name>}/patches}
	\item \texttt{package/\textit{<name>}/files}
\end{itemize}

The patches directory is optional and typically contains bug fixes or optimizations to
reduce the size of the executable. The package makefile is the important item, provides
the steps actually needed to download and compile the package.

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.

Looking at one of the package makefiles, you'd hardly recognize it as a makefile.
Through what can only be described as blatant disregard and abuse of the traditional
make format, the makefile has been transformed into an object oriented template which
simplifies the entire ordeal.

Here for example, is \texttt{package/bridge/Makefile}:

\begin{Verbatim}[frame=single,numbers=left]
#
# Copyright (C) 2006 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# $Id: Makefile 5624 2006-11-23 00:29:07Z nbd $

include $(TOPDIR)/rules.mk

PKG_NAME:=bridge
PKG_VERSION:=1.0.6
PKG_RELEASE:=1

PKG_SOURCE:=bridge-utils-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=@SF/bridge
PKG_MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd
PKG_CAT:=zcat

PKG_BUILD_DIR:=$(BUILD_DIR)/bridge-utils-$(PKG_VERSION)

include $(INCLUDE_DIR)/package.mk

define Package/bridge
  SECTION:=net
  CATEGORY:=Base system
  TITLE:=Ethernet bridging configuration utility
  DESCRIPTION:=\
    Manage ethernet bridging: a way to connect networks together to \\\
    form a larger network.
  URL:=http://bridge.sourceforge.net/
endef

define Build/Configure
    $(call Build/Configure/Default, \
        --with-linux-headers="$(LINUX_DIR)" \
    )
endef

define Package/bridge/install
    $(INSTALL_DIR) $(1)/usr/sbin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/brctl/brctl $(1)/usr/sbin/
endef

$(eval $(call BuildPackage,bridge))
\end{Verbatim}

As you can see, there's not much work to be done; everything is hidden in other makefiles
and abstracted to the point where you only need to specify a few variables.

\begin{itemize}
    \item \texttt{PKG\_NAME} \\
        The name of the package, as seen via menuconfig and ipkg
    \item \texttt{PKG\_VERSION} \\
        The upstream version number that we are downloading
    \item \texttt{PKG\_RELEASE} \\
        The version of this package Makefile
    \item \texttt{PKG\_SOURCE} \\
        The filename of the original sources
    \item \texttt{PKG\_SOURCE\_URL} \\
        Where to download the sources from (no trailing slash), you can add multiple download sources by separating them with a \\ and a carriage return.
    \item \texttt{PKG\_MD5SUM} \\
        A checksum to validate the download
    \item \texttt{PKG\_CAT} \\
        How to decompress the sources (zcat, bzcat, unzip)
    \item \texttt{PKG\_BUILD\_DIR} \\
        Where to compile the package
\end{itemize}

The \texttt{PKG\_*} variables define where to download the package from;
\texttt{@SF} is a special keyword for downloading packages from sourceforge. There is also
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.

The md5sum (if present) is used to verify the package was downloaded correctly and
\texttt{PKG\_BUILD\_DIR} defines where to find the package after the sources are
uncompressed into \texttt{\$(BUILD\_DIR)}.

At the bottom of the file is where the real magic happens, "BuildPackage" is a macro
set up by the earlier include statements. BuildPackage only takes one argument directly --
the name of the package to be built, in this case "\texttt{bridge}". All other information
is taken from the define blocks. This is a way of providing a level of verbosity, it's
inherently clear what the contents of the \texttt{description} template in
\texttt{Package/bridge} is, which wouldn't be the case if we passed this information
directly as the Nth argument to \texttt{BuildPackage}.

\texttt{BuildPackage} uses the following defines:

\textbf{\texttt{Package/\textit{<name>}}:} \\
    \texttt{\textit{<name>}} matches the argument passed to buildroot, this describes
    the package the menuconfig and ipkg entries. Within \texttt{Package/\textit{<name>}}
    you can define the following variables:

    \begin{itemize}
        \item \texttt{SECTION} \\
            The type of package (currently unused)
        \item \texttt{CATEGORY} \\
            Which menu it appears in menuconfig: Network, Sound, Utilities, Multimedia ...
        \item \texttt{TITLE} \\
            A short description of the package
        \item \texttt{URL} \\
            Where to find the original software
        \item \texttt{MAINTAINER} (optional) \\
            Who to contact concerning the package
        \item \texttt{DEPENDS} (optional) \\
            Which packages must be built/installed before this package. To reference a dependency defined in the same Makefile, use \textit{<dependency name>}. If defined as an external package, use \textit{+<dependency name>}. For a kernel version dependency use: \textit{@LINUX\_2\_<minor version>}
    \end{itemize}

\textbf{\texttt{Package/\textit{<name>}/conffiles} (optional):} \\
   A list of config files installed by this package, one file per line.

\textbf{\texttt{Build/Prepare} (optional):} \\
   A set of commands to unpack and patch the sources. You may safely leave this
   undefined.

\textbf{\texttt{Build/Configure} (optional):} \\
   You can leave this undefined if the source doesn't use configure or has a
   normal config script, otherwise you can put your own commands here or use
   "\texttt{\$(call Build/Configure/Default,\textit{<first list of arguments, second list>})}" as above to
   pass in additional arguments for a standard configure script. The first list of arguments will be passed to the configure script like that: $--arg 1$ $--arg 2$. The second list contains arguments that should be defined before running the configure script such as autoconf or compiler specific variables.

\textbf{\texttt{Build/Compile} (optional):} \\
   How to compile the source; in most cases you should leave this undefined.

\textbf{\texttt{Package/\textit{<name>}/install}:} \\
   A set of commands to copy files out of the compiled source and into the ipkg
   which is represented by the \texttt{\$(1)} directory. Note that there are currently
   4 defined install macros:
   \begin{itemize}
       \item \texttt{INSTALL\_DIR} \\
           install -d -m0755
       \item \texttt{INSTALL\_BIN} \\
           install -m0755
       \item \texttt{INSTALL\_DATA} \\
           install -m0644
       \item \texttt{INSTALL\_CONF} \\
           install -m0600
   \end{itemize}

The reason that some of the defines are prefixed by "\texttt{Package/\textit{<name>}}"
and others are simply "\texttt{Build}" is because of the possibility of generating
multiple packages from a single source. OpenWrt works under the assumption of one
source per package Makefile, but you can split that source into as many packages as
desired. Since you only need to compile the sources once, there's one global set of
"\texttt{Build}" defines, but you can add as many "Package/<name>" defines as you want
by adding extra calls to \texttt{BuildPackage} -- see the dropbear package for an example.

After you have created your \texttt{package/\textit{<name>}/Makefile}, the new package
will automatically show in the menu the next time you run "make menuconfig" and if selected
will be built automatically the next time "\texttt{make}" is run.


\subsection{Conventions}

There are a couple conventions to follow regarding packages:

\begin{itemize}
    \item \texttt{files}
    \begin{enumerate}
        \item configuration files follow the convention \\
        \texttt{\textit{<name>}.conf}
        \item init files follow the convention \\
        \texttt{\textit{<name>}.init}
    \end{enumerate}
    \item \texttt{patches}
    \begin{enumerate}
        \item patches are numerically prefixed and named related to what they do
    \end{enumerate}
\end{itemize}

\subsection{Troubleshooting}

If you find your package doesn't show up in menuconfig, try the following command to
see if you get the correct description:

\begin{Verbatim}
  TOPDIR=$PWD make -C package/<name> DUMP=1 V=99
\end{Verbatim}

If you're just having trouble getting your package to compile, there's a few
shortcuts you can take. Instead of waiting for make to get to your package, you can
run one of the following:

\begin{itemize}
    \item \texttt{make package/\textit{<name>}-clean V=99}
    \item \texttt{make package/\textit{<name>}-install V=99}
\end{itemize}

Another nice trick is that if the source directory under \texttt{build\_\textit{<arch>}}
is newer than the package directory, it won't clobber it by unpacking the sources again.
If you were working on a patch you could simply edit the sources under the
\texttt{build\_\textit{<arch>}/\textit{<source>}} directory and run the install command above,
when satisfied, copy the patched sources elsewhere and diff them with the unpatched
sources. A warning though - if you go modify anything under \texttt{package/\textit{<name>}}
it will remove the old sources and unpack a fresh copy.

Other useful targets include:

\begin{itemize}
    \item \texttt{make package/\textit{<name>}-prepare V=99}
    \item \texttt{make package/\textit{<name>}-compile V=99}
    \item \texttt{make package/\textit{<name>}-configure V=99}
\end{itemize}