aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/release.sh
blob: 638161f83732ebad8113635bfffc15c4ca7c60f4 (plain)
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
773
774
775
776
777
778
779
#! /bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# For OpenBSD, run using the following:
#
# scripts/release.sh 1 0 1 0 0 0 0 1 0 0 0 0
#
# For FreeBSD, run using the following:
#
# scripts/release.sh 1 0 1 0 0 0 0 1 0 1 0 0
#
# There is one problem with running this script on FreeBSD: it takes overcommit
# to the extreme. This means that some tests that try to create allocation
# failures instead make bc and dc crash. So running this script on FreeBSD does
# not work right now.
#
# For Linux, run two separate ones (in different checkouts), like so:
#
# scripts/release.sh 1 1 1 0 1 0 0 1 0 1 0 1
# scripts/release.sh 1 1 0 1 0 1 0 1 0 1 0 0
#
# Yes, I usually do sanitizers with Clang and Valgrind with GCC.
#
# To run sanitizers or Valgrind with generated tests, use the following:
#
# scripts/release.sh 1 1 1 0 1 0 0 1 0 1 0 1
# scripts/release.sh 1 1 0 1 0 1 0 1 0 1 0 0
#
# If this script fails on any platform when starting the Karatsuba test, check
# that Python is installed, especially if the error says something like:
# "karatsuba.py: not found".

# Print the usage and exit with an error. Each parameter should be an integer.
# Non-zero activates, and zero deactivates.
usage() {
	printf 'usage: %s [run_tests] [generate_tests] [test_with_clang] [test_with_gcc] \n' "$script"
	printf '          [run_sanitizers] [run_valgrind] [test_settings] [run_64_bit] \n'
	printf '          [run_gen_script] [test_c11] [test_128_bit] [test_computed_goto]\n'
	exit 1
}

# Print a header with a message. This is just to make it easy to track progress.
# @param msg  The message to print in the header.
header() {

	_header_msg="$1"
	shift

	printf '\n'
	printf '*******************\n'
	printf "$_header_msg"
	printf '\n'
	printf '*******************\n'
	printf '\n'
}

# Easy way to call make.
do_make() {
	# No reason to do 64 except to see if I actually can overload my system. :)
	# Well, also that it might actually improve throughput as other jobs can run
	# while some are waiting.
	make -j64 "$@"
}

# Run configure.sh.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param GEN_HOST         The setting for GEN_HOST.
# @param LONG_BIT         The setting for LONG_BIT.
configure() {

	_configure_CFLAGS="$1"
	shift

	_configure_CC="$1"
	shift

	_configure_configure_flags="$1"
	shift

	_configure_GEN_HOST="$1"
	shift

	_configure_LONG_BIT="$1"
	shift

	# Make sure to not generate tests if necessary.
	if [ "$gen_tests" -eq 0 ]; then
		_configure_configure_flags="-G $_configure_configure_flags"
	fi

	# Choose the right extra flags.
	if [ "$_configure_CC" = "clang" ]; then
		_configure_CFLAGS="$clang_flags $_configure_CFLAGS"
	elif [ "$_configure_CC" = "gcc" ]; then
		_configure_CFLAGS="$gcc_flags $_configure_CFLAGS"
	fi

	# Print the header and do the job.
	_configure_header=$(printf 'Running ./configure.sh %s ...' "$_configure_configure_flags")
	_configure_header=$(printf "$_configure_header\n    CC=\"%s\"\n" "$_configure_CC")
	_configure_header=$(printf "$_configure_header\n    CFLAGS=\"%s\"\n" "$_configure_CFLAGS")
	_configure_header=$(printf "$_configure_header\n    LONG_BIT=%s" "$_configure_LONG_BIT")
	_configure_header=$(printf "$_configure_header\n    GEN_HOST=%s" "$_configure_GEN_HOST")

	header "$_configure_header"
	CFLAGS="$_configure_CFLAGS" CC="$_configure_CC" GEN_HOST="$_configure_GEN_HOST" \
		LONG_BIT="$_configure_LONG_BIT" ./configure.sh $_configure_configure_flags > /dev/null
}

