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

Contents

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.

#!/bin/bash

#
# Script name: setup_codesourcery.sh
# 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.
# http://www.gnu.org/licenses/gpl-2.0.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 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()
{
	BINPATH="${INSTALLPATH}/${VERSION}/bin"
	SCRIPTFILE="${SCRIPTPATH}/${SCRIPTPREFIX}${VERSION}.sh"

	echo "Installing ${VERSION}:"

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

#	# 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
#!/bin/bash
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}"

	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'
DOINSTALL=0
for PACKAGE in ${PACKAGES}
 do
#	# 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=$?
	[ "${DOINSTALL}" -ne 0 ] && break
done
[ "${DOINSTALL}" -ne 0 ] && {
	aptitude update
	aptitude install ${PACKAGES}
}


###
### Install toolchains
###


### Mentor Graphics' (ex-CodeSourcery) toolchains
###   http://www.mentor.com/embedded-software/sourcery-tools/sourcery-codebench/lite-edition
###   (navigation: Products --> Embedded Software --> Sourcery Tools --> Sourcery CodeBench --> Editions --> Lite Edition Download)
#
###  before 2011:
###   http://www.codesourcery.com/sgpp/lite_edition.html
###   (navigation: Products --> Sourcery G++ --> Editions --> Lite)
#
### Note: the toolchains for the different targets can be installed in parallel

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

## -> ARM GNU/Linux target
ARCHCODE='arm'
CCPREFIX='arm-none-linux-gnueabi'
## (arm-20011.03)
VERSION='arm-2011.03'
DLFILE='arm-2011.03-41-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2'
#install_toolchain
## (arm-2009q3)
VERSION='arm-2009q3'
DLFILE='arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2'
install_toolchain
## (arm-2009q1)
VERSION='arm-2009q1'
DLFILE='arm-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2'
#install_toolchain

## -> MIPS GNU/Linux target
ARCHCODE='mips'
CCPREFIX='mips-linux-gnu'
## (mips-4.4)
VERSION='mips-4.4'
DLFILE='mips-4.4-57-mips-linux-gnu-i686-pc-linux-gnu.tar.bz2'
#install_toolchain

Make it executable for root.

chmod u+x ~/setup_codesourcery.sh

At last run it to install Mentor Graphics' toolchain.

~/setup_codesourcery.sh

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