Thursday, October 30, 2008

Build Cross-Compiled gcc for ARM by myself

This work is done by studying Cross-Compiled Linux From Scratch - Embedded (Version SVN-0.0.1-20080109-arm) material, The steps of building cross compiled gcc for ARM:

[Adding the ARMDEV User]

$ su -
# groupadd armdev
# useradd -s /bin/bash -g armdev -m -k /dev/null armdev
# useradd -s /bin/bash -g wizign -m armdev
# passwd armdev
# chown -Rv armdev ${ARMDEV}


[Preparing a New Partition]

# export ARMDEV='/mnt/armdev'
# install -dv ${ARMDEV}
# mkdir /vdisk/arm_disk
# mkdir -pv ${ARMDEV}
# mount --bind /vdisk/arm_disk ${ARMDEV}

[Create source directory]
# mkdir -v ${ARMDEV}/sources
# chmod -v a+wt ${ARMDEV}/sources

[Check and download packages]

$ su - armdev

[]Binutils (2.18)
$ wget http://ftp.gnu.org/gnu/binutils/binutils-2.18.tar.bz2

[]Busybox (1.4.1)
$ wget http://busybox.net/downloads/busybox-1.4.1.tar.bz2

[]CLFS-Bootscripts (1.0-pre4)
$ wget http://cross-lfs.org/files/packages/embedded-0.0.1/clfs-embedded-bootscripts-1.0-pre4.tar.bz2

[]E2fsprogs (1.39)
$ wget http://prdownloads.sourceforge.net/e2fsprogs/e2fsprogs-1.39.tar.gz

[]GCC (4.1.2)
$ wget http://ftp.gnu.org/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2

[]Iana-Etc (2.20)
$ wget http://www.sethwklein.net/projects/iana-etc/downloads/iana-etc-2.20.tar.bz2

[]Linux (2.6.20.1)
$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.20.1.tar.bz2

[]Linux-Headers (2.6.20.1-02252007)
$ wget http://cross-lfs.org/files/packages/embedded-0.0.1/linux-headers-2.6.20.1-02252007.tar.bz2

[]uClibc (0.9.29)
$ wget http://www.uclibc.org/downloads/uClibc-0.9.29.tar.bz2

[]Zlib (1.2.3)
$ wget http://www.zlib.net/zlib-1.2.3.tar.gz

[Check and download patched]

[]Binutils Posix Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-sysroot/patches/binutils-2.18-posix-1.patch

[]Binutils Posix Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/binutils-2.17-posix-1.patch

[]Binutils uClibc Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/binutils-2.17-uclibc-1.patch

[]Busybox Fixes Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/busybox-1.8.2-fixes-1.patch

[]GCC Cross Search Paths Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/gcc-4.1.2-cross_search_paths-1.patch

[]GCC Posix Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/gcc-4.1.2-posix-1.patch

[]GCC uClibc Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/gcc-4.1.2-uclibc-1.patch

[]Zlib DESTDIR Patch
$ wget http://svn.cross-lfs.org/svn/repos/cross-lfs/branches/clfs-embedded/patches/zlib-1.2.3-DESTDIR-1.patch

[Final Preparations]

[]Setting up the environment

$ cat > ~/.bash_profile << "EOF"
> exec env -i HOME=${HOME} TERM=${TERM} PS1='\u:\w\$ ' /bin/bash
> EOF

$ cat > ~/.bashrc << "EOF"
> set +h
> umask 022
> ARMDEV=/mnt/armdev
> LC_ALL=POSIX
> PATH=${ARMDEV}/cross-tools/bin:/bin:/usr/bin
> export ARMDEV LC_ALL PATH
> EOF

$ source ~/.bash_profile

[]Creating directories