# Build with make. This function also captures and outputs any warnings if they
# exists because as far as I am concerned, warnings are not acceptable for
# release.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param GEN_HOST         The setting for GEN_HOST.
# @param LONG_BIT         The setting for LONG_BIT.
build() {

	_build_CFLAGS="$1"
	shift

	_build_CC="$1"
	shift

	_build_configure_flags="$1"
	shift

	_build_GEN_HOST="$1"
	shift

	_build_LONG_BIT="$1"
	shift

	configure "$_build_CFLAGS" "$_build_CC" "$_build_configure_flags" "$_build_GEN_HOST" "$_build_LONG_BIT"

	_build_header=$(printf 'Building...\n    CC=%s' "$_build_CC")
	_build_header=$(printf "$_build_header\n    CFLAGS=\"%s\"" "$_build_CFLAGS")
	_build_header=$(printf "$_build_header\n    LONG_BIT=%s" "$_build_LONG_BIT")
	_build_header=$(printf "$_build_header\n    GEN_HOST=%s" "$_build_GEN_HOST")

	header "$_build_header"

	# Capture and print warnings.
	do_make > /dev/null 2> "$scriptdir/../.test.txt"

	if [ -s "$scriptdir/../.test.txt" ]; then
		printf '%s generated warning(s):\n' "$_build_CC"
		printf '\n'
		cat "$scriptdir/../.test.txt"
		exit 1
	fi
}

# Run tests with make.
runtest() {

	header "Running tests"

	if [ "$#" -gt 0 ]; then
		do_make "$@"
	else
		do_make test
	fi
}

# Builds and runs tests with both calculators, then bc only, then dc only. If
# run_tests is false, then it just does the builds.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param GEN_HOST         The setting for GEN_HOST.
# @param LONG_BIT         The setting for LONG_BIT.
# @param run_tests        Whether to run tests or not.
runconfigtests() {

	_runconfigtests_CFLAGS="$1"
	shift

	_runconfigtests_CC="$1"
	shift

	_runconfigtests_configure_flags="$1"
	shift

	_runconfigtests_GEN_HOST="$1"
	shift

	_runconfigtests_LONG_BIT="$1"
	shift

	_runconfigtests_run_tests="$1"
	shift

	if [ "$_runconfigtests_run_tests" -ne 0 ]; then
		_runconfigtests_header=$(printf 'Running tests with configure flags')
	else
		_runconfigtests_header=$(printf 'Building with configure flags')
	fi

	_runconfigtests_header=$(printf "$_runconfigtests_header \"%s\" ...\n" "$_runconfigtests_configure_flags")
	_runconfigtests_header=$(printf "$_runconfigtests_header\n    CC=%s\n" "$_runconfigseries_CC")
	_runconfigtests_header=$(printf "$_runconfigtests_header\n    CFLAGS=\"%s\"" "$_runconfigseries_CFLAGS")
	_runconfigtests_header=$(printf "$_runconfigtests_header\n    LONG_BIT=%s" "$_runconfigtests_LONG_BIT")
	_runconfigtests_header=$(printf "$_runconfigtests_header\n    GEN_HOST=%s" "$_runconfigtests_GEN_HOST")

	header "$_runconfigtests_header"

	build "$_runconfigtests_CFLAGS" "$_runconfigtests_CC" \
		"$_runconfigtests_configure_flags" "$_runconfigtests_GEN_HOST" \
		"$_runconfigtests_LONG_BIT"

	if [ "$_runconfigtests_run_tests" -ne 0 ]; then
		runtest
	fi

	do_make clean

	build "$_runconfigtests_CFLAGS" "$_runconfigtests_CC" \
		"$_runconfigtests_configure_flags -b" "$_runconfigtests_GEN_HOST" \
		"$_runconfigtests_LONG_BIT"

	if [ "$_runconfigtests_run_tests" -ne 0 ]; then
		runtest
	fi

	do_make clean

	build "$_runconfigtests_CFLAGS" "$_runconfigtests_CC" \
		"$_runconfigtests_configure_flags -d" "$_runconfigtests_GEN_HOST" \
		"$_runconfigtests_LONG_BIT"

	if [ "$_runconfigtests_run_tests" -ne 0 ]; then
		runtest
	fi

	do_make clean
}

