The GPIO toolchain


The toolchain is now complete; including support for C, C++.
If you get errors like this one...
error: possibly undefined macro: AC_MSG_ERROR
-Then it's likely because you do not have pkg-config installed.
GDC is no longer supported in this toolchain as building GDC breaks too often; I can't keep up.
  1. Set up versions
    gcc=gcc-4.9.3 binutils=binutils-2.28 newlib=newlib-2.1.0
  2. Set up build configuration
    target="arm-none-eabi" workdir="$HOME/toolchain"; [ "$1" ] && workdir="$1" [ "$toolchain" ] || toolchain="$target"; [ "$3" ] && toolchain="$3" [ "$prefix" ] || prefix="/opt"; [ "$2" ] && prefix="$2"
  3. Set up gcc short-version and choose compiler to use
    gcc_vers=${gcc##*-} gcc_vmaj=${gcc_vers%%.*} gcc_vmin=${gcc_vers#$gcc_vmaj.} gcc_vmin=${gcc_vmin%%.*} gcc_shortvers=$gcc_vmaj.$gcc_vmin [ $gcc_vmaj -ge 5 ] && { gcc-select 4.8 || gcc-select latest; }
  4. Set up binutils configuration
    binutils_cfg=(--disable-nls --disable-libssp --with-gcc --with-gnu-as) binutils_cfg=(${binutils_cfg[@]} --with-gnu-ld --enable-multilib --with-system-zlib) binutils_cfg=(${binutils_cfg[@]} --enable-interwork --enable-plugins)
  5. Set up gcc configuration
    gcc_optional=(--with-tune=cortex-m4 --with-mode=thumb) headers=--with-headers="$source/$newlib/newlib/libc/include/" gcc_cfg=(--disable-nls --disable-libssp --with-gnu-as --with-gnu-ld --with-system-zlib) gcc_cfg=(${gcc_cfg[@]} --program-prefix=$target- --program-suffix=-$gcc_shortvers) gcc_cfg=(${gcc_cfg[@]} --with-gcc --with-dwarf2 --enable-interwork --with-newlib) gcc_cfg=(${gcc_cfg[@]} --disable-libphobos --disable-decimal-float --disable-libffi) gcc_cfg=(${gcc_cfg[@]} --disable-libmudflap --disable-libquadmath --disable-shared) gcc_cfg=(${gcc_cfg[@]} --disable-libgomp --disable-threads --disable-tls) gcc_cfg=(${gcc_cfg[@]} --disable-libstdcxx-pch --disable-bootstrap ${gcc_optional[@]}) gcc1_cfg=(${gcc_cfg[@]} --enable-languages="c,c++,lto" --without-headers) gcc2_cfg=(${gcc_cfg[@]} --enable-languages="c,c++,lto" --with-headers)
  6. Set up newlib configuration
    newlib_cfg=(--enable-interwork --enable-multilib --disable-libssp --disable-nls) newlib_cfg=(${newlib_cfg[@]} --enable-newlib-io-long-long --enable-newlib-register-fini) newlib_cfg=(${newlib_cfg[@]} --disable-newlib-supplied-syscalls)
  7. Set up other variables and paths
    start=`date +%s`; let cpucount=2*`getconf _NPROCESSORS_ONLN`; pmake="make -j$cpucount" prefix="$prefix${toolchain:+/$toolchain}"; build="$workdir/Build" source="$workdir/Source"; archives="$workdir/Archives" bin="$workdir/bin"; export PATH="$bin:$PATH"; mkdir -p "$build" "$source" "$bin" [ -d /arc ] && [ ! -e "$archives" ] && ln -s /arc "$archives"; mkdir -p "$archives"
  8. Set up convenience functions
    cls(){ echo -en "\033c$@"; }; HR(){ eval printf '=%.0s' {1..$(tput cols)}; echo; } CMsg(){ echo -en "\033[1;$1m${*:2}\033[m${2:+\n}"; }; Msg(){ CMsg 34 "$*"; } MsgHR(){ Msg "$@\n`HR`"; }; Good(){ CMsg 32 "$*"; }; Bad(){ CMsg 31 "$*"; } success(){ Good "Success${pkg:+ ($pkg)}"; }; status="success || failure" failure(){ Bad "Failed${1:+ $*}${pkg:+ ($pkg)}"; [ "" == "$LINES" ] && exit 1; } ipkg(){ fpkg="$1"; pkg="${1%%[-_][0-9]*}"; MsgHR "install${x:+ $x} $pkg"; } cdpkg(){ ipkg "$1"; mkdir -p "$build/$pkg"; cd "$build/$pkg"; } sl(){ for f in ${@:2}; do ln -s "$source/$f" "$source/$1/${f%%[-_][0-9]*}" 2>/dev/null; done; }
  9. Setup build functions
    get_prefix(){ local p; for p in $@; do local q="${p#--prefix=*}"; [ "$q" != "$p" ] && prefixdir="$q"; done; true; } cfg(){ cdpkg "$1" && "$source/$1/configure" ${@:2} || failure configuring; prefixdir=; get_prefix $@; } destdir_writable(){ [ "$destdir" ] || [ -w "$destdir${prefixdir:-/usr}" ]; } setup_imake(){ imake="`destdir_writable || echo sudo` $pmake"; } imake(){ setup_imake; $imake ${destdir+DESTDIR=$destdir${fpkg:+/$fpkg}} ${1:-install} $@; } build(){ cfg $@ && $pmake && imake && success || failure; unset pkg fpkg; } targ(){ x=-xaf; case $1 in *.gz) x=-xzf ;; *.bz2) x=-xjf ;; *.xz) x=-xJf ;; esac; } utar(){ local x; targ "$1"; tar "$x" "$1" || failure extracting "${1##*/}"; } dpk(){ local f="${1##*/}"; [ -d "${f%.tar*}" ] || utar "$1"; } dload(){ curl -fLk --retry 3 "$1" -o "$2" || failure downloading ${1##*/}; } dl(){ local a="$archives/${1##*/}"; [ -f "$a" ] || dload "$1" "$a"; } dlc(){ unset pkg fpkg; local c="$DL_CACHE"; [ "$c" ] && f="$c/${1##*/}" || f="$1"; dl "$f"; } dlx(){ dlc $1; cd "$source" && dpk "$archives/${1##*/}"; } dlxb(){ dlx $1; local f=${1##*/}; f=${f%.*}; f="${f%.tar}"; build ${f%.src} ${@:2}; }
  10. Setup a temporary (local) bin directory for gcc-4.2
    type gcc-select &>/dev/null && { gcc-select 4.2; } || { which gcc-4.2 >/dev/null && gcc_symlink=1; } [ $gcc_symlink ] && ln -s "/usr/bin/gcc-4.2" "$bin/gcc" 2>/dev/null [ $gcc_symlink ] && ln -s "/usr/bin/gcc-4.2" "$bin/gcc-4.2" 2>/dev/null [ $gcc_symlink ] && ln -s "/usr/bin/gcov-4.2" "$bin/gcov" 2>/dev/null [ $gcc_symlink ] && ln -s "/usr/bin/g++-4.2" "$bin/g++" 2>/dev/null
  11. Mirror repositories
    cd "$archives" [ -d newlib-nano-2 ] || git clone --mirror git://github.com/32bitmicro/newlib-nano-2 newlib-nano-2 [ -d hidapi ] || git clone --mirror git://github.com/signal11/hidapi hidapi [ -d openocd ] || git clone --mirror git://git.code.sf.net/p/openocd/code openocd
  12. Download archives and extract sources
    dlx "ftp://ftp.gnu.org/gnu/binutils/$binutils.tar.bz2" dlc "http://ftp.gnu.org/pub/gnu/gcc/$gcc/$gcc.tar.gz" dlx "ftp://ftp.gmplib.org/pub/gmp-5.1.3/gmp-5.1.3.tar.bz2" dlx "http://www.mpfr.org/mpfr-3.1.2/mpfr-3.1.2.tar.bz2" dlx "http://www.multiprecision.org/mpc/download/mpc-1.0.3.tar.gz" dlx "http://isl.gforge.inria.fr/isl-0.12.2.tar.bz2" dlx "http://www.bastoul.net/cloog/pages/download/count.php3?url=./cloog-0.18.1.tar.gz" dlx "http://www.mr511.de/software/libelf-0.8.13.tar.gz" dlc "ftp://sourceware.org/pub/newlib/$newlib.tar.gz" dlx "http://ftp.gnu.org/pub/gnu/gdb/gdb-10.1.tar.gz" dlx "http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.9/libusb-1.0.9.tar.bz2" dlx "http://www.cmake.org/files/v2.8/cmake-2.8.12.tar.gz" dlx "http://www.intra2net.com/en/developer/libftdi/download/libftdi1-1.1.tar.bz2" dlx "http://nongnu.uib.no//confuse/confuse-2.7.tar.gz" dlc "http://dl.gpio.dk/t-arm-elf-$gcc_vers.patch" dlc "http://dl.gpio.dk/cortex-m0-trap.S.patch" dlc "http://dl.gpio.dk/gcc-$gcc_vers-gperf.patch" dlc "http://dl.gpio.dk/SIM-div.patch"
  13. (update mirrored repository 'caches')
    ( cd "$archives/newlib-nano-2" && git remote update ) ( cd "$archives/hidapi" && git remote update ) ( cd "$archives/openocd" && git remote update )
    Note: hidapi and newlib-nano-2 can normally be skipped. Most of the time I only update OpenOCD.


  14. Prepare GCC for multiple ARM libraries and archive the patched sources (in case we mess up):
    MsgHR "Preparing GCC..."; cd "$source" gccp="$gcc+patch"; p="$archives/t-arm-elf-$gcc_vers.patch" p2="$archives/gcc-$gcc_vers-gperf.patch" optpatch(){ [ -f "$1" ] && patch ${@:2} <"$1"; return 0; } f="$archives/$gccp.tar.bz2"; [ -f "$f" ] && dpk "$f" [ -e "$gccp" ] || dpk "$archives/$gcc.tar.gz" [ -e "$gccp" ] || { ( cd "$source/$gcc/" && patch -p1 < "$p" && optpatch "$p2" -p1 ) && mv "$gcc" "$gccp"; } [ -e "$f" ] || tar -cjf "$f" "$gccp"
  15. Fix Cortex-M0 exception bug in newlib and archive the patched sources (in case we mess up):
    MsgHR "Preparing newlib..."; cd "$source" nlp="$newlib+patch"; p="$archives/cortex-m0-trap.S.patch" f="$archives/$nlp.tar.bz2"; [ -f "$f" ] && dpk "$f" [ -e "$nlp" ] || dpk "$archives/$newlib.tar.gz" [ -e "$nlp" ] || { ( cd "$source/$newlib/" && patch -p1 < "$p" ) && mv "$newlib" "$nlp"; } [ -e "$f" ] || tar -cjf "$f" "$nlp"
  16. Prepare GCC prerequisites
    sl $gcc+patch gmp-5.1.3 mpfr-3.1.2 mpc-1.0.3 libelf-0.8.13 isl-0.12.2 cloog-0.18.1 $newlib+patch/newlib
  17. Clone sources
    cd "$source" ( rm -Rf "$source/newlib-nano-2"; git clone "$archives/newlib-nano-2" && cd newlib-nano-2 && git checkout newlib-nano-2.1 && chmod 755 configure ) rm -Rf "$source/hidapi"; git clone "$archives/hidapi" rm -Rf "$source/openocd"; git clone "$archives/openocd" openocd
  18. Remove old build and any previously installed toolchain of the same kind, to avoid conflict
    MsgHR "Removing old toolchain..."; cd "$workdir"; rm -Rf "${build:+$build/*}" [ -d "$prefix" ] && [ "$toolchain" ] && sudo rm -Rf "$prefix"/{bin,lib,include,libexec,share} ${target:+$prefix/$target}
  19. Build Binutils
    #gcc-select 4.8 #if building binutils >2.28 build "$binutils" --target="$target" --prefix="$prefix" ${binutils_cfg[@]}
  20. Remove old gcc build directory and build GCC without bootstrap
    rm -Rf "$build/gcc"; mkdir -p "$build/gcc"; cd "$build/gcc" cfg "$gcc+patch" --target="$target" --prefix="$prefix" ${gcc1_cfg[@]} $pmake all-gcc && $pmake all-target-libgcc && $pmake all-target-libstdc++-v3 || failure imake install-gcc && imake install-target-libgcc && imake install-target-libstdc++-v3 && $status
  21. Add new $prefix/bin to $PATH, so the new binutils will be found:
    export PATH="$prefix/bin:$PATH"
  22. Make symlinks to our new gcc, so that it will be used for building newlib:
    for f in c++ cc cpp g++ gcc gcov; do p=$prefix/bin/$target-$f-$gcc_shortvers; [ -f $p ] && ln -s $p $bin/$target-$f; done
  23. Show supported multilib
    MsgHR "Supported multilib:"; $target-gcc -print-multi-lib; MsgHR
    .;
    thumb/arm7tdmi-s;@mthumb@mcpu=arm7tdmi-s
    thumb/cortex-m0;@mthumb@mcpu=cortex-m0
    thumb/cortex-m3;@mthumb@mcpu=cortex-m3
    thumb/cortex-m4;@mthumb@mcpu=cortex-m4
    thumb/cortex-m4/float-abi-hard/fpuv4-sp-d16;@mthumb@mcpu=cortex-m4@mfloat-abi=hard@mfpu=fpv4-sp-d16
  24. Build newlib
    rm -Rf "$build/newlib"; mkdir -p "$build/newlib"; cd "$build/newlib" cfg "$newlib+patch" --target="$target" --prefix="$prefix" ${newlib_cfg[@]} cflags="-D__IEEE_BIG_ENDIAN -D__IEEE_BYTES_LITTLE_ENDIAN -D__BUFSIZ__=64" $pmake CFLAGS_FOR_TARGET="$cflags" || failure ienv="`destdir_writable || echo sudo` env" $ienv "PATH=$PATH" ${destdir+DESTDIR=$destdir${fpkg:+/$fpkg}} $pmake install && $status
    Note: If install fails, try it again - it was necessary to double-install gcc-4.9.2 with newlib 2.1.0 on both Mac OS X and Linux.


  25. Build final GCC with newlib (we're not reconfiguring as that will prevent the build from completing)
    [ "$gcc2_cfg" ] && cd "$build/gcc" #[ "$gcc2_cfg" ] && cfg "gcc" --target="$target" --prefix="$prefix" ${gcc2_cfg[@]} [ "$gcc2_cfg" ] && $pmake all && imake && $status
  26. Build GDB with PSIM (takes a while - not as long as GCC though; this requires ncurses on Linux)
    # replace 'std::log2' by 'log2' in gdb-10.1/gdb/dwarf2/index-write.c: sed 's/std::log2/log2/g' "$source/gdb-10.1/gdb/dwarf2/index-write.c" >"$source/index-write.c" mv "$source/index-write.c" "$source/gdb-10.1/gdb/dwarf2/index-write.c" patch -p1 < "$archives/SIM-div.patch" mkdir -p "$build/gdb"; cd "$build/gdb" gcc-select 4.8 cfg "gdb-10.1" --target="$target" --prefix="$prefix" --disable-nls --enable-sim-arm --enable-sim-stdio $pmake INHIBIT_LIBC_CFLAGS="-DUSE_TM_CLONE_REGISTRY=0" all && imake && $status
  27. Build LibConfuse 2.7
    dlxb "http://nongnu.uib.no//confuse/confuse-2.7.tar.gz"
  28. Build LibUSB 1.0
    dlxb "http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.23/libusb-1.0.23.tar.bz2"
  29. Install cmake (required for libftdi1)
    cd "$source/cmake-2.8.12" ./bootstrap && $pmake && sudo $pmake install && $status ./bootstrap && build cmake-2.8.12
  30. Build LibFTDI 1.1
    mkdir -p "$build/libftdi1"; cd "$build/libftdi1" cmake -DLIBUSB_INCLUDE_DIR="/usr/local/include/libusb-1.0" -DCMAKE_INSTALL_PREFIX="/usr/local" "$source/libftdi1-1.1/" $pmake all && imake && $status
  31. Build HIDAPI (requires libudev on Linux; see http://git.kernel.org/cgit/linux/hotplug/udev.git)
    cd "$source/hidapi" ./bootstrap && build hidapi
  32. Build OpenOCD
    cd "$source/openocd" gcc-select 4.8 git checkout 9de7d9c81d91a5cfc16a1476d558d92b08d7e596 ./bootstrap CFLAGS=-Wno-error build openocd --enable-internal-jimtcl --disable-shared --enable-aice --enable-dummy --enable-ftdi --enable-jlink --enable-jtag_vpi --enable-legacy-ft2232_libftdi --enable-opendous --enable-osbdm --enable-presto_libftdi --enable-remote-bitbang --enable-stlink --enable-ti-icdi --enable-ulink --enable-usb-blaster-2 --enable-usb_blaster_libftdi --disable-ft232r --disable-xds110 sudo cp $source/openocd/tcl/target/stm8* /usr/local/share/openocd/scripts/target/
  33. Make tools available in PATH
    [ -d "/etc/paths.d" ] && { echo "$prefix/bin" >"/tmp/arm-toolchain"; sudo mv "/tmp/arm-toolchain" "/etc/paths.d/arm-toolchain"; } [ -d "/etc/paths.d" ] || grep "PATH=\"\$PATH:$prefix/bin\"" "$HOME/.bashrc" >/dev/null || echo "export PATH=\"\$PATH:$prefix/bin\"" >>"$HOME/.bashrc"

  34. The last two lines may look a bit cryptic, but I'll explain them here.

    The first line starts by checking if you have a modern system with a "/etc/paths.d" directory.
    This is a feature, that allows easy maintaining of PATH elements.
    Only a single file containing the path needs to be placed inside this directory, and $PATH will be constructed from the file "/etc/paths" plus the contents of all the files in "/etc/paths.d/".
    If the "/etc/paths.d" directory is available to us, we'll just install our path there.
    We can not use 'sudo echo' > file, as the redirection will not be executed with the new priviledges.
    Instead, we construct a temporary file in "/tmp", then we move it to "/etc/paths.d".

    In the next line, we first check if the "/etc/paths.d" directory exists.
    If it does, we don't need to do anything, because then we already installed our path above.
    If it doesn't, we check if ~/.bashrc contains the path already. If it doesn't, we'll finally add the path to ~/.bashrc!

  35. Make shorthand symlinks...
    cd "$prefix/bin" sudo ln -s $target-gcc arm-gcc sudo ln -s $target-g++ arm-g++ sudo ln -s $target-as arm-as sudo ln -s $target-ld arm-ld sudo ln -s $target-gdb arm-gdb sudo ln -s $target-objcopy arm-objcopy sudo ln -s $target-objdump arm-objdump sudo ln -s $target-run arm-run
  36. Test installation...
    cd "$workdir" which arm-gcc arm-gcc --version
  37. Build
    echo '#include <stdio.h>' >test.c; echo 'void main(){ printf("Hello World!\n"); }' >>test.c arm-gcc test.c -o test
    Note: This worked with earlier versions of gcc, but now you will most likely get a link error with a lot of undefined symbols. If you do, don't worry, the toolchain works.


  38. Run
    arm-run test

I hope this didn't give you any headache and hopefully you got the expected result.
I wish you happy coding!