InstallCrossCompiler.sh 10.2 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env bash
#
# Version: 0.2.0 - Alpha
#
# Changelog:
Ralf Stemmer's avatar
Ralf Stemmer committed
6
#  0.2.1 - 21.03.19: Exit if user is root
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#  0.2.0 - 07.03.19: Build ARM Cortex-A9 Cross Compiler
#  0.1.0 - 06.03.19: Build MicroBlaze Cross Compiler
#
# Contributors:
#  Ralf Stemmer - ralf.stemmer@uni-oldenburg.de
#

# This directory is used to download the source code and build the Compiler.
# After installation, this directory can be removed.
TMPDIR="$HOME/.bstemp"
MBXCINSTALLPATH="$TMPDIR/mbxc"
A9XCINSTALLPATH="$TMPDIR/armxc"

#GCC_VERSION="6.3.0"
GCC_VERSION="8.2.0"
#BINUTILS_VERSION="2.27"
BINUTILS_VERSION="2.31.1"
NEWLIB_VERSION="3.1.0"

# TODO: Make user do this:
#su
#mkdir /opt/mbxc
#chown -R ralf:ralf /opt/mbxc
#exit
# TODO: Check permissions - no run for root
Ralf Stemmer's avatar
Ralf Stemmer committed
32 33 34 35
if [ "$(id -u)" == 0 ] ; then
    echo -e "\e[1;31mThis script shall  not  be executed with root permissions!\e[0m"
    exit 1
fi
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

# Create temporary director for building the compiler
mkdir -p $TMPDIR


CURDIR="$pwd"

function ExtractArchive {   # Archive-file Extracted-directory-name
    ARCHIVE=$1
    DIRNAME=$2
    echo -e -n "\e[1;34mExtracting \e[0;36m$ARCHIVE\e[0m "
    if [ ! -f "$ARCHIVE" ]; then
        echo -e "\e[1;31m✘\e[1;30m (File \"$ARCHIVE\" missing)\e[0m"
        exit 1
    fi

    if [ -d "$DIRNAME" ]; then
        echo -e "\e[1;33m✔\e[1;30m (Directory \"$DIRNAME\" already existing)\e[0m"
        return
    fi

    tar xf $ARCHIVE

    if [ ! -d "$DIRNAME" ]; then
        echo -e "\e[1;31m✘\e[1;30m (Directory \"$DIRNAME\" not unpacked)\e[0m"
        exit 1
    fi
    echo -e "\e[1;32m✔\e[0m"
}

function DownloadArchive { # URL(without archive name) ARCHIVE
    URL=$1/$2
    FILENAME=$2

    echo -e -n "\e[1;34mDownloading \e[0;36m$FILENAME\e[0m "
    if [ -f "$FILENAME" ]; then
        echo -e "\e[1;33m✔\e[1;30m (File \"$FILENAME\" already downloaded)\e[0m"
        return
    fi

    wget -o ./wget.log $URL

    if [ ! -f "$FILENAME" ]; then
        echo -e "\e[1;31m✘\e[1;30m (Downloading \"$FILENAME\" failed. See wget.log)\e[0m"
        exit 1
    fi

    echo -e "\e[1;32m✔\e[0m"
    rm ./wget.log   # on success, the log file is not needed anymore
}


function DownloadSourceCode {
    cd $TMPDIR
    DownloadArchive ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-$GCC_VERSION gcc-$GCC_VERSION.tar.gz
    DownloadArchive ftp://sourceware.org/pub/newlib newlib-$NEWLIB_VERSION.tar.gz
    DownloadArchive http://ftp.gnu.org/gnu/binutils binutils-$BINUTILS_VERSION.tar.gz
    cd $CURDIR
}


function BuildBinutils {    # target:"MicroBlaze","Cortex-A9"
    TARGETID="$1"

    # Check if already installed
    cd $TMPDIR
    if [ -f ./binutils.$TARGETID.flag ]; then
        echo -e "\e[1;33mbinutis already installed\e[0m"
        return
    fi

    ExtractArchive binutils-$BINUTILS_VERSION.tar.gz binutils-$BINUTILS_VERSION
    cd $TMPDIR/binutils-$BINUTILS_VERSION

    # Prepare configuration
    echo -e -n "Preparing configuration "
    if [ "$TARGETID" = "MicroBlaze" ] ; then
        PREFIX=$MBXCINSTALLPATH
        PPREFIX="mb-"
        TARGET="microblazeel-xilinx-elf"
    elif [ "$TARGETID" = "Cortex-A9" ] ; then
        PREFIX=$A9XCINSTALLPATH
        PPREFIX="arm-none-eabi-"
        TARGET="arm-none-eabi"
    else
        echo -e "\e[1;31m✘\e[1;30m (Invalid Target \"$TARGETID\". Valid: \"MicroBlaze\", \"Cortex-A9\")\e[0m"
        exit 1
    fi
    echo -e "\e[1;32m✔\e[0m"

    # Run build process
    echo -e "Configuring source code"
    ./configure --prefix=$PREFIX --program-prefix=$PPREFIX --target=$TARGET
    if [ $? -ne 0 ] ; then
        echo -e "\e[1;31mfailed! \e[1;30m(Install missing dependencies and rerun this script)\e[0m"
    fi

    make
    make install
    make clean

    # Flag as complete
    cd $TMPDIR
    touch ./binutils.$TARGETID.flag
    rm -rf binutils-$BINUTILS_VERSION
    cd $CURDIR
    echo -e "\e[1;32mbinutils successfully installed.\e[0m"
}


