/
toolchain.sh
executable file
·355 lines (295 loc) · 12.3 KB
/
toolchain.sh
1
2
3
4
5
6
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
32
33
34
35
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
351
352
353
354
#/bin/env bash
# .------------------------------------------------------.
# | tachyon toolchain bootstrap and environment scripts |
# '------------------------------------------------------'
# the tools array implicitly defines the build order, so be sure to keep those aligned.
# each entry has this format: <name>:<version>:<configure flags>:<all target>:<install target>
tools=(
"binutils:2.24:--build=\${_tt_host} --target=x86_64-pc-elf"
"gmp:5.1.3:--build=\${_tt_host}"
"mpfr:3.1.2:--build=\${_tt_host}"
"mpc:1.0.2:--build=\${_tt_host} --with-gmp=\${_tt_prefix} --with-mpfr=\${_tt_prefix}"
"gcc:4.8.2:--build=\${_tt_host} --with-gnu-ld --with-gnu-as --with-mpfr=\${_tt_prefix} --with-gmp=\${_tt_prefix} --with-mpc=\${_tt_prefix} --target=x86_64-pc-elf --enable-languages=c,c++:all-gcc:install-gcc"
"gdb:7.7:--build=\${_tt_host} --target=x86_64-pc-linux-gnu --disable-werror"
"cgdb:0.6.7:--build=\${_tt_host}"
"grub:2.00:--build=\${_tt_host} --disable-werror"
"xorriso:1.3.6:--build=\${_tt_host}"
"qemu:2.0.0-rc0:--python=/usr/bin/python2 --disable-user --enable-system --enable-curses --enable-sdl --target-list=i386-softmmu,x86_64-softmmu --enable-debug"
"bochs:2.6.2:--with-x11 --with-x --with-term --disable-docbook --enable-cdrom --enable-pci --enable-usb --enable-usb-ohci --enable-a20-pin --enable-cpu-level=6 --enable-x86-64 --enable-fpu --enable-disasm --enable-idle-hack --enable-all-optimizations --enable-repeat-speedups --enable-plugins --enable-sb16=linux --enable-ne2000 --enable-pcidev --enable-pnic --enable-smp --enable-logging"
)
sites=(
"binutils|http://ftp.gnu.org/gnu/binutils/\${P}-\${V}.tar.gz"
"gmp|https://gmplib.org/download/gmp/\${P}-\${V}.tar.bz2"
"mpfr|http://www.mpfr.org/mpfr-current/\${P}-\${V}.tar.bz2"
"mpc|http://ftp.gnu.org/gnu/mpc/\${P}-\${V}.tar.gz"
"gcc|http://gd.tuwien.ac.at/gnu/gcc/releases/gcc-4.8.2/\${P}-\${V}.tar.bz2"
"gdb|http://ftp.gnu.org/gnu/gdb/\${P}-\${V}.tar.bz2"
"cgdb|http://cgdb.me/files/\${P}-\${V}.tar.gz"
"grub|http://ftp.gnu.org/gnu/grub/\${P}-\${V}.tar.gz"
"xorriso|http://ftp.gnu.org/gnu/xorriso/\${P}-\${V}.tar.gz"
"qemu|http://wiki.qemu-project.org/download/\${P}-\${V}.tar.bz2"
"bochs|http://downloads.sourceforge.net/project/bochs/bochs/2.6.2/\${P}-\${V}.tar.gz"
)
function info() { [[ ${_tt_verbose} == true ]] && echo ">>> " "$@"; }
function warn() { echo "!!! " "$@"; }
function error() { echo "*** " "$@"; }
function fatal() { error "$@"; exit 1; }
function tt_exit_help() {
echo "${help}"
exit $1
}
_tt_home="$(cd "$(dirname "$0")"; pwd)"
_tt_verbose=false
_tt_source="${_tt_home}/.source"
_tt_bdir="${_tt_home}/.build"
_tt_prefix="$(cd "${_tt_home}/.."; pwd)/.package"
_tt_preflight=false
_tt_clean=false
_tt_download=false
_tt_jobs=1
_tt_env="${_tt_prefix}/env.sh"
_tt_mkenv="${_tt_prefix}/env.mk"
_tt_pkg=
_tt_host="$(uname -m)-pc-linux-gnu"
help="$0 usage:
--help print this message.
-v | --verbose verbose output.
-j <j> | --jobs=<j> use 'j' jobs during builds.
--download download sources
--clean remove existing build directories before building.
--preflight check package completeness (source availability) before
--package=<p> only build a single package 'p'.
-e <e> | --env=<e> generate the environment script to 'e' instead of the
default '${_tt_env}'.
starting the builds.
-s <s> | --source=s use 's' as source directory containing the tools sources.
defaults to '${_tt_source}'.
-t <t> | --temp=<t> use 't' as temporary build directory.
defaults to '${_tt_bdir}'.
-p <p> | --prefix=<p> define the prefix where to install all the tools. defaults
to '${_tt_prefix}'."
# .------------------------------------------.
# | parses the command line parameters given |
# | to the script. |
# '------------------------------------------'
function tt_parse_cl() {
while [[ $# -gt 0 ]]; do
case "$1" in
"-v"|"--verbose") _tt_verbose=true ;;
"-s") shift; _tt_source="$1" ;;
"--source="*) _tt_source="${1#--source=}" ;;
"-p") shift; _tt_prefix="$1" ;;
"--prefix="*) _tt_prefix="${1#--prefix=}" ;;
"-t") shift; _tt_bdir="$1" ;;
"--temp="*) _tt_bdir="${1#--temp=}" ;;
"-j") shift; _tt_jobs="$1" ;;
"-j"[0-9]*) _tt_jobs="${1#-j}" ;;
"--jobs=") _tt_jobs="${1#--jobs=}" ;;
"--preflight") _tt_preflight=true ;;
"--clean") _tt_clean=true ;;
"--download") _tt_download=true ;;
"--debug") set -xv ;;
"--package="*) _tt_pkg="${1#--package=}" ;;
"--help") tt_exit_help 0 ;;
*) error "unknown argument: '$1'";
tt_exit_help 1 ;;
esac
shift
done
}
# .--------------------------------------------.
# | creates and changes to the build directory |
# | for the given package. |
# '--------------------------------------------'
function tt_build_dir() {
local dir="${_tt_cur_bdir}"
[[ -d ${dir} && ${_tt_clean} == false ]] && { cd ${dir}; return; }
[[ -d ${dir} ]] && rm -rf ${dir}
mkdir -p "${dir}"
cd "${dir}"
}
function tt_download() {
[[ -d "${_tt_source}" ]] || mkdir -p "${_tt_source}"
for pkg in "${tools[@]}"; do
local sIFS=${IFS}
IFS=':'
local p=( ${pkg} )
IFS=${sIFS}
P="${p[0]}"
V="${p[1]}"
_tt_cur_name="${P}"
_tt_cur_version="${V}"
src="$(tt_find_source)"
[[ -n "${src}" ]] && { info "source for ${P} already present, not downloading"; continue; }
for site in "${sites[@]}"; do
local sIFS=${IFS}
IFS='|'
local s=( ${site} )
IFS=${sIFS}
sP="${s[0]}"
if [[ ${sP} == ${P} ]]; then
url="$(eval echo "${s[1]}")"
wget -O "${_tt_source}/${url##*/}" "${url}"
fi
done
done
}
# .--------------------------------------------.
# | finds the source package for the given tool|
# '--------------------------------------------'
function tt_find_source() {
for file in ${_tt_source}/${_tt_cur_name}-${_tt_cur_version}.*; do
if [[ ${file} == *.tar.gz || ${file} == *.tar.bz2 || ${file} == *.tgz || ${file} == *.tar ]]; then
echo "${file}"
return
fi
done
}
# .--------------------------------------------.
# | unpacks the source package into the build |
# | directory. |
# '--------------------------------------------'
function tt_unpack() {
info "unpacking ${_tt_cur_name}-${_tt_cur_version}"
local tflags=
case "${_tt_cur_src}" in
*.tgz|*.tar.gz) tflags="xfz";;
*.tar.bz2) tflags="xfj";;
*.tar) tflags="xf";;
esac
tar ${tflags} "${_tt_cur_src}"
cd ${_tt_cur_name}* || fatal "missing expected directory"
for patch in "${_tt_home}/patches/${_tt_cur_name}-${_tt_cur_version}"*.patch; do
[[ ${patch} == *'*.patch' ]] && break
local _p=undef
for p in 0 1 2 3 4; do
if patch --quiet -p${p} --dry-run < "${patch}" > /dev/null 2>&1; then
_p=${p}
break
fi
done
if [[ ${_p} == undef ]]; then
warn "patch ${patch} does not apply!"
continue
fi
info "applying ${patch}"
patch --quiet -p${_p} < "${patch}"
done
cd ..
}
# .--------------------------------------------.
# | configures the current package. |
# '--------------------------------------------'
function tt_configure() {
info "configuring ${_tt_cur_name}-${_tt_cur_version}"
[[ $(pwd) == ${_tt_bdir}/* ]] \
|| fatal "oups. configuring outside the build dir?"
[[ -x ./configure ]] \
|| fatal "oups. cannot find configure script"
case "${_tt_cur_name}" in
bochs|qemu)
# debugable emulator/simulator comes handy when searching
# the source of interrupts, etc.
CFLAGS="-g"
CXXFLAGS="-g"
;;
esac
./configure --prefix=${_tt_prefix} $(eval echo ${_tt_cur_flags})
}
# .--------------------------------------------.
# | builds the current package. |
# '--------------------------------------------'
function tt_build() {
info "building ${_tt_cur_name}-${_tt_cur_version}"
make -j${_tt_jobs} ${_tt_cur_build_target}
}
# .--------------------------------------------.
# | installs the current package. |
# '--------------------------------------------'
function tt_install() {
info "installing ${_tt_cur_name}-${_tt_cur_version}"
make -j${_tt_jobs} ${_tt_cur_install_target}
}
_tt_cur_name=
_tt_cur_version=
_tt_cur_flags=
_tt_cur_bdir=
_tt_cur_src=
_tt_cur_build_target=
_tt_cur_install_target=
# .--------------------------------------------.
# | assures that all required information for |
# | the current package is available. |
# '--------------------------------------------'
function tt_assure_complete() {
[[ -n "${_tt_cur_name}" ]] || fatal "package not complete: name missing"
[[ -n "${_tt_cur_version}" ]] || fatal "${_tt_cur_name} not complete: version missing"
[[ -n "${_tt_cur_src}" ]] || fatal "${_tt_cur_name} not complete: source missing"
[[ -n "${_tt_cur_install_target}" ]] || _tt_cur_install_target=install
info "package ${_tt_cur_name} seems complete ..."
}
# .--------------------------------------------.
# | parses a packages information and sets the |
# | global package information variables to |
# | according values. |
# '--------------------------------------------'
function tt_use_package() {
local pkginfo="$1"
local sIFS=${IFS}
IFS=':'
local pkg=( ${pkginfo} )
IFS=${sIFS}
_tt_cur_name="${pkg[0]}"
_tt_cur_version="${pkg[1]}"
_tt_cur_flags="${pkg[2]}"
_tt_cur_build_target="${pkg[3]}"
_tt_cur_install_target="${pkg[4]}"
_tt_cur_src="$(tt_find_source)"
_tt_cur_bdir="${_tt_bdir}/${_tt_cur_name}"
tt_assure_complete
}
tt_parse_cl "$@"
if [[ ${_tt_download} == true ]]; then
tt_download
fi
if [[ ${_tt_preflight} == true ]]; then
for package in "${tools[@]}"; do
[[ -n ${_tt_pkg} && ${package} != ${_tt_pkg}* ]] && continue
tt_use_package "${package}"
done
fi
#
# TODO: find a better way to do this...?
#
export LDFLAGS="-L${_tt_prefix}/lib -Wl,-rpath,${_tt_prefix}/lib"
export CFLAGS="-I${_tt_prefix}/include"
export CXXFLAGS="${CFLAGS}"
for package in "${tools[@]}"; do
[[ -n ${_tt_pkg} && ${package} != ${_tt_pkg}* ]] && continue
tt_use_package "${package}"
_tfbase="${_tt_cur_bdir}/"
[[ ${_tt_clean} == true ]] && rm -rf "${_tt_cur_bdir}"
tt_build_dir || fatal "cannot create build directory!"
[[ -f ${_tfbase}.unpacked ]] \
|| tt_unpack || fatal "failed to unpack ${_tt_cur_name}-${_tt_cur_version}"
cd ${_tt_cur_name}* || fatal "missing expected directory"
touch ${_tfbase}.unpacked
[[ -f ${_tfbase}.configured ]] \
|| tt_configure || fatal "failed to configure ${_tt_cur_name}-${_tt_cur_version}"
touch ${_tfbase}.configured
[[ -f ${_tfbase}.built ]] \
|| tt_build || fatal "failed to build ${_tt_cur_name}-${_tt_cur_version}"
touch ${_tfbase}.built
[[ -f ${_tfbase}.installed ]] \
|| tt_install || fatal "failed to install ${_tt_cur_name}-${_tt_cur_version}"
touch ${_tfbase}.installed
done
if [[ ${_tt_clean} == true ]]; then
info "cleaning up ..."
[[ -n "${_tt_bdir}" && -e ${_tt_bdir} ]] \
&& rm -rf "${_tt_bdir}"
fi
info "generating environment to ${_tt_env}"
echo "export PATH=${_tt_prefix}/bin:${_tt_prefix}/sbin:${_tt_home}/apps/bin:\${PATH}" > "${_tt_env}"
info "generating environment to ${_tt_mkenv}"
echo "export PATH := ${_tt_prefix}/bin:${_tt_prefix}/sbin:${_tt_home}/apps/bin:\$(PATH)" > "${_tt_mkenv}"