C HAPTER 14 ■ ■ ■ Building and Using Cross-

One of the most popular uses of today is in developing and deploying embedded systems, which are small, typically stand-alone computer systems used throughout industry and consumer electronics for a variety of purposes. In industry, embedded Linux systems are used for everything from factory and communications infrastructure process control to monitoring and maintenance systems. In consumer electronics, embedded Linux systems are used in an incredible number of devices, including cell phones, home networking gateways and routers, wireless access points, PDAs, digital video recorders such as the TiVo, and home entertainment systems. The primary issue in developing embedded Linux systems is . In order to develop applications that run on top of an embedded , you might think that you would need a stable version of that same embedded Linux distribution on which to compile your applications. This is a significant problem for many embedded systems, which often feature low-power proces- sors, limited amounts of memory, and limited amounts of physical storage (and sometimes none at all). In many cases, this makes it difficult or impossible to develop embedded Linux applications directly on the system where they will finally be deployed. Enter cross-compilation.

What Is Cross-Compilation?

Cross-compilation is a technique in which a special is used that runs on a desktop devel- opment system yet produces that run on a different platform. This enables developers to take full advantage of the CPU horsepower, memory, and storage available on their desktop devel- opment systems (known as hostt systems in cross-compilation lingo), while still producing executables that are tailored to the architecture, instruction set, and capabilities of their (known as target systems in cross-compilation terms). Because the GCC compilers have been ported to so many different types of desktop development systems and have also been fine-tuned to produce binaries for so many different types of systems in general, GCC is extremely well-suited for use as a cross-compiler—everything that you need is already in there, you just have to tweak and build the compilers correctly. This is not to say that building cross-compilers is easy. A full desktop environment for cross- compilation requires not only special versions of the compilers, configured for cross-compilation, but also requires similarly configured versions of the utilities used to build application binaries and libraries (known as binutils) and a similarly configured version of the C that holds most of the functions required by those applications and libraries. As I will explain throughout the remainder of this chapter, a variety of different ways to build cross-compilers are available today. The one that you choose depends on the and the operating system distribution on which you are doing your development and the type of cross- compiler that you are trying to build. This chapter focuses on the most popular open source tools for

W. Hagen, The Definitive Guide to GCC 299 © William von Hagen 2006 300 C HAPTER 14 ■ BUILDING AND USING C CROSS-COMPILERS

building cross-compilers—crosstool and —and also discusses how to manually build cross- compilers when necessary. Other tools for building cross-compilers are also available, such as Gentoo’s crossdev project (http://dev.gentoo.org/~redhatter/misc/xdistcc.sh.gz) and DENX Software Engineering’s ELDK (Embedded Linux Development Kit) (http://www.denx.de/wiki/DULG/ELDK), but are not discussed here because they are too distribution-specific or less commonly used than the other approaches discussed in this chapter.

CROSS-COMPILATION TARGETS SUPPORTED BY GCC

If you’ll pardon the expression, the list of cross-compilation targets supported by GCC is a moving target, changing with each release of GCC. Rather than just listing available targets, which will certainly have changed by the time that the major release of GCC occurs, this section explains how to find the list of valid targets that can be produced from the for the version of the compiler that you are building. The Wikipedia entry for GCC lists a variety of architectures and processors for which GCC is (or has been) supported. The definitive list of valid patterns for standard GCC compiler and tool prefixes is stored in the file gcc/config.gcc delivered with any GCC source code release. This file lists all of the wildcard matches that are supported in prefixes, and therefore shows you the targets that are available. GCC prefixes are traditionally of the form CPUTYPE-MANUFACTURER-OPERATINGSYSTEM, though more recent GCC prefixes have adopted a new four-part form, CPUTYPE-MANUFACTURER-KERNEL-OPERATINGSYSTEM. For example, the standard prefix for a generic PowerPC compiler is powerpc-unknown-linux-gnu: a PowerPC CPU, no specific manufacturer, the Linux kernel, and a GNU operating system. You can also examine the default prefix name that GCC’s configuration utilities will try to use by executing the file config.sub in the main GCC source directory, supplying a sample prefix as an argument. For example, executing the following command shows me the prefix that the GCC tools will use if I specify a target of powerpc-linux:

$ ./config.sub powerpc-linux

powerpc-unknown-linux-gnu

This sort of test also has the advantage of displaying an “invalid configuration” message when you specify a prefix that GCC’s configuration utilities do not recognize at all. When building any cross-compiler manually, you should check the host/target–specific information in the installation notes for the version of the GCC source that you are building from. These are always located in the file doc/HTML/specific.html, relative to the root of your GCC source tree. The version of this document for the latest offi- cial release of GCC is always available at the URL http://gcc.gnu.org/install/specific.html. Tools such as crosstool and buildroot encapsulate this information for the versions of GCC that they can build and the targets that they support; but if you have to build a cross-compiler manually, you should always check this file for any specific information about the target platform that you are building a cross-compiler for.

Using crosstool to Build Cross-Compilers

crosstool (http://www.kegel.com/crosstool) is a set of shell scripts and platform- and package-specific definition files by Dan Kegel—a well-known embedded and cross-platform Linux developer and general open source advocate—that takes a significant amount of the magic out of building cross- compilers as long as you want to use a Glibc-based cross-compiler. The crosstool package was inspired by an earlier set of scripts and related information called crossgcc, which was written and maintained by Bill Gatliff (http://www.billgatliff.com), a well-known embedded systems developer and