$ mkdir -pv ${ARMDEV}/build
$ mkdir -pv ${ARMDEV}/{bin,boot,dev,{etc/,}opt,home,lib/{firmware,modules},mnt}
$ mkdir -pv ${ARMDEV}/{proc,media/{floppy,cdrom},sbin,srv,sys}
$ mkdir -pv ${ARMDEV}/var/{lock,log,mail,run,spool}
$ mkdir -pv ${ARMDEV}/var/{opt,cache,lib/{misc,locate},local}
$ install -dv -m 0750 ${ARMDEV}/root
$ install -dv -m 1777 ${ARMDEV}{/var,}/tmp
$ mkdir -pv ${ARMDEV}/usr/{,local/}{bin,include,lib,sbin,src}
$ mkdir -pv ${ARMDEV}/usr/{,local/}share/{doc,info,locale,man}
$ mkdir -pv ${ARMDEV}/usr/{,local/}share/{misc,terminfo,zoneinfo}
$ mkdir -pv ${ARMDEV}/usr/{,local/}share/man/man{1,2,3,4,5,6,7,8}
$ mkdir -pv ${ARMDEV}/cross-tools{,/bin}
$ for dir in ${ARMDEV}/usr{,/local}; do
> ln -sv share/{man,doc,info} ${dir}
> done


[]Creating the passwd, group, and log Files

$ ln -svf ../proc/mounts ${ARMDEV}/etc/mtab

$ cat > ${ARMDEV}/etc/passwd << "EOF"
> root::0:0:root:/root:/bin/ash
> bin:x:1:1:bin:/bin:/bin/false
> daemon:x:2:6:daemon:/sbin:/bin/false
> adm:x:3:16:adm:/var/adm:/bin/false
> lp:x:10:9:lp:/var/spool/lp:/bin/false
> mail:x:30:30:mail:/var/mail:/bin/false
> news:x:31:31:news:/var/spool/news:/bin/false
> uucp:x:32:32:uucp:/var/spool/uucp:/bin/false
> operator:x:50:0:operator:/root:/bin/ash
> postmaster:x:51:30:postmaster:/var/spool/mail:/bin/false
> nobody:x:65534:65534:nobody:/:/bin/false
> EOF


$ cat > ${ARMDEV}/etc/group << "EOF"
> root:x:0:
> bin:x:1:
> sys:x:2:
> kmem:x:3:
> tty:x:4:
> tape:x:5:
> daemon:x:6:
> floppy:x:7:
> disk:x:8:
> lp:x:9:
> dialout:x:10:
> audio:x:11:
> video:x:12:
> utmp:x:13:
> usb:x:14:
> cdrom:x:15:
> adm:x:16:root,adm,daemon
> console:x:17:
> cdrw:x:18:
> mail:x:30:mail
> news:x:31:news
> uucp:x:32:uucp
> users:x:100:
> nogroup:x:65533:
> nobody:x:65534:
> EOF


$ touch ${ARMDEV}/var/run/utmp ${ARMDEV}/var/log/{btmp,lastlog,wtmp}
$ chmod -v 664 ${ARMDEV}/var/run/utmp ${ARMDEV}/var/log/lastlog


[Constructing Cross-Compile Tools]

[]Build CFLAGS
$ echo "Build CFLAGS"
$ echo unset CFLAGS >> ~/.bashrc
$ echo unset CXXFLAGS >> ~/.bashrc

# Application Binary Interface (ABI) for the ARM Architecture
# ABI Variables
# Table 6.1. List of Build Variables
# ABI BUILD=Value
# ------------------------- --------------------------------
# 32-Bit MIPS -mabi=32
# 32-Bit All Others -m32
# N32 MIPS -mabi=n32
# 64-Bit MIPS -mabi=64
# 64-Bit All Others -m64
echo "ABI Variables"
export BUILD="-m32"

# Build Variables
# Table 6.2. Processor Type and Target Triplets
# Processor Target Triplet
# ---------------------------------------------- -----------------------------------------
# Generic arm, little endian arm-unknown-linux-uclibc
# Generic arm, version 5, little endian armv5l-unknown-linux-uclibc
# Generic arm, version 5, big endian armv5b-unknown-linux-uclibc

$ export ARM_HOST="$(echo $MACHTYPE | sed "s/$(echo $MACHTYPE | cut -d- -f2)/cross/")"
# Generic arm, little endian
$ export ARM_TARGET="arm-unknown-linux-uclibc"
$ echo export ARM_HOST=\""${ARM_HOST}\"" >> ~/.bashrc
$ echo export ARM_TARGET=\""${ARM_TARGET}\"" >> ~/.bashrc

