Setting up the codesourcery toolchain for X86 to ARM9 cross compiling

Preamble
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 "setup_codesourcery.sh" in the home directory by opening it with the preferred editor, e.g. nano. nano ~/setup_codesourcery.sh Fill it with the following code, then save and exit. Maybe create a backup for another installation in the future.
 * 1) !/bin/sh


 * 1) Script name: setup_codesourcery.sh
 * 2) Version: 2.2 - 2012-07-19
 * 3) Copyright (C) 2009-2012  Matthias "Maddes" Buecher
 * 1) Copyright (C) 2009-2012  Matthias "Maddes" Buecher
 * 1) Copyright (C) 2009-2012  Matthias "Maddes" Buecher


 * 1) This program is free software; you can redistribute it and/or modify
 * 2) it under the terms of the GNU General Public License as published by
 * 3) the Free Software Foundation; either version 2 of the License, or
 * 4) (at your option) any later version.
 * 5) http://www.gnu.org/licenses/gpl-2.0.txt
 * 6) This program is distributed in the hope that it will be useful,
 * 7) but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 8) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 9) GNU General Public License for more details.
 * 10) You should have received a copy of the GNU General Public License
 * 11) along with this program; if not, write to the Free Software
 * 12) Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 1) You should have received a copy of the GNU General Public License
 * 2) along with this program; if not, write to the Free Software
 * 3) Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

install_toolchain {	BINPATH="${INSTALLPATH}/${VERSION}/bin" SCRIPTFILE="${SCRIPTPATH}/${SCRIPTPREFIX}${VERSION}.sh" DLFILE="${VERSION}${SUBVERSION}-${CCPREFIX}-i686-pc-linux-gnu.tar.bz2"
 * 1) Script Functions
 * 1) Script Functions

echo "Installing ${VERSION}:"

[ ! -d "${DLDIR}" ] && mkdir -p "${DLDIR}" wget -N -P "${DLDIR}" "${DLBASEPATH}/${CCPREFIX}/${DLFILE}"
 * 1) 	# Download toolchain

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

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}' export CROSS_COMPILE='${CCPREFIX}-' echo "NOW in crosscompile environment for \${ARCH} (\${CROSS_COMPILE})" /bin/bash echo 'Back in non-crosscompile environment' __EOF [ ! -x "${SCRIPTFILE}" ] && chmod +x "${SCRIPTFILE}"
 * 1) 	# Create toolchain environment script
 * 1) !/bin/sh

echo 'Done.' }


 * 1) Install prerequisites
 * 1) Install prerequisites

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

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

dpkg -l | grep -q -F -e "${PACKAGE}" DOINSTALL=$? [ "${DOINSTALL}" -ne 0 ] && break done [ "${DOINSTALL}" -ne 0 ] && { apt-get update apt-get install ${PACKAGES} }


 * 1) Install toolchains
 * 1) Install toolchains


 * 1) Mentor Graphics' (ex-CodeSourcery) toolchains
 * 2)   http://www.mentor.com/embedded-software/sourcery-tools/sourcery-codebench/lite-edition
 * 3)   (navigation 2012: Products and/or Embedded Software --> Sourcery CodeBench [--> Lean more] [--> Editions] --> Sourcery CodeBench Lite Edition (at the bottom))
 * 4)   (navigation 2011: Products --> Embedded Software --> Sourcery Tools --> Sourcery CodeBench --> Editions --> Lite Edition Download)
 * 5)  before 2011:
 * 6)   http://www.codesourcery.com/sgpp/lite_edition.html
 * 7)   (navigation: Products --> Sourcery G++ --> Editions --> Lite)
 * 8) Note: the toolchains for the different targets can be installed in parallel
 * 1) Note: the toolchains for the different targets can be installed in parallel
 * 1) Note: the toolchains for the different targets can be installed in parallel

INSTALLPATH='/usr/local/codesourcery' SCRIPTPATH='/usr/local/bin' SCRIPTPREFIX='codesourcery-' DLDIR="/root/work/codesourcery" DLBASEPATH='https://sourcery.mentor.com/public/gnu_toolchain/'
 * 1) Pathes for toolchains, scripts and downloads
 * 1) before 2011: DLBASEPATH='http://www.codesourcery.com/public/gnu_toolchain/'

ARCHCODE='arm' CCPREFIX='arm-none-linux-gnueabi' VERSION='arm-2011.03' SUBVERSION='-41' install_toolchain VERSION='arm-2009q3' SUBVERSION='-67' VERSION='arm-2009q1' SUBVERSION='-203'
 * 1) -> ARM GNU/Linux target
 * 1) (arm-2011.03-41)
 * 1) (arm-2009q3-67)
 * 1) install_toolchain
 * 2) (arm-2009q1-203)
 * 1) install_toolchain

ARCHCODE='mips' CCPREFIX='mips-linux-gnu' VERSION='mips-2011.03' SUBVERSION='-93' install_toolchain VERSION='mips-4.4' SUBVERSION='-303' VERSION='mips-4.3' SUBVERSION='-154' Make it executable for root. chmod u+x ~/setup_codesourcery.sh At last run it to install Mentor Graphics' toolchain. ~/setup_codesourcery.sh
 * 1) -> MIPS GNU/Linux target
 * 1) (mips-2011.03-93)
 * 1) (mips-4.4-303)
 * 1) install_toolchain
 * 2) (mips-4.3-154)
 * 1) install_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. codesourcery-arm-2009q3.sh 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. exit

Links

 * Buffalo:Precompiled C Cross-Toolchain
 * Buffalo:Build (and_use) a cross-toolchain using Dan Kegel's crosstool 0.38