# Builds and runs tests with runconfigtests(), but also does 64-bit, 32-bit, and
# 128-bit rand, if requested. It also does it with the gen script (strgen.sh) if
# requested. If run_tests is false, it just does the builds.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param run_tests        Whether to run tests or not.
runconfigseries() {

	_runconfigseries_CFLAGS="$1"
	shift

	_runconfigseries_CC="$1"
	shift

	_runconfigseries_configure_flags="$1"
	shift

	_runconfigseries_run_tests="$1"
	shift

	if [ "$run_64_bit" -ne 0 ]; then

		if [ "$test_128_bit" -ne 0 ]; then
			runconfigtests "$_runconfigseries_CFLAGS" "$_runconfigseries_CC" \
				"$_runconfigseries_configure_flags" 1 64 "$_runconfigseries_run_tests"
		fi

		if [ "$run_gen_script" -ne 0 ]; then
			runconfigtests "$_runconfigseries_CFLAGS" "$_runconfigseries_CC" \
				"$_runconfigseries_configure_flags" 0 64 "$_runconfigseries_run_tests"
		fi

		runconfigtests "$_runconfigseries_CFLAGS -DBC_RAND_BUILTIN=0" "$_runconfigseries_CC" \
			"$_runconfigseries_configure_flags" 1 64 "$_runconfigseries_run_tests"

	fi

	runconfigtests "$_runconfigseries_CFLAGS" "$_runconfigseries_CC" \
		"$_runconfigseries_configure_flags" 1 32 "$_runconfigseries_run_tests"

	if [ "$run_gen_script" -ne 0 ]; then
		runconfigtests "$_runconfigseries_CFLAGS" "$_runconfigseries_CC" \
			"$_runconfigseries_configure_flags" 0 32 "$_runconfigseries_run_tests"
	fi
}

# Builds and runs tests with each setting combo running runconfigseries(). If
# run_tests is false, it just does the builds.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param run_tests        Whether to run tests or not.
runsettingsseries() {

	_runsettingsseries_CFLAGS="$1"
	shift

	_runsettingsseries_CC="$1"
	shift

	_runsettingsseries_configure_flags="$1"
	shift

	_runsettingsseries_run_tests="$1"
	shift

	if [ "$test_settings" -ne 0 ]; then

		while read _runsettingsseries_s; do
			runconfigseries "$_runsettingsseries_CFLAGS" "$_runsettingsseries_CC" \
				"$_runsettingsseries_configure_flags $_runsettingsseries_s" \
				"$_runsettingsseries_run_tests"
		done < "$scriptdir/release_settings.txt"

	else
		runconfigseries "$_runsettingsseries_CFLAGS" "$_runsettingsseries_CC" \
			"$_runsettingsseries_configure_flags" "$_runsettingsseries_run_tests"
	fi
}

# Builds and runs tests with each build type running runsettingsseries(). If
# run_tests is false, it just does the builds.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param run_tests        Whether to run tests or not.
runtestseries() {

	_runtestseries_CFLAGS="$1"
	shift

	_runtestseries_CC="$1"
	shift

	_runtestseries_configure_flags="$1"
	shift

	_runtestseries_run_tests="$1"
	shift

	_runtestseries_flags="E H N EH EN HN EHN"

	runsettingsseries "$_runtestseries_CFLAGS" "$_runtestseries_CC" \
		"$_runtestseries_configure_flags" "$_runtestseries_run_tests"

	for _runtestseries_f in $_runtestseries_flags; do
		runsettingsseries "$_runtestseries_CFLAGS" "$_runtestseries_CC" \
			"$_runtestseries_configure_flags -$_runtestseries_f" "$_runtestseries_run_tests"
	done
}