function BuildGCCStage1 {
    TARGETID="$1"

    # Check if already installed
    cd $TMPDIR
    if [ -f ./gccstage1.$TARGETID.flag ]; then
        echo -e "\e[1;33mGCC Stage 1 already installed\e[0m"
        return
    fi

    ExtractArchive gcc-$GCC_VERSION.tar.gz gcc-$GCC_VERSION

    # Prepare configuration
    echo -e -n "\e[1;34mPreparing configuration \e[0m"
    if [ "$TARGETID" = "MicroBlaze" ] ; then
        PREFIX=$MBXCINSTALLPATH
        PPREFIX="mb-"
        TARGET="microblazeel-xilinx-elf"
        FLAGS="--enable-languages=c --disable-nls --without-headers --disable-multilib --disable-libssp --with-newlib"
    elif [ "$TARGETID" = "Cortex-A9" ] ; then
        PREFIX=$A9XCINSTALLPATH
        PPREFIX="arm-none-eabi-"
        TARGET="arm-none-eabi"
        FLAGS="--without-headers --enable-boostrap --enable-languages=c --disable-threads --enable-__cxa_atexit --disable-libmudflap --with-gnu-ld --with-gnu-as --disable-libssp --disable-libgomp --disable-nls --disable-shared --with-arch=armv7-a --with-tune=cortex-a9 --with-fpu=neon --with-float=hard"
        # Source: http://ibuzzlog.blogspot.com/2014/12/how-to-build-gcc-arm-cross-compiler-for.html
    else
        echo -e "\e[1;31m✘\e[1;30m (Invalid Target \"$TARGETID\". Valid: \"MicroBlaze\", \"Cortex-A9\")\e[0m"
        exit 1
    fi
    echo -e "\e[1;32m✔\e[0m"
    
    cd $TMPDIR/gcc-$GCC_VERSION
 
    ./contrib/download_prerequisites
 
    mkdir build
    cd build/
 
    ../configure --prefix=$PREFIX --program-prefix=$PPREFIX --target=$TARGET $FLAGS
    make all-host
    make install-host

# FOR ARM?
#make -j4 -k all-target-libgcc
#make -i install-target-libgcc

    make clean
    cd ..
    rm -r build

    # Flag as complete
    cd $TMPDIR
    touch ./gccstage1.$TARGETID.flag
    rm -rf gcc-$GCC_VERSION
    cd $CURDIR
    echo -e "\e[1;32mGCC successfully installed.\e[0m"
}


function BuildNewlib {

    TARGETID="$1"

    # Check if already installed
    cd $TMPDIR
    if [ -f ./newlib.$TARGETID.flag ]; then
        echo -e "\e[1;33mGCC Stage 1 already installed\e[0m"
        return
    fi
    ExtractArchive newlib-$NEWLIB_VERSION.tar.gz newlib-$NEWLIB_VERSION

    # Prepare configuration
    echo -e -n "\e[1;34mPreparing configuration \e[0m"
    if [ "$TARGETID" = "MicroBlaze" ] ; then
        PREFIX=$MBXCINSTALLPATH
        PPREFIX="mb-"
        TARGET="microblazeel-xilinx-elf"
        FLAGS="--enable-newlib-hw-fp"
    elif [ "$TARGETID" = "Cortex-A9" ] ; then
        PREFIX=$A9XCINSTALLPATH
        PPREFIX="arm-none-eabi-"
        TARGET="arm-none-eabi"
        FLAGS="--enable-newlib-hw-fp --disable-newlib-supplied-syscalls"
    else
        echo -e "\e[1;31m✘\e[1;30m (Invalid Target \"$TARGETID\". Valid: \"MicroBlaze\", \"Cortex-A9\")\e[0m"
        exit 1
    fi
    echo -e "\e[1;32m✔\e[0m"
    
    cd $TMPDIR/newlib-$NEWLIB_VERSION

    export     CC_FOR_TARGET="$PREFIX/bin/$PPREFIX"gcc
    export    CXX_FOR_TARGET="$PREFIX/bin/$PPREFIX"g++
    export    GCC_FOR_TARGET="$PREFIX/bin/$PPREFIX"gcc
    export     AR_FOR_TARGET="$PREFIX/bin/$PPREFIX"ar
    export     AS_FOR_TARGET="$PREFIX/bin/$PPREFIX"as
    export     LD_FOR_TARGET="$PREFIX/bin/$PPREFIX"ld
    export     NM_FOR_TARGET="$PREFIX/bin/$PPREFIX"nm
    export RANLIB_FOR_TARGET="$PREFIX/bin/$PPREFIX"ranlib
    export  STRIP_FOR_TARGET="$PREFIX/bin/$PPREFIX"strip
 
 
    mkdir build
    cd build
 
    ../configure --prefix=$PREFIX --target=$TARGET $FLAGS
    make
    make install
    make clean

    cd ..
    rm -r build
 
    unset CC_FOR_TARGET
    unset CXX_FOR_TARGET
    unset GCC_FOR_TARGET
    unset AS_FOR_TARGET
    unset AR_FOR_TARGET
    unset LD_FOR_TARGET
    unset NM_FOR_TARGET
    unset STRIP_FOR_TARGET
    unset RANLIB_FOR_TARGET

    # Flag as complete
    cd $TMPDIR
    touch ./newlib.$TARGETID.flag
    rm -rf newlib-$NEWLIB_VERSION
    cd $CURDIR
    echo -e "\e[1;32mnewlib successfully installed.\e[0m"
}


