Building gcc as a Cross-

Neil Klingensmith Department of Electrical and Computer Engineering University of Wisconsin-Madison, USA [email protected]

September 8, 2011

1 Introduction mands to suit your needs. For example, to com- pile for ARM, specify This document describes the process for build- ing gcc as a cross-compiler on UNIX-based sys- --target=arm-eabi tems. Cross-compiling versions of gcc are availi- ble in binary format from commercial organiza- on the configure command line instead of tions such as Code Sourcery [5] and Microcross --target=m68k-elf. [7]. It is usually faster to download and install a free binary distribution than to build one from 1.2 Disclaimer scratch. The instructions in this document are known The author of this document, the University to work in -based operating systems as well of Wisconsin, and the Engineering Department, as in Mac OS X versions 10.5 and 10.6. Please assume no responsibility for damage caused to refer to section 1.3 for more details on software property during attempts to follow the instruc- prerequisites on different systems. tions in the guide. Instructions in this guide are Please note that this document is a work in provided without warranty of any kind, explicit progress. Suggestions on how to improve or clar- or implied. ify its content are greatly appreciated. 1.3 Requirements 1.1 Target Architectures This section explains prerequisites for different This guide provides explicit instructions for operating systems. In general, the procedure building gcc to target the ColdFire (m68k) ar- should work on any system with a working native chitecture processors manufactured by Freescale. version of gcc. However, the instructions in this guide have been Please note that the compilation and in- confirmed to work for ARM targets as well. In stallation process should be run as the order to build for a different target architecture, root user to avoid problems with file ac- modify the --target= field in the configure com- cess permissions.

1 In the past, some users reported problems 1. Compile binutils for the embedded target. compiling a working version of gcc and related utilities using certain combinations of gcc and 2. Build a bootstrap version of gcc. 1 binutils [6]. 3. Build (an embedded ) using the bootstrap version of gcc. 1.3.1 Linux 4. Rebuild gcc against newlib. The procedure outlined in this guide has been tested and is known to work with Gentoo The compilation process is complicated by a Linux[2]. No modifications to the procedure or circular dependence between gcc and newlib. additional software needs to be installed. The compiler must be present on the system to Other distributions of the GNU/Linux oper- compile newlib for the embedded target. How- ating system may require the user to explicitly ever, gcc refers to symbols in the C library when install gcc. compiling user programs, so gcc must be recom- piled against newlib. 1.3.2 Mac OS X This guide is known to be compatible with Mac 3 Procedure OS versions 10.5 and 10.6. It has not been tested on versions 10.7 or higher. Reports of success or The for gcc [1] and binutils [3] is failure would be appreciated. available from the GNU website, and newlib [4] Since Mac OS X is not distributed with a na- is available from Red Hat. tive version of gcc, the Apple developer tools (XCode) must be installed before proceed- 3.1 Binutils ing with the instructions in this guide. Extract binutils to a working directory: Additionally, Mac OS users need to install a GNU version of the mpfr library. MPFR can be installed with the Macports package manager tar xvjf binutils-2.19.1.tar.bz2 (http://www.macports.org). Once Macports is present, use the command Use this procedure to extract the gcc and newlib packages as well. port install mpfr Create a new directory under binutils called to install mpfr. build. This is the directory from which the com- pilation commands will be issued. Also, make a directory for the binaries called m68k-elftools. 2 Procedural Overview mkdir binutils-2.19.1/build The build process will proceed as follows: mkdir /opt/local/m68k-elftools 1GNU object file manipulation suite, required to build gcc cd binutils-2.19.1/build

2 Note that the directory paths for the above make -j32 command lines will vary depending on the ver- sion of binutils. make install Configure binutils. 3.3 Newlib ../configure --target=m68k-elf Change to the newlib directory and configure --prefix=/opt/local/m68k-elftools newlib using the bootstrap version of gcc. --enable-interwork --enable-multilib --with-gnu-as --with-gnu-ld --disable-nls cd ~/newlib-x.x.x

Build and install binutils. mkdir build

make -j3 cd build make install Build and install newlib.

3.2 Bootstrap gcc ../configure --target=m68k-elf Change to the gcc/build directory and config- ure the bootstrap version of gcc. --prefix=/opt/local/m68k-elftools/ --enable-interwork --enable-multilib cd ~/gcc-x.x.x --with-gnu-as --with-gnu-ld --disable-nls mkdir build cd build make -j3 Build and install the bootstrap version of gcc. make install

../configure 3.4 Final gcc --with-libiconv-prefix=/opt/local Change back to the gcc directory and rebuild --target=m68k-elf gcc against newlib. --prefix=/opt/local/m68k-elftools/ --enable-interwork --enable-multilib cd ~/gcc-x.x.x/build --enable-languages="c" --with-newlib rm -rf * --without-headers --disable-shared 2The -jN option specifies the number of concurrent --with-gnu-as --with-gnu-ld jobs run by make. A commonly accepted number of jobs --with-gmp=/opt/local is 1 + (number of cores). For example, on a dual-core machine, the command to begin the build process would --with-mpfr=/opt/local --disable-libssp be make -j3.

3 ../configure [5] CodeSourcery. http://www.codesourcery.com. --with-libiconv-prefix=/opt/local [6] Dan Kegel. Crosstool build results. --target=m68k-elf http://kegel.com/crosstool/crosstool- --prefix=/opt/local/m68k-elftools/ 0.43/buildlogs/. --enable-interwork --enable-multilib [7] Microcross. http://microcross.com. --enable-languages="c" --with-newlib --disable-shared --with-gnu-as --with-gnu-ld --with-gmp=/opt/local --with-mpfr=/opt/local --disable-libssp

make -j3 make install Add the binary directory to the envi- ronment variable. export PATH=/opt/local/m68k-elftools/bin:$PATH

This will cause the terminal to automatically recognize commands like m68k-elf-gcc. The PATH variable is reset each time a users logs in, so the above command should be added to each user’s bash profile script. The location of the profile script varies depending on the version of bash, but is usually located in the user’s home directory and named .profile or similar.

References

[1] Gcc, the . http://gcc.gnu.org/.

[2] Gentoo linux. http://www.gentoo.org.

[3] Gnu binutils. http://www.gnu.org/software/binutils/.

[4] Newlib. http://sourceware.org/newlib/.

4