# Builds and runs the tests for bcl. If run_tests is false, it just does the
# builds.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param run_tests        Whether to run tests or not.
runlibtests() {

	_runlibtests_CFLAGS="$1"
	shift

	_runlibtests_CC="$1"
	shift

	_runlibtests_configure_flags="$1"
	shift

	_runlibtests_run_tests="$1"
	shift

	_runlibtests_configure_flags="$_runlibtests_configure_flags -a"

	build "$_runlibtests_CFLAGS" "$_runlibtests_CC" "$_runlibtests_configure_flags" 1 64

	if [ "$_runlibtests_run_tests" -ne 0 ]; then
		runtest
	fi

	build "$_runlibtests_CFLAGS" "$_runlibtests_CC" "$_runlibtests_configure_flags" 1 32

	if [ "$_runlibtests_run_tests" -ne 0 ]; then
		runtest
	fi
}

# Builds and runs tests under C99, then C11, if requested, using
# runtestseries(). If run_tests is false, it just does the builds.
# @param CFLAGS           The CFLAGS.
# @param CC               The C compiler.
# @param configure_flags  The flags for configure.sh itself.
# @param run_tests        Whether to run tests or not.
runtests() {

	_runtests_CFLAGS="$1"
	shift

	_runtests_CC="$1"
	shift

	_runtests_configure_flags="$1"
	shift

	_runtests_run_tests="$1"
	shift

	runtestseries "-std=c99 $_runtests_CFLAGS" "$_runtests_CC" "$_runtests_configure_flags" "$_runtests_run_tests"

	if [ "$test_c11" -ne 0 ]; then
		runtestseries "-std=c11 $_runtests_CFLAGS" "$_runtests_CC" "$_runtests_configure_flags" "$_runtests_run_tests"
	fi
}

# Runs the karatsuba tests.
karatsuba() {

	header "Running Karatsuba tests"
	do_make karatsuba_test
}

# Builds and runs under valgrind. It runs both, bc only, then dc only.
vg() {

	header "Running valgrind"

	if [ "$run_64_bit" -ne 0 ]; then
		_vg_bits=64
	else
		_vg_bits=32
	fi

	build "$debug -std=c99" "gcc" "-O3 -gv" "1" "$_vg_bits"
	runtest test

	do_make clean_config

	build "$debug -std=c99" "gcc" "-O3 -gvb" "1" "$_vg_bits"
	runtest test

	do_make clean_config

	build "$debug -std=c99" "gcc" "-O3 -gvd" "1" "$_vg_bits"
	runtest test

	do_make clean_config

	build "$debug -std=c99" "gcc" "-O3 -gva" "1" "$_vg_bits"
	runtest test

	do_make clean_config
}

# Builds the debug series and runs the tests if run_tests allows. If sanitizers
# are enabled, it also does UBSan.
# @param CC         The C compiler.
# @param run_tests  Whether to run tests or not.
debug() {

	_debug_CC="$1"
	shift

	_debug_run_tests="$1"
	shift


	if [ "$_debug_CC" = "clang" -a "$run_sanitizers" -ne 0 ]; then
		runtests "$debug -fsanitize=undefined" "$_debug_CC" "-gm" "$_debug_run_tests"
	else
		runtests "$debug" "$_debug_CC" "-g" "$_debug_run_tests"
	fi


	if [ "$_debug_CC" = "clang" -a "$run_sanitizers" -ne 0 ]; then
		runlibtests "$debug -fsanitize=undefined" "$_debug_CC" "-gm" "$_debug_run_tests"
	else
		runlibtests "$debug" "$_debug_CC" "-g" "$_debug_run_tests"
	fi
}

