#!/bin/bash
# Build script for MSP430 GCC v5_000_00_00 by Simply Embedded Inc.
# Inspired by the README file provided by the compiler source
# Run from the directory where the source and support packages are located
# Must run as root!
# See simplyembedded.org/tutorials/build-msp430-gcc-toolchain for details

# Installation directory of the toolchain
export PREFIX=/opt/msp430-toolchain

# Target architecture
export TARGET=msp430-none-elf

# Include the toolchain in the path
export PATH=$PREFIX/bin:$PATH

# Download prerequisites
sudo apt-get install texinfo expect libx11-dev g++ flex bison libncurses5-dev

# Unpack the source
tar xvf msp430-gcc-6.2.1.16_source-full.tar.bz2
pushd msp430-gcc-6.2.1.16_source-full

# Download gcc specific prerequisites an create symbolic links to newlib
pushd gcc
source ./contrib/download_prerequisites
ln -fns ../newlib/libgloss libgloss
ln -fns ../newlib/newlib/ newlib
popd

# Create build directories
mkdir build
cd build
mkdir binutils
mkdir gcc
mkdir gdb

# Build and install binutils
pushd binutils
../../binutils/configure --target=$TARGET --prefix=$PREFIX --program-prefix=msp430- --enable-languages=c,c++ --disable-nls
make
sudo make install
popd

# Build and install gcc
pushd gcc
../../gcc/configure --target=$TARGET --prefix=$PREFIX --program-prefix=msp430- --enable-languages=c,c++ --disable-nls
make
sudo make install
popd

# Build and install gdb
pushd gdb
../../gdb/configure --target=$TARGET --prefix=$PREFIX --program-prefix=msp430- --enable-languages=c,c++ --disable-nls
make
sudo make install
popd

# Install the support files
popd
unzip msp430-gcc-support-files-1.198.zip 
cd msp430-gcc-support-files/include/
chmod -R 644 *
sudo cp *.ld $PREFIX/msp430-none-elf/lib/430/
sudo cp *.h $PREFIX//msp430-none-elf/include/

