xref: /freebsd/contrib/bc/configure (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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
34builddir=$(pwd)
35
36. "$scriptdir/scripts/functions.sh"
37
38# Simply prints the help message and quits based on the argument.
39# @param val  The value to pass to exit. Must be an integer.
40usage() {
41
42	if [ $# -gt 0 ]; then
43
44		_usage_val=1
45
46		printf "%s\n\n" "$1"
47
48	else
49		_usage_val=0
50	fi
51
52	printf 'usage:\n'
53	printf '    %s -h\n' "$script"
54	printf '    %s --help\n' "$script"
55	printf '    %s [-a|-bD|-dB|-c] [-CEfgGHlmMNtTvz] [-O OPT_LEVEL] [-k KARATSUBA_LEN]\\\n' "$script"
56	printf '       [-s SETTING] [-S SETTING]\n'
57	printf '    %s \\\n' "$script"
58	printf '       [--library|--bc-only --disable-dc|--dc-only --disable-bc|--coverage]  \\\n'
59	printf '       [--force --debug --disable-extra-math --disable-generated-tests]      \\\n'
60	printf '       [--disable-history --disable-man-pages --disable-nls --disable-strip] \\\n'
61	printf '       [--install-all-locales] [--opt=OPT_LEVEL]                             \\\n'
62	printf '       [--karatsuba-len=KARATSUBA_LEN]                                       \\\n'
63	printf '       [--set-default-on=SETTING] [--set-default-off=SETTING]                \\\n'
64	printf '       [--prefix=PREFIX] [--bindir=BINDIR] [--datarootdir=DATAROOTDIR]       \\\n'
65	printf '       [--datadir=DATADIR] [--mandir=MANDIR] [--man1dir=MAN1DIR]             \\\n'
66	printf '\n'
67	printf '    -a, --library\n'
68	printf '        Build the libbcl instead of the programs. This is meant to be used with\n'
69	printf '        Other software like programming languages that want to make use of the\n'
70	printf '        parsing and math capabilities. This option will install headers using\n'
71	printf '        `make install`.\n'
72	printf '    -b, --bc-only\n'
73	printf '        Build bc only. It is an error if "-d", "--dc-only", "-B", or\n'
74	printf '        "--disable-bc" are specified too.\n'
75	printf '    -B, --disable-bc\n'
76	printf '        Disable bc. It is an error if "-b", "--bc-only", "-D", or "--disable-dc"\n'
77	printf '        are specified too.\n'
78	printf '    -c, --coverage\n'
79	printf '        Generate test coverage code. Requires gcov and gcovr.\n'
80	printf '        It is an error if either "-b" ("-D") or "-d" ("-B") is specified.\n'
81	printf '        Requires a compiler that use gcc-compatible coverage options\n'
82	printf '    -C, --disable-clean\n'
83	printf '        Disable the clean that configure.sh does before configure.\n'
84	printf '    -d, --dc-only\n'
85	printf '        Build dc only. It is an error if "-b", "--bc-only", "-D", or\n'
86	printf '        "--disable-dc" are specified too.\n'
87	printf '    -D, --disable-dc\n'
88	printf '        Disable dc. It is an error if "-d", "--dc-only", "-B", or "--disable-bc"\n'
89	printf '        are specified too.\n'
90	printf '    -E, --disable-extra-math\n'
91	printf '        Disable extra math. This includes: "$" operator (truncate to integer),\n'
92	printf '        "@" operator (set number of decimal places), and r(x, p) (rounding\n'
93	printf '        function). Additionally, this option disables the extra printing\n'
94	printf '        functions in the math library.\n'
95	printf '    -f, --force\n'
96	printf '        Force use of all enabled options, even if they do not work. This\n'
97	printf '        option is to allow the maintainer a way to test that certain options\n'
98	printf '        are not failing invisibly. (Development only.)'
99	printf '    -g, --debug\n'
100	printf '        Build in debug mode. Adds the "-g" flag, and if there are no\n'
101	printf '        other CFLAGS, and "-O" was not given, this also adds the "-O0"\n'
102	printf '        flag. If this flag is *not* given, "-DNDEBUG" is added to CPPFLAGS\n'
103	printf '        and a strip flag is added to the link stage.\n'
104	printf '    -G, --disable-generated-tests\n'
105	printf '        Disable generating tests. This is for platforms that do not have a\n'
106	printf '        GNU bc-compatible bc to generate tests.\n'
107	printf '    -h, --help\n'
108	printf '        Print this help message and exit.\n'
109	printf '    -H, --disable-history\n'
110	printf '        Disable history.\n'
111	printf '    -k KARATSUBA_LEN, --karatsuba-len KARATSUBA_LEN\n'
112	printf '        Set the karatsuba length to KARATSUBA_LEN (default is 64).\n'
113	printf '        It is an error if KARATSUBA_LEN is not a number or is less than 16.\n'
114	printf '    -l, --install-all-locales\n'
115	printf '        Installs all locales, regardless of how many are on the system. This\n'
116	printf '        option is useful for package maintainers who want to make sure that\n'
117	printf '        a package contains all of the locales that end users might need.\n'
118	printf '    -m, --enable-memcheck\n'
119	printf '        Enable memcheck mode, to ensure no memory leaks. For development only.\n'
120	printf '    -M, --disable-man-pages\n'
121	printf '        Disable installing manpages.\n'
122	printf '    -N, --disable-nls\n'
123	printf '        Disable POSIX locale (NLS) support.\n'
124	printf '    -O OPT_LEVEL, --opt OPT_LEVEL\n'
125	printf '        Set the optimization level. This can also be included in the CFLAGS,\n'
126	printf '        but it is provided, so maintainers can build optimized debug builds.\n'
127	printf '        This is passed through to the compiler, so it must be supported.\n'
128	printf '    -s SETTING, --set-default-on SETTING\n'
129	printf '        Set the default named by SETTING to on. See below for possible values\n'
130	printf '        for SETTING. For multiple instances of the -s or -S for the the same\n'
131	printf '        setting, the last one is used.\n'
132	printf '    -S SETTING, --set-default-off SETTING\n'
133	printf '        Set the default named by SETTING to off. See below for possible values\n'
134	printf '        for SETTING. For multiple instances of the -s or -S for the the same\n'
135	printf '        setting, the last one is used.\n'
136	printf '    -t, --enable-test-timing\n'
137	printf '        Enable the timing of tests. This is for development only.\n'
138	printf '    -T, --disable-strip\n'
139	printf '        Disable stripping symbols from the compiled binary or binaries.\n'
140	printf '        Stripping symbols only happens when debug mode is off.\n'
141	printf '    -v, --enable-valgrind\n'
142	printf '        Enable a build appropriate for valgrind. For development only.\n'
143	printf '    -z, --enable-fuzz-mode\n'
144	printf '        Enable fuzzing mode. THIS IS FOR DEVELOPMENT ONLY.\n'
145	printf '    --prefix PREFIX\n'
146	printf '        The prefix to install to. Overrides "$PREFIX" if it exists.\n'
147	printf '        If PREFIX is "/usr", install path will be "/usr/bin".\n'
148	printf '        Default is "/usr/local".\n'
149	printf '    --bindir BINDIR\n'
150	printf '        The directory to install binaries in. Overrides "$BINDIR" if it exists.\n'
151	printf '        Default is "$PREFIX/bin".\n'
152	printf '    --includedir INCLUDEDIR\n'
153	printf '        The directory to install headers in. Overrides "$INCLUDEDIR" if it\n'
154	printf '        exists. Default is "$PREFIX/include".\n'
155	printf '    --libdir LIBDIR\n'
156	printf '        The directory to install libraries in. Overrides "$LIBDIR" if it exists.\n'
157	printf '        Default is "$PREFIX/lib".\n'
158	printf '    --datarootdir DATAROOTDIR\n'
159	printf '        The root location for data files. Overrides "$DATAROOTDIR" if it exists.\n'
160	printf '        Default is "$PREFIX/share".\n'
161	printf '    --datadir DATADIR\n'
162	printf '        The location for data files. Overrides "$DATADIR" if it exists.\n'
163	printf '        Default is "$DATAROOTDIR".\n'
164	printf '    --mandir MANDIR\n'
165	printf '        The location to install manpages to. Overrides "$MANDIR" if it exists.\n'
166	printf '        Default is "$DATADIR/man".\n'
167	printf '    --man1dir MAN1DIR\n'
168	printf '        The location to install Section 1 manpages to. Overrides "$MAN1DIR" if\n'
169	printf '        it exists. Default is "$MANDIR/man1".\n'
170	printf '    --man3dir MAN3DIR\n'
171	printf '        The location to install Section 3 manpages to. Overrides "$MAN3DIR" if\n'
172	printf '        it exists. Default is "$MANDIR/man3".\n'
173	printf '\n'
174	printf 'In addition, the following environment variables are used:\n'
175	printf '\n'
176	printf '    CC           C compiler. Must be compatible with POSIX c99. If there is a\n'
177	printf '                 space in the basename of the compiler, the items after the\n'
178	printf '                 first space are assumed to be compiler flags, and in that case,\n'
179	printf '                 the flags are automatically moved into CFLAGS. Default is\n'
180	printf '                 "c99".\n'
181	printf '    HOSTCC       Host C compiler. Must be compatible with POSIX c99. If there is\n'
182	printf '                 a space in the basename of the compiler, the items after the\n'
183	printf '                 first space are assumed to be compiler flags, and in the case,\n'
184	printf '                 the flags are automatically moved into HOSTCFLAGS. Default is\n'
185	printf '                 "$CC".\n'
186	printf '    HOST_CC      Same as HOSTCC. If HOSTCC also exists, it is used.\n'
187	printf '    CFLAGS       C compiler flags.\n'
188	printf '    HOSTCFLAGS   CFLAGS for HOSTCC. Default is "$CFLAGS".\n'
189	printf '    HOST_CFLAGS  Same as HOST_CFLAGS. If HOST_CFLAGS also exists, it is used.\n'
190	printf '    CPPFLAGS     C preprocessor flags. Default is "".\n'
191	printf '    LDFLAGS      Linker flags. Default is "".\n'
192	printf '    PREFIX       The prefix to install to. Default is "/usr/local".\n'
193	printf '                 If PREFIX is "/usr", install path will be "/usr/bin".\n'
194	printf '    BINDIR       The directory to install binaries in. Default is "$PREFIX/bin".\n'
195	printf '    INCLUDEDIR   The directory to install header files in. Default is\n'
196	printf '                 "$PREFIX/include".\n'
197	printf '    LIBDIR       The directory to install libraries in. Default is\n'
198	printf '                 "$PREFIX/lib".\n'
199	printf '    DATAROOTDIR  The root location for data files. Default is "$PREFIX/share".\n'
200	printf '    DATADIR      The location for data files. Default is "$DATAROOTDIR".\n'
201	printf '    MANDIR       The location to install manpages to. Default is "$DATADIR/man".\n'
202	printf '    MAN1DIR      The location to install Section 1 manpages to. Default is\n'
203	printf '                 "$MANDIR/man1".\n'
204	printf '    MAN3DIR      The location to install Section 3 manpages to. Default is\n'
205	printf '                 "$MANDIR/man3".\n'
206	printf '    NLSPATH      The location to install locale catalogs to. Must be an absolute\n'
207	printf '                 path (or contain one). This is treated the same as the POSIX\n'
208	printf '                 definition of $NLSPATH (see POSIX environment variables for\n'
209	printf '                 more information). Default is "/usr/share/locale/%%L/%%N".\n'
210	printf '    PC_PATH      The location to install pkg-config files to. Must be an\n'
211	printf '                 path or contain one. Default is the first path given by the\n'
212	printf '                 output of `pkg-config --variable=pc_path pkg-config`.\n'
213	printf '    EXECSUFFIX   The suffix to append to the executable names, used to not\n'
214	printf '                 interfere with other installed bc executables. Default is "".\n'
215	printf '    EXECPREFIX   The prefix to append to the executable names, used to not\n'
216	printf '                 interfere with other installed bc executables. Default is "".\n'
217	printf '    DESTDIR      For package creation. Default is "". If it is empty when\n'
218	printf '                 `%s` is run, it can also be passed to `make install`\n' "$script"
219	printf '                 later as an environment variable. If both are specified,\n'
220	printf '                 the one given to `%s` takes precedence.\n' "$script"
221	printf '    LONG_BIT     The number of bits in a C `long` type. This is mostly for the\n'
222	printf '                 embedded space since this `bc` uses `long`s internally for\n'
223	printf '                 overflow checking. In C99, a `long` is required to be 32 bits.\n'
224	printf '                 For most normal desktop systems, setting this is unnecessary,\n'
225	printf '                 except that 32-bit platforms with 64-bit longs may want to set\n'
226	printf '                 it to `32`. Default is the default of `LONG_BIT` for the target\n'
227	printf '                 platform. Minimum allowed is `32`. It is a build time error if\n'
228	printf '                 the specified value of `LONG_BIT` is greater than the default\n'
229	printf '                 value of `LONG_BIT` for the target platform.\n'
230	printf '    GEN_HOST     Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to\n'
231	printf '                 produce the C files that contain the help texts as well as the\n'
232	printf '                 math libraries. By default, `gen/strgen.c` is used, compiled by\n'
233	printf '                 "$HOSTCC" and run on the host machine. Using `gen/strgen.sh`\n'
234	printf '                 removes the need to compile and run an executable on the host\n'
235	printf '                 machine since `gen/strgen.sh` is a POSIX shell script. However,\n'
236	printf '                 `gen/lib2.bc` is perilously close to 4095 characters, the max\n'
237	printf '                 supported length of a string literal in C99 (and it could be\n'
238	printf '                 added to in the future), and `gen/strgen.sh` generates a string\n'
239	printf '                 literal instead of an array, as `gen/strgen.c` does. For most\n'
240	printf '                 production-ready compilers, this limit probably is not\n'
241	printf '                 enforced, but it could be. Both options are still available for\n'
242	printf '                 this reason. If you are sure your compiler does not have the\n'
243	printf '                 limit and do not want to compile and run a binary on the host\n'
244	printf '                 machine, set this variable to "0". Any other value, or a\n'
245	printf '                 non-existent value, will cause the build system to compile and\n'
246	printf '                 run `gen/strgen.c`. Default is "".\n'
247	printf '    GEN_EMU      Emulator to run string generator code under (leave empty if not\n'
248	printf '                 necessary). This is not necessary when using `gen/strgen.sh`.\n'
249	printf '                 Default is "".\n'
250	printf '\n'
251	printf 'WARNING: even though `configure.sh` supports both option types, short and\n'
252	printf 'long, it does not support handling both at the same time. Use only one type.\n'
253	printf '\n'
254	printf 'Settings\n'
255	printf '========\n'
256	printf '\n'
257	printf 'bc and dc have some settings that, while they cannot be removed by build time\n'
258	printf 'options, can have their defaults changed at build time by packagers. Users are\n'
259	printf 'also able to change each setting with environment variables.\n'
260	printf '\n'
261	printf 'The following is a table of settings, along with their default values and the\n'
262	printf 'environment variables users can use to change them. (For the defaults, non-zero\n'
263	printf 'means on, and zero means off.)\n'
264	printf '\n'
265	printf '| Setting         | Description          | Default      | Env Variable         |\n'
266	printf '| =============== | ==================== | ============ | ==================== |\n'
267	printf '| bc.banner       | Whether to display   |            0 | BC_BANNER            |\n'
268	printf '|                 | the bc version       |              |                      |\n'
269	printf '|                 | banner when in       |              |                      |\n'
270	printf '|                 | interactive mode.    |              |                      |\n'
271	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
272	printf '| bc.sigint_reset | Whether SIGINT will  |            1 | BC_SIGINT_RESET      |\n'
273	printf '|                 | reset bc, instead of |              |                      |\n'
274	printf '|                 | exiting, when in     |              |                      |\n'
275	printf '|                 | interactive mode.    |              |                      |\n'
276	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
277	printf '| dc.sigint_reset | Whether SIGINT will  |            1 | DC_SIGINT_RESET      |\n'
278	printf '|                 | reset dc, instead of |              |                      |\n'
279	printf '|                 | exiting, when in     |              |                      |\n'
280	printf '|                 | interactive mode.    |              |                      |\n'
281	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
282	printf '| bc.tty_mode     | Whether TTY mode for |            1 | BC_TTY_MODE          |\n'
283	printf '|                 | bc should be on when |              |                      |\n'
284	printf '|                 | available.           |              |                      |\n'
285	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
286	printf '| dc.tty_mode     | Whether TTY mode for |            0 | BC_TTY_MODE          |\n'
287	printf '|                 | dc should be on when |              |                      |\n'
288	printf '|                 | available.           |              |                      |\n'
289	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
290	printf '| bc.prompt       | Whether the prompt   | $BC_TTY_MODE | BC_PROMPT            |\n'
291	printf '|                 | for bc should be on  |              |                      |\n'
292	printf '|                 | in tty mode.         |              |                      |\n'
293	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
294	printf '| dc.prompt       | Whether the prompt   | $DC_TTY_MODE | DC_PROMPT            |\n'
295	printf '|                 | for dc should be on  |              |                      |\n'
296	printf '|                 | in tty mode.         |              |                      |\n'
297	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
298	printf '| bc.expr_exit    | Whether to exit bc   |            1 | BC_EXPR_EXIT         |\n'
299	printf '|                 | if an expression or  |              |                      |\n'
300	printf '|                 | expression file is   |              |                      |\n'
301	printf '|                 | given with the -e or |              |                      |\n'
302	printf '|                 | -f options.          |              |                      |\n'
303	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
304	printf '| dc.expr_exit    | Whether to exit dc   |            1 | DC_EXPR_EXIT         |\n'
305	printf '|                 | if an expression or  |              |                      |\n'
306	printf '|                 | expression file is   |              |                      |\n'
307	printf '|                 | given with the -e or |              |                      |\n'
308	printf '|                 | -f options.          |              |                      |\n'
309	printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
310	printf '\n'
311	printf 'These settings are not meant to be changed on a whim. They are meant to ensure\n'
312	printf 'that this bc and dc will conform to the expectations of the user on each\n'
313	printf 'platform.\n'
314
315	exit "$_usage_val"
316}
317
318# Replaces a file extension in a filename. This is used mostly to turn filenames
319# like `src/num.c` into `src/num.o`. In other words, it helps to link targets to
320# the files they depend on.
321#
322# @param file  The filename.
323# @param ext1  The extension to replace.
324# @param ext2  The new extension.
325replace_ext() {
326
327	if [ "$#" -ne 3 ]; then
328		err_exit "Invalid number of args to $0"
329	fi
330
331	_replace_ext_file="$1"
332	_replace_ext_ext1="$2"
333	_replace_ext_ext2="$3"
334
335	_replace_ext_result="${_replace_ext_file%.$_replace_ext_ext1}.$_replace_ext_ext2"
336
337	printf '%s\n' "$_replace_ext_result"
338}
339
340# Replaces a file extension in every filename given in a list. The list is just
341# a space-separated list of words, so filenames are expected to *not* have
342# spaces in them. See the documentation for `replace_ext()`.
343#
344# @param files  The list of space-separated filenames to replace extensions for.
345# @param ext1   The extension to replace.
346# @param ext2   The new extension.
347replace_exts() {
348
349	if [ "$#" -ne 3 ]; then
350		err_exit "Invalid number of args to $0"
351	fi
352
353	_replace_exts_files="$1"
354	_replace_exts_ext1="$2"
355	_replace_exts_ext2="$3"
356
357	for _replace_exts_file in $_replace_exts_files; do
358		_replace_exts_new_name=$(replace_ext "$_replace_exts_file" "$_replace_exts_ext1" "$_replace_exts_ext2")
359		_replace_exts_result="$_replace_exts_result $_replace_exts_new_name"
360	done
361
362	printf '%s\n' "$_replace_exts_result"
363}
364
365# Finds a placeholder in @a str and replaces it. This is the workhorse of
366# configure.sh. It's what replaces placeholders in Makefile.in with the data
367# needed for the chosen build. Below, you will see a lot of calls to this
368# function.
369#
370# Note that needle can never contain an exclamation point. For more information,
371# see substring_replace() in scripts/functions.sh.
372#
373# @param str          The string to find and replace placeholders in.
374# @param needle       The placeholder name.
375# @param replacement  The string to use to replace the placeholder.
376replace() {
377
378	if [ "$#" -ne 3 ]; then
379		err_exit "Invalid number of args to $0"
380	fi
381
382	_replace_str="$1"
383	_replace_needle="$2"
384	_replace_replacement="$3"
385
386	substring_replace "$_replace_str" "%%$_replace_needle%%" "$_replace_replacement"
387}
388
389# This function finds all the source files that need to be built. If there is
390# only one argument and it is empty, then all source files are built. Otherwise,
391# the arguments are all assumed to be source files that should *not* be built.
392find_src_files() {
393
394	_find_src_files_args=""
395
396	if [ "$#" -ge 1 ] && [ "$1" != "" ]; then
397
398		while [ "$#" -ge 1 ]; do
399			_find_src_files_a="${1## }"
400			shift
401			_find_src_files_args=$(printf '%s\n%s/src/%s\n' "$_find_src_files_args" "$scriptdir" "${_find_src_files_a}")
402		done
403
404	fi
405
406	_find_src_files_files=$(find "$scriptdir/src/" -depth -name "*.c" -print)
407
408	_find_src_files_result=""
409
410	for _find_src_files_f in $_find_src_files_files; do
411
412		# If this is true, the file is part of args, and therefore, unneeded.
413		if [ "${_find_src_files_args##*$_find_src_files_f}" != "${_find_src_files_args}" ]; then
414			continue
415		fi
416
417		_find_src_files_result=$(printf '%s\n%s\n' "$_find_src_files_result" "$_find_src_files_f")
418
419	done
420
421	printf '%s\n' "$_find_src_files_result"
422}
423
424# This function generates a list of files to go into the Makefile. It generates
425# the list of object files, as well as the list of test coverage files.
426#
427# @param contents  The contents of the Makefile template to put the list of
428#                  files into.
429gen_file_list() {
430
431	if [ "$#" -lt 1 ]; then
432		err_exit "Invalid number of args to $0"
433	fi
434
435	_gen_file_list_contents="$1"
436	shift
437
438	if [ "$#" -ge 1 ]; then
439		_gen_file_list_unneeded="$@"
440	else
441		_gen_file_list_unneeded=""
442	fi
443
444	_gen_file_list_needle_src="SRC"
445	_gen_file_list_needle_obj="OBJ"
446	_gen_file_list_needle_gcda="GCDA"
447	_gen_file_list_needle_gcno="GCNO"
448
449	_gen_file_list_replacement=$(find_src_files $_gen_file_list_unneeded | tr '\n' ' ')
450	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
451		"$_gen_file_list_needle_src" "$_gen_file_list_replacement")
452
453	_gen_file_list_cbases=""
454
455	for _gen_file_list_f in $_gen_file_list_replacement; do
456		_gen_file_list_b=$(basename "$_gen_file_list_f")
457		_gen_file_list_cbases="$_gen_file_list_cbases src/$_gen_file_list_b"
458	done
459
460	_gen_file_list_replacement=$(replace_exts "$_gen_file_list_cbases" "c" "o")
461	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
462		"$_gen_file_list_needle_obj" "$_gen_file_list_replacement")
463
464	_gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "o" "gcda")
465	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
466		"$_gen_file_list_needle_gcda" "$_gen_file_list_replacement")
467
468	_gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "gcda" "gcno")
469	_gen_file_list_contents=$(replace "$_gen_file_list_contents" \
470		"$_gen_file_list_needle_gcno" "$_gen_file_list_replacement")
471
472	printf '%s\n' "$_gen_file_list_contents"
473}
474
475# Generates the proper test targets for each test to have its own target. This
476# allows `make test` to run in parallel.
477#
478# @param name        Which calculator to generate tests for.
479# @param extra_math  An integer that, if non-zero, activates extra math tests.
480# @param time_tests  An integer that, if non-zero, tells the test suite to time
481#                    the execution of each test.
482gen_std_tests() {
483
484	_gen_std_tests_name="$1"
485	shift
486
487	_gen_std_tests_extra_math="$1"
488	shift
489
490	_gen_std_tests_time_tests="$1"
491	shift
492
493	_gen_std_tests_extra_required=$(cat "$scriptdir/tests/extra_required.txt")
494
495	for _gen_std_tests_t in $(cat "$scriptdir/tests/$_gen_std_tests_name/all.txt"); do
496
497		if [ "$_gen_std_tests_extra_math" -eq 0 ]; then
498
499			if [ -z "${_gen_std_tests_extra_required##*$_gen_std_tests_t*}" ]; then
500				printf 'test_%s_%s:\n\t@printf "Skipping %s %s\\n"\n\n' \
501					"$_gen_std_tests_name" "$_gen_std_tests_t" "$_gen_std_tests_name" \
502					"$_gen_std_tests_t" >> "Makefile"
503				continue
504			fi
505
506		fi
507
508		printf 'test_%s_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh \$(TESTSDIR)/test.sh %s %s %s %s %s\n\n' \
509			"$_gen_std_tests_name" "$_gen_std_tests_t" "$builddir" "$_gen_std_tests_name" \
510			"$_gen_std_tests_t" "$generate_tests" "$time_tests" \
511			"$*" >> "Makefile"
512
513	done
514}
515
516# Generates a list of test targets that will be used as prerequisites for other
517# targets.
518#
519# @param name  The name of the calculator to generate test targets for.
520gen_std_test_targets() {
521
522	_gen_std_test_targets_name="$1"
523	shift
524
525	_gen_std_test_targets_tests=$(cat "$scriptdir/tests/${_gen_std_test_targets_name}/all.txt")
526
527	for _gen_std_test_targets_t in $_gen_std_test_targets_tests; do
528		printf ' test_%s_%s' "$_gen_std_test_targets_name" "$_gen_std_test_targets_t"
529	done
530
531	printf '\n'
532}
533
534# Generates the proper test targets for each error test to have its own target.
535# This allows `make test_bc_errors` and `make test_dc_errors` to run in
536# parallel.
537#
538# @param name        Which calculator to generate tests for.
539gen_err_tests() {
540
541	_gen_err_tests_name="$1"
542	shift
543
544	_gen_err_tests_fs=$(ls "$scriptdir/tests/$_gen_err_tests_name/errors/")
545
546	for _gen_err_tests_t in $_gen_err_tests_fs; do
547
548		printf 'test_%s_error_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh \$(TESTSDIR)/error.sh %s %s %s\n\n' \
549			"$_gen_err_tests_name" "$_gen_err_tests_t" "$builddir" "$_gen_err_tests_name" \
550			"$_gen_err_tests_t" "$*" >> "Makefile"
551
552	done
553
554}
555
556# Generates a list of error test targets that will be used as prerequisites for
557# other targets.
558#
559# @param name  The name of the calculator to generate test targets for.
560gen_err_test_targets() {
561
562	_gen_err_test_targets_name="$1"
563	shift
564
565	_gen_err_test_targets_tests=$(ls "$scriptdir/tests/$_gen_err_test_targets_name/errors/")
566
567	for _gen_err_test_targets_t in $_gen_err_test_targets_tests; do
568		printf ' test_%s_error_%s' "$_gen_err_test_targets_name" "$_gen_err_test_targets_t"
569	done
570
571	printf '\n'
572}
573
574# Generates the proper script test targets for each script test to have its own
575# target. This allows `make test` to run in parallel.
576#
577# @param name        Which calculator to generate tests for.
578# @param extra_math  An integer that, if non-zero, activates extra math tests.
579# @param generate    An integer that, if non-zero, activates generated tests.
580# @param time_tests  An integer that, if non-zero, tells the test suite to time
581#                    the execution of each test.
582gen_script_tests() {
583
584	_gen_script_tests_name="$1"
585	shift
586
587	_gen_script_tests_extra_math="$1"
588	shift
589
590	_gen_script_tests_generate="$1"
591	shift
592
593	_gen_script_tests_time="$1"
594	shift
595
596	_gen_script_tests_tests=$(cat "$scriptdir/tests/$_gen_script_tests_name/scripts/all.txt")
597
598	for _gen_script_tests_f in $_gen_script_tests_tests; do
599
600		_gen_script_tests_b=$(basename "$_gen_script_tests_f" ".${_gen_script_tests_name}")
601
602		printf 'test_%s_script_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh \$(TESTSDIR)/script.sh %s %s %s 1 %s %s %s\n\n' \
603			"$_gen_script_tests_name" "$_gen_script_tests_b" "$builddir" "$_gen_script_tests_name" \
604			"$_gen_script_tests_f" "$_gen_script_tests_extra_math" "$_gen_script_tests_generate" \
605			"$_gen_script_tests_time" "$*" >> "Makefile"
606	done
607}
608
609set_default() {
610
611	_set_default_on="$1"
612	shift
613
614	_set_default_name="$1"
615	shift
616
617	# The reason that the variables that are being set do not have the same
618	# non-collision avoidance that the other variables do is that we *do* want
619	# the settings of these variables to leak out of the function. They adjust
620	# the settings outside of the function.
621	case "$_set_default_name" in
622
623		bc.banner) bc_default_banner="$_set_default_on" ;;
624		bc.sigint_reset) bc_default_sigint_reset="$_set_default_on" ;;
625		dc.sigint_reset) dc_default_sigint_reset="$_set_default_on" ;;
626		bc.tty_mode) bc_default_tty_mode="$_set_default_on" ;;
627		dc.tty_mode) dc_default_tty_mode="$_set_default_on" ;;
628		bc.prompt) bc_default_prompt="$_set_default_on" ;;
629		dc.prompt) dc_default_prompt="$_set_default_on" ;;
630		bc.expr_exit) bc_default_expr_exit="$_set_default_on";;
631		dc.expr_exit) dc_default_expr_exit="$_set_default_on";;
632		?) usage "Invalid setting: $_set_default_name" ;;
633
634	esac
635}
636
637# Generates a list of script test targets that will be used as prerequisites for
638# other targets.
639#
640# @param name  The name of the calculator to generate script test targets for.
641gen_script_test_targets() {
642
643	_gen_script_test_targets_name="$1"
644	shift
645
646	_gen_script_test_targets_tests=$(cat "$scriptdir/tests/$_gen_script_test_targets_name/scripts/all.txt")
647
648	for _gen_script_test_targets_f in $_gen_script_test_targets_tests; do
649		_gen_script_test_targets_b=$(basename "$_gen_script_test_targets_f" \
650			".$_gen_script_test_targets_name")
651		printf ' test_%s_script_%s' "$_gen_script_test_targets_name" \
652			"$_gen_script_test_targets_b"
653	done
654
655	printf '\n'
656}
657
658# This is a list of defaults, but it is also the list of possible options for
659# users to change.
660#
661# The development options are: force (force options even if they fail), valgrind
662# (build in a way suitable for valgrind testing), memcheck (same as valgrind),
663# and fuzzing (build in a way suitable for fuzzing).
664bc_only=0
665dc_only=0
666coverage=0
667karatsuba_len=32
668debug=0
669hist=1
670extra_math=1
671optimization=""
672generate_tests=1
673install_manpages=1
674nls=1
675force=0
676strip_bin=1
677all_locales=0
678library=0
679fuzz=0
680time_tests=0
681vg=0
682memcheck=0
683clean=1
684
685# The empty strings are because they depend on TTY mode. If they are directly
686# set, though, they will be integers. We test for empty strings later.
687bc_default_banner=0
688bc_default_sigint_reset=1
689dc_default_sigint_reset=1
690bc_default_tty_mode=1
691dc_default_tty_mode=0
692bc_default_prompt=""
693dc_default_prompt=""
694bc_default_expr_exit=1
695dc_default_expr_exit=1
696
697# getopts is a POSIX utility, but it cannot handle long options. Thus, the
698# handling of long options is done by hand, and that's the reason that short and
699# long options cannot be mixed.
700while getopts "abBcdDEfgGhHk:lMmNO:S:s:tTvz-" opt; do
701
702	case "$opt" in
703		a) library=1 ;;
704		b) bc_only=1 ;;
705		B) dc_only=1 ;;
706		c) coverage=1 ;;
707		C) clean=0 ;;
708		d) dc_only=1 ;;
709		D) bc_only=1 ;;
710		E) extra_math=0 ;;
711		f) force=1 ;;
712		g) debug=1 ;;
713		G) generate_tests=0 ;;
714		h) usage ;;
715		H) hist=0 ;;
716		k) karatsuba_len="$OPTARG" ;;
717		l) all_locales=1 ;;
718		m) memcheck=1 ;;
719		M) install_manpages=0 ;;
720		N) nls=0 ;;
721		O) optimization="$OPTARG" ;;
722		S) set_default 0 "$OPTARG" ;;
723		s) set_default 1 "$OPTARG" ;;
724		t) time_tests=1 ;;
725		T) strip_bin=0 ;;
726		v) vg=1 ;;
727		z) fuzz=1 ;;
728		-)
729			arg="$1"
730			arg="${arg#--}"
731			LONG_OPTARG="${arg#*=}"
732			case $arg in
733				help) usage ;;
734				library) library=1 ;;
735				bc-only) bc_only=1 ;;
736				dc-only) dc_only=1 ;;
737				coverage) coverage=1 ;;
738				debug) debug=1 ;;
739				force) force=1 ;;
740				prefix=?*) PREFIX="$LONG_OPTARG" ;;
741				prefix)
742					if [ "$#" -lt 2 ]; then
743						usage "No argument given for '--$arg' option"
744					fi
745					PREFIX="$2"
746					shift ;;
747				bindir=?*) BINDIR="$LONG_OPTARG" ;;
748				bindir)
749					if [ "$#" -lt 2 ]; then
750						usage "No argument given for '--$arg' option"
751					fi
752					BINDIR="$2"
753					shift ;;
754				includedir=?*) INCLUDEDIR="$LONG_OPTARG" ;;
755				includedir)
756					if [ "$#" -lt 2 ]; then
757						usage "No argument given for '--$arg' option"
758					fi
759					INCLUDEDIR="$2"
760					shift ;;
761				libdir=?*) LIBDIR="$LONG_OPTARG" ;;
762				libdir)
763					if [ "$#" -lt 2 ]; then
764						usage "No argument given for '--$arg' option"
765					fi
766					LIBDIR="$2"
767					shift ;;
768				datarootdir=?*) DATAROOTDIR="$LONG_OPTARG" ;;
769				datarootdir)
770					if [ "$#" -lt 2 ]; then
771						usage "No argument given for '--$arg' option"
772					fi
773					DATAROOTDIR="$2"
774					shift ;;
775				datadir=?*) DATADIR="$LONG_OPTARG" ;;
776				datadir)
777					if [ "$#" -lt 2 ]; then
778						usage "No argument given for '--$arg' option"
779					fi
780					DATADIR="$2"
781					shift ;;
782				mandir=?*) MANDIR="$LONG_OPTARG" ;;
783				mandir)
784					if [ "$#" -lt 2 ]; then
785						usage "No argument given for '--$arg' option"
786					fi
787					MANDIR="$2"
788					shift ;;
789				man1dir=?*) MAN1DIR="$LONG_OPTARG" ;;
790				man1dir)
791					if [ "$#" -lt 2 ]; then
792						usage "No argument given for '--$arg' option"
793					fi
794					MAN1DIR="$2"
795					shift ;;
796				man3dir=?*) MAN3DIR="$LONG_OPTARG" ;;
797				man3dir)
798					if [ "$#" -lt 2 ]; then
799						usage "No argument given for '--$arg' option"
800					fi
801					MAN3DIR="$2"
802					shift ;;
803				localedir=?*) LOCALEDIR="$LONG_OPTARG" ;;
804				localedir)
805					if [ "$#" -lt 2 ]; then
806						usage "No argument given for '--$arg' option"
807					fi
808					LOCALEDIR="$2"
809					shift ;;
810				karatsuba-len=?*) karatsuba_len="$LONG_OPTARG" ;;
811				karatsuba-len)
812					if [ "$#" -lt 2 ]; then
813						usage "No argument given for '--$arg' option"
814					fi
815					karatsuba_len="$1"
816					shift ;;
817				opt=?*) optimization="$LONG_OPTARG" ;;
818				opt)
819					if [ "$#" -lt 2 ]; then
820						usage "No argument given for '--$arg' option"
821					fi
822					optimization="$1"
823					shift ;;
824				set-default-on=?*) set_default 1 "$LONG_OPTARG" ;;
825				set-default-on)
826					if [ "$#" -lt 2 ]; then
827						usage "No argument given for '--$arg' option"
828					fi
829					set_default 1 "$1"
830					shift ;;
831				set-default-off=?*) set_default 0 "$LONG_OPTARG" ;;
832				set-default-off)
833					if [ "$#" -lt 2 ]; then
834						usage "No argument given for '--$arg' option"
835					fi
836					set_default 0 "$1"
837					shift ;;
838				disable-bc) dc_only=1 ;;
839				disable-dc) bc_only=1 ;;
840				disable-clean) clean=0 ;;
841				disable-extra-math) extra_math=0 ;;
842				disable-generated-tests) generate_tests=0 ;;
843				disable-history) hist=0 ;;
844				disable-man-pages) install_manpages=0 ;;
845				disable-nls) nls=0 ;;
846				disable-strip) strip_bin=0 ;;
847				enable-test-timing) time_tests=1 ;;
848				enable-valgrind) vg=1 ;;
849				enable-fuzz-mode) fuzz=1 ;;
850				enable-memcheck) memcheck=1 ;;
851				install-all-locales) all_locales=1 ;;
852				help* | bc-only* | dc-only* | coverage* | debug*)
853					usage "No arg allowed for --$arg option" ;;
854				disable-bc* | disable-dc* | disable-clean*)
855					usage "No arg allowed for --$arg option" ;;
856				disable-extra-math*)
857					usage "No arg allowed for --$arg option" ;;
858				disable-generated-tests* | disable-history*)
859					usage "No arg allowed for --$arg option" ;;
860				disable-man-pages* | disable-nls* | disable-strip*)
861					usage "No arg allowed for --$arg option" ;;
862				enable-fuzz-mode* | enable-test-timing* | enable-valgrind*)
863					usage "No arg allowed for --$arg option" ;;
864				enable-memcheck* | install-all-locales*)
865					usage "No arg allowed for --$arg option" ;;
866				'') break ;; # "--" terminates argument processing
867				* ) usage "Invalid option $LONG_OPTARG" ;;
868			esac
869			shift
870			OPTIND=1 ;;
871		?) usage "Invalid option: $opt" ;;
872	esac
873
874done
875
876# Sometimes, developers don't want configure.sh to do a config clean. But
877# sometimes they do.
878if [ "$clean" -ne 0 ]; then
879	if [ -f ./Makefile ]; then
880		make clean_config > /dev/null
881	fi
882fi
883
884# It is an error to say that bc only should be built and likewise for dc.
885if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then
886	usage "Can only specify one of -b(-D) or -d(-B)"
887fi
888
889# The library is mutually exclusive to the calculators, so it's an error to
890# give an option for either of them.
891if [ "$library" -ne 0 ]; then
892	if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
893		usage "Must not specify -b(-D) or -d(-B) when building the library"
894	fi
895fi
896
897# KARATSUBA_LEN must be an integer and must be 16 or greater.
898case $karatsuba_len in
899	(*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;;
900	(*) ;;
901esac
902
903if [ "$karatsuba_len" -lt 16 ]; then
904	usage "KARATSUBA_LEN is less than 16"
905fi
906
907set -e
908
909if [ -z "${LONG_BIT+set}" ]; then
910	LONG_BIT_DEFINE=""
911elif [ "$LONG_BIT" -lt 32 ]; then
912	usage "LONG_BIT is less than 32"
913else
914	LONG_BIT_DEFINE="-DBC_LONG_BIT=\$(BC_LONG_BIT)"
915fi
916
917if [ -z "$CC" ]; then
918	CC="c99"
919else
920
921	# I had users complain that, if they gave CFLAGS as part of CC, which
922	# autotools allows in its braindead way, the build would fail with an error.
923	# I don't like adjusting for autotools, but oh well. These lines puts the
924	# stuff after the first space into CFLAGS.
925	ccbase=$(basename "$CC")
926	suffix=" *"
927	prefix="* "
928
929	if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
930		ccflags="${ccbase#$prefix}"
931		cc="${ccbase%%$suffix}"
932		ccdir=$(dirname "$CC")
933		if [ "$ccdir" = "." ] && [ "${CC#.}" = "$CC" ]; then
934			ccdir=""
935		else
936			ccdir="$ccdir/"
937		fi
938		CC="${ccdir}${cc}"
939		CFLAGS="$CFLAGS $ccflags"
940	fi
941fi
942
943if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then
944	HOSTCC="$CC"
945elif [ -z "$HOSTCC" ]; then
946	HOSTCC="$HOST_CC"
947fi
948
949if [ "$HOSTCC" != "$CC" ]; then
950
951	# Like above, this splits HOSTCC and HOSTCFLAGS.
952	ccbase=$(basename "$HOSTCC")
953	suffix=" *"
954	prefix="* "
955
956	if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
957		ccflags="${ccbase#$prefix}"
958		cc="${ccbase%%$suffix}"
959		ccdir=$(dirname "$HOSTCC")
960		if [ "$ccdir" = "." ] && [ "${HOSTCC#.}" = "$HOSTCC" ]; then
961			ccdir=""
962		else
963			ccdir="$ccdir/"
964		fi
965		HOSTCC="${ccdir}${cc}"
966		HOSTCFLAGS="$HOSTCFLAGS $ccflags"
967	fi
968fi
969
970if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then
971	HOSTCFLAGS="$CFLAGS"
972elif [ -z "${HOSTCFLAGS+set}" ]; then
973	HOSTCFLAGS="$HOST_CFLAGS"
974fi
975
976# Store these for the cross compilation detection later.
977OLDCFLAGS="$CFLAGS"
978OLDHOSTCFLAGS="$HOSTCFLAGS"
979
980link="@printf 'No link necessary\\\\n'"
981main_exec="BC"
982executable="BC_EXEC"
983
984tests="test_bc timeconst test_dc"
985
986bc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh bc $extra_math 1 $generate_tests $time_tests \$(BC_EXEC)"
987bc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n bc $extra_math 1 $generate_tests $time_tests \$(BC_EXEC)"
988dc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh dc $extra_math 1 $generate_tests $time_tests \$(DC_EXEC)"
989dc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n dc $extra_math 1 $generate_tests $time_tests \$(DC_EXEC)"
990
991timeconst="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/bc/timeconst.sh \$(TESTSDIR)/bc/scripts/timeconst.bc \$(BC_EXEC)"
992
993# In order to have cleanup at exit, we need to be in
994# debug mode, so don't run valgrind without that.
995if [ "$vg" -ne 0 ]; then
996	debug=1
997	bc_test_exec='valgrind $(VALGRIND_ARGS) $(BC_EXEC)'
998	dc_test_exec='valgrind $(VALGRIND_ARGS) $(DC_EXEC)'
999else
1000	bc_test_exec='$(BC_EXEC)'
1001	dc_test_exec='$(DC_EXEC)'
1002fi
1003
1004test_bc_history_prereqs="test_bc_history_all"
1005test_dc_history_prereqs="test_dc_history_all"
1006
1007karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
1008karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
1009
1010bc_lib="\$(GEN_DIR)/lib.o"
1011bc_help="\$(GEN_DIR)/bc_help.o"
1012dc_help="\$(GEN_DIR)/dc_help.o"
1013
1014default_target_prereqs="\$(BIN) \$(OBJS)"
1015default_target_cmd="\$(CC) \$(CFLAGS) \$(OBJS) \$(LDFLAGS) -o \$(EXEC)"
1016default_target="\$(DC_EXEC)"
1017
1018second_target_prereqs=""
1019second_target_cmd="$default_target_cmd"
1020second_target="\$(BC_EXEC)"
1021
1022# This if/else if chain is for setting the defaults that change based on whether
1023# the library is being built, bc only, dc only, or both calculators.
1024if [ "$library" -ne 0 ]; then
1025
1026	extra_math=1
1027	nls=0
1028	hist=0
1029	bc=1
1030	dc=1
1031
1032	default_target_prereqs="\$(BIN) \$(OBJ)"
1033	default_target_cmd="ar -r -cu \$(LIBBC) \$(OBJ)"
1034	default_target="\$(LIBBC)"
1035	tests="test_library"
1036	test_bc_history_prereqs=" test_bc_history_skip"
1037	test_dc_history_prereqs=" test_dc_history_skip"
1038
1039	install_prereqs=" install_library"
1040	uninstall_prereqs=" uninstall_library"
1041	install_man_prereqs=" install_bcl_manpage"
1042	uninstall_man_prereqs=" uninstall_bcl_manpage"
1043
1044elif [ "$bc_only" -eq 1 ]; then
1045
1046	bc=1
1047	dc=0
1048
1049	dc_help=""
1050
1051	executables="bc"
1052
1053	dc_test="@printf 'No dc tests to run\\\\n'"
1054	dc_test_np="@printf 'No dc tests to run\\\\n'"
1055	test_dc_history_prereqs=" test_dc_history_skip"
1056
1057	install_prereqs=" install_execs"
1058	install_man_prereqs=" install_bc_manpage"
1059	uninstall_prereqs=" uninstall_bc"
1060	uninstall_man_prereqs=" uninstall_bc_manpage"
1061
1062	default_target="\$(BC_EXEC)"
1063	second_target="\$(DC_EXEC)"
1064	tests="test_bc timeconst"
1065
1066elif [ "$dc_only" -eq 1 ]; then
1067
1068	bc=0
1069	dc=1
1070
1071	bc_lib=""
1072	bc_help=""
1073
1074	executables="dc"
1075
1076	main_exec="DC"
1077	executable="DC_EXEC"
1078
1079	bc_test="@printf 'No bc tests to run\\\\n'"
1080	bc_test_np="@printf 'No bc tests to run\\\\n'"
1081	test_bc_history_prereqs=" test_bc_history_skip"
1082
1083	timeconst="@printf 'timeconst cannot be run because bc is not built\\\\n'"
1084
1085	install_prereqs=" install_execs"
1086	install_man_prereqs=" install_dc_manpage"
1087	uninstall_prereqs=" uninstall_dc"
1088	uninstall_man_prereqs=" uninstall_dc_manpage"
1089
1090	tests="test_dc"
1091
1092else
1093
1094	bc=1
1095	dc=1
1096
1097	executables="bc and dc"
1098
1099	karatsuba="@\$(KARATSUBA) 30 0 \$(BC_EXEC)"
1100	karatsuba_test="@\$(KARATSUBA) 1 100 \$(BC_EXEC)"
1101
1102	if [ "$library" -eq 0 ]; then
1103		install_prereqs=" install_execs"
1104		install_man_prereqs=" install_bc_manpage install_dc_manpage"
1105		uninstall_prereqs=" uninstall_bc uninstall_dc"
1106		uninstall_man_prereqs=" uninstall_bc_manpage uninstall_dc_manpage"
1107	else
1108		install_prereqs=" install_library install_bcl_header"
1109		install_man_prereqs=" install_bcl_manpage"
1110		uninstall_prereqs=" uninstall_library uninstall_bcl_header"
1111		uninstall_man_prereqs=" uninstall_bcl_manpage"
1112		tests="test_library"
1113	fi
1114
1115	second_target_prereqs="$default_target_prereqs"
1116	default_target_prereqs="$second_target"
1117	default_target_cmd="\$(LINK) \$(BIN) \$(EXEC_PREFIX)\$(DC)"
1118
1119fi
1120
1121# We need specific stuff for fuzzing.
1122if [ "$fuzz" -ne 0 ]; then
1123	debug=1
1124	hist=0
1125	nls=0
1126	optimization="3"
1127fi
1128
1129# This sets some necessary things for debug mode.
1130if [ "$debug" -eq 1 ]; then
1131
1132	if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then
1133		CFLAGS="-O0"
1134	fi
1135
1136	CFLAGS="-g $CFLAGS"
1137
1138else
1139
1140	CPPFLAGS="-DNDEBUG $CPPFLAGS"
1141
1142	if [ "$strip_bin" -ne 0 ]; then
1143		LDFLAGS="-s $LDFLAGS"
1144	fi
1145fi
1146
1147# Set optimization CFLAGS.
1148if [ -n "$optimization" ]; then
1149	CFLAGS="-O$optimization $CFLAGS"
1150fi
1151
1152# Set test coverage defaults.
1153if [ "$coverage" -eq 1 ]; then
1154
1155	if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
1156		usage "Can only specify -c without -b or -d"
1157	fi
1158
1159	CFLAGS="-fprofile-arcs -ftest-coverage -g -O0 $CFLAGS"
1160	CPPFLAGS="-DNDEBUG $CPPFLAGS"
1161
1162	COVERAGE_OUTPUT="@gcov -pabcdf \$(GCDA) \$(BC_GCDA) \$(DC_GCDA) \$(HISTORY_GCDA) \$(RAND_GCDA)"
1163	COVERAGE_OUTPUT="$COVERAGE_OUTPUT;\$(RM) -f \$(GEN)*.gc*"
1164	COVERAGE_OUTPUT="$COVERAGE_OUTPUT;gcovr --exclude-unreachable-branches --exclude-throw-branches --html-details --output index.html"
1165	COVERAGE_PREREQS=" test coverage_output"
1166
1167else
1168	COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'"
1169	COVERAGE_PREREQS=""
1170fi
1171
1172
1173# Set some defaults.
1174if [ -z "${DESTDIR+set}" ]; then
1175	destdir=""
1176else
1177	destdir="DESTDIR = $DESTDIR"
1178fi
1179
1180if [ -z "${PREFIX+set}" ]; then
1181	PREFIX="/usr/local"
1182fi
1183
1184if [ -z "${BINDIR+set}" ]; then
1185	BINDIR="$PREFIX/bin"
1186fi
1187
1188if [ -z "${INCLUDEDIR+set}" ]; then
1189	INCLUDEDIR="$PREFIX/include"
1190fi
1191
1192if [ -z "${LIBDIR+set}" ]; then
1193	LIBDIR="$PREFIX/lib"
1194fi
1195
1196if [ -z "${PC_PATH+set}" ]; then
1197
1198	set +e
1199
1200	command -v pkg-config > /dev/null
1201	err=$?
1202
1203	set -e
1204
1205	if [ "$err" -eq 0 ]; then
1206		PC_PATH=$(pkg-config --variable=pc_path pkg-config)
1207		PC_PATH="${PC_PATH%%:*}"
1208	else
1209		PC_PATH=""
1210	fi
1211
1212fi
1213
1214# Set a default for the DATAROOTDIR. This is done if either manpages will be
1215# installed, or locales are enabled because that's probably where NLS_PATH
1216# points.
1217if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then
1218	if [ -z "${DATAROOTDIR+set}" ]; then
1219		DATAROOTDIR="$PREFIX/share"
1220	fi
1221fi
1222
1223# Set defaults for manpage environment variables.
1224if [ "$install_manpages" -ne 0 ]; then
1225
1226	if [ -z "${DATADIR+set}" ]; then
1227		DATADIR="$DATAROOTDIR"
1228	fi
1229
1230	if [ -z "${MANDIR+set}" ]; then
1231		MANDIR="$DATADIR/man"
1232	fi
1233
1234	if [ -z "${MAN1DIR+set}" ]; then
1235		MAN1DIR="$MANDIR/man1"
1236	fi
1237
1238	if [ -z "${MAN3DIR+set}" ]; then
1239		MAN3DIR="$MANDIR/man3"
1240	fi
1241
1242else
1243	install_man_prereqs=""
1244	uninstall_man_prereqs=""
1245fi
1246
1247# Here is where we test NLS (the locale system). This is done by trying to
1248# compile src/vm.c, which has the relevant code. If it fails, then it is
1249# disabled.
1250if [ "$nls" -ne 0 ]; then
1251
1252	set +e
1253
1254	printf 'Testing NLS...\n'
1255
1256	flags="-DBC_ENABLE_NLS=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
1257	flags="$flags -DBC_ENABLE_HISTORY=$hist -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0"
1258	flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/"
1259	flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
1260
1261	"$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/vm.c" -o "./vm.o" > /dev/null 2>&1
1262
1263	err="$?"
1264
1265	rm -rf "./vm.o"
1266
1267	# If this errors, it is probably because of building on Windows,
1268	# and NLS is not supported on Windows, so disable it.
1269	if [ "$err" -ne 0 ]; then
1270		printf 'NLS does not work.\n'
1271		if [ $force -eq 0 ]; then
1272			printf 'Disabling NLS...\n\n'
1273			nls=0
1274		else
1275			printf 'Forcing NLS...\n\n'
1276		fi
1277	else
1278		printf 'NLS works.\n\n'
1279
1280		printf 'Testing gencat...\n'
1281		gencat "./en_US.cat" "$scriptdir/locales/en_US.msg" > /dev/null 2>&1
1282
1283		err="$?"
1284
1285		rm -rf "./en_US.cat"
1286
1287		if [ "$err" -ne 0 ]; then
1288			printf 'gencat does not work.\n'
1289			if [ $force -eq 0 ]; then
1290				printf 'Disabling NLS...\n\n'
1291				nls=0
1292			else
1293				printf 'Forcing NLS...\n\n'
1294			fi
1295		else
1296
1297			printf 'gencat works.\n\n'
1298
1299			# It turns out that POSIX locales are really terrible, and running
1300			# gencat on one machine is not guaranteed to make those cat files
1301			# portable to another machine, so we had better warn the user here.
1302			if [ "$HOSTCC" != "$CC" ] || [ "$OLDHOSTCFLAGS" != "$OLDCFLAGS" ]; then
1303				printf 'Cross-compile detected.\n\n'
1304				printf 'WARNING: Catalog files generated with gencat may not be portable\n'
1305				printf '         across different architectures.\n\n'
1306			fi
1307
1308			if [ -z "$NLSPATH" ]; then
1309				NLSPATH="/usr/share/locale/%L/%N"
1310			fi
1311
1312			install_locales_prereqs=" install_locales"
1313			uninstall_locales_prereqs=" uninstall_locales"
1314
1315		fi
1316
1317	fi
1318
1319	set -e
1320
1321else
1322	install_locales_prereqs=""
1323	uninstall_locales_prereqs=""
1324	all_locales=0
1325fi
1326
1327if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then
1328	install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
1329else
1330	install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
1331fi
1332
1333# Like the above tested locale support, this tests history.
1334if [ "$hist" -eq 1 ]; then
1335
1336	set +e
1337
1338	printf 'Testing history...\n'
1339
1340	flags="-DBC_ENABLE_HISTORY=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
1341	flags="$flags -DBC_ENABLE_NLS=$nls -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0"
1342	flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/"
1343	flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
1344
1345	"$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/history.c" -o "./history.o" > /dev/null 2>&1
1346
1347	err="$?"
1348
1349	rm -rf "./history.o"
1350
1351	# If this errors, it is probably because of building on Windows,
1352	# and history is not supported on Windows, so disable it.
1353	if [ "$err" -ne 0 ]; then
1354		printf 'History does not work.\n'
1355		if [ $force -eq 0 ]; then
1356			printf 'Disabling history...\n\n'
1357			hist=0
1358		else
1359			printf 'Forcing history...\n\n'
1360		fi
1361	else
1362		printf 'History works.\n\n'
1363	fi
1364
1365	set -e
1366
1367fi
1368
1369# We have to disable the history tests if it is disabled or valgrind is on.
1370if [ "$hist" -eq 0 ] || [ "$vg" -ne 0 ]; then
1371	test_bc_history_prereqs=" test_bc_history_skip"
1372	test_dc_history_prereqs=" test_dc_history_skip"
1373	history_tests="@printf 'Skipping history tests...\\\\n'"
1374else
1375	history_tests="@printf '\$(TEST_STARS)\\\\n\\\\nRunning history tests...\\\\n\\\\n' \&\& \$(TESTSDIR)/history.sh bc -a \&\& \$(TESTSDIR)/history.sh dc -a \&\& printf '\\\\nAll history tests passed.\\\\n\\\\n\$(TEST_STARS)\\\\n'"
1376fi
1377
1378# Test OpenBSD. This is not in an if statement because regardless of whatever
1379# the user says, we need to know if we are on OpenBSD to activate _BSD_SOURCE.
1380# No, I cannot `#define _BSD_SOURCE` in a header because OpenBSD's patched GCC
1381# and Clang complain that that is only allowed for system headers. Sigh....So we
1382# have to check at configure time and set it on the compiler command-line. And
1383# we have to set it because we also set _POSIX_C_SOURCE, which OpenBSD headers
1384# detect, and when they detect it, they turn off _BSD_SOURCE unless it is
1385# specifically requested.
1386set +e
1387printf 'Testing for OpenBSD...\n'
1388
1389flags="-DBC_TEST_OPENBSD -DBC_ENABLE_AFL=0"
1390"$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/include/status.h" > /dev/null 2>&1
1391
1392err="$?"
1393
1394if [ "$err" -ne 0 ]; then
1395	printf 'On OpenBSD. Using _BSD_SOURCE.\n\n'
1396	bsd="-D_BSD_SOURCE"
1397else
1398	printf 'Not on OpenBSD.\n\n'
1399	bsd=""
1400fi
1401
1402if [ "$library" -eq 1 ]; then
1403	bc_lib=""
1404fi
1405
1406if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ] && [ "$library" -eq 0 ]; then
1407	BC_LIB2_O="\$(GEN_DIR)/lib2.o"
1408else
1409	BC_LIB2_O=""
1410fi
1411
1412# These lines set the appropriate targets based on whether `gen/strgen.c` or
1413# `gen/strgen.sh` is used.
1414GEN="strgen"
1415GEN_EXEC_TARGET="\$(HOSTCC) \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)"
1416CLEAN_PREREQS=" clean_gen clean_coverage"
1417
1418if [ -z "${GEN_HOST+set}" ]; then
1419	GEN_HOST=1
1420else
1421	if [ "$GEN_HOST" -eq 0 ]; then
1422		GEN="strgen.sh"
1423		GEN_EXEC_TARGET="@printf 'Do not need to build gen/strgen.c\\\\n'"
1424		CLEAN_PREREQS=" clean_coverage"
1425	fi
1426fi
1427
1428manpage_args=""
1429unneeded=""
1430headers="\$(HEADERS)"
1431
1432# This series of if statements figure out what source files are *not* needed.
1433if [ "$extra_math" -eq 0 ]; then
1434	manpage_args="E"
1435	unneeded="$unneeded rand.c"
1436else
1437	headers="$headers \$(EXTRA_MATH_HEADERS)"
1438fi
1439
1440# All of these next if statements set the build type and mark certain source
1441# files as unneeded so that they won't have targets generated for them.
1442
1443if [ "$hist" -eq 0 ]; then
1444	manpage_args="${manpage_args}H"
1445	unneeded="$unneeded history.c"
1446else
1447	headers="$headers \$(HISTORY_HEADERS)"
1448fi
1449
1450if [ "$nls" -eq 0 ]; then
1451	manpage_args="${manpage_args}N"
1452fi
1453
1454if [ "$bc" -eq 0 ]; then
1455	unneeded="$unneeded bc.c bc_lex.c bc_parse.c"
1456else
1457	headers="$headers \$(BC_HEADERS)"
1458fi
1459
1460if [ "$dc" -eq 0 ]; then
1461	unneeded="$unneeded dc.c dc_lex.c dc_parse.c"
1462else
1463	headers="$headers \$(DC_HEADERS)"
1464fi
1465
1466# This convoluted mess does pull the version out. If you change the format of
1467# include/version.h, you may have to change this line.
1468version=$(cat "$scriptdir/include/version.h" | grep "VERSION " - | awk '{ print $3 }' -)
1469
1470if [ "$library" -ne 0 ]; then
1471
1472	unneeded="$unneeded args.c opt.c read.c file.c main.c"
1473	unneeded="$unneeded lang.c lex.c parse.c program.c"
1474	unneeded="$unneeded bc.c bc_lex.c bc_parse.c"
1475	unneeded="$unneeded dc.c dc_lex.c dc_parse.c"
1476	headers="$headers \$(LIBRARY_HEADERS)"
1477
1478	if [ "$PC_PATH" != "" ]; then
1479
1480		contents=$(cat "$scriptdir/bcl.pc.in")
1481
1482		contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR")
1483		contents=$(replace "$contents" "LIBDIR" "$LIBDIR")
1484		contents=$(replace "$contents" "VERSION" "$version")
1485
1486		printf '%s\n' "$contents" > "./bcl.pc"
1487
1488		pkg_config_install="\$(SAFE_INSTALL) \$(PC_INSTALL_ARGS) \"\$(BCL_PC)\" \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\""
1489		pkg_config_uninstall="\$(RM) -f \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\""
1490
1491	else
1492
1493		pkg_config_install=""
1494		pkg_config_uninstall=""
1495
1496	fi
1497
1498else
1499
1500	unneeded="$unneeded library.c"
1501
1502	PC_PATH=""
1503	pkg_config_install=""
1504	pkg_config_uninstall=""
1505
1506fi
1507
1508# library.c is not needed under normal circumstances.
1509if [ "$unneeded" = "" ]; then
1510	unneeded="library.c"
1511fi
1512
1513# This sets the appropriate manpage for a full build.
1514if [ "$manpage_args" = "" ]; then
1515	manpage_args="A"
1516fi
1517
1518if [ "$vg" -ne 0 ]; then
1519	memcheck=1
1520fi
1521
1522if [ "$bc_default_prompt" = "" ]; then
1523	bc_default_prompt="$bc_default_tty_mode"
1524fi
1525
1526if [ "$dc_default_prompt" = "" ]; then
1527	dc_default_prompt="$dc_default_tty_mode"
1528fi
1529
1530# Generate the test targets and prerequisites.
1531bc_tests=$(gen_std_test_targets bc)
1532bc_script_tests=$(gen_script_test_targets bc)
1533bc_err_tests=$(gen_err_test_targets bc)
1534dc_tests=$(gen_std_test_targets dc)
1535dc_script_tests=$(gen_script_test_targets dc)
1536dc_err_tests=$(gen_err_test_targets dc)
1537
1538# Print out the values; this is for debugging.
1539printf 'Version: %s\n' "$version"
1540
1541if [ "$bc" -ne 0 ]; then
1542	printf 'Building bc\n'
1543else
1544	printf 'Not building bc\n'
1545fi
1546if [ "$dc" -ne 0 ]; then
1547	printf 'Building dc\n'
1548else
1549	printf 'Not building dc\n'
1550fi
1551printf '\n'
1552printf 'BC_ENABLE_LIBRARY=%s\n\n' "$library"
1553printf 'BC_ENABLE_HISTORY=%s\n' "$hist"
1554printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math"
1555printf 'BC_ENABLE_NLS=%s\n' "$nls"
1556printf 'BC_ENABLE_AFL=%s\n' "$fuzz"
1557printf '\n'
1558printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len"
1559printf '\n'
1560printf 'CC=%s\n' "$CC"
1561printf 'CFLAGS=%s\n' "$CFLAGS"
1562printf 'HOSTCC=%s\n' "$HOSTCC"
1563printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS"
1564printf 'CPPFLAGS=%s\n' "$CPPFLAGS"
1565printf 'LDFLAGS=%s\n' "$LDFLAGS"
1566printf 'PREFIX=%s\n' "$PREFIX"
1567printf 'BINDIR=%s\n' "$BINDIR"
1568printf 'INCLUDEDIR=%s\n' "$INCLUDEDIR"
1569printf 'LIBDIR=%s\n' "$LIBDIR"
1570printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR"
1571printf 'DATADIR=%s\n' "$DATADIR"
1572printf 'MANDIR=%s\n' "$MANDIR"
1573printf 'MAN1DIR=%s\n' "$MAN1DIR"
1574printf 'MAN3DIR=%s\n' "$MAN3DIR"
1575printf 'NLSPATH=%s\n' "$NLSPATH"
1576printf 'PC_PATH=%s\n' "$PC_PATH"
1577printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX"
1578printf 'EXECPREFIX=%s\n' "$EXECPREFIX"
1579printf 'DESTDIR=%s\n' "$DESTDIR"
1580printf 'LONG_BIT=%s\n' "$LONG_BIT"
1581printf 'GEN_HOST=%s\n' "$GEN_HOST"
1582printf 'GEN_EMU=%s\n' "$GEN_EMU"
1583printf '\n'
1584printf 'Setting Defaults\n'
1585printf '================\n'
1586printf 'bc.banner=%s\n' "$bc_default_banner"
1587printf 'bc.sigint_reset=%s\n' "$bc_default_sigint_reset"
1588printf 'dc.sigint_reset=%s\n' "$dc_default_sigint_reset"
1589printf 'bc.tty_mode=%s\n' "$bc_default_tty_mode"
1590printf 'dc.tty_mode=%s\n' "$dc_default_tty_mode"
1591printf 'bc.prompt=%s\n' "$bc_default_prompt"
1592printf 'dc.prompt=%s\n' "$dc_default_prompt"
1593printf 'bc.expr_exit=%s\n' "$bc_default_expr_exit"
1594printf 'dc.expr_exit=%s\n' "$dc_default_expr_exit"
1595
1596# This is where the real work begins. This is the point at which the Makefile.in
1597# template is edited and output to the Makefile.
1598
1599contents=$(cat "$scriptdir/Makefile.in")
1600
1601needle="WARNING"
1602replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***'
1603
1604contents=$(replace "$contents" "$needle" "$replacement")
1605
1606# The contents are edited to have the list of files to build.
1607contents=$(gen_file_list "$contents" $unneeded)
1608
1609SRC_TARGETS=""
1610
1611# This line and loop generates the individual targets for source files. I used
1612# to just use an implicit target, but that was found to be inadequate when I
1613# added the library.
1614src_files=$(find_src_files $unneeded)
1615
1616for f in $src_files; do
1617	o=$(replace_ext "$f" "c" "o")
1618	o=$(basename "$o")
1619	SRC_TARGETS=$(printf '%s\n\nsrc/%s: src %s %s\n\t$(CC) $(CFLAGS) -o src/%s -c %s\n' \
1620		"$SRC_TARGETS" "$o" "$headers" "$f" "$o" "$f")
1621done
1622
1623# Replace all the placeholders.
1624contents=$(replace "$contents" "ROOTDIR" "$scriptdir")
1625contents=$(replace "$contents" "BUILDDIR" "$builddir")
1626
1627contents=$(replace "$contents" "HEADERS" "$headers")
1628
1629contents=$(replace "$contents" "BC_ENABLED" "$bc")
1630contents=$(replace "$contents" "DC_ENABLED" "$dc")
1631
1632contents=$(replace "$contents" "BC_ALL_TESTS" "$bc_test")
1633contents=$(replace "$contents" "BC_ALL_TESTS_NP" "$bc_test_np")
1634contents=$(replace "$contents" "BC_TESTS" "$bc_tests")
1635contents=$(replace "$contents" "BC_SCRIPT_TESTS" "$bc_script_tests")
1636contents=$(replace "$contents" "BC_ERROR_TESTS" "$bc_err_tests")
1637contents=$(replace "$contents" "BC_TEST_EXEC" "$bc_test_exec")
1638contents=$(replace "$contents" "TIMECONST_ALL_TESTS" "$timeconst")
1639
1640contents=$(replace "$contents" "DC_ALL_TESTS" "$dc_test")
1641contents=$(replace "$contents" "DC_ALL_TESTS_NP" "$dc_test_np")
1642contents=$(replace "$contents" "DC_TESTS" "$dc_tests")
1643contents=$(replace "$contents" "DC_SCRIPT_TESTS" "$dc_script_tests")
1644contents=$(replace "$contents" "DC_ERROR_TESTS" "$dc_err_tests")
1645contents=$(replace "$contents" "DC_TEST_EXEC" "$dc_test_exec")
1646
1647contents=$(replace "$contents" "BUILD_TYPE" "$manpage_args")
1648
1649contents=$(replace "$contents" "LIBRARY" "$library")
1650contents=$(replace "$contents" "HISTORY" "$hist")
1651contents=$(replace "$contents" "EXTRA_MATH" "$extra_math")
1652contents=$(replace "$contents" "NLS" "$nls")
1653contents=$(replace "$contents" "FUZZ" "$fuzz")
1654contents=$(replace "$contents" "MEMCHECK" "$memcheck")
1655
1656contents=$(replace "$contents" "BC_LIB_O" "$bc_lib")
1657contents=$(replace "$contents" "BC_HELP_O" "$bc_help")
1658contents=$(replace "$contents" "DC_HELP_O" "$dc_help")
1659contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O")
1660contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len")
1661
1662contents=$(replace "$contents" "NLSPATH" "$NLSPATH")
1663contents=$(replace "$contents" "DESTDIR" "$destdir")
1664contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX")
1665contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX")
1666contents=$(replace "$contents" "BINDIR" "$BINDIR")
1667contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR")
1668contents=$(replace "$contents" "LIBDIR" "$LIBDIR")
1669contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR")
1670contents=$(replace "$contents" "MAN3DIR" "$MAN3DIR")
1671contents=$(replace "$contents" "CFLAGS" "$CFLAGS")
1672contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS")
1673contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS")
1674contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS")
1675contents=$(replace "$contents" "CC" "$CC")
1676contents=$(replace "$contents" "HOSTCC" "$HOSTCC")
1677contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT")
1678contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS")
1679contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs")
1680contents=$(replace "$contents" "INSTALL_MAN_PREREQS" "$install_man_prereqs")
1681contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales")
1682contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs")
1683contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs")
1684contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs")
1685contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs")
1686
1687contents=$(replace "$contents" "PC_PATH" "$PC_PATH")
1688contents=$(replace "$contents" "PKG_CONFIG_INSTALL" "$pkg_config_install")
1689contents=$(replace "$contents" "PKG_CONFIG_UNINSTALL" "$pkg_config_uninstall")
1690
1691contents=$(replace "$contents" "DEFAULT_TARGET" "$default_target")
1692contents=$(replace "$contents" "DEFAULT_TARGET_PREREQS" "$default_target_prereqs")
1693contents=$(replace "$contents" "DEFAULT_TARGET_CMD" "$default_target_cmd")
1694contents=$(replace "$contents" "SECOND_TARGET" "$second_target")
1695contents=$(replace "$contents" "SECOND_TARGET_PREREQS" "$second_target_prereqs")
1696contents=$(replace "$contents" "SECOND_TARGET_CMD" "$second_target_cmd")
1697
1698contents=$(replace "$contents" "ALL_PREREQ" "$ALL_PREREQ")
1699contents=$(replace "$contents" "BC_EXEC_PREREQ" "$bc_exec_prereq")
1700contents=$(replace "$contents" "BC_EXEC_CMD" "$bc_exec_cmd")
1701contents=$(replace "$contents" "DC_EXEC_PREREQ" "$dc_exec_prereq")
1702contents=$(replace "$contents" "DC_EXEC_CMD" "$dc_exec_cmd")
1703
1704contents=$(replace "$contents" "EXECUTABLES" "$executables")
1705contents=$(replace "$contents" "MAIN_EXEC" "$main_exec")
1706contents=$(replace "$contents" "EXEC" "$executable")
1707contents=$(replace "$contents" "TESTS" "$tests")
1708
1709contents=$(replace "$contents" "BC_HISTORY_TEST_PREREQS" "$test_bc_history_prereqs")
1710contents=$(replace "$contents" "DC_HISTORY_TEST_PREREQS" "$test_dc_history_prereqs")
1711contents=$(replace "$contents" "HISTORY_TESTS" "$history_tests")
1712
1713contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test")
1714contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test")
1715
1716contents=$(replace "$contents" "TIMECONST" "$timeconst")
1717
1718contents=$(replace "$contents" "KARATSUBA" "$karatsuba")
1719contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test")
1720
1721contents=$(replace "$contents" "LONG_BIT" "$LONG_BIT")
1722contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE")
1723
1724contents=$(replace "$contents" "GEN" "$GEN")
1725contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET")
1726contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS")
1727contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU")
1728
1729contents=$(replace "$contents" "BSD" "$bsd")
1730
1731contents=$(replace "$contents" "BC_DEFAULT_BANNER" "$bc_default_banner")
1732contents=$(replace "$contents" "BC_DEFAULT_SIGINT_RESET" "$bc_default_sigint_reset")
1733contents=$(replace "$contents" "DC_DEFAULT_SIGINT_RESET" "$dc_default_sigint_reset")
1734contents=$(replace "$contents" "BC_DEFAULT_TTY_MODE" "$bc_default_tty_mode")
1735contents=$(replace "$contents" "DC_DEFAULT_TTY_MODE" "$dc_default_tty_mode")
1736contents=$(replace "$contents" "BC_DEFAULT_PROMPT" "$bc_default_prompt")
1737contents=$(replace "$contents" "DC_DEFAULT_PROMPT" "$dc_default_prompt")
1738contents=$(replace "$contents" "BC_DEFAULT_EXPR_EXIT" "$bc_default_expr_exit")
1739contents=$(replace "$contents" "DC_DEFAULT_EXPR_EXIT" "$dc_default_expr_exit")
1740
1741# Do the first print to the Makefile.
1742printf '%s\n%s\n\n' "$contents" "$SRC_TARGETS" > "Makefile"
1743
1744# Generate the individual test targets.
1745if [ "$bc" -ne 0 ]; then
1746	gen_std_tests bc "$extra_math" "$time_tests" $bc_test_exec
1747	gen_script_tests bc "$extra_math" "$generate_tests" "$time_tests" $bc_test_exec
1748	gen_err_tests bc $bc_test_exec
1749fi
1750
1751if [ "$dc" -ne 0 ]; then
1752	gen_std_tests dc "$extra_math" "$time_tests" $dc_test_exec
1753	gen_script_tests dc "$extra_math" "$generate_tests" "$time_tests" $dc_test_exec
1754	gen_err_tests dc $dc_test_exec
1755fi
1756
1757# Copy the correct manuals to the expected places.
1758mkdir -p manuals
1759cp -f "$scriptdir/manuals/bc/$manpage_args.1.md" manuals/bc.1.md
1760cp -f "$scriptdir/manuals/bc/$manpage_args.1" manuals/bc.1
1761cp -f "$scriptdir/manuals/dc/$manpage_args.1.md" manuals/dc.1.md
1762cp -f "$scriptdir/manuals/dc/$manpage_args.1" manuals/dc.1
1763
1764make clean > /dev/null
1765