1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14#   this list of conditions and the following disclaimer in the documentation
15#   and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30script="$0"
31scriptdir=$(dirname "$script")
32script=$(basename "$script")
33
34. "$scriptdir/functions.sh"
35
36cd "$scriptdir"
37
38usage() {
39
40	if [ $# -gt 0 ]; then
41
42		_usage_val=1
43
44		printf "%s\n\n" "$1"
45
46	else
47		_usage_val=0
48	fi
49
50	printf 'usage:\n'
51	printf '    %s -h\n' "$script"
52	printf '    %s --help\n' "$script"
53	printf '    %s [-a|-bD|-dB|-c] [-CEfgGHlmMNPtTvz] [-O OPT_LEVEL] [-k KARATSUBA_LEN]\n' "$script"
54	printf '    %s \\\n' "$script"
55	printf '       [--library|--bc-only --disable-dc|--dc-only --disable-bc|--coverage]\\\n'
56	printf '       [--force --debug --disable-extra-math --disable-generated-tests]    \\\n'
57	printf '       [--disable-history --disable-man-pages --disable-nls]               \\\n'
58	printf '       [--disable-prompt --disable-strip] [--install-all-locales]          \\\n'
59	printf '       [--opt=OPT_LEVEL] [--karatsuba-len=KARATSUBA_LEN]                   \\\n'
60	printf '       [--prefix=PREFIX] [--bindir=BINDIR] [--datarootdir=DATAROOTDIR]     \\\n'
61	printf '       [--datadir=DATADIR] [--mandir=MANDIR] [--man1dir=MAN1DIR]           \\\n'
62	printf '\n'
63	printf '    -a, --library\n'
64	printf '        Build the libbc instead of the programs. This is meant to be used with\n'
65	printf '        Other software like programming languages that want to make use of the\n'
66	printf '        parsing and math capabilities. This option will install headers using\n'
67	printf '        `make install`.\n'
68	printf '    -b, --bc-only\n'
69	printf '        Build bc only. It is an error if "-d", "--dc-only", "-B", or\n'
70	printf '        "--disable-bc" are specified too.\n'
71	printf '    -B, --disable-bc\n'
72	printf '        Disable bc. It is an error if "-b", "--bc-only", "-D", or "--disable-dc"\n'
73	printf '        are specified too.\n'
74	printf '    -c, --coverage\n'
75	printf '        Generate test coverage code. Requires gcov and regcovr.\n'
76	printf '        It is an error if either "-b" ("-D") or "-d" ("-B") is specified.\n'
77	printf '        Requires a compiler that use gcc-compatible coverage options\n'
78	printf '    -C, --disable-clean\n'
79	printf '        Disable the clean that configure.sh does before configure.\n'
80	printf '    -d, --dc-only\n'
81	printf '        Build dc only. It is an error if "-b", "--bc-only", "-D", or\n'
82	printf '        "--disable-dc" are specified too.\n'
83	printf '    -D, --disable-dc\n'
84	printf '        Disable dc. It is an error if "-d", "--dc-only", "-B", or "--disable-bc"\n'
85	printf '        are specified too.\n'
86	printf '    -E, --disable-extra-math\n'
87	printf '        Disable extra math. This includes: "$" operator (truncate to integer),\n'
88	printf '        "@" operator (set number of decimal places), and r(x, p) (rounding\n'
89	printf '        function). Additionally, this option disables the extra printing\n'
90	printf '        functions in the math library.\n'
91	printf '    -f, --force\n'
92	printf '        Force use of all enabled options, even if they do not work. This\n'
93	printf '        option is to allow the maintainer a way to test that certain options\n'
94	printf '        are not failing invisibly. (Development only.)'
95	printf '    -g, --debug\n'
96	printf '        Build in debug mode. Adds the "-g" flag, and if there are no\n'
97	printf '        other CFLAGS, and "-O" was not given, this also adds the "-O0"\n'
98	printf '        flag. If this flag is *not* given, "-DNDEBUG" is added to CPPFLAGS\n'
99	printf '        and a strip flag is added to the link stage.\n'
100	printf '    -G, --disable-generated-tests\n'
101	printf '        Disable generating tests. This is for platforms that do not have a\n'
102	printf '        GNU bc-compatible bc to generate tests.\n'
103	printf '    -h, --help\n'
104	printf '        Print this help message and exit.\n'
105	printf '    -H, --disable-history\n'
106	printf '        Disable history.\n'
107	printf '    -k KARATSUBA_LEN, --karatsuba-len KARATSUBA_LEN\n'
108	printf '        Set the karatsuba length to KARATSUBA_LEN (default is 64).\n'
109	printf '        It is an error if KARATSUBA_LEN is not a number or is less than 16.\n'
110	printf '    -l, --install-all-locales\n'
111	printf '        Installs all locales, regardless of how many are on the system. This\n'
112	printf '        option is useful for package maintainers who want to make sure that\n'
113	printf '        a package contains all of the locales that end users might need.\n'
114	printf '    -m, --enable-memcheck\n'
115	printf '        Enable memcheck mode, to ensure no memory leaks. For development only.\n'
116	printf '    -M, --disable-man-pages\n'
117	printf '        Disable installing manpages.\n'
118	printf '    -N, --disable-nls\n'
119	printf '        Disable POSIX locale (NLS) support.\n'
120	printf '    -O OPT_LEVEL, --opt OPT_LEVEL\n'
121	printf '        Set the optimization level. This can also be included in the CFLAGS,\n'
122	printf '        but it is provided, so maintainers can build optimized debug builds.\n'
123	printf '        This is passed through to the compiler, so it must be supported.\n'
124	printf '    -P, --disable-prompt\n'
125	printf '        Disables the prompt in the built bc. The prompt will never show up,\n'
126	printf '        or in other words, it will be permanently disabled and cannot be\n'
127	printf '        enabled.\n'
128	printf '    -t, --enable-test-timing\n'
129	printf '        Enable the timing of tests. This is for development only.\n'
130	printf '    -T, --disable-strip\n'
131	printf '        Disable stripping symbols from the compiled binary or binaries.\n'
132	printf '        Stripping symbols only happens when debug mode is off.\n'
133	printf '    -v, --enable-valgrind\n'
134	printf '        Enable a build appropriate for valgrind. For development only.\n'
135	printf '    -z, --enable-fuzz-mode\n'
136	printf '        Enable fuzzing mode. THIS IS FOR DEVELOPMENT ONLY.\n'
137	printf '    --prefix PREFIX\n'
138	printf '        The prefix to install to. Overrides "$PREFIX" if it exists.\n'
139	printf '        If PREFIX is "/usr", install path will be "/usr/bin".\n'
140	printf '        Default is "/usr/local".\n'
141	printf '    --bindir BINDIR\n'
142	printf '        The directory to install binaries in. Overrides "$BINDIR" if it exists.\n'
143	printf '        Default is "$PREFIX/bin".\n'
144	printf '    --includedir INCLUDEDIR\n'
145	printf '        The directory to install headers in. Overrides "$INCLUDEDIR" if it\n'
146	printf '        exists. Default is "$PREFIX/include".\n'
147	printf '    --libdir LIBDIR\n'
148	printf '        The directory to install libraries in. Overrides "$LIBDIR" if it exists.\n'
149	printf '        Default is "$PREFIX/lib".\n'
150	printf '    --datarootdir DATAROOTDIR\n'
151	printf '        The root location for data files. Overrides "$DATAROOTDIR" if it exists.\n'
152	printf '        Default is "$PREFIX/share".\n'
153	printf '    --datadir DATADIR\n'
154	printf '        The location for data files. Overrides "$DATADIR" if it exists.\n'
155	printf '        Default is "$DATAROOTDIR".\n'
156	printf '    --mandir MANDIR\n'
157	printf '        The location to install manpages to. Overrides "$MANDIR" if it exists.\n'
158	printf '        Default is "$DATADIR/man".\n'
159	printf '    --man1dir MAN1DIR\n'
160	printf '        The location to install Section 1 manpages to. Overrides "$MAN1DIR" if\n'
161	printf '        it exists. Default is "$MANDIR/man1".\n'
162	printf '    --man3dir MAN3DIR\n'
163	printf '        The location to install Section 3 manpages to. Overrides "$MAN3DIR" if\n'
164	printf '        it exists. Default is "$MANDIR/man3".\n'
165	printf '\n'
166	printf 'In addition, the following environment variables are used:\n'
167	printf '\n'
168	printf '    CC           C compiler. Must be compatible with POSIX c99. If there is a\n'
169	printf '                 space in the basename of the compiler, the items after the\n'
170	printf '                 first space are assumed to be compiler flags, and in that case,\n'
171	printf '                 the flags are automatically moved into CFLAGS. Default is\n'
172	printf '                 "c99".\n'
173	printf '    HOSTCC       Host C compiler. Must be compatible with POSIX c99. If there is\n'
174	printf '                 a space in the basename of the compiler, the items after the\n'
175	printf '                 first space are assumed to be compiler flags, and in the case,\n'
176	printf '                 the flags are automatically moved into HOSTCFLAGS. Default is\n'
177	printf '                 "$CC".\n'
178	printf '    HOST_CC      Same as HOSTCC. If HOSTCC also exists, it is used.\n'
179	printf '    CFLAGS       C compiler flags.\n'
180	printf '    HOSTCFLAGS   CFLAGS for HOSTCC. Default is "$CFLAGS".\n'
181	printf '    HOST_CFLAGS  Same as HOST_CFLAGS. If HOST_CFLAGS also exists, it is used.\n'
182	printf '    CPPFLAGS     C preprocessor flags. Default is "".\n'
183	printf '    LDFLAGS      Linker flags. Default is "".\n'
184	printf '    PREFIX       The prefix to install to. Default is "/usr/local".\n'
185	printf '                 If PREFIX is "/usr", install path will be "/usr/bin".\n'
186	printf '    BINDIR       The directory to install binaries in. Default is "$PREFIX/bin".\n'
187	printf '    INCLUDEDIR   The directory to install header files in. Default is\n'
188	printf '                 "$PREFIX/include".\n'
189	printf '    LIBDIR       The directory to install libraries in. Default is\n'
190	printf '                 "$PREFIX/lib".\n'
191	printf '    DATAROOTDIR  The root location for data files. Default is "$PREFIX/share".\n'
192	printf '    DATADIR      The location for data files. Default is "$DATAROOTDIR".\n'
193	printf '    MANDIR       The location to install manpages to. Default is "$DATADIR/man".\n'
194	printf '    MAN1DIR      The location to install Section 1 manpages to. Default is\n'
195	printf '                 "$MANDIR/man1".\n'
196	printf '    MAN3DIR      The location to install Section 3 manpages to. Default is\n'
197	printf '                 "$MANDIR/man3".\n'
198	printf '    NLSPATH      The location to install locale catalogs to. Must be an absolute\n'
199	printf '                 path (or contain one). This is treated the same as the POSIX\n'
200	printf '                 definition of $NLSPATH (see POSIX environment variables for\n'
201	printf '                 more information). Default is "/usr/share/locale/%%L/%%N".\n'
202	printf '    EXECSUFFIX   The suffix to append to the executable names, used to not\n'
203	printf '                 interfere with other installed bc executables. Default is "".\n'
204	printf '    EXECPREFIX   The prefix to append to the executable names, used to not\n'
205	printf '                 interfere with other installed bc executables. Default is "".\n'
206	printf '    DESTDIR      For package creation. Default is "". If it is empty when\n'
207	printf '                 `%s` is run, it can also be passed to `make install`\n' "$script"
208	printf '                 later as an environment variable. If both are specified,\n'
209	printf '                 the one given to `%s` takes precedence.\n' "$script"
210	printf '    LONG_BIT     The number of bits in a C `long` type. This is mostly for the\n'
211	printf '                 embedded space since this `bc` uses `long`s internally for\n'
212	printf '                 overflow checking. In C99, a `long` is required to be 32 bits.\n'
213	printf '                 For most normal desktop systems, setting this is unnecessary,\n'
214	printf '                 except that 32-bit platforms with 64-bit longs may want to set\n'
215	printf '                 it to `32`. Default is the default of `LONG_BIT` for the target\n'
216	printf '                 platform. Minimum allowed is `32`. It is a build time error if\n'
217	printf '                 the specified value of `LONG_BIT` is greater than the default\n'
218	printf '                 value of `LONG_BIT` for the target platform.\n'
219	printf '    GEN_HOST     Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to\n'
220	printf '                 produce the C files that contain the help texts as well as the\n'
221	printf '                 math libraries. By default, `gen/strgen.c` is used, compiled by\n'
222	printf '                 "$HOSTCC" and run on the host machine. Using `gen/strgen.sh`\n'
223	printf '                 removes the need to compile and run an executable on the host\n'
224	printf '                 machine since `gen/strgen.sh` is a POSIX shell script. However,\n'
225	printf '                 `gen/lib2.bc` is perilously close to 4095 characters, the max\n'
226	printf '                 supported length of a string literal in C99 (and it could be\n'
227	printf '                 added to in the future), and `gen/strgen.sh` generates a string\n'
228	printf '                 literal instead of an array, as `gen/strgen.c` does. For most\n'
229	printf '                 production-ready compilers, this limit probably is not\n'
230	printf '                 enforced, but it could be. Both options are still available for\n'
231	printf '                 this reason. If you are sure your compiler does not have the\n'
232	printf '                 limit and do not want to compile and run a binary on the host\n'
233	printf '                 machine, set this variable to "0". Any other value, or a\n'
234	printf '                 non-existent value, will cause the build system to compile and\n'
235	printf '                 run `gen/strgen.c`. Default is "".\n'
236	printf '    GEN_EMU      Emulator to run string generator code under (leave empty if not\n'
237	printf '                 necessary). This is not necessary when using `gen/strgen.sh`.\n'
238	printf '                 Default is "".\n'
239	printf '\n'
240	printf 'WARNING: even though `configure.sh` supports both option types, short and\n'
241	printf 'long, it does not support handling both at the same time. Use only one type.\n'
242
243	exit "$_usage_val"
244}
245
246replace_ext() {
247
248	if [ "$#" -ne 3 ]; then
249		err_exit "Invalid number of args to $0"
250	fi
251
252	_replace_ext_file="$1"
253	_replace_ext_ext1="$2"
254	_replace_ext_ext2="$3"
255
256	_replace_ext_result="${_replace_ext_file%.$_replace_ext_ext1}.$_replace_ext_ext2"
257
258	printf '%s\n' "$_replace_ext_result"
259}
260
261replace_exts() {
262
263	if [ "$#" -ne 3 ]; then
264		err_exit "Invalid number of args to $0"
265	fi
266
267	_replace_exts_files="$1"
268	_replace_exts_ext1="$2"
269	_replace_exts_ext2="$3"
270
271	for _replace_exts_file in $_replace_exts_files; do
272		_replace_exts_new_name=$(replace_ext "$_replace_exts_file" "$_replace_exts_ext1" "$_replace_exts_ext2")
273		_replace_exts_result="$_replace_exts_result $_replace_exts_new_name"
274	done
275
276	printf '%s\n' "$_replace_exts_result"
277}
278
279replace() {
280
281	if [ "$#" -ne 3 ]; then
282		err_exit "Invalid number of args to $0"
283	fi
284
285	_replace_str="$1"
286	_replace_needle="$2"
287	_replace_replacement="$3"
288
289	substring_replace "$_replace_str" "%%$_replace_needle%%" "$_replace_replacement"
290}
291
292find_src_files() {
293
294	if [ "$#" -ge 1 ] && [ "$1" != "" ]; then
295
296		while [ "$#" -ge 1 ]; do
297			_find_src_files_a="${1## }"
298			shift
299			_find_src_files_args="$_find_src_files_args ! -path src/${_find_src_files_a}"
300		done
301
302	else
303		_find_src_files_args="-print"
304	fi
305
306	printf '%s\n' $(find src/ -depth -name "*.c" $_find_src_files_args)
307}
308
309gen_file_list() {
310
311	if [ "$#" -lt 1 ]; then
312		err_exit "Invalid number of args to $0"
313	fi
314
315	_gen_file_list_contents="$1"
316	shift
317
318	p=$(pwd)
319
320	cd "$scriptdir"
321
322	if [ "$#" -ge 1 ]; then
323		_gen_file_list_unneeded="$@"
324	else
325		_gen_file_list_unneeded=""
326	fi
327
328	_gen_file_list_needle_src="SRC"
329	_gen_file_list_needle_obj="OBJ"
330	_gen_file_list_needle_gcda="GCDA"
331	_gen_file_list_needle_gcno="GCNO"
332
333	_gen_file_list_replacement=$(find_src_files $_gen_file_list_unneeded | tr '\n' ' ')
334	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
335		"$_gen_file_list_needle_src" "$_gen_file_list_replacement")
336
337	_gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "c" "o")
338	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
339		"$_gen_file_list_needle_obj" "$_gen_file_list_replacement")
340
341	_gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "o" "gcda")
342	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
343		"$_gen_file_list_needle_gcda" "$_gen_file_list_replacement")
344
345	_gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "gcda" "gcno")
346	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
347		"$_gen_file_list_needle_gcno" "$_gen_file_list_replacement")
348
349	cd "$p"
350
351	printf '%s\n' "$_gen_file_list_contents"
352}
353
354gen_tests() {
355
356	_gen_tests_name="$1"
357	shift
358
359	_gen_tests_uname="$1"
360	shift
361
362	_gen_tests_extra_math="$1"
363	shift
364
365	_gen_tests_time_tests="$1"
366	shift
367
368	_gen_tests_extra_required=$(cat tests/extra_required.txt)
369
370	for _gen_tests_t in $(cat "$scriptdir/tests/$_gen_tests_name/all.txt"); do
371
372		if [ "$_gen_tests_extra_math" -eq 0 ]; then
373
374			if [ -z "${_gen_tests_extra_required##*$_gen_tests_t*}" ]; then
375				printf 'test_%s_%s:\n\t@printf "Skipping %s %s\\n"\n\n' \
376					"$_gen_tests_name" "$_gen_tests_t" "$_gen_tests_name" \
377					"$_gen_tests_t" >> "$scriptdir/Makefile"
378				continue
379			fi
380
381		fi
382
383		printf 'test_%s_%s:\n\t@sh tests/test.sh %s %s %s %s %s\n\n' \
384			"$_gen_tests_name" "$_gen_tests_t" "$_gen_tests_name" \
385			"$_gen_tests_t" "$generate_tests" "$time_tests" \
386			"$*" >> "$scriptdir/Makefile"
387
388	done
389}
390
391gen_test_targets() {
392
393	_gen_test_targets_name="$1"
394	shift
395
396	_gen_test_targets_tests=$(cat "$scriptdir/tests/${_gen_test_targets_name}/all.txt")
397
398	for _gen_test_targets_t in $_gen_test_targets_tests; do
399		printf ' test_%s_%s' "$_gen_test_targets_name" "$_gen_test_targets_t"
400	done
401
402	printf '\n'
403}
404
405gen_script_tests() {
406
407	_gen_script_tests_name="$1"
408	shift
409
410	_gen_script_tests_extra_math="$1"
411	shift
412
413	_gen_script_tests_generate="$1"
414	shift
415
416	_gen_script_tests_time="$1"
417	shift
418
419	_gen_script_tests_tests=$(cat "$scriptdir/tests/$_gen_script_tests_name/scripts/all.txt")
420
421	for _gen_script_tests_f in $_gen_script_tests_tests; do
422
423		_gen_script_tests_b=$(basename "$_gen_script_tests_f" ".${_gen_script_tests_name}")
424
425		printf 'test_%s_script_%s:\n\t@sh tests/script.sh %s %s %s 1 %s %s %s\n\n' \
426			"$_gen_script_tests_name" "$_gen_script_tests_b" "$_gen_script_tests_name" \
427			"$_gen_script_tests_f" "$_gen_script_tests_extra_math" "$_gen_script_tests_generate" \
428			"$_gen_script_tests_time" "$*" >> "$scriptdir/Makefile"
429	done
430}
431
432gen_script_test_targets() {
433
434	_gen_script_test_targets_name="$1"
435	shift
436
437	_gen_script_test_targets_tests=$(cat "$scriptdir/tests/$_gen_script_test_targets_name/scripts/all.txt")
438
439	for _gen_script_test_targets_f in $_gen_script_test_targets_tests; do
440		_gen_script_test_targets_b=$(basename "$_gen_script_test_targets_f" \
441			".$_gen_script_test_targets_name")
442		printf ' test_%s_script_%s' "$_gen_script_test_targets_name" \
443			"$_gen_script_test_targets_b"
444	done
445
446	printf '\n'
447}
448
449bc_only=0
450dc_only=0
451coverage=0
452karatsuba_len=32
453debug=0
454hist=1
455extra_math=1
456optimization=""
457generate_tests=1
458install_manpages=1
459nls=1
460prompt=1
461force=0
462strip_bin=1
463all_locales=0
464library=0
465fuzz=0
466time_tests=0
467vg=0
468memcheck=0
469clean=1
470
471while getopts "abBcdDEfgGhHk:lMmNO:PStTvz-" opt; do
472
473	case "$opt" in
474		a) library=1 ;;
475		b) bc_only=1 ;;
476		B) dc_only=1 ;;
477		c) coverage=1 ;;
478		C) clean=0 ;;
479		d) dc_only=1 ;;
480		D) bc_only=1 ;;
481		E) extra_math=0 ;;
482		f) force=1 ;;
483		g) debug=1 ;;
484		G) generate_tests=0 ;;
485		h) usage ;;
486		H) hist=0 ;;
487		k) karatsuba_len="$OPTARG" ;;
488		l) all_locales=1 ;;
489		m) memcheck=1 ;;
490		M) install_manpages=0 ;;
491		N) nls=0 ;;
492		O) optimization="$OPTARG" ;;
493		P) prompt=0 ;;
494		t) time_tests=1 ;;
495		T) strip_bin=0 ;;
496		v) vg=1 ;;
497		z) fuzz=1 ;;
498		-)
499			arg="$1"
500			arg="${arg#--}"
501			LONG_OPTARG="${arg#*=}"
502			case $arg in
503				help) usage ;;
504				library) library=1 ;;
505				bc-only) bc_only=1 ;;
506				dc-only) dc_only=1 ;;
507				coverage) coverage=1 ;;
508				debug) debug=1 ;;
509				force) force=1 ;;
510				prefix=?*) PREFIX="$LONG_OPTARG" ;;
511				prefix)
512					if [ "$#" -lt 2 ]; then
513						usage "No argument given for '--$arg' option"
514					fi
515					PREFIX="$2"
516					shift ;;
517				bindir=?*) BINDIR="$LONG_OPTARG" ;;
518				bindir)
519					if [ "$#" -lt 2 ]; then
520						usage "No argument given for '--$arg' option"
521					fi
522					BINDIR="$2"
523					shift ;;
524				includedir=?*) INCLUDEDIR="$LONG_OPTARG" ;;
525				includedir)
526					if [ "$#" -lt 2 ]; then
527						usage "No argument given for '--$arg' option"
528					fi
529					INCLUDEDIR="$2"
530					shift ;;
531				libdir=?*) LIBDIR="$LONG_OPTARG" ;;
532				libdir)
533					if [ "$#" -lt 2 ]; then
534						usage "No argument given for '--$arg' option"
535					fi
536					LIBDIR="$2"
537					shift ;;
538				datarootdir=?*) DATAROOTDIR="$LONG_OPTARG" ;;
539				datarootdir)
540					if [ "$#" -lt 2 ]; then
541						usage "No argument given for '--$arg' option"
542					fi
543					DATAROOTDIR="$2"
544					shift ;;
545				datadir=?*) DATADIR="$LONG_OPTARG" ;;
546				datadir)
547					if [ "$#" -lt 2 ]; then
548						usage "No argument given for '--$arg' option"
549					fi
550					DATADIR="$2"
551					shift ;;
552				mandir=?*) MANDIR="$LONG_OPTARG" ;;
553				mandir)
554					if [ "$#" -lt 2 ]; then
555						usage "No argument given for '--$arg' option"
556					fi
557					MANDIR="$2"
558					shift ;;
559				man1dir=?*) MAN1DIR="$LONG_OPTARG" ;;
560				man1dir)
561					if [ "$#" -lt 2 ]; then
562						usage "No argument given for '--$arg' option"
563					fi
564					MAN1DIR="$2"
565					shift ;;
566				man3dir=?*) MAN3DIR="$LONG_OPTARG" ;;
567				man3dir)
568					if [ "$#" -lt 2 ]; then
569						usage "No argument given for '--$arg' option"
570					fi
571					MAN3DIR="$2"
572					shift ;;
573				localedir=?*) LOCALEDIR="$LONG_OPTARG" ;;
574				localedir)
575					if [ "$#" -lt 2 ]; then
576						usage "No argument given for '--$arg' option"
577					fi
578					LOCALEDIR="$2"
579					shift ;;
580				karatsuba-len=?*) karatsuba_len="$LONG_OPTARG" ;;
581				karatsuba-len)
582					if [ "$#" -lt 2 ]; then
583						usage "No argument given for '--$arg' option"
584					fi
585					karatsuba_len="$1"
586					shift ;;
587				opt=?*) optimization="$LONG_OPTARG" ;;
588				opt)
589					if [ "$#" -lt 2 ]; then
590						usage "No argument given for '--$arg' option"
591					fi
592					optimization="$1"
593					shift ;;
594				disable-bc) dc_only=1 ;;
595				disable-dc) bc_only=1 ;;
596				disable-clean) clean=0 ;;
597				disable-extra-math) extra_math=0 ;;
598				disable-generated-tests) generate_tests=0 ;;
599				disable-history) hist=0 ;;
600				disable-man-pages) install_manpages=0 ;;
601				disable-nls) nls=0 ;;
602				disable-prompt) prompt=0 ;;
603				disable-strip) strip_bin=0 ;;
604				enable-test-timing) time_tests=1 ;;
605				enable-valgrind) vg=1 ;;
606				enable-fuzz-mode) fuzz=1 ;;
607				enable-memcheck) memcheck=1 ;;
608				install-all-locales) all_locales=1 ;;
609				help* | bc-only* | dc-only* | coverage* | debug*)
610					usage "No arg allowed for --$arg option" ;;
611				disable-bc* | disable-dc* | disable-clean*)
612					usage "No arg allowed for --$arg option" ;;
613				disable-extra-math*)
614					usage "No arg allowed for --$arg option" ;;
615				disable-generated-tests* | disable-history*)
616					usage "No arg allowed for --$arg option" ;;
617				disable-man-pages* | disable-nls* | disable-strip*)
618					usage "No arg allowed for --$arg option" ;;
619				enable-fuzz-mode* | enable-test-timing* | enable-valgrind*)
620					usage "No arg allowed for --$arg option" ;;
621				enable-memcheck* | install-all-locales*)
622					usage "No arg allowed for --$arg option" ;;
623				'') break ;; # "--" terminates argument processing
624				* ) usage "Invalid option $LONG_OPTARG" ;;
625			esac
626			shift
627			OPTIND=1 ;;
628		?) usage "Invalid option $opt" ;;
629	esac
630
631done
632
633if [ "$clean" -ne 0 ]; then
634	if [ -f ./Makefile ]; then
635		make clean_config > /dev/null
636	fi
637fi
638
639if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then
640	usage "Can only specify one of -b(-D) or -d(-B)"
641fi
642
643if [ "$library" -ne 0 ]; then
644	if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
645		usage "Must not specify -b(-D) or -d(-B) when building the library"
646	fi
647fi
648
649case $karatsuba_len in
650	(*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;;
651	(*) ;;
652esac
653
654if [ "$karatsuba_len" -lt 16 ]; then
655	usage "KARATSUBA_LEN is less than 16"
656fi
657
658set -e
659
660if [ -z "${LONG_BIT+set}" ]; then
661	LONG_BIT_DEFINE=""
662elif [ "$LONG_BIT" -lt 32 ]; then
663	usage "LONG_BIT is less than 32"
664else
665	LONG_BIT_DEFINE="-DBC_LONG_BIT=\$(BC_LONG_BIT)"
666fi
667
668if [ -z "$CC" ]; then
669	CC="c99"
670else
671	ccbase=$(basename "$CC")
672	suffix=" *"
673	prefix="* "
674
675	if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
676		ccflags="${ccbase#$prefix}"
677		cc="${ccbase%%$suffix}"
678		ccdir=$(dirname "$CC")
679		if [ "$ccdir" = "." ] && [ "${CC#.}" = "$CC" ]; then
680			ccdir=""
681		else
682			ccdir="$ccdir/"
683		fi
684		CC="${ccdir}${cc}"
685		CFLAGS="$CFLAGS $ccflags"
686	fi
687fi
688
689if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then
690	HOSTCC="$CC"
691elif [ -z "$HOSTCC" ]; then
692	HOSTCC="$HOST_CC"
693fi
694
695if [ "$HOSTCC" != "$CC" ]; then
696	ccbase=$(basename "$HOSTCC")
697	suffix=" *"
698	prefix="* "
699
700	if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
701		ccflags="${ccbase#$prefix}"
702		cc="${ccbase%%$suffix}"
703		ccdir=$(dirname "$HOSTCC")
704		if [ "$ccdir" = "." ] && [ "${HOSTCC#.}" = "$HOSTCC" ]; then
705			ccdir=""
706		else
707			ccdir="$ccdir/"
708		fi
709		HOSTCC="${ccdir}${cc}"
710		HOSTCFLAGS="$HOSTCFLAGS $ccflags"
711	fi
712fi
713
714if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then
715	HOSTCFLAGS="$CFLAGS"
716elif [ -z "${HOSTCFLAGS+set}" ]; then
717	HOSTCFLAGS="$HOST_CFLAGS"
718fi
719
720link="@printf 'No link necessary\\\\n'"
721main_exec="BC"
722executable="BC_EXEC"
723
724tests="test_bc timeconst test_dc"
725
726bc_test="@tests/all.sh bc $extra_math 1 $generate_tests 0 \$(BC_EXEC)"
727dc_test="@tests/all.sh dc $extra_math 1 $generate_tests 0 \$(DC_EXEC)"
728
729timeconst="@tests/bc/timeconst.sh tests/bc/scripts/timeconst.bc \$(BC_EXEC)"
730
731# In order to have cleanup at exit, we need to be in
732# debug mode, so don't run valgrind without that.
733if [ "$vg" -ne 0 ]; then
734	debug=1
735	bc_test_exec='valgrind $(VALGRIND_ARGS) $(BC_EXEC)'
736	dc_test_exec='valgrind $(VALGRIND_ARGS) $(DC_EXEC)'
737else
738	bc_test_exec='$(BC_EXEC)'
739	dc_test_exec='$(DC_EXEC)'
740fi
741
742karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
743karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
744
745bc_lib="\$(GEN_DIR)/lib.o"
746bc_help="\$(GEN_DIR)/bc_help.o"
747dc_help="\$(GEN_DIR)/dc_help.o"
748
749default_target_prereqs="\$(BIN) \$(OBJS)"
750default_target_cmd="\$(CC) \$(CFLAGS) \$(OBJS) \$(LDFLAGS) -o \$(EXEC)"
751default_target="\$(DC_EXEC)"
752
753second_target_prereqs=""
754second_target_cmd="$default_target_cmd"
755second_target="\$(BC_EXEC)"
756
757if [ "$library" -ne 0 ]; then
758
759	extra_math=1
760	nls=0
761	hist=0
762	prompt=0
763	bc=1
764	dc=1
765
766	default_target_prereqs="\$(BIN) \$(OBJ)"
767	default_target_cmd="ar -r -cu \$(LIBBC) \$(OBJ)"
768	default_target="\$(LIBBC)"
769	tests="test_library"
770
771elif [ "$bc_only" -eq 1 ]; then
772
773	bc=1
774	dc=0
775
776	dc_help=""
777
778	executables="bc"
779
780	dc_test="@printf 'No dc tests to run\\\\n'"
781
782	install_prereqs=" install_execs"
783	install_man_prereqs=" install_bc_manpage"
784	uninstall_prereqs=" uninstall_bc"
785	uninstall_man_prereqs=" uninstall_bc_manpage"
786
787	default_target="\$(BC_EXEC)"
788	second_target="\$(DC_EXEC)"
789	tests="test_bc timeconst"
790
791elif [ "$dc_only" -eq 1 ]; then
792
793	bc=0
794	dc=1
795
796	bc_lib=""
797	bc_help=""
798
799	executables="dc"
800
801	main_exec="DC"
802	executable="DC_EXEC"
803
804	bc_test="@printf 'No bc tests to run\\\\n'"
805
806	timeconst="@printf 'timeconst cannot be run because bc is not built\\\\n'"
807
808	install_prereqs=" install_execs"
809	install_man_prereqs=" install_dc_manpage"
810	uninstall_prereqs=" uninstall_dc"
811	uninstall_man_prereqs=" uninstall_dc_manpage"
812
813	tests="test_dc"
814
815else
816
817	bc=1
818	dc=1
819
820	executables="bc and dc"
821
822	karatsuba="@\$(KARATSUBA) 30 0 \$(BC_EXEC)"
823	karatsuba_test="@\$(KARATSUBA) 1 100 \$(BC_EXEC)"
824
825	if [ "$library" -eq 0 ]; then
826		install_prereqs=" install_execs"
827		install_man_prereqs=" install_bc_manpage install_dc_manpage"
828		uninstall_prereqs=" uninstall_bc uninstall_dc"
829		uninstall_man_prereqs=" uninstall_bc_manpage uninstall_dc_manpage"
830	else
831		install_prereqs=" install_library install_bcl_header"
832		install_man_prereqs=" install_bcl_manpage"
833		uninstall_prereqs=" uninstall_library uninstall_bcl_header"
834		uninstall_man_prereqs=" uninstall_bcl_manpage"
835		tests="test_library"
836	fi
837
838	second_target_prereqs="$default_target_prereqs"
839	default_target_prereqs="$second_target"
840	default_target_cmd="\$(LINK) \$(BIN) \$(EXEC_PREFIX)\$(DC)"
841
842fi
843
844if [ "$fuzz" -ne 0 ]; then
845	debug=1
846	hist=0
847	prompt=0
848	nls=0
849	optimization="3"
850fi
851
852if [ "$debug" -eq 1 ]; then
853
854	if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then
855		CFLAGS="-O0"
856	fi
857
858	CFLAGS="-g $CFLAGS"
859
860else
861	CPPFLAGS="-DNDEBUG $CPPFLAGS"
862	if [ "$strip_bin" -ne 0 ]; then
863		LDFLAGS="-s $LDFLAGS"
864	fi
865fi
866
867if [ -n "$optimization" ]; then
868	CFLAGS="-O$optimization $CFLAGS"
869fi
870
871if [ "$coverage" -eq 1 ]; then
872
873	if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
874		usage "Can only specify -c without -b or -d"
875	fi
876
877	CFLAGS="-fprofile-arcs -ftest-coverage -g -O0 $CFLAGS"
878	CPPFLAGS="-DNDEBUG $CPPFLAGS"
879
880	COVERAGE_OUTPUT="@gcov -pabcdf \$(GCDA) \$(BC_GCDA) \$(DC_GCDA) \$(HISTORY_GCDA) \$(RAND_GCDA)"
881	COVERAGE_OUTPUT="$COVERAGE_OUTPUT;\$(RM) -f \$(GEN)*.gc*"
882	COVERAGE_OUTPUT="$COVERAGE_OUTPUT;gcovr --html-details --output index.html"
883	COVERAGE_PREREQS=" test coverage_output"
884
885else
886	COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'"
887	COVERAGE_PREREQS=""
888fi
889
890if [ -z "${DESTDIR+set}" ]; then
891	destdir=""
892else
893	destdir="DESTDIR = $DESTDIR"
894fi
895
896if [ -z "${PREFIX+set}" ]; then
897	PREFIX="/usr/local"
898fi
899
900if [ -z "${BINDIR+set}" ]; then
901	BINDIR="$PREFIX/bin"
902fi
903
904if [ -z "${INCLUDEDIR+set}" ]; then
905	INCLUDEDIR="$PREFIX/include"
906fi
907
908if [ -z "${LIBDIR+set}" ]; then
909	LIBDIR="$PREFIX/lib"
910fi
911
912if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then
913	if [ -z "${DATAROOTDIR+set}" ]; then
914		DATAROOTDIR="$PREFIX/share"
915	fi
916fi
917
918if [ "$install_manpages" -ne 0 ]; then
919
920	if [ -z "${DATADIR+set}" ]; then
921		DATADIR="$DATAROOTDIR"
922	fi
923
924	if [ -z "${MANDIR+set}" ]; then
925		MANDIR="$DATADIR/man"
926	fi
927
928	if [ -z "${MAN1DIR+set}" ]; then
929		MAN1DIR="$MANDIR/man1"
930	fi
931
932	if [ -z "${MAN3DIR+set}" ]; then
933		MAN3DIR="$MANDIR/man3"
934	fi
935
936else
937	install_man_prereqs=""
938	uninstall_man_prereqs=""
939fi
940
941if [ "$nls" -ne 0 ]; then
942
943	set +e
944
945	printf 'Testing NLS...\n'
946
947	flags="-DBC_ENABLE_NLS=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
948	flags="$flags -DBC_ENABLE_HISTORY=$hist"
949	flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I./include/"
950	flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
951
952	"$CC" $CPPFLAGS $CFLAGS $flags -c "src/vm.c" -o "$scriptdir/vm.o" > /dev/null 2>&1
953
954	err="$?"
955
956	rm -rf "$scriptdir/vm.o"
957
958	# If this errors, it is probably because of building on Windows,
959	# and NLS is not supported on Windows, so disable it.
960	if [ "$err" -ne 0 ]; then
961		printf 'NLS does not work.\n'
962		if [ $force -eq 0 ]; then
963			printf 'Disabling NLS...\n\n'
964			nls=0
965		else
966			printf 'Forcing NLS...\n\n'
967		fi
968	else
969		printf 'NLS works.\n\n'
970
971		printf 'Testing gencat...\n'
972		gencat "$scriptdir/en_US.cat" "$scriptdir/locales/en_US.msg" > /dev/null 2>&1
973
974		err="$?"
975
976		rm -rf "$scriptdir/en_US.cat"
977
978		if [ "$err" -ne 0 ]; then
979			printf 'gencat does not work.\n'
980			if [ $force -eq 0 ]; then
981				printf 'Disabling NLS...\n\n'
982				nls=0
983			else
984				printf 'Forcing NLS...\n\n'
985			fi
986		else
987
988			printf 'gencat works.\n\n'
989
990			if [ "$HOSTCC" != "$CC" ]; then
991				printf 'Cross-compile detected.\n\n'
992				printf 'WARNING: Catalog files generated with gencat may not be portable\n'
993				printf '         across different architectures.\n\n'
994			fi
995
996			if [ -z "$NLSPATH" ]; then
997				NLSPATH="/usr/share/locale/%L/%N"
998			fi
999
1000			install_locales_prereqs=" install_locales"
1001			uninstall_locales_prereqs=" uninstall_locales"
1002
1003		fi
1004
1005	fi
1006
1007	set -e
1008
1009else
1010	install_locales_prereqs=""
1011	uninstall_locales_prereqs=""
1012	all_locales=0
1013fi
1014
1015if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then
1016	install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
1017else
1018	install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
1019fi
1020
1021if [ "$hist" -eq 1 ]; then
1022
1023	set +e
1024
1025	printf 'Testing history...\n'
1026
1027	flags="-DBC_ENABLE_HISTORY=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
1028	flags="$flags -DBC_ENABLE_NLS=$nls -DBC_ENABLE_LIBRARY=0"
1029	flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I./include/"
1030	flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
1031
1032	"$CC" $CPPFLAGS $CFLAGS $flags -c "src/history.c" -o "$scriptdir/history.o" > /dev/null 2>&1
1033
1034	err="$?"
1035
1036	rm -rf "$scriptdir/history.o"
1037
1038	# If this errors, it is probably because of building on Windows,
1039	# and history is not supported on Windows, so disable it.
1040	if [ "$err" -ne 0 ]; then
1041		printf 'History does not work.\n'
1042		if [ $force -eq 0 ]; then
1043			printf 'Disabling history...\n\n'
1044			hist=0
1045		else
1046			printf 'Forcing history...\n\n'
1047		fi
1048	else
1049		printf 'History works.\n\n'
1050	fi
1051
1052	set -e
1053
1054fi
1055
1056if [ "$library" -eq 1 ]; then
1057	bc_lib=""
1058fi
1059
1060if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ] && [ "$library" -eq 0 ]; then
1061	BC_LIB2_O="\$(GEN_DIR)/lib2.o"
1062else
1063	BC_LIB2_O=""
1064fi
1065
1066GEN="strgen"
1067GEN_EXEC_TARGET="\$(HOSTCC) \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)"
1068CLEAN_PREREQS=" clean_gen"
1069
1070if [ -z "${GEN_HOST+set}" ]; then
1071	GEN_HOST=1
1072else
1073	if [ "$GEN_HOST" -eq 0 ]; then
1074		GEN="strgen.sh"
1075		GEN_EXEC_TARGET="@printf 'Do not need to build gen/strgen.c\\\\n'"
1076		CLEAN_PREREQS=""
1077	fi
1078fi
1079
1080manpage_args=""
1081unneeded=""
1082headers="\$(HEADERS)"
1083
1084if [ "$extra_math" -eq 0 ]; then
1085	manpage_args="E"
1086	unneeded="$unneeded rand.c"
1087else
1088	headers="$headers \$(EXTRA_MATH_HEADERS)"
1089fi
1090
1091if [ "$hist" -eq 0 ]; then
1092	manpage_args="${manpage_args}H"
1093	unneeded="$unneeded history.c"
1094else
1095	headers="$headers \$(HISTORY_HEADERS)"
1096fi
1097
1098if [ "$nls" -eq 0 ]; then
1099	manpage_args="${manpage_args}N"
1100fi
1101
1102if [ "$prompt" -eq 0 ]; then
1103	manpage_args="${manpage_args}P"
1104fi
1105
1106if [ "$bc" -eq 0 ]; then
1107	unneeded="$unneeded bc.c bc_lex.c bc_parse.c"
1108else
1109	headers="$headers \$(BC_HEADERS)"
1110fi
1111
1112if [ "$dc" -eq 0 ]; then
1113	unneeded="$unneeded dc.c dc_lex.c dc_parse.c"
1114else
1115	headers="$headers \$(DC_HEADERS)"
1116fi
1117
1118if [ "$library" -ne 0 ]; then
1119	unneeded="$unneeded args.c opt.c read.c file.c main.c"
1120	unneeded="$unneeded lang.c lex.c parse.c program.c"
1121	unneeded="$unneeded bc.c bc_lex.c bc_parse.c"
1122	unneeded="$unneeded dc.c dc_lex.c dc_parse.c"
1123	headers="$headers \$(LIBRARY_HEADERS)"
1124else
1125	unneeded="$unneeded library.c"
1126fi
1127
1128if [ "$manpage_args" = "" ]; then
1129	manpage_args="A"
1130fi
1131
1132if [ "$vg" -ne 0 ]; then
1133	memcheck=1
1134fi
1135
1136bc_tests=$(gen_test_targets bc)
1137bc_script_tests=$(gen_script_test_targets bc)
1138dc_tests=$(gen_test_targets dc)
1139dc_script_tests=$(gen_script_test_targets dc)
1140
1141# Print out the values; this is for debugging.
1142if [ "$bc" -ne 0 ]; then
1143	printf 'Building bc\n'
1144else
1145	printf 'Not building bc\n'
1146fi
1147if [ "$dc" -ne 0 ]; then
1148	printf 'Building dc\n'
1149else
1150	printf 'Not building dc\n'
1151fi
1152printf '\n'
1153printf 'BC_ENABLE_LIBRARY=%s\n\n' "$library"
1154printf 'BC_ENABLE_HISTORY=%s\n' "$hist"
1155printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math"
1156printf 'BC_ENABLE_NLS=%s\n' "$nls"
1157printf 'BC_ENABLE_PROMPT=%s\n' "$prompt"
1158printf 'BC_ENABLE_AFL=%s\n' "$fuzz"
1159printf '\n'
1160printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len"
1161printf '\n'
1162printf 'CC=%s\n' "$CC"
1163printf 'CFLAGS=%s\n' "$CFLAGS"
1164printf 'HOSTCC=%s\n' "$HOSTCC"
1165printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS"
1166printf 'CPPFLAGS=%s\n' "$CPPFLAGS"
1167printf 'LDFLAGS=%s\n' "$LDFLAGS"
1168printf 'PREFIX=%s\n' "$PREFIX"
1169printf 'BINDIR=%s\n' "$BINDIR"
1170printf 'INCLUDEDIR=%s\n' "$INCLUDEDIR"
1171printf 'LIBDIR=%s\n' "$LIBDIR"
1172printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR"
1173printf 'DATADIR=%s\n' "$DATADIR"
1174printf 'MANDIR=%s\n' "$MANDIR"
1175printf 'MAN1DIR=%s\n' "$MAN1DIR"
1176printf 'MAN3DIR=%s\n' "$MAN3DIR"
1177printf 'NLSPATH=%s\n' "$NLSPATH"
1178printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX"
1179printf 'EXECPREFIX=%s\n' "$EXECPREFIX"
1180printf 'DESTDIR=%s\n' "$DESTDIR"
1181printf 'LONG_BIT=%s\n' "$LONG_BIT"
1182printf 'GEN_HOST=%s\n' "$GEN_HOST"
1183printf 'GEN_EMU=%s\n' "$GEN_EMU"
1184
1185contents=$(cat "$scriptdir/Makefile.in")
1186
1187needle="WARNING"
1188replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***'
1189
1190contents=$(replace "$contents" "$needle" "$replacement")
1191
1192if [ "$unneeded" = "" ]; then
1193	unneeded="library.c"
1194fi
1195
1196contents=$(gen_file_list "$contents" $unneeded)
1197
1198SRC_TARGETS=""
1199
1200src_files=$(find_src_files $unneeded)
1201
1202for f in $src_files; do
1203	o=$(replace_ext "$f" "c" "o")
1204	SRC_TARGETS=$(printf '%s\n\n%s: %s %s\n\t$(CC) $(CFLAGS) -o %s -c %s\n' \
1205		"$SRC_TARGETS" "$o" "$headers" "$f" "$o" "$f")
1206done
1207
1208contents=$(replace "$contents" "HEADERS" "$headers")
1209
1210contents=$(replace "$contents" "BC_ENABLED" "$bc")
1211contents=$(replace "$contents" "DC_ENABLED" "$dc")
1212
1213contents=$(replace "$contents" "BC_ALL_TESTS" "$bc_test")
1214contents=$(replace "$contents" "BC_TESTS" "$bc_tests")
1215contents=$(replace "$contents" "BC_SCRIPT_TESTS" "$bc_script_tests")
1216contents=$(replace "$contents" "BC_TEST_EXEC" "$bc_test_exec")
1217contents=$(replace "$contents" "TIMECONST_ALL_TESTS" "$timeconst")
1218
1219contents=$(replace "$contents" "DC_ALL_TESTS" "$dc_test")
1220contents=$(replace "$contents" "DC_TESTS" "$dc_tests")
1221contents=$(replace "$contents" "DC_SCRIPT_TESTS" "$dc_script_tests")
1222contents=$(replace "$contents" "DC_TEST_EXEC" "$dc_test_exec")
1223
1224contents=$(replace "$contents" "BUILD_TYPE" "$manpage_args")
1225
1226contents=$(replace "$contents" "LIBRARY" "$library")
1227contents=$(replace "$contents" "HISTORY" "$hist")
1228contents=$(replace "$contents" "EXTRA_MATH" "$extra_math")
1229contents=$(replace "$contents" "NLS" "$nls")
1230contents=$(replace "$contents" "PROMPT" "$prompt")
1231contents=$(replace "$contents" "FUZZ" "$fuzz")
1232contents=$(replace "$contents" "MEMCHECK" "$memcheck")
1233
1234contents=$(replace "$contents" "BC_LIB_O" "$bc_lib")
1235contents=$(replace "$contents" "BC_HELP_O" "$bc_help")
1236contents=$(replace "$contents" "DC_HELP_O" "$dc_help")
1237contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O")
1238contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len")
1239
1240contents=$(replace "$contents" "NLSPATH" "$NLSPATH")
1241contents=$(replace "$contents" "DESTDIR" "$destdir")
1242contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX")
1243contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX")
1244contents=$(replace "$contents" "BINDIR" "$BINDIR")
1245contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR")
1246contents=$(replace "$contents" "LIBDIR" "$LIBDIR")
1247contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR")
1248contents=$(replace "$contents" "MAN3DIR" "$MAN3DIR")
1249contents=$(replace "$contents" "CFLAGS" "$CFLAGS")
1250contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS")
1251contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS")
1252contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS")
1253contents=$(replace "$contents" "CC" "$CC")
1254contents=$(replace "$contents" "HOSTCC" "$HOSTCC")
1255contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT")
1256contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS")
1257contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs")
1258contents=$(replace "$contents" "INSTALL_MAN_PREREQS" "$install_man_prereqs")
1259contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales")
1260contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs")
1261contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs")
1262contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs")
1263contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs")
1264
1265contents=$(replace "$contents" "DEFAULT_TARGET" "$default_target")
1266contents=$(replace "$contents" "DEFAULT_TARGET_PREREQS" "$default_target_prereqs")
1267contents=$(replace "$contents" "DEFAULT_TARGET_CMD" "$default_target_cmd")
1268contents=$(replace "$contents" "SECOND_TARGET" "$second_target")
1269contents=$(replace "$contents" "SECOND_TARGET_PREREQS" "$second_target_prereqs")
1270contents=$(replace "$contents" "SECOND_TARGET_CMD" "$second_target_cmd")
1271
1272contents=$(replace "$contents" "ALL_PREREQ" "$ALL_PREREQ")
1273contents=$(replace "$contents" "BC_EXEC_PREREQ" "$bc_exec_prereq")
1274contents=$(replace "$contents" "BC_EXEC_CMD" "$bc_exec_cmd")
1275contents=$(replace "$contents" "DC_EXEC_PREREQ" "$dc_exec_prereq")
1276contents=$(replace "$contents" "DC_EXEC_CMD" "$dc_exec_cmd")
1277
1278contents=$(replace "$contents" "EXECUTABLES" "$executables")
1279contents=$(replace "$contents" "MAIN_EXEC" "$main_exec")
1280contents=$(replace "$contents" "EXEC" "$executable")
1281contents=$(replace "$contents" "TESTS" "$tests")
1282
1283contents=$(replace "$contents" "BC_TEST" "$bc_test")
1284contents=$(replace "$contents" "DC_TEST" "$dc_test")
1285
1286contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test")
1287contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test")
1288
1289contents=$(replace "$contents" "TIMECONST" "$timeconst")
1290
1291contents=$(replace "$contents" "KARATSUBA" "$karatsuba")
1292contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test")
1293
1294contents=$(replace "$contents" "LONG_BIT" "$LONG_BIT")
1295contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE")
1296
1297contents=$(replace "$contents" "GEN" "$GEN")
1298contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET")
1299contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS")
1300contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU")
1301
1302printf '%s\n%s\n\n' "$contents" "$SRC_TARGETS" > "$scriptdir/Makefile"
1303
1304if [ "$bc" -ne 0 ]; then
1305	gen_tests bc BC "$extra_math" "$time_tests" $bc_test_exec
1306	gen_script_tests bc "$extra_math" "$generate_tests" "$time_tests" $bc_test_exec
1307fi
1308
1309if [ "$dc" -ne 0 ]; then
1310	gen_tests dc DC "$extra_math" "$time_tests" $dc_test_exec
1311	gen_script_tests dc "$extra_math" "$generate_tests" "$time_tests" $dc_test_exec
1312fi
1313
1314cd "$scriptdir"
1315
1316cp -f manuals/bc/$manpage_args.1.md manuals/bc.1.md
1317cp -f manuals/bc/$manpage_args.1 manuals/bc.1
1318cp -f manuals/dc/$manpage_args.1.md manuals/dc.1.md
1319cp -f manuals/dc/$manpage_args.1 manuals/dc.1
1320
1321make clean > /dev/null
1322