Skip to content

Instantly share code, notes, and snippets.

@LumitoLuma
Last active June 12, 2021 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LumitoLuma/9007955747513dbdc34430c011ed096e to your computer and use it in GitHub Desktop.
Save LumitoLuma/9007955747513dbdc34430c011ed096e to your computer and use it in GitHub Desktop.
Build script for legacy AVR-GCC https://avr.lumito.net/
#!/bin/bash
# http://www.nongnu.org/avr-libc/user-manual/install_tools.html
# For optimum compile time this should generally be set to the number of CPU cores your machine has
JOBCOUNT=$(getconf _NPROCESSORS_ONLN)
# Build for Linux
# A Linux AVR-GCC toolchain is required to build a Windows toolchain
# If the Linux toolchain has already been built then you can set this to 0
FOR_LINUX=1
# Build for 32 bit Windows
FOR_WINX86=1
# Build for 64 bit Windows
FOR_WINX64=1
# Build Binutils for selected OSs
BUILD_BINUTILS=1
# Build GCC for selected OSs (requires AVR-Binutils)
BUILD_GCC=1
# Build GDB for selected OSs
BUILD_GDB=1
# Build AVR-LibC (requires AVR-GCC)
BUILD_LIBC=1
NAME_BINUTILS="binutils-2.36.1"
NAME_GCC="gcc-8.5.0"
NAME_GDB="gdb-10.2"
NAME_LIBC="avr-libc-2.0.0svn"
# Output locations for built toolchains
BASE=/avr-${NAME_GCC}-1/
PREFIX_GCC_LINUX=${BASE}avr-${NAME_GCC}-1-x64-linux
PREFIX_GCC_WINX86=${BASE}avr-${NAME_GCC}-1-x86-windows
PREFIX_GCC_WINX64=${BASE}avr-${NAME_GCC}-1-x64-windows
PREFIX_LIBC=${BASE}avr-libc # The contents of the avr-libc directory will need to be copied/merged with each of the target toolchain directories
HOST_WINX86="i686-w64-mingw32"
HOST_WINX64="x86_64-w64-mingw32"
OPTS_BINUTILS="
--target=avr
--disable-nls
--disable-werror
--with-pkgversion=AVR-Binutils
"
OPTS_GCC="
--target=avr
--enable-languages=c,c++,lto
--disable-nls
--disable-libssp
--disable-libada
--with-dwarf2
--disable-shared
--enable-static
--enable-mingw-wildcard
--enable-plugin
--with-gnu-as
--with-pkgversion=AVR-GCC
"
OPTS_GDB="
--target=avr
--with-static-standard-libraries
--with-pkgversion=AVR-GDB
"
OPTS_LIBC=""
# Install packages
if hash apt-get 2>/dev/null; then
apt install -y wget make mingw-w64 gcc g++ bzip2 autoconf texinfo dpkg-dev subversion
elif hash yum 2>/dev/null; then
# This works for CentOS 7
yum install -y wget texinfo
rpm -q epel-release-7-6.noarch >/dev/null
if [ $? -ne 0 ]; then
# EPEL is for the MinGW stuff
rm -f epel-release-7-6.noarch.rpm
wget https://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel//7/x86_64/e/epel-release-7-6.noarch.rpm
rpm -Uvh epel-release-7-6.noarch.rpm
fi
yum install -y make mingw64-gcc mingw64-gcc-c++ mingw32-gcc mingw32-gcc-c++ gcc gcc-c++ bzip2 autoconf subversion
elif hash pacman 2>/dev/null; then
# Things have changed with Arch and this is now broken :/
pacman -S --needed --noconfirm wget make mingw-w64-binutils mingw-w64-gcc mingw-w64-crt mingw-w64-headers mingw-w64-winpthreads subversion gcc bzip2 autoconf texinfo
fi
# Stop on errors
set -e
TIME_START=$(date +%s)
makeDir()
{
rm -rf "$1/"
mkdir -p "$1"
}
echo "Clearing output directories..."
[ $FOR_LINUX -eq 1 ] && makeDir "$PREFIX_GCC_LINUX"
[ $FOR_WINX86 -eq 1 ] && makeDir "$PREFIX_GCC_WINX86"
[ $FOR_WINX64 -eq 1 ] && makeDir "$PREFIX_GCC_WINX64"
[ $BUILD_LIBC -eq 1 ] && makeDir "$PREFIX_LIBC"
echo "Clearing old downloads..."
rm -f $NAME_BINUTILS.tar.xz
rm -rf $NAME_BINUTILS/
rm -f $NAME_GCC.tar.xz
rm -rf $NAME_GCC/
rm -f $NAME_GDB.tar.xz
rm -rf $NAME_GDB/
rm -f $NAME_LIBC.tar.bz2
rm -rf $NAME_LIBC/
echo "Downloading sources..."
[ $BUILD_BINUTILS -eq 1 ] && wget https://ftpmirror.gnu.org/binutils/$NAME_BINUTILS.tar.xz
[ $BUILD_GCC -eq 1 ] && wget https://ftpmirror.gnu.org/gcc/$NAME_GCC/$NAME_GCC.tar.xz
[ $BUILD_GDB -eq 1 ] && wget https://ftpmirror.gnu.org/gdb/$NAME_GDB.tar.xz
if [ $BUILD_LIBC -eq 1 ]; then
if [ "$NAME_LIBC" = "avr-libc-2.0.0svn" ]; then
svn co svn://svn.savannah.nongnu.org/avr-libc/trunk .
else
wget https://download.savannah.gnu.org/releases/avr-libc/$NAME_LIBC.tar.bz2
fi
fi
PATH="$PREFIX_GCC_LINUX"/bin:"$PATH"
export PATH
CC=""
export CC
confMake()
{
../configure --prefix=$1 $2 $3 $4
make -j $JOBCOUNT
make install-strip
rm -rf *
}
confMakeGDB()
{
# install-strip doesn't work properly with GDB, so we have to manually strip the massive 100MB+ executable down to ~5MB then do a normal install
../configure --prefix=$2 $3 $4 $5
make -j $JOBCOUNT
$1
make install
rm -rf *
}
# Make AVR-Binutils
if [ $BUILD_BINUTILS -eq 1 ]; then
echo "Making AVR-Binutils..."
echo "Extracting..."
tar xf $NAME_BINUTILS.tar.xz
mkdir -p $NAME_BINUTILS/obj-avr
cd $NAME_BINUTILS/obj-avr
[ $FOR_LINUX -eq 1 ] && confMake "$PREFIX_GCC_LINUX" "$OPTS_BINUTILS"
[ $FOR_WINX86 -eq 1 ] && confMake "$PREFIX_GCC_WINX86" "$OPTS_BINUTILS" --host=$HOST_WINX86 --build=`../config.guess`
[ $FOR_WINX64 -eq 1 ] && confMake "$PREFIX_GCC_WINX64" "$OPTS_BINUTILS" --host=$HOST_WINX64 --build=`../config.guess`
cd ../../
else
echo "Skipping AVR-Binutils..."
fi
# Make AVR-GCC
if [ $BUILD_GCC -eq 1 ]; then
echo "Making AVR-GCC..."
echo "Extracting..."
tar xf $NAME_GCC.tar.xz
mkdir -p $NAME_GCC/obj-avr
cd $NAME_GCC
chmod +x ./contrib/download_prerequisites
./contrib/download_prerequisites
cd obj-avr
[ $FOR_LINUX -eq 1 ] && confMake "$PREFIX_GCC_LINUX" "$OPTS_GCC"
[ $FOR_WINX86 -eq 1 ] && confMake "$PREFIX_GCC_WINX86" "$OPTS_GCC" --host=$HOST_WINX86 --build=`../config.guess`
[ $FOR_WINX64 -eq 1 ] && confMake "$PREFIX_GCC_WINX64" "$OPTS_GCC" --host=$HOST_WINX64 --build=`../config.guess`
cd ../../
else
echo "Skipping AVR-GCC..."
fi
# Make GDB
if [ $BUILD_GDB -eq 1 ]; then
echo "Making AVR-GDB..."
echo "Extracting..."
tar xf $NAME_GDB.tar.xz
mkdir -p $NAME_GDB/obj-avr
cd $NAME_GDB/obj-avr
[ $FOR_LINUX -eq 1 ] && confMakeGDB "strip gdb/gdb" "$PREFIX_GCC_LINUX" "$OPTS_GDB"
[ $FOR_WINX86 -eq 1 ] && confMakeGDB "$HOST_WINX86-strip gdb/gdb.exe" "$PREFIX_GCC_WINX86" "$OPTS_GDB" --host=$HOST_WINX86 --build=`../config.guess`
[ $FOR_WINX64 -eq 1 ] && confMakeGDB "$HOST_WINX64-strip gdb/gdb.exe" "$PREFIX_GCC_WINX64" "$OPTS_GDB" --host=$HOST_WINX64 --build=`../config.guess`
cd ../../
else
echo "Skipping AVR-GDB..."
fi
# Make AVR-LibC
if [ $BUILD_LIBC -eq 1 ]; then
echo "Making AVR-LibC..."
if [ "$NAME_LIBC" = "avr-libc-2.0.0svn" ]; then
echo "Preparing..."
cd avr-libc
./bootstrap
cd ..
mkdir -p avr-libc/obj-avr
cd avr-libc/obj-avr
else
echo "Extracting..."
bunzip2 -c $NAME_LIBC.tar.bz2 | tar xf -
mkdir -p $NAME_LIBC/obj-avr
cd $NAME_LIBC/obj-avr
fi
confMake "$PREFIX_LIBC" "$OPTS_LIBC" --host=avr --build=`../config.guess`
cd ../../
else
echo "Skipping AVR-LibC..."
fi
TIME_END=$(date +%s)
TIME_RUN=$(($TIME_END - $TIME_START))
echo ""
echo "Done in $TIME_RUN seconds"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment