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