-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnorm_common.functions
772 lines (682 loc) · 22 KB
/
norm_common.functions
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/bin/bash
CAVEATS=()
pushd() {
command pushd "$@" > /dev/null
}
popd() {
command popd "$@" > /dev/null
}
pushd_src() {
pushd "$SRCDIR"
}
popd_src() {
popd
}
do_configure() {
local STARTTIME="$SECONDS"
[[ -z "$QUIET" ]] && fgrep -q disable-silent-rules "$SRCDIR/configure" && set -- "$@" --disable-silent-rules ## easier to debug build failures when logs show entire invocation flags
[[ -n "$QUIET" ]] && fgrep -q enable-silent-rules "$SRCDIR/configure" && set -- "$@" --enable-silent-rules ## easier to debug build failures when logs show entire invocation flags
log "Configuring $FORMULA --prefix=\"$PREFIX\" $@"
if [[ -n "$QUIET" ]]; then
"$SRCDIR"/configure --prefix="$PREFIX" "$@" >normconfig.log 2>&1 || (cat normconfig.log; return 1)
else
"$SRCDIR"/configure --prefix="$PREFIX" "$@" || return 1
fi
local ELAPSED="$(($SECONDS - $STARTTIME))"
log "Configuring $FORMULA took $ELAPSED seconds"
}
do_cmake() {
local STARTTIME="$SECONDS"
local OPTS=(-DCMAKE_INSTALL_PREFIX=$ESCAPED_PREFIX
-DCMAKE_PREFIX_PATH=$ESCAPED_PREFIX ## this ensures cmake searches in our prefix first
-DCMAKE_BUILD_TYPE=RelWithDebInfo
-DCMAKE_INSTALL_NAME_DIR=$ESCAPED_PREFIX/lib)
set -- "$SRCDIR" "${OPTS[@]}" "$@"
log "Configuring $FORMULA: cmake $@"
cmake "$@" || return 1
local ELAPSED="$(($SECONDS - $STARTTIME))"
log "Configuring $FORMULA took $ELAPSED seconds"
}
do_auto_configure() {
if [ -e "$SRCDIR"/configure ]; then
do_configure "$@" || return 1
elif [ -e "$SRCDIR"/CMakeLists.txt ] && hash cmake 2>/dev/null; then
do_cmake "$@" || return 1
else
log "Couldn't figure out how to configure formula '$FORMULA', bailing out"
return 1
fi
}
do_make() {
local STARTTIME="$SECONDS"
log "Making $FORMULA $@"
local PARALLEL="-j$NUMJOBS"
[ "$NOPARALLEL" = "1" ] && PARALLEL=
if [[ -n "$QUIET" ]]; then
make $PARALLEL "$@" >normbuild.log 2>&1 || (cat normbuild.log && return 1)
else
[ "$NEEDV1" = "1" ] && set V=1 "$@"
make $PARALLEL "$@" || return 1
fi
local ELAPSED="$(($SECONDS - $STARTTIME))"
log "Making $FORMULA took $ELAPSED seconds"
}
do_install() {
local STARTTIME="$SECONDS"
log "Installing $FORMULA $@"
if [[ -n "$QUIET" ]]; then
make install "$@" >norminstall.log 2>&1 || (cat norminstall.log && return 1)
else
make install "$@" || return 1
fi
local ELAPSED="$(($SECONDS - $STARTTIME))"
log "Installing $FORMULA took $ELAPSED seconds"
}
do_make_and_install() {
do_make "$@" || return 1
do_install "$@" || return 1
}
do_test() {
if ! is_without tests; then
local STARTTIME="$SECONDS"
log "Doing tests for $FORMULA"
env - PATH="$PATH" CTEST_OUTPUT_ON_FAILURE=1 make check "$@" ||
env - PATH="$PATH" CTEST_OUTPUT_ON_FAILURE=1 make test "$@" ||
env - PATH="$PATH" CTEST_OUTPUT_ON_FAILURE=1 make tests "$@" || return 1
local ELAPSED="$(($SECONDS - $STARTTIME))"
log "Doing tests for $FORMULA took $ELAPSED seconds"
fi
}
do_compile_notest() {
pushd_src || return 1
do_auto_configure "$@" || return 1
do_make_and_install || return 1
popd || return 1
}
do_compile_test() {
pushd_src || return 1
do_auto_configure "$@" || return 1
do_make || return 1
do_test || return 1
do_install || return 1
popd || return 1
}
do_compile_outside_notest() {
BUILDDIR="$SRCDIR"-build
rm -rf "$BUILDDIR" || return 1
mkdir -p "$BUILDDIR" || return 1
pushd "$BUILDDIR" || return 1
do_auto_configure "$@" || return 1
do_make_and_install || return 1
popd || return 1
}
do_compile_outside_test() {
BUILDDIR="$SRCDIR"-build
rm -rf "$BUILDDIR" || return 1
mkdir -p "$BUILDDIR" || return 1
pushd "$BUILDDIR" || return 1
do_auto_configure "$@" || return 1
do_make || return 1
do_test || return 1
do_install || return 1
popd || return 1
}
check_input_filter_exists_and_set() {
if ! hash "$1" 2>/dev/null; then
log_error "Error: \"$1\" is not available, you must install it."
log_error
log_error Try:
log_error " norm install $1"
exit 1
fi
export INPUT_FILTER="$@"
}
do_untar() {
[ -z "$1" ] && set -- "$CACHEDIR/$TARNAME"
[ -z "$2" ] && set -- "$1" "$SRCDIR"
local INPUT_TARFILE="$1"
local OUTPUT_DIR="$2"
shift 2
INPUT_FILTER="cat"
## we can't rely on tar being able to identify .xz -- some users had systems that had tar so old it didn't know about .xz or .bz2
case "$INPUT_TARFILE" in
*.tar) check_input_filter_exists_and_set cat ;;
*.tar.gz |*.tgz) check_input_filter_exists_and_set gzip -d ;;
*.tar.bz2|*.tbz) check_input_filter_exists_and_set bzip2 -d ;;
*.tar.xz |*.txz) check_input_filter_exists_and_set xz -d ;;
*) log "Couldn't identify tar format for $INPUT_TARFILE" && return 1;;
esac
cat "$INPUT_TARFILE" | $INPUT_FILTER | tar xf - -C "$OUTPUT_DIR" --strip-components 1 "$@" || return 1
return 0
}
## sets global SRCDIR
set_srcdir() {
SRCDIR="$COMPILEDIR/$FORMULA/$1"
}
prep_srcdir() {
set_srcdir "$@"
log "Cleaning $SRCDIR"
rm -rf "$SRCDIR" || return 1
mkdir -p "$SRCDIR" || return 1
}
##
## does a git clone of source code
do_gitclone() {
[ -z "$1" ] && log "do_gitclone called without arguments!" && return 1
needs_tool git
prep_srcdir git
git clone "$1" "$SRCDIR"
}
##
## unpacks source code
## sets global SRCDIR and TARNAME
##
## usage: do_unpack <-- gets filename from fetch_source
##
## alternative usage: do_unpack foo-1.2.3.tar.bz2
do_unpack() {
[ -f "$CACHEDIR/$1" ] && TARNAME="$1" && shift
[ -z "$TARNAME" ] && log "TARNAME is empty!" && return 1
prep_srcdir "$TARNAME"
log "Unpacking $TARNAME"
do_untar "$CACHEDIR/$TARNAME" "$SRCDIR" "$@" || return 1
log "Unpacked $TARNAME"
}
##
## unpacks source code inside other formula's source code
##
## usage: intree_unpack <-- unpacks into same directory of do_unpack'ed formula
## usage: intree_unpack tools/clang <-- unpacks into tools/clang subdirectory
intree_unpack() {
log "Unpacking $TARNAME into $SRCDIR/$1"
pushd_src
if [ -n "$1" ]; then
mkdir -p "$1"
do_untar "$CACHEDIR/$TARNAME" "$1"
fi
if [ -z "$1" ]; then
# mv -n would've worked better, but not all versions of mv have that flag
mkdir -p "$TARNAME"
do_untar "$CACHEDIR/$TARNAME" "$TARNAME"
for x in "$TARNAME"/*; do
local file=${x#$TARNAME/}
[ ! -e "$file" ] && ln -s "$x" .
done
fi
popd
log "Unpacked $TARNAME into $SRCDIR/$1"
}
##
## unpacks source code and applies debian patches
## sets global TARNAME and DEBPATCH
##
## usage: do_undebian <-- gets filenames from fetch_source and fetch_debian
##
## alternative usage: do_undebian foo-1.2.3.orig.tar.bz2 foo-1.2.3.debian.tar.gz
do_undebian() {
[ -f "$CACHEDIR/$1" ] && TARNAME="$1"
[ -f "$CACHEDIR/$2" ] && DEBPATCH="$2"
[ -z "$DEBPATCH" ] && log_error "Error: do_undebian was called but fetch_debian was not called in this formula" && exit 1
## sets SRCDIR
do_unpack "$TARNAME" || return 1
do_debianize "$DEBPATCH" || return 1
}
##
## applies debian patches, sets global DEBPATCH
##
## usage: do_debianize <-- gets filename from fetch_debian
##
## alternative usage: do_debianize foo-1.2.3.debian.tar.gz
do_debianize() {
[ -f "$CACHEDIR/$1" ] && DEBPATCH="$1"
[ -z "$DEBPATCH" ] && log_error "Error: do_debianize was called but fetch_debian was not called in this formula" && exit 1
do_undebian_overlay "$DEBPATCH"
do_debian_patches
}
do_undebian_overlay() {
[ -f "$CACHEDIR/$1" ] && DEBPATCH="$1"
[ -z "$DEBPATCH" ] && log_error "Error: do_undebian_overlay was called but fetch_debian was not called in this formula" && exit 1
## unpack debian overlay -- try tar, gzip, xz, bz
INPUT_FILTER="cat"
case "$DEBPATCH" in
*.diff) check_input_filter_exists_and_set cat ;;
*.diff.gz) check_input_filter_exists_and_set gzip -d ;;
*.diff.xz) check_input_filter_exists_and_set xz -d ;;
*.diff.bz2) check_input_filter_exists_and_set bzip2 -d ;;
*) do_untar "$CACHEDIR/$DEBPATCH" "$SRCDIR" --strip-components 0 && return 0 ;;
esac
cat "$CACHEDIR/$DEBPATCH" | $INPUT_FILTER | patch -f -p1 -i - -d "$SRCDIR" || return 1
}
do_debian_patches() {
## apply debian patches
local SERIES="$SRCDIR"/debian/patches/series
local SERIES2="$SRCDIR"/debian/patches-applied/series
local PATCHES="$SRCDIR"/debian/patches/*.patch
if [ -e "$SERIES" ]; then
PATCHES=`cat "$SERIES"|sed -e "s/#.*$//g" -e "s|^|$SRCDIR/debian/patches/|"`
elif [ -e "$SERIES2" ]; then
PATCHES=`cat "$SERIES2"|sed -e "s/#.*$//g" -e "s|^|$SRCDIR/debian/patches-applied/|"`
fi
for i in $PATCHES; do
[ -e "$i" ] || continue
[ ! -d "$i" ] || continue
log Applying patch "$i"
patch -f -p1 -i "$i" -d "$SRCDIR" ||
patch -f -p0 -i "$i" -d "$SRCDIR" ||
patch -f -p2 -i "$i" -d "$SRCDIR" || true
done
}
##
## unpacks source code, applies debian patches, runs configure, make, install
##
## usage: do_undebian_compile <-- gets filenames from fetch_source and fetch_debian
##
## alternative usage: do_undebian_compile foo-1.2.3.orig.tar.bz2 foo-1.2.3.debian.tar.gz
do_undebian_compile_test() {
if [ -f "$CACHEDIR/$1" -a -f "$CACHEDIR/$2" ]; then
TARNAME="$1"
DEBPATCH="$2"
shift 2
fi
do_undebian || return 1
do_compile_test "$@" || return 1
}
##
## same as do_undebian_compile, but run test suite before install
##
do_undebian_compile_notest() {
if [ -f "$CACHEDIR/$1" -a -f "$CACHEDIR/$2" ]; then
TARNAME="$1"
DEBPATCH="$2"
shift 2
fi
do_undebian || return 1
do_compile_notest "$@" || return 1
}
##
## unpacks source code, runs do_compile_test
##
## usage: do_unpack_compile <-- gets filename from fetch_source
do_unpack_compile_test() {
if [ -f "$CACHEDIR/$1" ]; then
TARNAME="$1"
shift 1
fi
do_unpack "$TARNAME" || return 1
do_compile_test "$@" || return 1
}
##
## same as do_unpack_compile_test, but doesn't run test suite before install
##
do_unpack_compile_notest() {
if [ -f "$CACHEDIR/$1" ]; then
TARNAME="$1"
shift 1
fi
do_unpack "$TARNAME" || return 1
do_compile_notest "$@" || return 1
}
##
## applies a patch in $SRCDIR, takes it from 'patches' directory in NORM
## default is -p1, but you can override that after patchname
##
## usage: do_patch gcc-4.9.2-multiarch-support.patch
## usage: do_patch binutils-multiarch-support.patch -p2
##
do_patch() {
local PATCHNAME="$1"
shift
local FULLPATH="$NORMDIR/patches/$PATCHNAME"
if [ ! -e "$FULLPATH" ]; then
FULLPATH="$CACHEDIR/$PATCHNAME"
fi
if [ ! -e "$FULLPATH" ]; then
log "do_patch() was given a filename that doesn't exist"
return 1
fi
patch -p1 -f -i "$FULLPATH" -d "$SRCDIR" "$@" || return 1
}
do_patch_inline() {
patch -p1 -l -N -f -d "$SRCDIR" "$@" || return 1
}
do_autoreconf() {
pushd_src || return 1
autoreconf -f -i -v || return 1
popd || return 1
}
## usage: compile_formula FORMULA [what formula asked for this one]
## note about force option -- it's passed as parameter so forcing formula A won't force formula B that A depends on
compile_formula() {
local COMPILING_FOR force FORMULA
local DATE=`LANG=C LC_ALL=C date`
[ "$1" == "-f" ] && force=-f && shift
FORMULA="$1"
[ ! -e "$NORMDIR"/packages/"$FORMULA" ] && log_error "Error: Formula file '$FORMULA' does not exist" && return 1
local SHA1SUM=`$SHASUM_BIN "$NORMDIR/packages/$FORMULA" | cut -f1 -d' '`
[ -n "$2" ] && COMPILING_FOR="$2"
local STATEDIR="$PREFIX"/var/norm
local NEEDS_INSTALLING=true
[[ -e "$STATEDIR"/"$FORMULA".installed ]] && fgrep -q "$SHA1SUM" "$STATEDIR"/"$FORMULA".installed && local NEEDS_INSTALLING=false
[[ -n "$force" ]] && local NEEDS_INSTALLING=true
if [[ "$NEEDS_INSTALLING" == "true" ]]; then
[ -z "$COMPILING_FOR" ] && log "Building formula: $FORMULA" \
|| log "Building formula: $FORMULA -- dependency of $COMPILING_FOR"
## scan if we need bzip2 or xz
fgrep -q '.xz' "$NORMDIR"/packages/"$FORMULA" && needs_tool xz
fgrep -q '.bz2' "$NORMDIR"/packages/"$FORMULA" && needs_tool bzip2
unset DEBPATCH
local STARTTIME="$SECONDS"
. "$NORMDIR"/packages/"$FORMULA"
mkdir -p `dirname "$STATEDIR/$FORMULA"`
local ELAPSED="$(($SECONDS - $STARTTIME))"
log_success "$ELAPSED"
echo "[$DATE] $SHA1SUM $ELAPSED $COMPILING_FOR" >> "$STATEDIR/$FORMULA".installed
## remove artifacts
if [[ -z "$norm_keep_sources" && -n "$COMPILEDIR" && -n "$FORMULA" ]]; then
rm -rf "$COMPILEDIR/$FORMULA"
fi
else
[ -z "$COMPILING_FOR" ] && log "Formula already installed: $FORMULA"
echo "[$DATE] $SHA1SUM 0 $COMPILING_FOR" >> "$STATEDIR/$FORMULA".installed
fi
}
tool_to_formula() {
case "$1" in
msgfmt) echo "gettext" ;;
pod2man) echo "perl" ;;
python3) echo "python" ;;
autoreconf) echo "automake" ;;
*) echo "$i" ;;
esac
}
needs_tool() {
for i; do
if ! hash "$i" 2>/dev/null; then
log "System does not have tool $i, will build our own"
local frm=$(tool_to_formula "$i")
compile_formula "$frm" $FORMULA
fi
done
}
depends_on() {
for i; do
compile_formula "$i" $FORMULA
done
}
do_download() {
[[ -z $2 ]] && log "do_download() needs two arguments" && return 1
local URL="$1"
local SAVETO="$2"
case "$DOWNLOADER" in
wget) wget "$URL" -O "$SAVETO" ;;
curl) curl --fail -L -R "$URL" -o "$SAVETO" ;;
*) log "\$DOWNLOADER is unknown ($DOWNLOADER)" && return 1 ;;
esac
}
check_sha1() {
local FILEPATH="$1"
local SHA1="$2"
## if file does not exist, do nothing
[ -s "$FILEPATH" ] || return 1
## get SHA1
local SHA1SUM=`$SHASUM_BIN "$FILEPATH" | cut -f1 -d' '`
## if no hash provided, print it and do nothing
if [ -z "$SHA1" ]; then
log "For future -- SHA1 of $FILEPATH is $SHA1SUM"
return 0
fi
## we're good to go, compare the checksums
if [ "$SHA1SUM" != "$SHA1" ]; then
log "SHA1sum mismatch! $SHA1SUM vs $SHA1 for file $FILEPATH"
return 1
fi
## they match
return 0;
}
##
## usage: fetch_file http://example.com/file.tar.gz AABBCC [optional_output_filename]
## sets FILENAME
fetch_file() {
[ -z "$1" ] && log "fetch_file() needs at least 1 argument" && return 1
local URL="$1"
local SHA1="$2"
local VARNAME="FILENAME"
[ -n "$4" ] && VARNAME="$4"
export $VARNAME=`basename -- "$URL"`
[ -n "$3" ] && export $VARNAME="$3"
local FILEPATH="$CACHEDIR/${!VARNAME}"
mkdir -p "$CACHEDIR"
if ! check_sha1 "$FILEPATH" "$SHA1"; then
log "Downloading $URL"
rm -f "$FILEPATH"
local TEMPFILE=`mktemp "$FILEPATH.XXXXXX"`
do_download "$URL" "$TEMPFILE" || (rm -f "$TEMPFILE" && return 1)
mv -f "$TEMPFILE" "$FILEPATH"
fi
if ! check_sha1 "$FILEPATH" "$SHA1"; then
return 1
fi
}
##
## usage: fetch_source http://example.com/file.tar.gz AABBCC [optional_output_filename]
## sets TARNAME
fetch_source() {
local URL="$1"
local CHECKSUM="$2"
local NEWNAME="${FORMULA}"_`basename -- "$URL"`
[ -n "$3" ] && NEWNAME="$3"
fetch_file "$URL" "$CHECKSUM" "$NEWNAME" "TARNAME"
}
##
## usage: fetch_debian http://example.com/file.tar.gz AABBCC [optional_output_filename]
## sets DEBPATCH
fetch_debian() {
local URL="$1"
local CHECKSUM="$2"
local NEWNAME="$3"
DEBPATCH="${FORMULA}_"`basename -- "$URL"`
[ -z "$NEWNAME" ] && NEWNAME="$DEBPATCH"
fetch_file "$URL" "$CHECKSUM" "$NEWNAME"
}
##
## example: test_cc 'int main() { return 0;}' -O2
test_cc() {
CODE="$1"
shift
[[ -z $QUIET ]] && echo -n "Testing if $CC compiles '$CODE' $@... "
TEMPFILE=`mktemp`
ERRFILE=`mktemp`
echo "$CODE" | $CC -x c "$@" -Werror -o "$TEMPFILE" - 2>"$ERRFILE" || true
local STATUS=${PIPESTATUS[1]}
rm -f "$TEMPFILE" "$ERRFILE"
[[ -z $QUIET && $STATUS == 0 ]] && echo "yes"
[[ -z $QUIET && $STATUS != 0 ]] && echo "no"
[ -n "$STATUS" ] && return $STATUS
return 1
}
##
## example: test_cc 'int main() { return 0;}' -O2
test_cc_quiet() {
local QUIET=1
test_cc "$@"
}
##
## example: cc_checkflags -Wl,-Bsymbolic -O2
cc_checkflags() {
[[ -z $QUIET ]] && echo -n "Testing if $CC supports $@... "
test_cc_quiet 'int main() { return 0;}' "$@"
local STATUS=$?
[[ $STATUS == 0 ]] && [[ -z $QUIET ]] && echo "yes"
[[ $STATUS != 0 ]] && [[ -z $QUIET ]] && echo "no"
return $STATUS
}
##
## check if compiler will accept the flag
## first one accepted is added to specified variable
##
## example: cc_addflag EXTRA_CFLAGS -march=native -march=core2 -march=i686
## example: cc_addflag EXTRA_CFLAGS -Wextra -W
cc_addflag() {
[ -z "$1" -o -z "$2" ] && return
local value VARNAME=$1
shift 1
for value; do
[[ "${!VARNAME}" =~ (^| )$value( |$) ]] && return # already have it
cc_checkflags $CFLAGS ${!VARNAME} $value || continue
export $VARNAME="${!VARNAME} $value"
return
done
}
##
## check if compiler will accept the flags
## any flag that gets accepted will be added to specified variable
cc_addflags() {
[ -z "$1" -o -z "$2" ] && return
local value VARNAME=$1
shift 1
for value; do
[[ "${!VARNAME}" =~ (^| )$value( |$) ]] && continue # already have it
cc_checkflags $CFLAGS ${!VARNAME} $value || continue
export $VARNAME="${!VARNAME} $value"
done
}
## test_cc_expr "fcntl.h" "int a = O_CLOEXEC"
test_cc_expr() {
test_cc "#define _GNU_SOURCE
#include <$1>
int main() { $2; return 0;}"
}
## test_cc_value "fcntl.h" "O_CLOEXEC"
test_cc_value() {
test_cc_expr "$1" "int a = $2"
}
## test_cc_func "stdio.h" 'printf("Hello, world!\n");'
test_cc_func() {
test_cc_expr "$1" "$2"
}
is_cc_clang() {
RES=`$CC --version 2>&1 | fgrep clang | wc -l`
[[ $RES == 1 ]] && return 0
return 1
}
## Example: have_gcc 4.7
have_gcc() {
[ -z "$1" ] && log "have_gcc() needs an argument" && return 1
local HAVE_GCC_RAW=`$CC -dumpversion`
local HAVE_GCC=`echo "$HAVE_GCC_RAW" | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$/&00/'`
local ASKED_GCC=`echo "$1" | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$/&00/'`
[ "$HAVE_GCC" -lt "$ASKED_GCC" ] && return 1
return 0
}
## Example: require_gcc 4.7
## Same as have_gcc, but prints a message doesn't print a message
requires_gcc() {
[ -z "$1" ] && log "require_gcc() needs an argument" && return 1
if ! have_gcc "$1"; then
local NEED_GCC_RAW="$1"
shift
local HAVE_GCC_RAW=`$CC -dumpversion`
log "Formula $FORMULA needs GCC at least $NEED_GCC_RAW $@"
log "You have $HAVE_GCC_RAW"
log ""
log "Type 'norm install gcc' to install fresh gcc"
exit 1 ## we don't want onerror() to type 'formula failed to build'
fi
return 0
}
add_to_ccache() {
mkdir -p $PREFIX/bin/ccache_wrap
for name in "$@"; do
ln -fs ../ccache $PREFIX/bin/ccache_wrap/"$name"
done
}
add_caveat() {
CAVEATS+=("Formula $FORMULA has a message for you:")
for line; do
CAVEATS+=("$line")
done
CAVEATS+=("")
}
is_without() {
[ -z "$1" ] && return 1
[[ "$1" == *" "* ]] && return ## don't allow whitespace
local name=is_without_${1}
[ -n "${!name}" ] && return 0
return 1
}
is_with() {
[ -z "$1" ] && return 1
[[ "$1" == *" "* ]] && return ## don't allow whitespace
local name=is_with_${1}
[ -n "${!name}" ] && return 0
return 1
}
set_without() {
[ -z "$1" ] && return
[[ "$1" == *" "* ]] && return ## don't allow whitespace
log Setting without "$1"
printf -v "is_without_${1}" %s "1"
}
set_with() {
[ -z "$1" ] && return
[[ "$1" == *" "* ]] && return ## don't allow whitespace
log Setting with "$1"
printf -v "is_with_${1}" %s "1"
}
in_array() {
local haystack="${1}[@]"
local needle="${2}"
for i in ${!haystack}; do
if [[ ${i} == ${needle} ]]; then
return 0
fi
done
return 1
}
## oneliners that control default behaviour
do_compile() { do_compile_notest "$@" || return 1; }
do_compile_outside() { do_compile_outside_notest "$@" || return 1; }
do_undebian_compile() { do_undebian_compile_notest "$@"; }
do_unpack_compile() { do_unpack_compile_notest "$@"; }
## calculates formula ID, sets FORMULA_ID
calc_formula_id() {
[[ -z "$1" ]] && echo "calc_formula_id() was called with empty arguments!" && exit 64
unset FORMULA_ID
local FORMULA_NAME="${1#$NORMDIR/packages/}"
## remember old IFS value
local OLDIFS="$IFS"
## keep track of seen formulas
local SEEN=()
## get formula text into array, use IFS newline splitter
## we need to support bash 3.2+, so don't use mapfile
## ignore comments in the formula
IFS=$'\n'
local TEXT=($(cat "$1"|grep -v '^#'))
## go through each line and see if it has a dependency line
while true; do
local added=0 ## if wasn't set to one, break the loop
for i in "${TEXT[@]}"; do
if [[ $i =~ ^[^#]*depends_on\s*([^#]*) ]]; then
## found a subdependency, extract it
IFS=$' \t\n'
for f in ${BASH_REMATCH[1]}; do
if in_array SEEN "$f"; then
continue
fi
SEEN+=("$f")
IFS=$'\n'
TEXT+=($(cat $NORMDIR/packages/$f))
added=1
done
fi
done
if [[ $added == 0 ]]; then
break
fi
done
# calculate formula ID
local SHA1SUM=`printf "%s\n" "${TEXT[@]}" | $SHASUM_BIN | cut -f1 -d' '`
# local SHA1SUM=`$SHASUM_BIN "$1" | cut -f1 -d' '`
export FORMULA_ID="$FORMULA_NAME.$SHA1SUM"
IFS="$OLDIFS"
}