# Builds the release series and runs the test if run_tests allows.
# @param CC         The C compiler.
# @param run_tests  Whether to run tests or not.
release() {

	_release_CC="$1"
	shift

	_release_run_tests="$1"
	shift

	runtests "$release" "$_release_CC" "-O3" "$_release_run_tests"

	runlibtests "$release" "$_release_CC" "-O3" "$_release_run_tests"
}

# Builds the release debug series and runs the test if run_tests allows. If
# sanitizers are enabled, it also does ASan and MSan.
# @param CC         The C compiler.
# @param run_tests  Whether to run tests or not.
reldebug() {

	_reldebug_CC="$1"
	shift

	_reldebug_run_tests="$1"
	shift


	if [ "$_reldebug_CC" = "clang" -a "$run_sanitizers" -ne 0 ]; then
		runtests "$debug -fsanitize=address" "$_reldebug_CC" "-mgO3" "$_reldebug_run_tests"
		runtests "$debug -fsanitize=memory" "$_reldebug_CC" "-mgO3" "$_reldebug_run_tests"
	else
		runtests "$debug" "$_reldebug_CC" "-gO3" "$_reldebug_run_tests"
	fi


	if [ "$_reldebug_CC" = "clang" -a "$run_sanitizers" -ne 0 ]; then
		runlibtests "$debug -fsanitize=address" "$_reldebug_CC" "-mgO3" "$_reldebug_run_tests"
		runlibtests "$debug -fsanitize=memory" "$_reldebug_CC" "-mgO3" "$_reldebug_run_tests"
	else
		runlibtests "$debug" "$_reldebug_CC" "-gO3" "$_reldebug_run_tests"
	fi
}

# Builds the min size release series and runs the test if run_tests allows.
# @param CC         The C compiler.
# @param run_tests  Whether to run tests or not.
minsize() {

	_minsize_CC="$1"
	shift

	_minsize_run_tests="$1"
	shift

	runtests "$release" "$_minsize_CC" "-Os" "$_minsize_run_tests"

	runlibtests "$release" "$_minsize_CC" "-Os" "$_minsize_run_tests"
}

# Builds all sets: debug, release, release debug, and min size, and runs the
# tests if run_tests allows.
# @param CC         The C compiler.
# @param run_tests  Whether to run tests or not.
build_set() {

	_build_set_CC="$1"
	shift

	_build_set_run_tests="$1"
	shift

	debug "$_build_set_CC" "$_build_set_run_tests"
	release "$_build_set_CC" "$_build_set_run_tests"
	reldebug "$_build_set_CC" "$_build_set_run_tests"
	minsize "$_build_set_CC" "$_build_set_run_tests"
}

# Set some strict warning flags. Clang's -Weverything can be way too strict, so
# we actually have to turn off some things.
clang_flags="-Weverything -Wno-padded -Wno-switch-enum -Wno-format-nonliteral"
clang_flags="$clang_flags -Wno-cast-align -Wno-missing-noreturn -Wno-disabled-macro-expansion"
clang_flags="$clang_flags -Wno-unreachable-code -Wno-unreachable-code-return"
clang_flags="$clang_flags -Wno-implicit-fallthrough -Wno-unused-macros -Wno-gnu-label-as-value"
gcc_flags="-Wno-maybe-uninitialized -Wno-clobbered"

# Common CFLAGS.
cflags="-Wall -Wextra -Werror -pedantic -Wno-conditional-uninitialized"

# Common debug and release flags.
debug="$cflags -fno-omit-frame-pointer"
release="$cflags -DNDEBUG"

set -e

script="$0"
scriptdir=$(dirname "$script")

# Whether to run tests.
if [ "$#" -gt 0 ]; then
	run_tests="$1"
	shift
else
	run_tests=1
fi

# Whether to generate tests. On platforms like OpenBSD, there is no GNU bc to
# generate tests, so this must be off.
if [ "$#" -gt 0 ]; then
	gen_tests="$1"
	shift
else
	gen_tests=1
