patches for the mpss gcc
[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 #-------------------------------------------------------------------------------
19
20 # Modify these versions to match the corresponding patch.
21 BINUTILS=binutils-2.22+mpss3.2
22 GCC=gcc-4.7.0+mpss3.2
23
24 # Path of your Barrelfish source and build tree.
25 BARRELFISH_SOURCE=/home/acreto/barrelfish.xeon-phi
26 BARRELFISH_BUILD=${BARRELFISH_SOURCE}/build
27
28 # Where the toolchain will be built and installed.
29 # Note: the toolchain is specific to the Barrelfish tree mentioned above.
30 TOOLCHAIN_PREFIX=${BARRELFISH_SOURCE}/toolchain
31
32 # Cross compiler target.
33 #TARGET=x86_64-pc-barrelfish
34 TARGET=k1om-pc-barrelfish
35 #TARGET=i586-pc-barrelfish
36 #TARGET=i586-scc-barrelfish
37
38 # Directory this shell script is stored in.
39 # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
40 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
41
42 # Paths to patches.
43 BINUTILS_PATCH="${SCRIPT_DIR}/${BINUTILS}-barrelfish.patch"
44 GCC_PATCH="${SCRIPT_DIR}/${GCC}-barrelfish.patch"
45
46 # Build parallelism
47 MAKE_JOBS=
48 if [[ -z "${MAKE_JOBS}" ]]; then
49   # Guess a sensible value - default: #cores + 2.
50   MAKE_JOBS=$(($(grep "^core id" /proc/cpuinfo | sort -u | wc -l) + 2))
51 fi
52
53 #-------------------------------------------------------------------------------
54
55 # Sanity checks.
56 exit_with_error() { echo "error: $1" && exit 1; }
57 [[ ! -d "${BARRELFISH_SOURCE}" ]] && \
58     exit_with_error "Barrelfish source not found (${BARRELFISH_SOURCE})."
59 [[ ! -d "${BARRELFISH_BUILD}" ]] && \
60     exit_with_error "Barrelfish tree not found (${BARRELFISH_BUILD})."
61 [[   -d "${TOOLCHAIN_PREFIX}" ]] && \
62     exit_with_error "toolchain already built in ${TOOLCHAIN_PREFIX}."
63 [[ ! -f "${BINUTILS_PATCH}" ]] && \
64     exit_with_error "binutils patch not found (${BINUTILS_PATCH})."
65 [[ ! -f "${GCC_PATCH}" ]] && \
66     exit_with_error "GCC patch not found (${GCC_PATCH})."
67
68 TOOLCHAIN_BUILD="$(mktemp -d --tmpdir barrelfish-toolchain-build.XXXXXXXXXX)"
69
70 # Build the toolchain.
71 export PATH=${PATH}:${TOOLCHAIN_PREFIX}/bin
72
73 pushd "${TOOLCHAIN_BUILD}"
74
75 # 1. binutils - GNU Binary Utilities
76 cp "${SCRIPT_DIR}/${BINUTILS}.tar.bz2" "${TOOLCHAIN_BUILD}/${BINUTILS}.tar.bz2"
77 tar xjvf "${TOOLCHAIN_BUILD}/${BINUTILS}.tar.bz2"
78 pushd ${BINUTILS}/
79 patch -p1 < "${BINUTILS_PATCH}"
80 popd  # ${BINUTILS}/
81
82 mkdir -p ${BINUTILS}-build/
83 pushd ${BINUTILS}-build/
84 ../${BINUTILS}/configure \
85     --prefix="${TOOLCHAIN_PREFIX}" \
86     --target="${TARGET}" \
87     --enable-threads \
88     --enable-lto \
89     --enable-plugins \
90     --disable-nls \
91     --disable-doc \
92     MAKEINFO=missing
93 make MAKEINFO=true -j${MAKE_JOBS}
94 make MAKEINFO=true install-strip 
95 popd  # ${BINUTILS}-build/
96
97 # 2. GCC - GNU Compiler Collection
98 cp "${SCRIPT_DIR}/${GCC}.tar.bz2" "${TOOLCHAIN_BUILD}/${GCC}.tar.bz2"
99 tar xjvf "${TOOLCHAIN_BUILD}/${GCC}.tar.bz2"
100 pushd ${GCC}/
101 source ./contrib/download_prerequisites
102 # http://stackoverflow.com/questions/407523/escape-a-string-for-sed-search-pattern
103 BF_SOURCE_ESCAPED=$(echo "${BARRELFISH_SOURCE}" | sed -e 's/[\/&]/\\&/g')
104 BF_BUILD_ESCAPED=$(echo "${BARRELFISH_BUILD}" | sed -e 's/[\/&]/\\&/g')
105 sed -r -e "s/\{\{BF_SRC\}\}/${BF_SOURCE_ESCAPED}/g" \
106        -e "s/\{\{BF_BUILD\}\}/${BF_BUILD_ESCAPED}/g" \
107        "${GCC_PATCH}" | patch -p1
108 popd  # ${GCC}/
109
110 mkdir -p ${GCC}-build/
111 pushd ${GCC}-build/
112 CC=gcc-4.7 ../${GCC}/configure \
113     --prefix="${TOOLCHAIN_PREFIX}" \
114     --target="${TARGET}" \
115     --enable-languages=c \
116     --enable-initfini-array \
117     --disable-nls \
118     --disable-multilib \
119     --disable-libssp \
120     --with-newlib \
121   #  --with-cpu-64=k1om \
122   #  --with-arch-64=k1om \
123   #  --with-tune-64=k1om \
124     MAKEINFO=missing
125 make MAKEINFO=true -j$MAKE_JOBS
126 make MAKEINFO=true install-strip
127 popd  # ${GCC}-build/
128
129 popd  # ${TOOLCHAIN_BUILD}
130
131 rm -rf "${TOOLCHAIN_BUILD}"
132 rm -rf "${TOOLCHAIN_BUILD}"