AVR/ARM Cross Toolchain for OS X
This document contains instructions to build AVR and ARM cross compilers on Mac OS X from source packages. Note, that precompiled binaries are available in our download area.
So far, only the ARM toolchain had been tested on Leopard, running on a PPC. Be warned, that building on other OS X releases or building the AVR toolchain may not work as described.
Apple's Xcode package contains all the tools we need to build the cross toolchain. Open Terminal and enter
$ gcc --version
If the response is similar to
powerpc-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465) Copyright (C) 2005 Free Software Foundation, Inc.
then GCC is already available and probably there is no need to install it again. However, if the response is
-bash: gcc: command not found
then you need to download Xcode from
A registration is required, but this is free of charge.
Leave the Terminal window open. We'll need it later on.
Download Toolchain Sources
In the Terminal window create a new directory and change to it. Feel free to use any name and create it at any place.
$ mkdir toolchain $ cd toolchain
Now use the following commands to download the sources.
$ curl -O ftp://gcc.gnu.org/pub/binutils/releases/binutils-2.18.tar.bz2 $ curl -O ftp://gcc.gnu.org/pub/gcc/releases/gcc-4.2.2/gcc-core-4.2.2.tar.bz2 $ curl -O ftp://gcc.gnu.org/pub/gcc/releases/gcc-4.2.2/gcc-g++-4.2.2.tar.bz2 $ curl -O ftp://sourceware.org/pub/gdb/releases/gdb-6.7.1.tar.bz2
Additionally we need the C libraries, avr-libc for the AVR or newlib for the ARM platform. Get the newlib sources with
The avr-libc sources can be downloaded with
Next we unpack the sources, using
$ tar -xjf binutils-2.18.tar.bz2 $ tar -xjf gcc-core-4.2.2.tar.bz2 $ tar -xjf gcc-g++-4.2.2.tar.bz2 $ tar -xjf gdb-6.7.1.tar.bz2
$ tar -xjf avr-libc-1.6.1.tar.bz2
for the AVR library or
$ tar -xzf newlib-1.16.0.tar.gz
We will finally end up with 3 new subdirectories in our toolcahin folder. You may remove the downloaded archives or keep them in case you want to start all over again later on.
Build Environment Setup
The toolchain will be built in 5 steps:
- Building the binary utilities
- Building the compiler
- Building the libraries
- Rebuilding the compiler (ARM)
- Building the debugger
In the second step we get different results while building for AVR or ARM. For ARM targets we are building an intermediate compiler first, which is rebuilt in step 4.
The last step, building GDB, is optional and needed for in-circuit debugging only. This requires some additional tools, which are not discussed in this document.
In all steps we are going to use the same environment. Thus, once set in the Terminal window, you need to keep the window open to preserve these settings.
First we specify the target platform:
$ export target=avr
for the AVR or
$ export target=arm-elf
for the ARM toolchain. For both targets we set
$ export prefix=/usr/local/$target
which defines the directory where the final binaries will be installed. If you prefer any different location, go ahead. Within this folder we need to create a bin directory in advance and add that to our PATH variable.
$ sudo mkdir -p $prefix/bin $ export PATH=$prefix/bin:$PATH
Note, that we need root authorization to access the path /usr/local, which had been stored in the variable $prefix. Thus the prepended sudo command, which will prompt you for your user password. If you chose a different location, this may not be required.
Building the Binary Utilities
GNU binutils is a collection of binary tools, which needs to be build first. As with all parts of the toolchain, we create a specific build directory inside the package's directory.
$ cd binutils-2.18 $ mkdir build-$target $ cd build-$target/
The next command, which configures the build, differs slightly among targets. For the AVR we simply use
$ ../configure --target=$target --prefix=$prefix \ --disable-nls --disable-shared --disable-threads --with-gcc --with-gnu-as --with-gnu-ld
while the ARM requires
$ ../configure --target=$target --prefix=$prefix --enable-interwork --enable-multilib \ --disable-nls --disable-shared --disable-threads --with-gcc --with-gnu-as --with-gnu-ld
When this went through without errors, we can start building and installing the binaries.
$ make $ sudo make install $ cd ../..
This will take several minutes. When done, try
$ avr-as --version
to check the AVR build or
$ arm-elf-as --version
to check the ARM variant.
This should display the GNU assembler version information. If the command can't be found, check your PATH and prefix settings.
Building the Compiler
Building the ARM compiler requires a readily built library. Building the newlib library, however, requires a compiler. To solve this chicken and egg problem, we build an intermediate bootstrap compiler first, which requires the library's header files only. For the AVR, however, the final compiler is built in this step.
For both targets, create a build directory and change to it.
$ cd gcc-4.2.2 $ mkdir build-$target $ cd build-$target/
As expected, the configure options differ among our targets. For the AVR use
$ ../configure --target=$target --prefix=$prefix \ --disable-nls --disable-shared --disable-threads \ --with-gcc --with-gnu-ld --with-gnu-as --with-dwarf2 \ --enable-languages=c,c++ --disable-libssp -v
The intermediate ARM compiler is configured as follows.
$ sudo ../configure --target=$target --prefix=$prefix \ --disable-nls --disable-shared --disable-threads \ --with-gcc --with-gnu-ld --with-gnu-as --with-dwarf2 \ --enable-languages=c,c++ --enable-interwork \ --enable-multilib --with-newlib \ --with-headers=../../newlib-1.16.0/newlib/libc/include \ --disable-libssp --disable-libstdcxx-pch \ --disable-libmudflap --disable-libgomp -v
Note, that we may need the sudo command to gain access to the installation directory, where the newlib header files will be copied to.
Building and installing the AVR compiler is done using the standard commands.
$ make $ sudo make install $ cd ../..
To build and install the intermediate ARM compiler use
$ mkdir -p libiberty libcpp fixincludes $ make all-gcc $ sudo make install-gcc $ cd ../..
Again, this will take several minutes. When done, we can use
$ avr-gcc --version
to test the AVR compiler or
$ arm-elf-gcc --version
for the ARM compiler. This should display the compiler version information.
If the build fails, you may try to exclude the C++ compiler by simply removing ,c++ from the --enable-languages option.
Building the Libraries
As stated earlier, we use avr-libc for the AVR and newlib for the ARM.
To configure the AVR build, use
$ cd avr-libc-1.6.1 $ mkdir build-$target $ cd build-$target/ $ ../configure --prefix=$prefix --build=`../config.guess` --host=$target
Note the grave accents, acute accents will not work.
The following commands are used to configure the ARM library.
$ cd newlib-1.16.0 $ mkdir build-$target $ cd build-$target/ $ ../configure --target=$target --prefix=$prefix --enable-interwork --enable-multilib
Both libraries are built with
$ make $ sudo make install $ cd ../..
Rebuilding the Compiler
As explained above, this step is only required when building for the ARM target.
$ cd gcc-4.2.2/build-$target $ make $ sudo make install $ cd ../..
Building the Debugger
Even if we do not have all tools available for in-circuit debugging, it is a good idea to build the GNU debugger now, so it will be available later. Here are the related commands, valid for both targets:
$ cd gdb-6.7.1 $ mkdir build-$target $ cd build-$target/ $ ../configure --target=$target --prefix=$prefix --disable-nls $ sudo make install $ cd ../..
Stripping the Binaries
In an additional step we may strip the installed binaries to save disk space and decrease load times.
$ sudo strip $prefix/bin/* $ sudo strip $prefix/$target/bin/* $ sudo strip $prefix/libexec/gcc/$target/4.2.2/*
Now we have all tools available on our OS X machine to create firmware binaries for AVR and/or ARM target boards, either from C, C++ or assembler source code.
If you want to compile applications running on Nut/OS, you may now download the Ethernut package from the local download page.
Selecting a tool for uploading firmware binaries to your target board highly depends on the specific CPU and the programming adapter being used. For AVR targets, AVRDUDE is a good choice, while we recommend OpenOCD for ARM targets.
The document OpenOCD and Ethernut 3 contains additional information about installing and running OpenOCD on a Mac.
Finally you may want to set up an integrated development environment, which includes a source code editor, compiler, linker, debugger and more. If you are already familiar with Xcode, this might be the right way to go. If you are, like me, working on several different platforms, then Eclipse is probably a good choice.
Building a GNU ARM cross-compiler toolchain on Mac OS X.
Describes another way to set up an ARM toolchain on Mac OS X.
Blog about setting up a cross development for the iPhone, which is based on an ARM CPU.
Xcode is Apple's premiere development environment for Mac OS X.
Homepage of the GNU Compiler Collection.
GNU Binutils' homepage.
GDB, the GNU Project Debugger.
An open development platform based on Java.
AVRDUDE is software for programming Atmel AVR Microcontrollers.
The Open On-Chip Debugger aims to provide debugging, in-system programming and boundary-scan testing for ARM based target devices.