fi

# Whether to test with clang.
if [ "$#" -gt 0 ]; then
	test_with_clang="$1"
	shift
else
	test_with_clang=1
fi

# Whether to test with gcc.
if [ "$#" -gt 0 ]; then
	test_with_gcc="$1"
	shift
else
	test_with_gcc=1
fi

# Whether to test with sanitizers.
if [ "$#" -gt 0 ]; then
	run_sanitizers="$1"
	shift
else
	run_sanitizers=1
fi

# Whether to test with valgrind.
if [ "$#" -gt 0 ]; then
	run_valgrind="$1"
	shift
else
	run_valgrind=1
fi

# Whether to test all settings combos.
if [ "$#" -gt 0 ]; then
	test_settings="$1"
	shift
else
	test_settings=1
fi

# Whether to test 64-bit in addition to 32-bit.
if [ "$#" -gt 0 ]; then
	run_64_bit="$1"
	shift
else
	run_64_bit=1
fi

# Whether to test with strgen.sh in addition to strgen.c.
if [ "$#" -gt 0 ]; then
	run_gen_script="$1"
	shift
else
	run_gen_script=0
fi

# Whether to test on C11 in addition to C99.
if [ "$#" -gt 0 ]; then
	test_c11="$1"
	shift
else
	test_c11=0
fi

# Whether to test 128-bit integers in addition to no 128-bit integers.
if [ "$#" -gt 0 ]; then
	test_128_bit="$1"
	shift
else
	test_128_bit=0
fi

# Whether to test with computed goto or not.
if [ "$#" -gt 0 ]; then
	test_computed_goto="$1"
	shift
else
	test_computed_goto=0
fi

if [ "$run_64_bit" -ne 0 ]; then
	bits=64
else
	bits=32
fi

if [ "$test_computed_goto" -eq 0 ]; then
	clang_flags="-DBC_NO_COMPUTED_GOTO $clang_flags"
	gcc_flags="-DBC_NO_COMPUTED_GOTO $gcc_flags"
fi

cd "$scriptdir/.."

# Setup a default compiler.
if [ "$test_with_clang" -ne 0 ]; then
	defcc="clang"
elif [ "$test_with_gcc" -ne 0 ]; then
	defcc="gcc"
else
	defcc="c99"
fi

export ASAN_OPTIONS="abort_on_error=1,allocator_may_return_null=1"
export UBSAN_OPTIONS="print_stack_trace=1,silence_unsigned_overflow=1"

build "$debug -std=c99" "$defcc" "-g" "1" "$bits"

header "Running math library under --standard"

# Make sure the math library is POSIX compliant.
printf 'quit\n' | bin/bc -ls

do_make clean_tests

# Run the clang build sets.
if [ "$test_with_clang" -ne 0 ]; then
	build_set "clang" "$run_tests"
fi

# Run the gcc build sets.
if [ "$test_with_gcc" -ne 0 ]; then
	build_set "gcc" "$run_tests"
fi

if [ "$run_tests" -ne 0 ]; then

	build "$release" "$defcc" "-O3" "1" "$bits"

	# Run karatsuba.
	karatsuba

	# Valgrind.
	if [ "$run_valgrind" -ne 0 -a "$test_with_gcc" -ne 0 ]; then
		vg
	fi

	printf '\n'
	printf 'Tests successful.\n'

	# I just assume that I am going to be fuzzing when I am done.
	header "Building for AFL++..."

	"$scriptdir/fuzz_prep.sh"

	printf '\n'
	printf 'Ready for scripts/randmath.py and for fuzzing.\n'
	printf '\n'
	printf 'Run scripts/randmath.py if you changed any math code.\n'
	printf '\n'
	printf 'Then if there are no problems, run the fuzzer.\n'
	printf '\n'
	printf 'Then run `scripts/fuzz_prep.sh -a`.\n'
	printf '\n'
	printf 'Then run `scripts/afl.py --asan`.\n'

fi