Setting up the codesourcery toolchain for X86 to ARM9 cross compiling

From NAS-Central Main Wiki
Revision as of 19:12, 19 September 2011 by Maddes (Talk | contribs)

Jump to: navigation, search



This wiki page will show how to install the Mentor Graphics' (ex-CodeSourcery) ARM toolchains to cross compile an ARMel kernel. One simple script per toolchain will set up the environment for cross-compiling (path, architecture and compiler prefix) in a separate shell process.

The example is based on a Debian x86 VM running in VirtualBox on Windows XP x86. Pure Linux users should be able to recognise the windows parts and ignore them, additionally it should be possible for them to transfer the Debian specific commands to their Linux system.

About the Toolchain

Mentor Graphics (ex-CodeSourcery) has created the cross toolchain for ARM processors in cooperation with ARM Ltd.

The cross toolchain version 2005q3 was used to compile the stock kernel of all arm9 based boxes from buffalo. As cross compiling on a x86 workstation is much faster than native compiling on the slow boxes, it is a good reason to install it and compile with it.

Installing VirtualBox and Debian VM

For Windows download and install a Virtual Machine software, e.g. VirtualBox. There are others for Windows like VMWare, VirtualPC, and more.

For installing Debian download an installation ISO image from a debian mirror under /debian-cd/current/i386/. DVD ISO #1 is sufficient to install a desktop environment, or use the small CD ISO NetInst and download the packages while installing.

Create a virtual machine with 512-1024MB RAM (not more than half of your RAM) and a dynamically growing harddisk with 32GB, that should be enough for several kernels and modules. Assign the installation ISO to the CD-ROM of the virtual machine. Then start the virtual machine to install Debian.

For cross compiling a Debian standard system is sufficient. A desktop could be installed too, which is nicer to handle several windows. 32MB for Video RAM should be sufficient. Otherwise there are several terminals available that can be selected with ALT + F1 through F6.

After installation some settings have to be adjusted because it is running inside a virtual machine, e.g. correct interpretation of the VM BIOS clock.

Installing Mentor Graphics' (ex-CodeSourcery) Toolchain

Log into Debian as root. Create the script "" in the home directory by opening it with the preferred editor, e.g. nano.

nano ~/

Fill it with the following code, then save and exit. Maybe create a backup for another installation in the future.


# Script name:
# Version: 2.1 - 2011-09-19
# Copyright (C) 2009-2011  Matthias "Maddes" Buecher

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

## Script Functions
function install_toolchain()

	echo "Installing ${VERSION}:"

#	# Download toolchain
	[ ! -d "${DLDIR}" ] && mkdir -p "${DLDIR}"

#	# Install toolchain (by extracting)
	echo "Extracting..."
	[ ! -d "${INSTALLPATH}" ] && mkdir -p "${INSTALLPATH}"
	tar -x --bzip2 -f "${DLDIR}"/"${DLFILE}" -C "${INSTALLPATH}"

#	# Create toolchain environment script
	echo "Creating script file ${SCRIPTFILE} ..."
	cat >"${SCRIPTFILE}" << __EOF
echo "Type 'exit' to return to non-crosscompile environment"
[ -n "\${CROSS_COMPILE}" ] && { echo "ALREADY in crosscompile environment for \${ARCH} (\${CROSS_COMPILE})"; exit; }
export PATH='${BINPATH}':\${PATH}
export ARCH='${ARCHCODE}'
echo "NOW in crosscompile environment for \${ARCH} (\${CROSS_COMPILE})"
echo 'Back in non-crosscompile environment'
	[ ! -x "${SCRIPTFILE}" ] && chmod +x "${SCRIPTFILE}"

	echo "Done."

### Install prerequisites

[ "`whoami`" != "root" ] && {
	echo "$0: ABORT!!! Only for root user"
	exit 1

# --> general buildtools & development packages
#     wget & bzip2 for downloading and unpacking
#     uboot's mkimage & devio for creating uImage
echo "Installing prerequisites:"
PACKAGES='build-essential linux-libc-dev wget bzip2 uboot-mkimage devio ncurses-dev'
#	# special cases
	[ "${PACKAGE}" = "ncurses-dev" ] && PACKAGE="libncurses5-dev" ; # package ncurses-dev is virtual on Debian, which can not be found via dpkg

	dpkg -l | grep -q -F -e "${PACKAGE}"
	[ "${DOINSTALL}" -ne 0 ] && break
[ "${DOINSTALL}" -ne 0 ] && {
	aptitude update
	aptitude install ${PACKAGES}

### Install toolchains

### Mentor Graphics' (ex-CodeSourcery) toolchains
###   (navigation: Products --> Embedded Software --> Sourcery Tools --> Sourcery CodeBench --> Editions --> Lite Edition Download)
###  before 2011:
###   (navigation: Products --> Sourcery G++ --> Editions --> Lite)
### Note: the toolchains for the different targets can be installed in parallel

## Pathes for toolchains, scripts and downloads
# before 2011: DLBASEPATH=''

## -> ARM GNU/Linux target
## (arm-20011.03)
## (arm-2009q3)
## (arm-2009q1)

## -> MIPS GNU/Linux target
## (mips-4.4)

Make it executable for root.

chmod u+x ~/

At last run it to install Mentor Graphics' toolchain.


The script should run as is on all Debian GNU/Linux derivates, e.g. Ubuntu, etc.

For installing an older or newer toolchain version the script can easily be adopted by changing the download links at the end of the script, or add download links for another toolchain plus calling "install_toolchain". Make sure that the tar extracts correctly in a new sub directory inside /usr/local/codesourcery.

Selecting and Using the Toolchain

Every user on the system can use the installed toolchains. The user has just to select which toolchain version to use.

This will start a new shell process with the cross compiling environment (mainly three exported environment variables: PATH, ARCH and CROSS_COMPILE).

Now the user is able to compile a kernel as described on Buffalo:Buffalo ARM9 Kernel Port (see there for the latest compilation info).

To get back to the normal environemt just exit.