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