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