function BuildGCCStage2 {
    TARGETID="$1"

    # Check if already installed
    cd $TMPDIR
    if [ -f ./gccstage2.$TARGETID.flag ]; then
        echo -e "\e[1;33mGCC Stage 2 already installed\e[0m"
        return
    fi

    ExtractArchive gcc-$GCC_VERSION.tar.gz gcc-$GCC_VERSION

    # Prepare configuration
    echo -e -n "\e[1;34mPreparing configuration \e[0m"
    if [ "$TARGETID" = "MicroBlaze" ] ; then
        PREFIX=$MBXCINSTALLPATH
        PPREFIX="mb-"
        TARGET="microblazeel-xilinx-elf"
        FLAGS="--enable-languages=c,c++ --disable-nls --without-headers --disable-multilib --disable-libssp --with-newlib"
    elif [ "$TARGETID" = "Cortex-A9" ] ; then
        PREFIX=$A9XCINSTALLPATH
        PPREFIX="arm-none-eabi-"
        TARGET="arm-none-eabi"
        FLAGS="--without-headers --enable-boostrap --enable-languages=c --disable-threads --enable-__cxa_atexit --disable-libmudflap --with-gnu-ld --with-gnu-as --disable-libssp --disable-libgomp --disable-nls --disable-shared --with-arch=armv7-a --with-tune=cortex-a9 --with-fpu=neon --with-float=hard"
        FLAGS="--enable-languages=c,c++ --without-headers --disable-multilib --with-float=soft --disable-sjlj-exceptions --disable-nls --disable-libmudflap --disable-libssp --enable-long-longx --enable-libgomp --with-arch=armv7-a --with-tune=cortex-a9 --with-fpu=neon --with-float=hard"
    else
        echo -e "\e[1;31m✘\e[1;30m (Invalid Target \"$TARGETID\". Valid: \"MicroBlaze\", \"Cortex-A9\")\e[0m"
        exit 1
    fi
    echo -e "\e[1;32m✔\e[0m"
    
    cd $TMPDIR/gcc-$GCC_VERSION
 
    mkdir build
    cd build/
 
    ../configure --prefix=$PREFIX --program-prefix=$PPREFIX --target=$TARGET $FLAGS
    make all
    make install all
    make clean
    cd ..
    rm -r build

    # Flag as complete
    cd $TMPDIR
    touch ./gccstage2.$TARGETID.flag
    rm -rf gcc-$GCC_VERSION
    cd $CURDIR
    echo -e "\e[1;32mGCC successfully installed.\e[0m"
}


echo -e "\e[1;37mPreparing Source Files\e[0m"
DownloadSourceCode

echo -e "\e[1;37mInstalling binutils\e[0m"
BuildBinutils "MicroBlaze"
echo -e "\e[1;37mInstalling gcc stage 1\e[0m"
BuildGCCStage1 "MicroBlaze"
echo -e "\e[1;37mInstalling newlib\e[0m"
BuildNewlib "MicroBlaze"
echo -e "\e[1;37mInstalling gcc stage 2\e[0m"
BuildGCCStage2 "MicroBlaze"

echo -e "\e[1;37mInstalling binutils\e[0m"
BuildBinutils "Cortex-A9"
echo -e "\e[1;37mInstalling gcc stage 1\e[0m"
BuildGCCStage1 "Cortex-A9"
echo -e "\e[1;37mInstalling newlib\e[0m"
BuildNewlib "Cortex-A9"
echo -e "\e[1;37mInstalling gcc stage 2\e[0m"
BuildGCCStage2 "Cortex-A9"