Added: the correct tool chain build script.
[barrelfish] / tools / k1om / build-toolchain.sh
1 #!/usr/bin/env bash
2
3 # C/C++ toolchain build script for Barrelfish.
4 # http://redmine.aluzina.org/projects/barrelfish/wiki/Toolchain_from_sources
5 # http://wiki.barrelfish.org/CrossCompiler
6 #
7 # In order to build a toolchain you will need to install the following packages:
8 #   $ sudo apt-get install gcc g++ make patch bison flex texinfo
9 #
10 # These are also required but will be downloaded automatically during the build:
11 #   libgmp-dev, libmpc-dev, libmpfr-dev
12 #
13 # Optional (for Graphite optimizations): libcloog-ppl-dev.
14
15 set -e  # Die if any command fails.
16 set -x  # Trace each command before execution.
17
18 BASEDIR=$(pwd)
19 BASEDIR=~/eth/barrelfish
20
21
22 export TARGET=x86_64-k1om-barrelfish
23
24 # Path of your Barrelfish source and build tree.
25 BARRELFISH_SOURCE=$BASEDIR/barrelfish
26 BARRELFISH_BUILD=$BASEDIR/build
27
28 # Modify these versions to match the corresponding patch.
29 BINUTILS=binutils-2.24
30 GCC=gcc-4.8.2
31
32
33 # Directory this shell script is stored in.
34 # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
35 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
36
37 # Paths to patches.
38 BINUTILS_PATCH="${SCRIPT_DIR}/${BINUTILS}-barrelfish.patch"
39 GCC_PATCH="${SCRIPT_DIR}/${GCC}-barrelfish.patch"
40
41 #-------------------------------------------------------------------------------
42
43 # Where the toolchain will be built and installed.
44 # Note: the toolchain is specific to the Barrelfish tree mentioned above.
45 TOOLCHAIN_PREFIX=${BARRELFISH_SOURCE}/toolchain
46
47
48
49 #-------------------------------------------------------------------------------
50
51 # Sanity checks.
52 exit_with_error() { echo "error: $1" && exit 1; }
53 [[ ! -d "${BARRELFISH_SOURCE}" ]] && \
54     exit_with_error "Barrelfish source not found (${BARRELFISH_SOURCE})."
55 [[ ! -d "${BARRELFISH_BUILD}" ]] && \
56     exit_with_error "Barrelfish tree not found (${BARRELFISH_BUILD})."
57 [[   -d "${TOOLCHAIN_PREFIX}" ]] && \
58     exit_with_error "toolchain already built in ${TOOLCHAIN_PREFIX}."
59 [[ ! -f "${BINUTILS_PATCH}" ]] && \
60     exit_with_error "binutils patch not found (${BINUTILS_PATCH})."
61 [[ ! -f "${GCC_PATCH}" ]] && \
62     exit_with_error "GCC patch not found (${GCC_PATCH})."
63
64
65 # Build the toolchain.
66 export PATH=${PATH}:${TOOLCHAIN_PREFIX}/bin
67 export PREFIX=${TOOLCHAIN_PREFIX}
68
69
70 TOOLCHAIN_BUILD="$(mktemp -d --tmpdir barrelfish-toolchain-build.XXXXXXXXXX)"
71
72
73 # Build parallelism
74 MAKE_JOBS=
75 if [[ -z "${MAKE_JOBS}" ]]; then
76   # Guess a sensible value - default: #cores + 2.
77   MAKE_JOBS=$(($(grep "^core id" /proc/cpuinfo | sort -u | wc -l) + 2))
78 fi
79
80
81 pushd "${TOOLCHAIN_BUILD}"
82
83 # 1. binutils - GNU Binary Utilities
84 curl -L -O "http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.gz"
85 tar xzvf ${BINUTILS}.tar.gz
86 pushd ${BINUTILS}/
87 patch -p1 < "${BINUTILS_PATCH}"
88 popd  # ${BINUTILS}/
89 mkdir -p ${BINUTILS}-build/
90 pushd ${BINUTILS}-build/
91 ../${BINUTILS}/configure \
92     --prefix="${TOOLCHAIN_PREFIX}" \
93     --target="${TARGET}" \
94     --enable-gold=default \
95     --enable-threads \
96     --enable-lto \
97     --enable-plugins \
98     --disable-nls
99 make -j${MAKE_JOBS}
100 make install-strip
101 popd  # ${BINUTILS}-build/
102
103
104 # 2. GCC - GNU Compiler Collection
105 curl -L -O "ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/${GCC}/${GCC}.tar.bz2"
106 tar xjvf ${GCC}.tar.bz2
107 pushd ${GCC}/
108 source ./contrib/download_prerequisites
109 # http://stackoverflow.com/questions/407523/escape-a-string-for-sed-search-pattern
110 BF_SOURCE_ESCAPED=$(echo "${BARRELFISH_SOURCE}" | sed -e 's/[\/&]/\\&/g')
111 BF_BUILD_ESCAPED=$(echo "${BARRELFISH_BUILD}" | sed -e 's/[\/&]/\\&/g')
112 sed -r -e "s/\{\{BF_SRC\}\}/${BF_SOURCE_ESCAPED}/g" \
113        -e "s/\{\{BF_BUILD\}\}/${BF_BUILD_ESCAPED}/g" \
114        "${GCC_PATCH}" | patch -p1
115 popd  # ${GCC}/
116
117 mkdir -p ${GCC}-build/
118 pushd ${GCC}-build/
119 ../${GCC}/configure \
120     --prefix="${TOOLCHAIN_PREFIX}" \
121     --target="${TARGET}" \
122     --enable-languages=c,c++ \
123     --enable-initfini-array \
124     --disable-nls \
125     --disable-libssp \
126     --with-newlib \
127     --disable-multilib # from xeon phi inside cluster
128 make -j$MAKE_JOBS
129 make install-strip
130
131 popd  # ${GCC}-build/
132
133 popd  # ${TOOLCHAIN_BUILD}
134
135 rm -rf "${TOOLCHAIN_BUILD}"