-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·400 lines (308 loc) · 8.14 KB
/
build.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
whatscr() { echo "gccprefab - Wil's GCC easy build script"; }
whenscr() { echo "Last updated: Jun 11, 2022 (WYP)"; }
usage() { echo "Usage: $0 [options] configfile"; }
helptext() {
echo "Available options:"
echo " -h, --help Display this help text"
# echo " -v, --verbose Verbose mode" # FIXME
echo " -V, --versions List available versions to build"
}
stamp() { echo "$(date) $@"; }
now() { date "+%Y%m%d-%H%M%z"; }
push_flag() { FLAGS+=("$1"); }
push_lang() { LANGUAGES+=("$1"); }
push_check() { CHECKS+=("$1"); }
# Taken from https://stackoverflow.com/a/23342259
exe() { echo "\$ $@"; "$@"; }
# Output the value for a given key $1 inside the config file $2
grepln() { grep "$1=" "$2" | awk -F '=' '{print $2}'; }
# Read value from line $1
readvar() { echo "$1" | awk -F '=' '{print $2}'; }
# Output message $2 is verbosity is >= level $1
verb() { if [ ${VERBOSE} -ge $1 ]; then echo "$2"; fi; }
# Turn a Bash array into a comma-separated list
# Adapted from https://stackoverflow.com/a/16203497
# Usage: `export ENVVAR=$(commalist ${ARRAY[@]})`
commalist() { echo "$*" | awk -v OFS=',' '$1=$1'; }
versions() {
echo "Supported versions:"
VERS="$(ls -1 *.cfg | tr "\n" ' ')"
for cfg in ${VERS}; do
VERSTR=$(grepln "ver" "${cfg}")
VERDESC=$(grepln "desc" "${cfg}")
echo -e "- ${cfg}\t${VERSTR}\t${VERDESC}"
done
}
checkout() {
exe git checkout "${TREE}"
if [ $? -ne 0 ]; then
stamp "Cannot checkout ${TREE}"
stamp "Output of git status:"
git status
fi
}
# Read configuration file
# Usage: `readcfg [error-code] [cfgfile]`
readcfg() {
# First, check read access
if [ ! -r "$2" ]; then
stamp "Cannot read config file $2"
exit $1
else
# Internal vars
SEC=""
FLAG_YES="--enable-"
FLAG_NO="--disable-"
FLAG_WITH="--with-"
FLAG_CHK="check-"
stamp "Reading config file $2"
while read -r line; do
pfx="${line:0:1}"
eq=$(echo ${line} | grep -c -v '=')
if [ -z "${line}" ]; then
# Empty line (treat as end of section)
SEC=""
verb 1 "=========="
elif [ "${pfx}" == "[" ]; then
# Section header
SEC="${line:1:(-1)}"
verb 1 "Section header: ${SEC}"
elif [ "${pfx}" == '+' ]; then
val=$(echo "${line}" | awk -F '+' '{print $2}')
if [ "${SEC}" == "flags" ]; then
# Add flag
FLAG="${FLAG_YES}${val}"
push_flag "${FLAG}"
verb 1 "Addition flag: ${FLAG}"
else
echo "Addition not implemented for section ${SEC}"
fi # sec
elif [ "${pfx}" == '~' ]; then
val=$(echo "${line}" | awk -F '~' '{print $2}')
if [ "${SEC}" == "flags" ]; then
# Subtract flag
FLAG="${FLAG_NO}${val}"
push_flag "${FLAG}"
verb 1 "Removal flag: ${FLAG}"
else
echo "Removal not implemented for section ${SEC}"
fi # sec
else
if [ "${SEC}" == "version" ]; then
if [ ${eq} ]; then
case "${line}" in
ver* )
# Version code
export VER=$(readvar "${line}")
verb 1 "Version code: ${VER}"
;;
desc* )
# Version description
export DESC=$(readvar "${line}")
verb 1 "Version description: ${DESC}"
;;
git* )
# Custom Git repo link
export CUSTOMGIT=$(readvar "${line}")
verb 1 "Using custom Git repo at ${CUSTOMGIT}"
;;
source* )
# Source directory
export SRCDIR=$(eval echo $(readvar "${line}"))
verb 1 "Using source directory ${SRCDIR}"
;;
build* )
# Build directory
export BLDDIR=$(eval echo $(readvar "${line}"))
verb 1 "Using build directory ${BLDDIR}"
;;
tree* )
# Git branch to build
export TREE=$(readvar "${line}")
verb 1 "Using Git branch ${TREE}"
;;
tag*)
# Git tag to build
export TREE=$(readvar "${line}")
verb 1 "Using Git tag ${TREE}"
;;
* )
echo "Unsupported key pair ${line}"
;;
esac # line
else
echo "Please use a key=val pair in section ${SEC} instead of ${line}"
fi # eq
elif [ "${SEC}" == "build" ]; then
if [ ${eq} ]; then
case "${line}" in
njobs* )
# Number of jobs for `make -j`
export NJOBS=$(readvar "${line}")
;;
* )
echo "Unsupported key pair ${line}"
;;
esac # line
else
echo "Please use a key=val pair in section ${SEC} instead of ${line}"
fi # eq
elif [ "${SEC}" == "flag" ]; then
# Normal flag
FLAG="${line}"
push_flag "${FLAG}"
verb 1 "Normal flag: ${FLAG}"
elif [ "${SEC}" == "languages" ]; then
if [ "${line}" == "default" ]; then
def_languages
verb 1 "Using default languages: ${LANGUAGES[@]}"
else
LANG=${line}
push_lang ${LANG}
verb 1 "Add language: ${LANG}"
fi # default
elif [ "${SEC}" == "checks" ]; then
CHECK="${FLAG_CHK}${line}"
push_check "${CHECK}"
verb 1 "Add test: ${CHECK}"
else
echo "Option ${line} not implemented for section ${SEC}"
fi # sec
fi # pfx
done < "$2"
fi # readable
}
# Check status of previous operation
# Usage: `status [error-code] [phase-name] [logfile]`
status() {
ierr=$?
errcode=$1
phase="$2"
logfile="$3"
if [ ${ierr} -ne 0 ]; then
stamp "${phase^} phase failed with error ${ierr}"
stamp "Check $3 for more details"
exit $1
else
stamp "Finished ${phase} phase!"
# Compress logfile
stamp "Compressing ${logfile}..."
xz ${logfile}
stamp "Compressed into ${logfile}.xz"
fi
}
# Git repo URL
GCCGIT="git://gcc.gnu.org/git/gcc.git"
CUSTOMGIT=""
# Version to build
VER=""
# Directories and branches/tags
ROOTDIR="$(pwd)"
SRCDIR=""
BLDDIR=""
TREE=""
# Verbosity
VERBOSE=0
# Prerequisites
declare -a PREREQS
PREREQS+=("gmp")
PREREQS+=("isl")
PREREQS+=("mpc")
PREREQS+=("mpfr")
# Languages
declare -a LANGUAGES
def_languages() {
push_lang "c"
push_lang "c++"
push_lang "fortran"
}
# Set up configure flags
CFGFLAGS=""
declare -a FLAGS
# Set up list of tests
declare -a CHECKS
# Check argc and bail out if no options or build config files are specified
if [ $# -lt 1 ]; then
usage; versions; exit 0
else
case "$1" in
"-h" | "--help" ) whatscr; whenscr; usage; helptext; versions; exit 0 ;;
# "-v" | "--verbose" ) export VERBOSE=1; shift 1 ;; # FIXME
"-V" | "--versions" ) versions; exit 0 ;;
* )
# Read build config file
readcfg -1 "$1"
FLAGS+=("--enable-languages=$(commalist ${LANGUAGES[@]})")
FLAGS+=("--prefix=${BLDDIR}")
;;
esac
fi # argc
# Logfiles
NOW="$(now)"
CFGLOG="${ROOTDIR}/${NOW}-configure-gcc${VER}.log"
BLDLOG="${ROOTDIR}/${NOW}-build-gcc${VER}.log"
TSTLOG="${ROOTDIR}/${NOW}-test-gcc${VER}.log"
INSLOG="${ROOTDIR}/${NOW}-install-gcc${VER}.log"
# Start the build
whatscr; whenscr
stamp "Building GCC ${VER}"
stamp "Source dir: ${SRCDIR}"
stamp "Build dir: ${BLDDIR}"
if [ ! -d "${SRCDIR}" ]; then
# Download source tree if nonexistent
exe git clone "${GCCGIT}" "${SRCDIR}"
pushd "${SRCDIR}"
# Checkout selected branch/tag
checkout
# Download prerequisites
PRESCR="${SRCDIR}/contrib/download_prerequisites"
exe ${PRESCR} --verify
popd # SRCDIR
else
pushd "${SRCDIR}"
if [ "${VER}" != "dev" ]; then
# Stash work and checkout selected branch/tag
exe git stash push -m "WIP-$(now)"
checkout
fi # dev
# Check for prerequisites
cnt=0
for d in "${PREREQS[@]}"; do
if [ -h "$d" ]; then
cnt=$(( ${cnt} + 1 ))
else
stamp "Prerequisite $d not found as a linked dir"
stamp "Please rerun contrib/download_prerequisites"
exit 2
fi
done
popd # SRCDIR
fi # SRCDIR
# Delete and recreate build directory if it exists
if [ -d "${BLDDIR}" ]; then
exe rm -rf "${BLDDIR}"
fi
exe mkdir -p "${BLDDIR}"
pushd "${BLDDIR}"
# Configure
stamp "Configure..."
CFGSCR="${SRCDIR}/configure"
echo "$ ${CFGSCR} ${FLAGS[@]} > ${CFGLOG} 2>&1"
"${CFGSCR}" ${FLAGS[@]} > ${CFGLOG} 2>&1
status 3 "configure" "${CFGLOG}"
# Build
stamp "Build..."
echo "$ make -j ${NJOBS} > ${BLDLOG} 2>&1"
make -j ${NJOBS} > ${BLDLOG} 2>&1
status 4 "build" "${BLDLOG}"
# Test
stamp "Test..."
for t in ${CHECKS[@]}; do
echo "$ make -k $t > ${TSTLOG} 2>&1"
make -k $t > ${TSTLOG} 2>&1
status 5 "test" "${TSTLOG}"
done
# Install
#stamp "Install..."
popd # BLDDIR