[]Linux-Headers-2.6.20.1-02252007

$ mkdir ${ARMDEV}/build
$ cd ${ARMDEV}/build
$ tar -jxvf ${ARMDEV}/sources/linux-headers-2.6.20.1-02252007.tar.bz2
$ install -dv ${ARMDEV}/usr/include
$ cp -av ${ARMDEV}/build/linux-headers-2.6.20.1/include/{asm-generic,linux,mtd,scsi,sound} ${ARMDEV}/usr/include
$ cp -av ${ARMDEV}/build/linux-headers-2.6.20.1/include/asm-arm ${ARMDEV}/usr/include/asm


[]Cross Binutils-2.17

$ cd ${ARMDEV}/build
$ tar -jxvf ${ARMDEV}/sources/binutils-2.17.tar.bz2
$ cd binutils-2.17
$ patch -Np1 -i ${ARMDEV}/sources/binutils-2.17-posix-1.patch
$ patch -Np1 -i ${ARMDEV}/sources/binutils-2.17-uclibc-1.patch
$ mkdir -v ../binutils-build
$ cd ../binutils-build
$ ../binutils-2.17/configure --prefix=${ARMDEV}/cross-tools --host=${ARM_HOST} --target=${ARM_TARGET} --with-sysroot=${ARMDEV} --disable-nls --enable-shared --disable-multilib
$ make configure-host
$ make
$ make install
$ cp -v ../binutils-2.17/include/libiberty.h ${ARMDEV}/usr/include


[]uClibc-0.9.29

$ cd ${ARMDEV}/build
$ tar -jxvf ${ARMDEV}/sources/uClibc-0.9.29.tar.bz2
$ cd uClibc-0.9.29

$ cp extra/Configs/Config.arm extra/Configs/Config.arm.orig
$ sed -e "/default/s:OABI:EABI:" extra/Configs/Config.arm.orig > extra/Configs/Config.arm
$ cp Makefile{,.orig}
$ sed -e 's/$(LN) -fs/cp/g' Makefile.orig > Makefile
$ for file in `find libc/sysdeps/linux -name Makefile`; do
> cp $file{,.orig}
> sed -e 's/$(LN) -fs/cp/g' -e 's@../libc/@$(TOPDIR)libc/@g' $file.orig > $file
> done

$ make defconfig ARCH=arm
$ cp .config{,.orig}
$ sed -e "/^CROSS_COMPILER_PREFIX/s:=.*:=\"${ARM_TARGET}-\":" -e "/^KERNEL_HEADERS/s:=.*:=\"${ARMDEV}/usr/include\":" -e "/^SHARED_LIB_LOADER_PREFIX/s:=.*:=\"/lib\":" -e "/^DEVEL_PREFIX/s:=.*:=\"/usr\":" -e "/^RUNTIME_PREFIX/s:=.*:=\"/\":" .config.orig > .config

$ UCLIBC_OPTIONS="DO_C99_MATH UCLIBC_HAS_RPC UCLIBC_HAS_CTYPE_CHECKED UCLIBC_HAS_WCHAR UCLIBC_HAS_HEXADECIMAL_FLOATS LDSO_PRELOAD_FILE_SUPPORT UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE UCLIBC_HAS_PRINTF_M_SPEC UCLIBC_HAS_IPV6 UCLIBC_HAS_GLIBC_CUSTOM_PRINTF UCLIBC_USE_NETLINK UCLIBC_HAS_FTW"
$ for config in $UCLIBC_OPTIONS; do
> cp .config{,.orig}
> sed -e "s:# ${config} is not set:${config}=y:" .config.orig > .config
> done
$ UCLIBC_OPTIONS="UCLIBC_HAS_CTYPE_UNSAFE"
$ for config in $UCLIBC_OPTIONS; do
> cp .config{,.orig}
> sed -e "s:${config}=y:# ${config} is not set:" .config.orig > .config
> done
$ echo "UCLIBC_HAS_FULL_RPC=y" >> .config
$ echo "UCLIBC_HAS_REENTRANT_RPC=y" >> .config

