Build your own gcc compiler

Recently I needed a new c++ compiler with full support of C++11 standard. This support has the latest gcc (4.8.2). Unfortunatelly there is no built gcc 4.8.2 packages for my old Debian Squeeze system. The latest gcc build for Debian is 4.8.1 and it’s available only in testing and unstable. Because I don’t want to upgrade my system to testing or unstable and import of 4.8 package with resolving all of the dependencies is a mess, I decided to build gcc from source.

Actually it is not much different from building other programs from source. The process is pretty automated already. I’ve made a script that hopefully will help somebody to make this process even easier. Here you can find script and prebuilt gcc 4.8.2 (only c and c++ support) for Debian Squeeze.

 

#!/bin/bash

# enable extra checks in bash script
set -u

function get_latest_version()
{
    VERSION=$(curl –ftp-pasv ftp://$SERVER/$GCC_PATH/ | grep ‘^drwx.*gcc-‘ | awk ‘{ print $9 }’ | sed ‘s/gcc-//’ | sort -V -r |  head -n 1)
    if [ “$VERSION” == “”  ]; then
        echo ‘Failed to get latest version of gcc’
        exit
    fi
}

# ——————————————————————
# set the user version
SERVER=ftp.gnu.org
GCC_PATH=gnu/gcc
VERSION=${1:-}
if [ “$VERSION” == “” ]; then
    get_latest_version
fi

echo “Build Gnu C and C++ compilers $VERSION”

# ——————————————————————
# prepare directories
if [ ! -d $VERSION ]; then
    mkdir -p $VERSION
fi
cd $VERSION

if [ ! -d build-$VERSION ]; then
    mkdir -p build-$VERSION
fi

# ——————————————————————
# download gcc source code

SRC=gcc-$VERSION.tar.bz2
SRC_SIG=$SRC.sig
SRC_PATH=$GCC_PATH/gcc-$VERSION/

if [ ! -e $SRC ]; then
    wget -c ftp://$SERVER/$SRC_PATH/$SRC
    if [ $? -ne 0 ]; then exit 1; fi
fi

if [ ! -e $SRC_SIG ]; then
    wget -c ftp://$SERVER/$SRC_PATH/$SRC_SIG
    if [ $? -ne 0 ]; then exit 1; fi
fi

# check signatures
gpg –verify $SRC_SIG
if [ $? -ne 0 ]; then exit 1; fi

# ——————————————————————
# unpack source code
SRCDIR=gcc-$VERSION/
if [ ! -d $SRCDIR ]; then
    tar -xf $SRC
fi

# ——————————————————————
# download additional libraries needed for compilation
cd $SRCDIR
./contrib/download_prerequisites
cd ..

# ——————————————————————
#                            configure
# ——————————————————————
cd build-$VERSION

CONFIG_OPT=
# don’t use option
# –enable-version-specific-runtime-libs
# it seems to be broken for a long time
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32415

# –disable-multilib your final package will twice small as with
# multilib support (enabled by default)
CONFIG_OPT=”$CONFIG_OPT –disable-multilib”

# install not in the default /usr/local path
# I don’t want installed libraries to be overwritten (because of
CONFIG_OPT=”$CONFIG_OPT –prefix=/opt/x86_64-unknown-linux-gnu/$VERSION”

# compilers executable will have clearly different names
CONFIG_OPT=”$CONFIG_OPT –program-suffix=-$VERSION”

# build only needed programming languages
CONFIG_OPT=”$CONFIG_OPT –enable-languages=c,c++”
../$SRCDIR/configure $CONFIG_OPT  
if [ $? -ne 0 ]; then exit 1; fi

exit

# ——————————————————————

# build binaries
make disclean

CPU_CORES=$(cat /proc/cpuinfo | grep ‘^processor’ | wc -l)
make -j $(( $CPU_CORES + 1 ))
if [ $? -ne 0 ]; then exit 1; fi

# build debian package
PACKAGE_INFO=
PACKAGE_INFO=$PACKAGE_INFO –pkgname gcc-$VERSION
PACKAGE_INFO=$PACKAGE_INFO –pkgversion $VERSION
PACKAGE_INFO=$PACKAGE_INFO –maintainer “-“o
PACKAGE_INFO=$PACKAGE_INFO –provides gcc-$VERSION
PACKAGE_INFO=$PACKAGE_INFO –nodoc
PACKAGE_INFO=$PACKAGE_INFO –type “debian”
PACKAGE_INFO=$PACKAGE_INFO –pakdir “../”

echo “gcc and g++ $VERSION” > description-pak

sudo checkinstall $PACKAGE_INFO –install=yes –default make install

cd ..

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s