# If your ARM system is Big Endian, you will need at add the following lines to your configuration:

$ echo "ARCH_ANY_ENDIAN=y" >> .config
$ echo "ARCH_BIG_ENDIAN=y" >> .config
$ echo "ARCH_WANTS_BIG_ENDIAN=y" >> .config

# If your ARM system is Little Endian, you will need at add the following lines to your configuration:

$ echo "ARCH_ANY_ENDIAN=y" >> .config
$ echo "ARCH_LITTLE_ENDIAN=y" >> .config
$ echo "ARCH_WANTS_LITTLE_ENDIAN=y" >> .config

$ make oldconfig
$ make headers
$ mkdir ${ARMDEV}/usr/include/bits
$ make PREFIX=${ARMDEV} install_headers

[]Cross GCC-4.1.2 - Static

$ cd ${ARMDEV}/build
$ tar -jxvf ${ARMDEV}/sources/gcc-4.1.2.tar.bz2
$ cd gcc-4.1.2
$ patch -Np1 -i ${ARMDEV}/sources/gcc-4.1.2-posix-1.patch
$ patch -Np1 -i ${ARMDEV}/sources/gcc-4.1.2-uclibc-1.patch
$ patch -Np1 -i ${ARMDEV}/sources/gcc-4.1.2-cross_search_paths-1.patch
$ mkdir -v ../gcc-build
$ cd ../gcc-build
$ ../gcc-4.1.2/configure --prefix=${ARMDEV}/cross-tools --host=${ARM_HOST} --target=${ARM_TARGET} --disable-multilib --with-sysroot=${ARMDEV} --without-headers --disable-nls --disable-shared --disable-threads --enable-languages=c
$ make all-gcc
$ make install-gcc


[]ToolChain Variables

$ export CC="${ARM_TARGET}-gcc"
$ export CXX="${ARM_TARGET}-g++"
$ export AR="${ARM_TARGET}-ar"
$ export AS="${ARM_TARGET}-as"
$ export LD="${ARM_TARGET}-ld"
$ export RANLIB="${ARM_TARGET}-ranlib"
$ export STRIP="${ARM_TARGET}-strip"


[]uClibc-0.9.29

$ cd ${ARMDEV}/build
$ cd uClibc-0.9.29

$ make CC="${CC}"
$ make PREFIX=${ARMDEV} install


[]Clear ToolChain Variables

$ unset CC
$ unset CXX
$ unset AR
$ unset AS
$ unset LD
$ unset RANLIB
$ unset STRIP

[]GCC-4.1.2 - Cross Compiler Final

$ cd ${ARMDEV}/build
$ rm -fr gcc-4.1.2
$ tar -jxvf ${ARMDEV}/sources/gcc-4.1.2.tar.bz2
$ cd gcc-4.1.2
$ patch -Np1 -i ${ARMDEV}/sources/gcc-4.1.2-posix-1.patch
$ patch -Np1 -i ${ARMDEV}/sources/gcc-4.1.2-uclibc-1.patch
$ patch -Np1 -i ${ARMDEV}/sources/gcc-4.1.2-cross_search_paths-1.patch
$ rm -fr ../gcc-build
$ mkdir -v ../gcc-build
$ cd ../gcc-build
$ ../gcc-4.1.2/configure --prefix=${ARMDEV}/cross-tools --host=${ARM_HOST} --target=${ARM_TARGET} --disable-multilib --with-sysroot=${ARMDEV} --disable-nls --enable-shared --enable-languages=c,c++ --enable-__cxa_atexit --enable-c99 --enable-long-long --enable-threads=posix
$ make
$ make install

2 comments:

Jolie Lin said...

可以請問一下如果要build a GCC compiler for ARM(big-endian)的話,要怎麼做呢?因為default setting好像是little-endian~感謝

Causality said...

請參考以下連結上的說明:

6.4. Build Variables