Try to quiet Python syntax warnings
[gh-bc.git] / configure.sh
blob43bb502ea81779dd3b2a04b5a64db7cde29582e5
1 #! /bin/sh
3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2018-2024 Gavin D. Howard and contributors.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
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.
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.
30 script="$0"
31 scriptdir=$(dirname "$script")
32 script=$(basename "$script")
34 builddir=$(pwd)
36 . "$scriptdir/scripts/functions.sh"
38 # Simply prints the help message and quits based on the argument.
39 # @param msg The help message to print.
40 usage() {
42 if [ $# -gt 0 ]; then
44 _usage_val=1
46 printf '%s\n\n' "$1"
48 else
49 _usage_val=0
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|--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] [--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'
70 if [ "$_usage_val" -ne 0 ]; then
71 exit
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, --coverage\n'
87 printf ' Generate test coverage code. Requires gcov and gcovr.\n'
88 printf ' It is an error if either "-b" ("-D") or "-d" ("-B") is specified.\n'
89 printf ' Requires a compiler that use gcc-compatible coverage options\n'
90 printf ' -C, --disable-clean\n'
91 printf ' Disable the clean that configure.sh does before configure.\n'
92 printf ' -d, --dc-only\n'
93 printf ' Build dc only. It is an error if "-b", "--bc-only", "-D", or\n'
94 printf ' "--disable-dc" are specified too.\n'
95 printf ' -D, --disable-dc\n'
96 printf ' Disable dc. It is an error if "-d", "--dc-only", "-B", or "--disable-bc"\n'
97 printf ' are specified too.\n'
98 printf ' -e, --enable-editline\n'
99 printf ' Enable the use of libedit/editline. This is meant for those users that\n'
100 printf ' want vi-like or Emacs-like behavior in history. This option is ignored\n'
101 printf ' if history is disabled. If the -r or -i options are given with this\n'
102 printf ' option, the last occurrence of all of the three is used.\n'
103 printf ' -E, --disable-extra-math\n'
104 printf ' Disable extra math. This includes: "$" operator (truncate to integer),\n'
105 printf ' "@" operator (set number of decimal places), and r(x, p) (rounding\n'
106 printf ' function). Additionally, this option disables the extra printing\n'
107 printf ' functions in the math library.\n'
108 printf ' -f, --force\n'
109 printf ' Force use of all enabled options, even if they do not work. This\n'
110 printf ' option is to allow the maintainer a way to test that certain options\n'
111 printf ' are not failing invisibly. (Development only.)\n'
112 printf ' -g, --debug\n'
113 printf ' Build in debug mode. Adds the "-g" flag, and if there are no\n'
114 printf ' other CFLAGS, and "-O" was not given, this also adds the "-O0"\n'
115 printf ' flag. If this flag is *not* given, "-DNDEBUG" is added to CPPFLAGS\n'
116 printf ' and a strip flag is added to the link stage.\n'
117 printf ' -G, --disable-generated-tests\n'
118 printf ' Disable generating tests. This is for platforms that do not have a\n'
119 printf ' GNU bc-compatible bc to generate tests.\n'
120 printf ' -h, --help\n'
121 printf ' Print this help message and exit.\n'
122 printf ' -H, --disable-history\n'
123 printf ' Disable history.\n'
124 printf ' -i, --enable-internal-history\n'
125 printf ' Enable the internal history implementation and do not depend on either\n'
126 printf ' editline or readline. This option is ignored if history is disabled.\n'
127 printf ' If this option is given along with -e and -r, the last occurrence of\n'
128 printf ' all of the three is used.\n'
129 printf ' -k KARATSUBA_LEN, --karatsuba-len KARATSUBA_LEN\n'
130 printf ' Set the karatsuba length to KARATSUBA_LEN (default is 32).\n'
131 printf ' It is an error if KARATSUBA_LEN is not a number or is less than 16.\n'
132 printf ' -l, --install-all-locales\n'
133 printf ' Installs all locales, regardless of how many are on the system. This\n'
134 printf ' option is useful for package maintainers who want to make sure that\n'
135 printf ' a package contains all of the locales that end users might need.\n'
136 printf ' -m, --enable-memcheck\n'
137 printf ' Enable memcheck mode, to ensure no memory leaks. For development only.\n'
138 printf ' -M, --disable-man-pages\n'
139 printf ' Disable installing manpages.\n'
140 printf ' -N, --disable-nls\n'
141 printf ' Disable POSIX locale (NLS) support.\n'
142 printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n'
143 printf ' installed at a fixed location to work at all. If you do not want that\n'
144 printf ' to happen, you must disable locales (NLS) completely.\n'
145 printf ' -O OPT_LEVEL, --opt OPT_LEVEL\n'
146 printf ' Set the optimization level. This can also be included in the CFLAGS,\n'
147 printf ' but it is provided, so maintainers can build optimized debug builds.\n'
148 printf ' This is passed through to the compiler, so it must be supported.\n'
149 printf ' -p TYPE, --predefined-build-type=TYPE\n'
150 printf ' Sets a given predefined build type with specific defaults. This is for\n'
151 printf ' easy setting of predefined builds. For example, to get a build that\n'
152 printf ' acts like the GNU bc by default, TYPE should be "GNU" (without the\n'
153 printf ' quotes) This option *must* come before any others that might change the\n'
154 printf ' build options. Currently supported values for TYPE include: "BSD" (for\n'
155 printf ' matching the BSD bc and BSD dc), "GNU" (for matching the GNU bc and\n'
156 printf ' dc), "GDH" (for the preferred build of the author, Gavin D. Howard),\n'
157 printf ' and "DBG" (for the preferred debug build of the author). This will\n'
158 printf ' also automatically enable a release build (except for "DBG").\n'
159 printf ' -P, --disable-problematic-tests\n'
160 printf ' Disables problematic tests. These tests usually include tests that\n'
161 printf ' can cause a SIGKILL because of too much memory usage.\n'
162 printf ' -r, --enable-readline\n'
163 printf ' Enable the use of libreadline/readline. This is meant for those users\n'
164 printf ' that want vi-like or Emacs-like behavior in history. This option is\n'
165 printf ' ignored if history is disabled. If this option is given along with -e\n'
166 printf ' and -i, the last occurrence of all of the three is used.\n'
167 printf ' -s SETTING, --set-default-on SETTING\n'
168 printf ' Set the default named by SETTING to on. See below for possible values\n'
169 printf ' for SETTING. For multiple instances of the -s or -S for the the same\n'
170 printf ' setting, the last one is used.\n'
171 printf ' -S SETTING, --set-default-off SETTING\n'
172 printf ' Set the default named by SETTING to off. See below for possible values\n'
173 printf ' for SETTING. For multiple instances of the -s or -S for the the same\n'
174 printf ' setting, the last one is used.\n'
175 printf ' -t, --enable-test-timing\n'
176 printf ' Enable the timing of tests. This is for development only.\n'
177 printf ' -T, --disable-strip\n'
178 printf ' Disable stripping symbols from the compiled binary or binaries.\n'
179 printf ' Stripping symbols only happens when debug mode is off.\n'
180 printf ' -v, --enable-valgrind\n'
181 printf ' Enable a build appropriate for valgrind. For development only.\n'
182 printf ' -z, --enable-fuzz-mode\n'
183 printf ' Enable fuzzing mode. THIS IS FOR DEVELOPMENT ONLY.\n'
184 printf ' --prefix PREFIX\n'
185 printf ' The prefix to install to. Overrides "$PREFIX" if it exists.\n'
186 printf ' If PREFIX is "/usr", install path will be "/usr/bin".\n'
187 printf ' Default is "/usr/local".\n'
188 printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n'
189 printf ' installed at a fixed location to work at all. If you do not want that to\n'
190 printf ' happen, you must disable locales (NLS) completely.\n'
191 printf ' --bindir BINDIR\n'
192 printf ' The directory to install binaries in. Overrides "$BINDIR" if it exists.\n'
193 printf ' Default is "$PREFIX/bin".\n'
194 printf ' --includedir INCLUDEDIR\n'
195 printf ' The directory to install headers in. Overrides "$INCLUDEDIR" if it\n'
196 printf ' exists. Default is "$PREFIX/include".\n'
197 printf ' --libdir LIBDIR\n'
198 printf ' The directory to install libraries in. Overrides "$LIBDIR" if it exists.\n'
199 printf ' Default is "$PREFIX/lib".\n'
200 printf ' --datarootdir DATAROOTDIR\n'
201 printf ' The root location for data files. Overrides "$DATAROOTDIR" if it exists.\n'
202 printf ' Default is "$PREFIX/share".\n'
203 printf ' --datadir DATADIR\n'
204 printf ' The location for data files. Overrides "$DATADIR" if it exists.\n'
205 printf ' Default is "$DATAROOTDIR".\n'
206 printf ' --mandir MANDIR\n'
207 printf ' The location to install manpages to. Overrides "$MANDIR" if it exists.\n'
208 printf ' Default is "$DATADIR/man".\n'
209 printf ' --man1dir MAN1DIR\n'
210 printf ' The location to install Section 1 manpages to. Overrides "$MAN1DIR" if\n'
211 printf ' it exists. Default is "$MANDIR/man1".\n'
212 printf ' --man3dir MAN3DIR\n'
213 printf ' The location to install Section 3 manpages to. Overrides "$MAN3DIR" if\n'
214 printf ' it exists. Default is "$MANDIR/man3".\n'
215 printf '\n'
216 printf 'In addition, the following environment variables are used:\n'
217 printf '\n'
218 printf ' CC C compiler. Must be compatible with POSIX c99. If there is a\n'
219 printf ' space in the basename of the compiler, the items after the\n'
220 printf ' first space are assumed to be compiler flags, and in that case,\n'
221 printf ' the flags are automatically moved into CFLAGS. Default is\n'
222 printf ' "c99".\n'
223 printf ' HOSTCC Host C compiler. Must be compatible with POSIX c99. If there is\n'
224 printf ' a space in the basename of the compiler, the items after the\n'
225 printf ' first space are assumed to be compiler flags, and in the case,\n'
226 printf ' the flags are automatically moved into HOSTCFLAGS. Default is\n'
227 printf ' "$CC".\n'
228 printf ' HOST_CC Same as HOSTCC. If HOSTCC also exists, it is used.\n'
229 printf ' CFLAGS C compiler flags.\n'
230 printf ' HOSTCFLAGS CFLAGS for HOSTCC. Default is "$CFLAGS".\n'
231 printf ' HOST_CFLAGS Same as HOST_CFLAGS. If HOST_CFLAGS also exists, it is used.\n'
232 printf ' CPPFLAGS C preprocessor flags. Default is "".\n'
233 printf ' LDFLAGS Linker flags. Default is "".\n'
234 printf ' PREFIX The prefix to install to. Default is "/usr/local".\n'
235 printf ' If PREFIX is "/usr", install path will be "/usr/bin".\n'
236 printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n'
237 printf ' installed at a fixed location to work at all. If you do not\n'
238 printf ' want that to happen, you must disable locales (NLS) completely.\n'
239 printf ' BINDIR The directory to install binaries in. Default is "$PREFIX/bin".\n'
240 printf ' INCLUDEDIR The directory to install header files in. Default is\n'
241 printf ' "$PREFIX/include".\n'
242 printf ' LIBDIR The directory to install libraries in. Default is\n'
243 printf ' "$PREFIX/lib".\n'
244 printf ' DATAROOTDIR The root location for data files. Default is "$PREFIX/share".\n'
245 printf ' DATADIR The location for data files. Default is "$DATAROOTDIR".\n'
246 printf ' MANDIR The location to install manpages to. Default is "$DATADIR/man".\n'
247 printf ' MAN1DIR The location to install Section 1 manpages to. Default is\n'
248 printf ' "$MANDIR/man1".\n'
249 printf ' MAN3DIR The location to install Section 3 manpages to. Default is\n'
250 printf ' "$MANDIR/man3".\n'
251 printf ' NLSPATH The location to install locale catalogs to. Must be an absolute\n'
252 printf ' path (or contain one). This is treated the same as the POSIX\n'
253 printf ' definition of $NLSPATH (see POSIX environment variables for\n'
254 printf ' more information). Default is "/usr/share/locale/%%L/%%N".\n'
255 printf ' PC_PATH The location to install pkg-config files to. Must be an\n'
256 printf ' path or contain one. Default is the first path given by the\n'
257 printf ' output of `pkg-config --variable=pc_path pkg-config`.\n'
258 printf ' EXECSUFFIX The suffix to append to the executable names, used to not\n'
259 printf ' interfere with other installed bc executables. Default is "".\n'
260 printf ' EXECPREFIX The prefix to append to the executable names, used to not\n'
261 printf ' interfere with other installed bc executables. Default is "".\n'
262 printf ' DESTDIR For package creation. Default is "". If it is empty when\n'
263 printf ' `%s` is run, it can also be passed to `make install`\n' "$script"
264 printf ' later as an environment variable. If both are specified,\n'
265 printf ' the one given to `%s` takes precedence.\n' "$script"
266 printf ' LONG_BIT The number of bits in a C `long` type. This is mostly for the\n'
267 printf ' embedded space since this `bc` uses `long`s internally for\n'
268 printf ' overflow checking. In C99, a `long` is required to be 32 bits.\n'
269 printf ' For most normal desktop systems, setting this is unnecessary,\n'
270 printf ' except that 32-bit platforms with 64-bit longs may want to set\n'
271 printf ' it to `32`. Default is the default of `LONG_BIT` for the target\n'
272 printf ' platform. Minimum allowed is `32`. It is a build time error if\n'
273 printf ' the specified value of `LONG_BIT` is greater than the default\n'
274 printf ' value of `LONG_BIT` for the target platform.\n'
275 printf ' GEN_HOST Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to\n'
276 printf ' produce the C files that contain the help texts as well as the\n'
277 printf ' math libraries. By default, `gen/strgen.c` is used, compiled by\n'
278 printf ' "$HOSTCC" and run on the host machine. Using `gen/strgen.sh`\n'
279 printf ' removes the need to compile and run an executable on the host\n'
280 printf ' machine since `gen/strgen.sh` is a POSIX shell script. However,\n'
281 printf ' `gen/lib2.bc` is over 4095 characters, the max supported length\n'
282 printf ' of a string literal in C99, and `gen/strgen.sh` generates a\n'
283 printf ' string literal instead of an array, as `gen/strgen.c` does. For\n'
284 printf ' most production-ready compilers, this limit probably is not\n'
285 printf ' enforced, but it could be. Both options are still available for\n'
286 printf ' this reason. If you are sure your compiler does not have the\n'
287 printf ' limit and do not want to compile and run a binary on the host\n'
288 printf ' machine, set this variable to "0". Any other value, or a\n'
289 printf ' non-existent value, will cause the build system to compile and\n'
290 printf ' run `gen/strgen.c`. Default is "".\n'
291 printf ' GEN_EMU Emulator to run string generator code under (leave empty if not\n'
292 printf ' necessary). This is not necessary when using `gen/strgen.sh`.\n'
293 printf ' Default is "".\n'
294 printf '\n'
295 printf 'WARNING: even though `configure.sh` supports both option types, short and\n'
296 printf 'long, it does not support handling both at the same time. Use only one type.\n'
297 printf '\n'
298 printf 'Settings\n'
299 printf '========\n'
300 printf '\n'
301 printf 'bc and dc have some settings that, while they cannot be removed by build time\n'
302 printf 'options, can have their defaults changed at build time by packagers. Users are\n'
303 printf 'also able to change each setting with environment variables.\n'
304 printf '\n'
305 printf 'The following is a table of settings, along with their default values and the\n'
306 printf 'environment variables users can use to change them. (For the defaults, non-zero\n'
307 printf 'means on, and zero means off.)\n'
308 printf '\n'
309 printf '| Setting | Description | Default | Env Variable |\n'
310 printf '| =============== | ==================== | ============ | ==================== |\n'
311 printf '| bc.banner | Whether to display | 0 | BC_BANNER |\n'
312 printf '| | the bc version | | |\n'
313 printf '| | banner when in | | |\n'
314 printf '| | interactive mode. | | |\n'
315 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
316 printf '| bc.sigint_reset | Whether SIGINT will | 1 | BC_SIGINT_RESET |\n'
317 printf '| | reset bc, instead of | | |\n'
318 printf '| | exiting, when in | | |\n'
319 printf '| | interactive mode. | | |\n'
320 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
321 printf '| dc.sigint_reset | Whether SIGINT will | 1 | DC_SIGINT_RESET |\n'
322 printf '| | reset dc, instead of | | |\n'
323 printf '| | exiting, when in | | |\n'
324 printf '| | interactive mode. | | |\n'
325 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
326 printf '| bc.tty_mode | Whether TTY mode for | 1 | BC_TTY_MODE |\n'
327 printf '| | bc should be on when | | |\n'
328 printf '| | available. | | |\n'
329 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
330 printf '| dc.tty_mode | Whether TTY mode for | 0 | BC_TTY_MODE |\n'
331 printf '| | dc should be on when | | |\n'
332 printf '| | available. | | |\n'
333 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
334 printf '| bc.prompt | Whether the prompt | $BC_TTY_MODE | BC_PROMPT |\n'
335 printf '| | for bc should be on | | |\n'
336 printf '| | in tty mode. | | |\n'
337 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
338 printf '| dc.prompt | Whether the prompt | $DC_TTY_MODE | DC_PROMPT |\n'
339 printf '| | for dc should be on | | |\n'
340 printf '| | in tty mode. | | |\n'
341 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
342 printf '| bc.expr_exit | Whether to exit bc | 1 | BC_EXPR_EXIT |\n'
343 printf '| | if an expression or | | |\n'
344 printf '| | expression file is | | |\n'
345 printf '| | given with the -e or | | |\n'
346 printf '| | -f options. | | |\n'
347 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
348 printf '| dc.expr_exit | Whether to exit dc | 1 | DC_EXPR_EXIT |\n'
349 printf '| | if an expression or | | |\n'
350 printf '| | expression file is | | |\n'
351 printf '| | given with the -e or | | |\n'
352 printf '| | -f options. | | |\n'
353 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
354 printf '| bc.digit_clamp | Whether to have bc | 0 | BC_DIGIT_CLAMP |\n'
355 printf '| | clamp digits that | | |\n'
356 printf '| | are greater than or | | |\n'
357 printf '| | equal to the current | | |\n'
358 printf '| | ibase when parsing | | |\n'
359 printf '| | numbers. | | |\n'
360 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
361 printf '| dc.digit_clamp | Whether to have dc | 0 | DC_DIGIT_CLAMP |\n'
362 printf '| | clamp digits that | | |\n'
363 printf '| | are greater than or | | |\n'
364 printf '| | equal to the current | | |\n'
365 printf '| | ibase when parsing | | |\n'
366 printf '| | numbers. | | |\n'
367 printf '| --------------- | -------------------- | ------------ | -------------------- |\n'
368 printf '\n'
369 printf 'These settings are not meant to be changed on a whim. They are meant to ensure\n'
370 printf 'that this bc and dc will conform to the expectations of the user on each\n'
371 printf 'platform.\n'
373 exit "$_usage_val"
376 # Replaces a file extension in a filename. This is used mostly to turn filenames
377 # like `src/num.c` into `src/num.o`. In other words, it helps to link targets to
378 # the files they depend on.
380 # @param file The filename.
381 # @param ext1 The extension to replace.
382 # @param ext2 The new extension.
383 replace_ext() {
385 if [ "$#" -ne 3 ]; then
386 err_exit "Invalid number of args to $0"
389 _replace_ext_file="$1"
390 _replace_ext_ext1="$2"
391 _replace_ext_ext2="$3"
393 _replace_ext_result="${_replace_ext_file%.$_replace_ext_ext1}.$_replace_ext_ext2"
395 printf '%s\n' "$_replace_ext_result"
398 # Replaces a file extension in every filename given in a list. The list is just
399 # a space-separated list of words, so filenames are expected to *not* have
400 # spaces in them. See the documentation for `replace_ext()`.
402 # @param files The list of space-separated filenames to replace extensions for.
403 # @param ext1 The extension to replace.
404 # @param ext2 The new extension.
405 replace_exts() {
407 if [ "$#" -ne 3 ]; then
408 err_exit "Invalid number of args to $0"
411 _replace_exts_files="$1"
412 _replace_exts_ext1="$2"
413 _replace_exts_ext2="$3"
415 for _replace_exts_file in $_replace_exts_files; do
416 _replace_exts_new_name=$(replace_ext "$_replace_exts_file" "$_replace_exts_ext1" "$_replace_exts_ext2")
417 _replace_exts_result="$_replace_exts_result $_replace_exts_new_name"
418 done
420 printf '%s\n' "$_replace_exts_result"
423 # Finds a placeholder in @a str and replaces it. This is the workhorse of
424 # configure.sh. It's what replaces placeholders in Makefile.in with the data
425 # needed for the chosen build. Below, you will see a lot of calls to this
426 # function.
428 # Note that needle can never contain an exclamation point. For more information,
429 # see substring_replace() in scripts/functions.sh.
431 # @param str The string to find and replace placeholders in.
432 # @param needle The placeholder name.
433 # @param replacement The string to use to replace the placeholder.
434 replace() {
436 if [ "$#" -ne 3 ]; then
437 err_exit "Invalid number of args to $0"
440 _replace_str="$1"
441 _replace_needle="$2"
442 _replace_replacement="$3"
444 substring_replace "$_replace_str" "%%$_replace_needle%%" "$_replace_replacement"
447 # This function finds all the source files that need to be built. If there is
448 # only one argument and it is empty, then all source files are built. Otherwise,
449 # the arguments are all assumed to be source files that should *not* be built.
450 find_src_files() {
452 _find_src_files_args=""
454 if [ "$#" -ge 1 ] && [ "$1" != "" ]; then
456 while [ "$#" -ge 1 ]; do
457 _find_src_files_a="${1## }"
458 shift
459 _find_src_files_args=$(printf '%s\n%s/src/%s\n' "$_find_src_files_args" "$scriptdir" "${_find_src_files_a}")
460 done
464 _find_src_files_files=$(find "$scriptdir/src" -depth -name "*.c" -print | LC_ALL=C sort)
466 _find_src_files_result=""
468 for _find_src_files_f in $_find_src_files_files; do
470 # If this is true, the file is part of args, and therefore, unneeded.
471 if [ "${_find_src_files_args##*$_find_src_files_f}" != "${_find_src_files_args}" ]; then
472 continue
475 _find_src_files_result=$(printf '%s\n%s\n' "$_find_src_files_result" "$_find_src_files_f")
477 done
479 printf '%s\n' "$_find_src_files_result"
482 # This function generates a list of files to go into the Makefile. It generates
483 # the list of object files, as well as the list of test coverage files.
485 # @param contents The contents of the Makefile template to put the list of
486 # files into.
487 gen_file_list() {
489 if [ "$#" -lt 1 ]; then
490 err_exit "Invalid number of args to $0"
493 _gen_file_list_contents="$1"
494 shift
496 if [ "$#" -ge 1 ]; then
497 _gen_file_list_unneeded="$@"
498 else
499 _gen_file_list_unneeded=""
502 _gen_file_list_needle_src="SRC"
503 _gen_file_list_needle_obj="OBJ"
504 _gen_file_list_needle_gcda="GCDA"
505 _gen_file_list_needle_gcno="GCNO"
507 _gen_file_list_replacement=$(find_src_files $_gen_file_list_unneeded | tr '\n' ' ')
508 _gen_file_list_contents=$(replace "$_gen_file_list_contents" \
509 "$_gen_file_list_needle_src" "$_gen_file_list_replacement")
511 _gen_file_list_cbases=""
513 for _gen_file_list_f in $_gen_file_list_replacement; do
514 _gen_file_list_b=$(basename "$_gen_file_list_f")
515 _gen_file_list_cbases="$_gen_file_list_cbases src/$_gen_file_list_b"
516 done
518 _gen_file_list_replacement=$(replace_exts "$_gen_file_list_cbases" "c" "o")
519 _gen_file_list_contents=$(replace "$_gen_file_list_contents" \
520 "$_gen_file_list_needle_obj" "$_gen_file_list_replacement")
522 _gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "o" "gcda")
523 _gen_file_list_contents=$(replace "$_gen_file_list_contents" \
524 "$_gen_file_list_needle_gcda" "$_gen_file_list_replacement")
526 _gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "gcda" "gcno")
527 _gen_file_list_contents=$(replace "$_gen_file_list_contents" \
528 "$_gen_file_list_needle_gcno" "$_gen_file_list_replacement")
530 printf '%s\n' "$_gen_file_list_contents"
533 # Generates the proper test targets for each test to have its own target. This
534 # allows `make test` to run in parallel.
536 # @param name Which calculator to generate tests for.
537 # @param extra_math An integer that, if non-zero, activates extra math tests.
538 # @param time_tests An integer that, if non-zero, tells the test suite to time
539 # the execution of each test.
540 gen_std_tests() {
542 _gen_std_tests_name="$1"
543 shift
545 _gen_std_tests_extra_math="$1"
546 shift
548 _gen_std_tests_time_tests="$1"
549 shift
551 _gen_std_tests_extra_required=$(cat "$scriptdir/tests/extra_required.txt")
553 for _gen_std_tests_t in $(cat "$scriptdir/tests/$_gen_std_tests_name/all.txt"); do
555 if [ "$_gen_std_tests_extra_math" -eq 0 ]; then
557 if [ -z "${_gen_std_tests_extra_required##*$_gen_std_tests_t*}" ]; then
558 printf 'test_%s_%s:\n\t@printf "Skipping %s %s\\n"\n\n' \
559 "$_gen_std_tests_name" "$_gen_std_tests_t" "$_gen_std_tests_name" \
560 "$_gen_std_tests_t" >> "Makefile"
561 continue
566 printf 'test_%s_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/test.sh %s %s %s %s %s\n\n' \
567 "$_gen_std_tests_name" "$_gen_std_tests_t" "$builddir" "$_gen_std_tests_name" \
568 "$_gen_std_tests_t" "$generate_tests" "$time_tests" \
569 "$*" >> "Makefile"
571 done
574 # Generates a list of test targets that will be used as prerequisites for other
575 # targets.
577 # @param name The name of the calculator to generate test targets for.
578 gen_std_test_targets() {
580 _gen_std_test_targets_name="$1"
581 shift
583 _gen_std_test_targets_tests=$(cat "$scriptdir/tests/${_gen_std_test_targets_name}/all.txt")
585 for _gen_std_test_targets_t in $_gen_std_test_targets_tests; do
586 printf ' test_%s_%s' "$_gen_std_test_targets_name" "$_gen_std_test_targets_t"
587 done
589 printf '\n'
592 # Generates the proper test targets for each error test to have its own target.
593 # This allows `make test_bc_errors` and `make test_dc_errors` to run in
594 # parallel.
596 # @param name Which calculator to generate tests for.
597 gen_err_tests() {
599 _gen_err_tests_name="$1"
600 shift
602 _gen_err_tests_fs=$(ls "$scriptdir/tests/$_gen_err_tests_name/errors/")
604 for _gen_err_tests_t in $_gen_err_tests_fs; do
606 printf 'test_%s_error_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/error.sh %s %s %s %s\n\n' \
607 "$_gen_err_tests_name" "$_gen_err_tests_t" "$builddir" "$_gen_err_tests_name" \
608 "$_gen_err_tests_t" "$problematic_tests" "$*" >> "Makefile"
610 done
614 # Generates a list of error test targets that will be used as prerequisites for
615 # other targets.
617 # @param name The name of the calculator to generate test targets for.
618 gen_err_test_targets() {
620 _gen_err_test_targets_name="$1"
621 shift
623 _gen_err_test_targets_tests=$(ls "$scriptdir/tests/$_gen_err_test_targets_name/errors/")
625 for _gen_err_test_targets_t in $_gen_err_test_targets_tests; do
626 printf ' test_%s_error_%s' "$_gen_err_test_targets_name" "$_gen_err_test_targets_t"
627 done
629 printf '\n'
632 # Generates the proper script test targets for each script test to have its own
633 # target. This allows `make test` to run in parallel.
635 # @param name Which calculator to generate tests for.
636 # @param extra_math An integer that, if non-zero, activates extra math tests.
637 # @param generate An integer that, if non-zero, activates generated tests.
638 # @param time_tests An integer that, if non-zero, tells the test suite to time
639 # the execution of each test.
640 gen_script_tests() {
642 _gen_script_tests_name="$1"
643 shift
645 _gen_script_tests_extra_math="$1"
646 shift
648 _gen_script_tests_generate="$1"
649 shift
651 _gen_script_tests_time="$1"
652 shift
654 _gen_script_tests_tests=$(cat "$scriptdir/tests/$_gen_script_tests_name/scripts/all.txt")
656 for _gen_script_tests_f in $_gen_script_tests_tests; do
658 _gen_script_tests_b=$(basename "$_gen_script_tests_f" ".${_gen_script_tests_name}")
660 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' \
661 "$_gen_script_tests_name" "$_gen_script_tests_b" "$builddir" "$_gen_script_tests_name" \
662 "$_gen_script_tests_f" "$_gen_script_tests_extra_math" "$_gen_script_tests_generate" \
663 "$_gen_script_tests_time" "$*" >> "Makefile"
664 done
667 set_default() {
669 _set_default_on="$1"
670 shift
672 _set_default_name="$1"
673 shift
675 # The reason that the variables that are being set do not have the same
676 # non-collision avoidance that the other variables do is that we *do* want
677 # the settings of these variables to leak out of the function. They adjust
678 # the settings outside of the function.
679 case "$_set_default_name" in
681 bc.banner) bc_default_banner="$_set_default_on" ;;
682 bc.sigint_reset) bc_default_sigint_reset="$_set_default_on" ;;
683 dc.sigint_reset) dc_default_sigint_reset="$_set_default_on" ;;
684 bc.tty_mode) bc_default_tty_mode="$_set_default_on" ;;
685 dc.tty_mode) dc_default_tty_mode="$_set_default_on" ;;
686 bc.prompt) bc_default_prompt="$_set_default_on" ;;
687 dc.prompt) dc_default_prompt="$_set_default_on" ;;
688 bc.expr_exit) bc_default_expr_exit="$_set_default_on";;
689 dc.expr_exit) dc_default_expr_exit="$_set_default_on";;
690 bc.digit_clamp) bc_default_digit_clamp="$_set_default_on";;
691 dc.digit_clamp) dc_default_digit_clamp="$_set_default_on";;
692 ?) usage "Invalid setting: $_set_default_name" ;;
694 esac
697 predefined_build() {
699 _predefined_build_type="$1"
700 shift
702 # The reason that the variables that are being set do not have the same
703 # non-collision avoidance that the other variables do is that we *do* want
704 # the settings of these variables to leak out of the function. They adjust
705 # the settings outside of the function.
706 case "$_predefined_build_type" in
708 BSD)
709 bc_only=0
710 dc_only=0
711 coverage=0
712 debug=0
713 optimization="3"
714 hist=1
715 hist_impl="editline"
716 extra_math=1
717 generate_tests=$generate_tests
718 install_manpages=0
719 nls=1
720 force=0
721 strip_bin=1
722 all_locales=0
723 library=0
724 fuzz=0
725 time_tests=0
726 vg=0
727 memcheck=0
728 clean=1
729 bc_default_banner=0
730 bc_default_sigint_reset=1
731 dc_default_sigint_reset=1
732 bc_default_tty_mode=1
733 dc_default_tty_mode=0
734 bc_default_prompt=""
735 dc_default_prompt=""
736 bc_default_expr_exit=1
737 dc_default_expr_exit=1
738 bc_default_digit_clamp=0
739 dc_default_digit_clamp=0;;
741 GNU)
742 bc_only=0
743 dc_only=0
744 coverage=0
745 debug=0
746 optimization="3"
747 hist=1
748 hist_impl="internal"
749 extra_math=1
750 generate_tests=$generate_tests
751 install_manpages=1
752 nls=1
753 force=0
754 strip_bin=1
755 all_locales=0
756 library=0
757 fuzz=0
758 time_tests=0
759 vg=0
760 memcheck=0
761 clean=1
762 bc_default_banner=1
763 bc_default_sigint_reset=1
764 dc_default_sigint_reset=0
765 bc_default_tty_mode=1
766 dc_default_tty_mode=0
767 bc_default_prompt=""
768 dc_default_prompt=""
769 bc_default_expr_exit=1
770 dc_default_expr_exit=1
771 bc_default_digit_clamp=1
772 dc_default_digit_clamp=0;;
774 GDH)
775 CFLAGS="-flto -Weverything -Wno-padded -Wno-unsafe-buffer-usage -Wno-poison-system-directories -Werror -pedantic -std=c11"
776 bc_only=0
777 dc_only=0
778 coverage=0
779 debug=0
780 optimization="3"
781 hist=1
782 hist_impl="internal"
783 extra_math=1
784 generate_tests=1
785 install_manpages=1
786 nls=0
787 force=0
788 strip_bin=1
789 all_locales=0
790 library=0
791 fuzz=0
792 time_tests=0
793 vg=0
794 memcheck=0
795 clean=1
796 bc_default_banner=1
797 bc_default_sigint_reset=1
798 dc_default_sigint_reset=1
799 bc_default_tty_mode=1
800 dc_default_tty_mode=1
801 bc_default_prompt=""
802 dc_default_prompt=""
803 bc_default_expr_exit=0
804 dc_default_expr_exit=0
805 bc_default_digit_clamp=1
806 dc_default_digit_clamp=1;;
808 DBG)
809 CFLAGS="-Weverything -Wno-padded -Wno-unsafe-buffer-usage -Wno-poison-system-directories -Werror -pedantic -std=c11"
810 bc_only=0
811 dc_only=0
812 coverage=0
813 debug=1
814 optimization="0"
815 hist=1
816 hist_impl="internal"
817 extra_math=1
818 generate_tests=1
819 install_manpages=1
820 nls=1
821 force=0
822 strip_bin=1
823 all_locales=0
824 library=0
825 fuzz=0
826 time_tests=0
827 vg=0
828 memcheck=1
829 clean=1
830 bc_default_banner=1
831 bc_default_sigint_reset=1
832 dc_default_sigint_reset=1
833 bc_default_tty_mode=1
834 dc_default_tty_mode=1
835 bc_default_prompt=""
836 dc_default_prompt=""
837 bc_default_expr_exit=0
838 dc_default_expr_exit=0
839 bc_default_digit_clamp=1
840 dc_default_digit_clamp=1;;
842 ?|'') usage "Invalid user build: \"$_predefined_build_type\". Accepted types are BSD, GNU, GDH, DBG.";;
844 esac
847 # Generates a list of script test targets that will be used as prerequisites for
848 # other targets.
850 # @param name The name of the calculator to generate script test targets for.
851 gen_script_test_targets() {
853 _gen_script_test_targets_name="$1"
854 shift
856 _gen_script_test_targets_tests=$(cat "$scriptdir/tests/$_gen_script_test_targets_name/scripts/all.txt")
858 for _gen_script_test_targets_f in $_gen_script_test_targets_tests; do
859 _gen_script_test_targets_b=$(basename "$_gen_script_test_targets_f" \
860 ".$_gen_script_test_targets_name")
861 printf ' test_%s_script_%s' "$_gen_script_test_targets_name" \
862 "$_gen_script_test_targets_b"
863 done
865 printf '\n'
868 # This is a list of defaults, but it is also the list of possible options for
869 # users to change.
871 # The development options are: force (force options even if they fail), valgrind
872 # (build in a way suitable for valgrind testing), memcheck (same as valgrind),
873 # and fuzzing (build in a way suitable for fuzzing).
874 bc_only=0
875 dc_only=0
876 coverage=0
877 karatsuba_len=32
878 debug=0
879 hist=1
880 hist_impl="internal"
881 extra_math=1
882 optimization=""
883 generate_tests=1
884 install_manpages=1
885 nls=1
886 force=0
887 strip_bin=1
888 all_locales=0
889 library=0
890 fuzz=0
891 time_tests=0
892 vg=0
893 memcheck=0
894 clean=1
895 problematic_tests=1
897 # The empty strings are because they depend on TTY mode. If they are directly
898 # set, though, they will be integers. We test for empty strings later.
899 bc_default_banner=0
900 bc_default_sigint_reset=1
901 dc_default_sigint_reset=1
902 bc_default_tty_mode=1
903 dc_default_tty_mode=0
904 bc_default_prompt=""
905 dc_default_prompt=""
906 bc_default_expr_exit=1
907 dc_default_expr_exit=1
908 bc_default_digit_clamp=0
909 dc_default_digit_clamp=0
911 # getopts is a POSIX utility, but it cannot handle long options. Thus, the
912 # handling of long options is done by hand, and that's the reason that short and
913 # long options cannot be mixed.
914 while getopts "abBcdDeEfgGhHik:lMmNO:p:PrS:s:tTvz-" opt; do
916 case "$opt" in
917 a) library=1 ;;
918 b) bc_only=1 ;;
919 B) dc_only=1 ;;
920 c) coverage=1 ;;
921 C) clean=0 ;;
922 d) dc_only=1 ;;
923 D) bc_only=1 ;;
924 e) hist_impl="editline" ;;
925 E) extra_math=0 ;;
926 f) force=1 ;;
927 g) debug=1 ;;
928 G) generate_tests=0 ;;
929 h) usage ;;
930 H) hist=0 ;;
931 i) hist_impl="internal" ;;
932 k) karatsuba_len="$OPTARG" ;;
933 l) all_locales=1 ;;
934 m) memcheck=1 ;;
935 M) install_manpages=0 ;;
936 N) nls=0 ;;
937 O) optimization="$OPTARG" ;;
938 p) predefined_build "$OPTARG" ;;
939 P) problematic_tests=0 ;;
940 r) hist_impl="readline" ;;
941 S) set_default 0 "$OPTARG" ;;
942 s) set_default 1 "$OPTARG" ;;
943 t) time_tests=1 ;;
944 T) strip_bin=0 ;;
945 v) vg=1 ;;
946 z) fuzz=1 ;;
948 arg="$1"
949 arg="${arg#--}"
950 LONG_OPTARG="${arg#*=}"
951 case $arg in
952 help) usage ;;
953 library) library=1 ;;
954 bc-only) bc_only=1 ;;
955 dc-only) dc_only=1 ;;
956 coverage) coverage=1 ;;
957 debug) debug=1 ;;
958 force) force=1 ;;
959 prefix=?*) PREFIX="$LONG_OPTARG" ;;
960 prefix)
961 if [ "$#" -lt 2 ]; then
962 usage "No argument given for '--$arg' option"
964 PREFIX="$2"
965 shift ;;
966 bindir=?*) BINDIR="$LONG_OPTARG" ;;
967 bindir)
968 if [ "$#" -lt 2 ]; then
969 usage "No argument given for '--$arg' option"
971 BINDIR="$2"
972 shift ;;
973 includedir=?*) INCLUDEDIR="$LONG_OPTARG" ;;
974 includedir)
975 if [ "$#" -lt 2 ]; then
976 usage "No argument given for '--$arg' option"
978 INCLUDEDIR="$2"
979 shift ;;
980 libdir=?*) LIBDIR="$LONG_OPTARG" ;;
981 libdir)
982 if [ "$#" -lt 2 ]; then
983 usage "No argument given for '--$arg' option"
985 LIBDIR="$2"
986 shift ;;
987 datarootdir=?*) DATAROOTDIR="$LONG_OPTARG" ;;
988 datarootdir)
989 if [ "$#" -lt 2 ]; then
990 usage "No argument given for '--$arg' option"
992 DATAROOTDIR="$2"
993 shift ;;
994 datadir=?*) DATADIR="$LONG_OPTARG" ;;
995 datadir)
996 if [ "$#" -lt 2 ]; then
997 usage "No argument given for '--$arg' option"
999 DATADIR="$2"
1000 shift ;;
1001 mandir=?*) MANDIR="$LONG_OPTARG" ;;
1002 mandir)
1003 if [ "$#" -lt 2 ]; then
1004 usage "No argument given for '--$arg' option"
1006 MANDIR="$2"
1007 shift ;;
1008 man1dir=?*) MAN1DIR="$LONG_OPTARG" ;;
1009 man1dir)
1010 if [ "$#" -lt 2 ]; then
1011 usage "No argument given for '--$arg' option"
1013 MAN1DIR="$2"
1014 shift ;;
1015 man3dir=?*) MAN3DIR="$LONG_OPTARG" ;;
1016 man3dir)
1017 if [ "$#" -lt 2 ]; then
1018 usage "No argument given for '--$arg' option"
1020 MAN3DIR="$2"
1021 shift ;;
1022 karatsuba-len=?*) karatsuba_len="$LONG_OPTARG" ;;
1023 karatsuba-len)
1024 if [ "$#" -lt 2 ]; then
1025 usage "No argument given for '--$arg' option"
1027 karatsuba_len="$1"
1028 shift ;;
1029 opt=?*) optimization="$LONG_OPTARG" ;;
1030 opt)
1031 if [ "$#" -lt 2 ]; then
1032 usage "No argument given for '--$arg' option"
1034 optimization="$1"
1035 shift ;;
1036 set-default-on=?*) set_default 1 "$LONG_OPTARG" ;;
1037 set-default-on)
1038 if [ "$#" -lt 2 ]; then
1039 usage "No argument given for '--$arg' option"
1041 set_default 1 "$1"
1042 shift ;;
1043 set-default-off=?*) set_default 0 "$LONG_OPTARG" ;;
1044 set-default-off)
1045 if [ "$#" -lt 2 ]; then
1046 usage "No argument given for '--$arg' option"
1048 set_default 0 "$1"
1049 shift ;;
1050 predefined-build-type=?*) predefined_build "$LONG_OPTARG" ;;
1051 predefined-build-type)
1052 if [ "$#" -lt 2 ]; then
1053 usage "No argument given for '--$arg' option"
1055 predefined_build "$1"
1056 shift ;;
1057 disable-bc) dc_only=1 ;;
1058 disable-dc) bc_only=1 ;;
1059 disable-clean) clean=0 ;;
1060 disable-extra-math) extra_math=0 ;;
1061 disable-generated-tests) generate_tests=0 ;;
1062 disable-history) hist=0 ;;
1063 disable-man-pages) install_manpages=0 ;;
1064 disable-nls) nls=0 ;;
1065 disable-strip) strip_bin=0 ;;
1066 disable-problematic-tests) problematic_tests=0 ;;
1067 enable-editline) hist_impl="editline" ;;
1068 enable-readline) hist_impl="readline" ;;
1069 enable-internal-history) hist_impl="internal" ;;
1070 enable-test-timing) time_tests=1 ;;
1071 enable-valgrind) vg=1 ;;
1072 enable-fuzz-mode) fuzz=1 ;;
1073 enable-memcheck) memcheck=1 ;;
1074 install-all-locales) all_locales=1 ;;
1075 help* | bc-only* | dc-only* | coverage* | debug*)
1076 usage "No arg allowed for --$arg option" ;;
1077 disable-bc* | disable-dc* | disable-clean*)
1078 usage "No arg allowed for --$arg option" ;;
1079 disable-extra-math*)
1080 usage "No arg allowed for --$arg option" ;;
1081 disable-generated-tests* | disable-history*)
1082 usage "No arg allowed for --$arg option" ;;
1083 disable-man-pages* | disable-nls* | disable-strip*)
1084 usage "No arg allowed for --$arg option" ;;
1085 disable-problematic-tests*)
1086 usage "No arg allowed for --$arg option" ;;
1087 enable-fuzz-mode* | enable-test-timing* | enable-valgrind*)
1088 usage "No arg allowed for --$arg option" ;;
1089 enable-memcheck* | install-all-locales*)
1090 usage "No arg allowed for --$arg option" ;;
1091 enable-editline* | enable-readline*)
1092 usage "No arg allowed for --$arg option" ;;
1093 enable-internal-history*)
1094 usage "No arg allowed for --$arg option" ;;
1095 '') break ;; # "--" terminates argument processing
1096 * ) usage "Invalid option $LONG_OPTARG" ;;
1097 esac
1098 shift
1099 OPTIND=1 ;;
1100 ?) usage "Invalid option: $opt" ;;
1101 esac
1103 done
1105 # Sometimes, developers don't want configure.sh to do a config clean. But
1106 # sometimes they do.
1107 if [ "$clean" -ne 0 ]; then
1108 if [ -f ./Makefile ]; then
1109 make clean_config > /dev/null
1113 # It is an error to say that bc only should be built and likewise for dc.
1114 if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then
1115 usage "Can only specify one of -b(-D) or -d(-B)"
1118 # The library is mutually exclusive to the calculators, so it's an error to
1119 # give an option for either of them.
1120 if [ "$library" -ne 0 ]; then
1121 if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
1122 usage "Must not specify -b(-D) or -d(-B) when building the library"
1126 # KARATSUBA_LEN must be an integer and must be 16 or greater.
1127 case $karatsuba_len in
1128 (*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;;
1129 (*) ;;
1130 esac
1132 if [ "$karatsuba_len" -lt 16 ]; then
1133 usage "KARATSUBA_LEN is less than 16"
1136 set -e
1138 if [ -z "${LONG_BIT+set}" ]; then
1139 LONG_BIT_DEFINE=""
1140 elif [ "$LONG_BIT" -lt 32 ]; then
1141 usage "LONG_BIT is less than 32"
1142 else
1143 LONG_BIT_DEFINE="-DBC_LONG_BIT=$LONG_BIT"
1146 if [ -z "$CC" ]; then
1147 CC="c99"
1148 else
1150 # I had users complain that, if they gave CFLAGS as part of CC, which
1151 # autotools allows in its braindead way, the build would fail with an error.
1152 # I don't like adjusting for autotools, but oh well. These lines puts the
1153 # stuff after the first space into CFLAGS.
1154 ccbase=$(basename "$CC")
1155 suffix=" *"
1156 prefix="* "
1158 if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
1159 ccflags="${ccbase#$prefix}"
1160 cc="${ccbase%%$suffix}"
1161 ccdir=$(dirname "$CC")
1162 if [ "$ccdir" = "." ] && [ "${CC#.}" = "$CC" ]; then
1163 ccdir=""
1164 else
1165 ccdir="$ccdir/"
1167 CC="${ccdir}${cc}"
1168 CFLAGS="$CFLAGS $ccflags"
1172 if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then
1173 HOSTCC="$CC"
1174 elif [ -z "$HOSTCC" ]; then
1175 HOSTCC="$HOST_CC"
1178 if [ "$HOSTCC" != "$CC" ]; then
1180 # Like above, this splits HOSTCC and HOSTCFLAGS.
1181 ccbase=$(basename "$HOSTCC")
1182 suffix=" *"
1183 prefix="* "
1185 if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
1186 ccflags="${ccbase#$prefix}"
1187 cc="${ccbase%%$suffix}"
1188 ccdir=$(dirname "$HOSTCC")
1189 if [ "$ccdir" = "." ] && [ "${HOSTCC#.}" = "$HOSTCC" ]; then
1190 ccdir=""
1191 else
1192 ccdir="$ccdir/"
1194 HOSTCC="${ccdir}${cc}"
1195 HOSTCFLAGS="$HOSTCFLAGS $ccflags"
1199 if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then
1200 HOSTCFLAGS="$CFLAGS"
1201 elif [ -z "${HOSTCFLAGS+set}" ]; then
1202 HOSTCFLAGS="$HOST_CFLAGS"
1205 # Store these for the cross compilation detection later.
1206 OLDCFLAGS="$CFLAGS"
1207 OLDHOSTCFLAGS="$HOSTCFLAGS"
1209 link="@printf 'No link necessary\\\\n'"
1210 main_exec="BC"
1211 executable="BC_EXEC"
1213 tests="test_bc timeconst test_dc"
1215 bc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh bc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(BC_EXEC)"
1216 bc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n bc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(BC_EXEC)"
1217 dc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh dc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(DC_EXEC)"
1218 dc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n dc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(DC_EXEC)"
1220 timeconst="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/bc/timeconst.sh \$(TESTSDIR)/bc/scripts/timeconst.bc \$(BC_EXEC)"
1222 # In order to have cleanup at exit, we need to be in
1223 # debug mode, so don't run valgrind without that.
1224 if [ "$vg" -ne 0 ]; then
1225 debug=1
1226 bc_test_exec='valgrind $(VALGRIND_ARGS) $(BC_EXEC)'
1227 dc_test_exec='valgrind $(VALGRIND_ARGS) $(DC_EXEC)'
1228 bcl_test_exec='valgrind $(VALGRIND_ARGS) $(BCL_TEST)'
1229 else
1230 bc_test_exec='$(BC_EXEC)'
1231 dc_test_exec='$(DC_EXEC)'
1232 bcl_test_exec='$(BCL_TEST)'
1235 test_bc_history_prereqs="test_bc_history_all"
1236 test_dc_history_prereqs="test_dc_history_all"
1238 karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
1239 karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
1241 bc_lib="\$(GEN_DIR)/lib.o"
1242 bc_help="\$(GEN_DIR)/bc_help.o"
1243 dc_help="\$(GEN_DIR)/dc_help.o"
1245 default_target_prereqs="\$(BIN) \$(OBJS)"
1246 default_target_cmd="\$(CC) \$(CFLAGS) \$(OBJS) \$(LDFLAGS) -o \$(EXEC)"
1247 default_target="\$(DC_EXEC)"
1249 second_target_prereqs=""
1250 second_target_cmd="$default_target_cmd"
1251 second_target="\$(BC_EXEC)"
1253 # This if/else if chain is for setting the defaults that change based on whether
1254 # the library is being built, bc only, dc only, or both calculators.
1255 if [ "$library" -ne 0 ]; then
1257 extra_math=1
1258 nls=0
1259 hist=0
1260 bc=1
1261 dc=1
1263 default_target_prereqs="\$(BIN) \$(OBJ)"
1264 default_target_cmd="ar -r -cu \$(LIBBC) \$(OBJ)"
1265 default_target="\$(LIBBC)"
1266 tests="test_library"
1267 test_bc_history_prereqs=" test_bc_history_skip"
1268 test_dc_history_prereqs=" test_dc_history_skip"
1270 install_prereqs=" install_library"
1271 uninstall_prereqs=" uninstall_library"
1272 install_man_prereqs=" install_bcl_manpage"
1273 uninstall_man_prereqs=" uninstall_bcl_manpage"
1275 elif [ "$bc_only" -eq 1 ]; then
1277 bc=1
1278 dc=0
1280 dc_help=""
1282 executables="bc"
1284 dc_test="@printf 'No dc tests to run\\\\n'"
1285 dc_test_np="@printf 'No dc tests to run\\\\n'"
1286 test_dc_history_prereqs=" test_dc_history_skip"
1288 install_prereqs=" install_execs"
1289 install_man_prereqs=" install_bc_manpage"
1290 uninstall_prereqs=" uninstall_bc"
1291 uninstall_man_prereqs=" uninstall_bc_manpage"
1293 default_target="\$(BC_EXEC)"
1294 second_target="\$(DC_EXEC)"
1295 tests="test_bc timeconst"
1297 elif [ "$dc_only" -eq 1 ]; then
1299 bc=0
1300 dc=1
1302 bc_lib=""
1303 bc_help=""
1305 executables="dc"
1307 main_exec="DC"
1308 executable="DC_EXEC"
1310 bc_test="@printf 'No bc tests to run\\\\n'"
1311 bc_test_np="@printf 'No bc tests to run\\\\n'"
1312 test_bc_history_prereqs=" test_bc_history_skip"
1314 timeconst="@printf 'timeconst cannot be run because bc is not built\\\\n'"
1316 install_prereqs=" install_execs"
1317 install_man_prereqs=" install_dc_manpage"
1318 uninstall_prereqs=" uninstall_dc"
1319 uninstall_man_prereqs=" uninstall_dc_manpage"
1321 tests="test_dc"
1323 else
1325 bc=1
1326 dc=1
1328 executables="bc and dc"
1330 karatsuba="@\$(KARATSUBA) 30 0 \$(BC_EXEC)"
1331 karatsuba_test="@\$(KARATSUBA) 1 100 \$(BC_EXEC)"
1333 if [ "$library" -eq 0 ]; then
1334 install_prereqs=" install_execs"
1335 install_man_prereqs=" install_bc_manpage install_dc_manpage"
1336 uninstall_prereqs=" uninstall_bc uninstall_dc"
1337 uninstall_man_prereqs=" uninstall_bc_manpage uninstall_dc_manpage"
1338 else
1339 install_prereqs=" install_library install_bcl_header"
1340 install_man_prereqs=" install_bcl_manpage"
1341 uninstall_prereqs=" uninstall_library uninstall_bcl_header"
1342 uninstall_man_prereqs=" uninstall_bcl_manpage"
1343 tests="test_library"
1346 second_target_prereqs="$default_target_prereqs"
1347 default_target_prereqs="$second_target"
1348 default_target_cmd="\$(LINK) \$(BIN) \$(EXEC_PREFIX)\$(DC)"
1352 # We need specific stuff for fuzzing.
1353 if [ "$fuzz" -ne 0 ]; then
1354 debug=1
1355 hist=0
1356 nls=0
1357 optimization="3"
1360 # This sets some necessary things for debug mode.
1361 if [ "$debug" -eq 1 ]; then
1363 if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then
1364 CFLAGS="-O0"
1367 CFLAGS="-g $CFLAGS"
1369 else
1370 CPPFLAGS="-DNDEBUG $CPPFLAGS"
1373 # Set optimization CFLAGS.
1374 if [ -n "$optimization" ]; then
1375 CFLAGS="-O$optimization $CFLAGS"
1378 # Set test coverage defaults.
1379 if [ "$coverage" -eq 1 ]; then
1381 if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
1382 usage "Can only specify -c without -b or -d"
1385 CFLAGS="-fprofile-arcs -ftest-coverage -g -O0 $CFLAGS"
1386 CPPFLAGS="-DNDEBUG $CPPFLAGS"
1388 COVERAGE_OUTPUT="@gcov -pabcdf \$(GCDA) \$(BC_GCDA) \$(DC_GCDA) \$(HISTORY_GCDA) \$(RAND_GCDA)"
1389 COVERAGE_OUTPUT="$COVERAGE_OUTPUT;\$(RM) -f \$(GEN)*.gc*"
1390 COVERAGE_OUTPUT="$COVERAGE_OUTPUT;gcovr --exclude-unreachable-branches --exclude-throw-branches --html-details --output index.html"
1391 COVERAGE_PREREQS=" test coverage_output"
1393 else
1394 COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'"
1395 COVERAGE_PREREQS=""
1399 # Set some defaults.
1400 if [ -z "${DESTDIR+set}" ]; then
1401 destdir=""
1402 else
1403 destdir="DESTDIR = $DESTDIR"
1406 # defprefix is for a warning about locales later.
1407 if [ -z "${PREFIX+set}" ]; then
1408 PREFIX="/usr/local"
1409 defprefix=1
1410 else
1411 defprefix=0
1414 if [ -z "${BINDIR+set}" ]; then
1415 BINDIR="$PREFIX/bin"
1418 if [ -z "${INCLUDEDIR+set}" ]; then
1419 INCLUDEDIR="$PREFIX/include"
1422 if [ -z "${LIBDIR+set}" ]; then
1423 LIBDIR="$PREFIX/lib"
1426 if [ -z "${PC_PATH+set}" ]; then
1428 set +e
1430 command -v pkg-config > /dev/null
1431 err=$?
1433 set -e
1435 if [ "$err" -eq 0 ]; then
1436 PC_PATH=$(pkg-config --variable=pc_path pkg-config)
1437 PC_PATH="${PC_PATH%%:*}"
1438 else
1439 PC_PATH=""
1444 # Set a default for the DATAROOTDIR. This is done if either manpages will be
1445 # installed, or locales are enabled because that's probably where NLSPATH
1446 # points.
1447 if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then
1448 if [ -z "${DATAROOTDIR+set}" ]; then
1449 DATAROOTDIR="$PREFIX/share"
1453 # Set defaults for manpage environment variables.
1454 if [ "$install_manpages" -ne 0 ]; then
1456 if [ -z "${DATADIR+set}" ]; then
1457 DATADIR="$DATAROOTDIR"
1460 if [ -z "${MANDIR+set}" ]; then
1461 MANDIR="$DATADIR/man"
1464 if [ -z "${MAN1DIR+set}" ]; then
1465 MAN1DIR="$MANDIR/man1"
1468 if [ -z "${MAN3DIR+set}" ]; then
1469 MAN3DIR="$MANDIR/man3"
1472 else
1473 install_man_prereqs=""
1474 uninstall_man_prereqs=""
1477 # Here is where we test NLS (the locale system). This is done by trying to
1478 # compile src/vm.c, which has the relevant code. If it fails, then it is
1479 # disabled.
1480 if [ "$nls" -ne 0 ]; then
1482 set +e
1484 printf 'Testing NLS...\n'
1486 flags="-DBC_ENABLE_NLS=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
1487 flags="$flags -DBC_ENABLE_HISTORY=$hist -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0"
1488 flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/"
1489 flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
1491 ccbase=$(basename "$CC")
1493 if [ "$ccbase" = "clang" ]; then
1494 flags="$flags -Wno-unreachable-code"
1497 "$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/vm.c" -o "./vm.o" > /dev/null 2>&1
1499 err="$?"
1501 rm -rf "./vm.o"
1503 # If this errors, it is probably because of building on Windows,
1504 # and NLS is not supported on Windows, so disable it.
1505 if [ "$err" -ne 0 ]; then
1506 printf 'NLS does not work.\n'
1507 if [ $force -eq 0 ]; then
1508 printf 'Disabling NLS...\n\n'
1509 nls=0
1510 else
1511 printf 'Forcing NLS...\n\n'
1513 else
1514 printf 'NLS works.\n\n'
1516 printf 'Testing gencat...\n'
1517 gencat "./en_US.cat" "$scriptdir/locales/en_US.msg" > /dev/null 2>&1
1519 err="$?"
1521 rm -rf "./en_US.cat"
1523 if [ "$err" -ne 0 ]; then
1524 printf 'gencat does not work.\n'
1525 if [ $force -eq 0 ]; then
1526 printf 'Disabling NLS...\n\n'
1527 nls=0
1528 else
1529 printf 'Forcing NLS...\n\n'
1531 else
1533 printf 'gencat works.\n\n'
1535 # It turns out that POSIX locales are really terrible, and running
1536 # gencat on one machine is not guaranteed to make those cat files
1537 # portable to another machine, so we had better warn the user here.
1538 if [ "$HOSTCC" != "$CC" ] || [ "$OLDHOSTCFLAGS" != "$OLDCFLAGS" ]; then
1539 printf 'Cross-compile detected.\n\n'
1540 printf 'WARNING: Catalog files generated with gencat may not be portable\n'
1541 printf ' across different architectures.\n\n'
1544 if [ -z "$NLSPATH" ]; then
1545 NLSPATH="/usr/share/locale/%L/%N"
1548 install_locales_prereqs=" install_locales"
1549 uninstall_locales_prereqs=" uninstall_locales"
1555 set -e
1557 else
1558 install_locales_prereqs=""
1559 uninstall_locales_prereqs=""
1560 all_locales=0
1563 if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then
1564 install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
1565 else
1566 install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
1569 # Like the above tested locale support, this tests history.
1570 if [ "$hist" -eq 1 ]; then
1572 if [ "$hist_impl" = "editline" ]; then
1573 editline=1
1574 readline=0
1575 elif [ "$hist_impl" = "readline" ]; then
1576 editline=0
1577 readline=1
1578 else
1579 editline=0
1580 readline=0
1583 set +e
1585 printf 'Testing history...\n'
1587 flags="-DBC_ENABLE_HISTORY=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
1588 flags="$flags -DBC_ENABLE_NLS=$nls -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0"
1589 flags="$flags -DBC_ENABLE_EDITLINE=$editline -DBC_ENABLE_READLINE=$readline"
1590 flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/"
1591 flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
1593 "$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/history.c" -o "./history.o" > /dev/null 2>&1
1595 err="$?"
1597 rm -rf "./history.o"
1599 # If this errors, it is probably because of building on Windows,
1600 # and history is not supported on Windows, so disable it.
1601 if [ "$err" -ne 0 ]; then
1602 printf 'History does not work.\n'
1603 if [ $force -eq 0 ]; then
1604 printf 'Disabling history...\n\n'
1605 hist=0
1606 else
1607 printf 'Forcing history...\n\n'
1609 else
1610 printf 'History works.\n\n'
1613 set -e
1615 else
1617 editline=0
1618 readline=0
1622 # We have to disable the history tests if it is disabled or valgrind is on. Or
1623 # if we are using editline or readline.
1624 if [ "$hist" -eq 0 ] || [ "$vg" -ne 0 ]; then
1625 test_bc_history_prereqs=" test_bc_history_skip"
1626 test_dc_history_prereqs=" test_dc_history_skip"
1627 history_tests="@printf 'Skipping history tests...\\\\n'"
1628 CFLAGS="$CFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=0"
1629 else
1631 if [ "$editline" -eq 0 ] && [ "$readline" -eq 0 ]; then
1632 history_tests="@printf '\$(TEST_STARS)\\\\n\\\\nRunning history tests...\\\\n\\\\n'"
1633 history_tests="$history_tests \&\& \$(TESTSDIR)/history.sh bc -a \&\&"
1634 history_tests="$history_tests \$(TESTSDIR)/history.sh dc -a \&\& printf"
1635 history_tests="$history_tests '\\\\nAll history tests passed.\\\\n\\\\n\$(TEST_STARS)\\\\n'"
1636 else
1637 test_bc_history_prereqs=" test_bc_history_skip"
1638 test_dc_history_prereqs=" test_dc_history_skip"
1639 history_tests="@printf 'Skipping history tests...\\\\n'"
1642 # We are also setting the CFLAGS and LDFLAGS here.
1643 if [ "$editline" -ne 0 ]; then
1644 LDFLAGS="$LDFLAGS -ledit"
1645 CPPFLAGS="$CPPFLAGS -DBC_ENABLE_EDITLINE=1 -DBC_ENABLE_READLINE=0"
1646 elif [ "$readline" -ne 0 ]; then
1647 LDFLAGS="$LDFLAGS -lreadline"
1648 CPPFLAGS="$CPPFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=1"
1649 else
1650 CPPFLAGS="$CPPFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=0"
1655 # Test FreeBSD. This is not in an if statement because regardless of whatever
1656 # the user says, we need to know if we are on FreeBSD. If we are, we cannot set
1657 # _POSIX_C_SOURCE and _XOPEN_SOURCE. The FreeBSD headers turn *off* stuff when
1658 # that is done.
1659 set +e
1660 printf 'Testing for FreeBSD...\n'
1662 flags="-DBC_TEST_FREEBSD -DBC_ENABLE_AFL=0"
1663 "$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1
1665 err="$?"
1667 if [ "$err" -ne 0 ]; then
1668 printf 'On FreeBSD. Not using _POSIX_C_SOURCE and _XOPEN_SOURCE.\n\n'
1669 else
1670 printf 'Not on FreeBSD. Using _POSIX_C_SOURCE and _XOPEN_SOURCE.\n\n'
1671 CPPFLAGS="$CPPFLAGS -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
1674 # Test macOS. This is not in an if statement because regardless of whatever the
1675 # user says, we need to know if we are on macOS. If we are, we have to set
1676 # _DARWIN_C_SOURCE.
1677 printf 'Testing for macOS...\n'
1679 flags="-DBC_TEST_APPLE -DBC_ENABLE_AFL=0"
1680 "$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1
1682 err="$?"
1684 if [ "$err" -ne 0 ]; then
1685 printf 'On macOS. Using _DARWIN_C_SOURCE.\n\n'
1686 apple="-D_DARWIN_C_SOURCE"
1687 else
1688 printf 'Not on macOS.\n\n'
1689 apple=""
1692 # We can't use the linker's strip flag on macOS.
1693 if [ "$debug" -eq 0 ] && [ "$apple" = "" ] && [ "$strip_bin" -ne 0 ]; then
1694 LDFLAGS="-s $LDFLAGS"
1697 # Test OpenBSD. This is not in an if statement because regardless of whatever
1698 # the user says, we need to know if we are on OpenBSD to activate _BSD_SOURCE.
1699 # No, I cannot `#define _BSD_SOURCE` in a header because OpenBSD's patched GCC
1700 # and Clang complain that that is only allowed for system headers. Sigh....So we
1701 # have to check at configure time and set it on the compiler command-line. And
1702 # we have to set it because we also set _POSIX_C_SOURCE, which OpenBSD headers
1703 # detect, and when they detect it, they turn off _BSD_SOURCE unless it is
1704 # specifically requested.
1705 printf 'Testing for OpenBSD...\n'
1707 flags="-DBC_TEST_OPENBSD -DBC_ENABLE_AFL=0"
1708 "$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1
1710 err="$?"
1712 if [ "$err" -ne 0 ]; then
1714 printf 'On OpenBSD. Using _BSD_SOURCE.\n\n'
1715 bsd="-D_BSD_SOURCE"
1717 # Readline errors on OpenBSD, for some weird reason.
1718 if [ "$readline" -ne 0 ]; then
1719 usage "Cannot use readline on OpenBSD"
1722 else
1723 printf 'Not on OpenBSD.\n\n'
1724 bsd=""
1727 set -e
1729 if [ "$library" -eq 1 ]; then
1730 bc_lib=""
1733 if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ] && [ "$library" -eq 0 ]; then
1734 BC_LIB2_O="\$(GEN_DIR)/lib2.o"
1735 else
1736 BC_LIB2_O=""
1739 GEN_DIR="$scriptdir/gen"
1741 # These lines set the appropriate targets based on whether `gen/strgen.c` or
1742 # `gen/strgen.sh` is used.
1743 GEN="strgen"
1744 GEN_EXEC_TARGET="\$(HOSTCC) -DBC_ENABLE_AFL=0 -I$scriptdir/include/ \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)"
1745 CLEAN_PREREQS=" clean_gen clean_coverage"
1747 if [ -z "${GEN_HOST+set}" ]; then
1748 GEN_HOST=1
1749 else
1750 if [ "$GEN_HOST" -eq 0 ]; then
1751 GEN="strgen.sh"
1752 GEN_EXEC_TARGET="@printf 'Do not need to build gen/strgen.c\\\\n'"
1753 CLEAN_PREREQS=" clean_coverage"
1757 manpage_args=""
1758 unneeded=""
1759 headers="\$(HEADERS)"
1761 # This series of if statements figure out what source files are *not* needed.
1762 if [ "$extra_math" -eq 0 ]; then
1763 exclude_extra_math=1
1764 manpage_args="E"
1765 unneeded="$unneeded rand.c"
1766 else
1767 exclude_extra_math=0
1768 headers="$headers \$(EXTRA_MATH_HEADERS)"
1771 # All of these next if statements set the build type and mark certain source
1772 # files as unneeded so that they won't have targets generated for them.
1774 if [ "$hist" -eq 0 ]; then
1775 manpage_args="${manpage_args}H"
1776 unneeded="$unneeded history.c"
1777 else
1778 headers="$headers \$(HISTORY_HEADERS)"
1781 if [ "$nls" -eq 0 ]; then
1782 manpage_args="${manpage_args}N"
1785 if [ "$bc" -eq 0 ]; then
1786 unneeded="$unneeded bc.c bc_lex.c bc_parse.c"
1787 else
1788 headers="$headers \$(BC_HEADERS)"
1791 if [ "$dc" -eq 0 ]; then
1792 unneeded="$unneeded dc.c dc_lex.c dc_parse.c"
1793 else
1794 headers="$headers \$(DC_HEADERS)"
1797 # This convoluted mess does pull the version out. If you change the format of
1798 # include/version.h, you may have to change this line.
1799 version=$(cat "$scriptdir/include/version.h" | grep "VERSION " - | awk '{ print $3 }' -)
1801 if [ "$library" -ne 0 ]; then
1803 unneeded="$unneeded args.c opt.c read.c file.c main.c"
1804 unneeded="$unneeded lang.c lex.c parse.c program.c"
1805 unneeded="$unneeded bc.c bc_lex.c bc_parse.c"
1806 unneeded="$unneeded dc.c dc_lex.c dc_parse.c"
1807 headers="$headers \$(LIBRARY_HEADERS)"
1809 if [ "$PC_PATH" != "" ]; then
1811 contents=$(cat "$scriptdir/bcl.pc.in")
1813 contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR")
1814 contents=$(replace "$contents" "LIBDIR" "$LIBDIR")
1815 contents=$(replace "$contents" "VERSION" "$version")
1817 printf '%s\n' "$contents" > "$scriptdir/bcl.pc"
1819 pkg_config_install="\$(SAFE_INSTALL) \$(PC_INSTALL_ARGS) \"\$(BCL_PC)\" \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\""
1820 pkg_config_uninstall="\$(RM) -f \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\""
1822 else
1824 pkg_config_install=""
1825 pkg_config_uninstall=""
1829 else
1831 unneeded="$unneeded library.c"
1833 PC_PATH=""
1834 pkg_config_install=""
1835 pkg_config_uninstall=""
1839 # library.c is not needed under normal circumstances.
1840 if [ "$unneeded" = "" ]; then
1841 unneeded="library.c"
1844 # This sets the appropriate manpage for a full build.
1845 if [ "$manpage_args" = "" ]; then
1846 manpage_args="A"
1849 if [ "$vg" -ne 0 ]; then
1850 memcheck=1
1853 if [ "$bc_default_prompt" = "" ]; then
1854 bc_default_prompt="$bc_default_tty_mode"
1857 if [ "$dc_default_prompt" = "" ]; then
1858 dc_default_prompt="$dc_default_tty_mode"
1861 # Generate the test targets and prerequisites.
1862 bc_tests=$(gen_std_test_targets bc)
1863 bc_script_tests=$(gen_script_test_targets bc)
1864 bc_err_tests=$(gen_err_test_targets bc)
1865 dc_tests=$(gen_std_test_targets dc)
1866 dc_script_tests=$(gen_script_test_targets dc)
1867 dc_err_tests=$(gen_err_test_targets dc)
1869 printf 'unneeded: %s\n' "$unneeded"
1871 # Print out the values; this is for debugging.
1872 printf 'Version: %s\n' "$version"
1874 if [ "$bc" -ne 0 ]; then
1875 printf 'Building bc\n'
1876 else
1877 printf 'Not building bc\n'
1879 if [ "$dc" -ne 0 ]; then
1880 printf 'Building dc\n'
1881 else
1882 printf 'Not building dc\n'
1884 printf '\n'
1885 printf 'BC_ENABLE_LIBRARY=%s\n\n' "$library"
1886 printf 'BC_ENABLE_HISTORY=%s\n' "$hist"
1887 printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math"
1888 printf 'BC_ENABLE_NLS=%s\n\n' "$nls"
1889 printf 'BC_ENABLE_AFL=%s\n' "$fuzz"
1890 printf '\n'
1891 printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len"
1892 printf '\n'
1893 printf 'CC=%s\n' "$CC"
1894 printf 'CFLAGS=%s\n' "$CFLAGS"
1895 printf 'HOSTCC=%s\n' "$HOSTCC"
1896 printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS"
1897 printf 'CPPFLAGS=%s\n' "$CPPFLAGS"
1898 printf 'LDFLAGS=%s\n' "$LDFLAGS"
1899 printf 'PREFIX=%s\n' "$PREFIX"
1900 printf 'BINDIR=%s\n' "$BINDIR"
1901 printf 'INCLUDEDIR=%s\n' "$INCLUDEDIR"
1902 printf 'LIBDIR=%s\n' "$LIBDIR"
1903 printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR"
1904 printf 'DATADIR=%s\n' "$DATADIR"
1905 printf 'MANDIR=%s\n' "$MANDIR"
1906 printf 'MAN1DIR=%s\n' "$MAN1DIR"
1907 printf 'MAN3DIR=%s\n' "$MAN3DIR"
1908 printf 'NLSPATH=%s\n' "$NLSPATH"
1909 printf 'PC_PATH=%s\n' "$PC_PATH"
1910 printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX"
1911 printf 'EXECPREFIX=%s\n' "$EXECPREFIX"
1912 printf 'DESTDIR=%s\n' "$DESTDIR"
1913 printf 'LONG_BIT=%s\n' "$LONG_BIT"
1914 printf 'GEN_HOST=%s\n' "$GEN_HOST"
1915 printf 'GEN_EMU=%s\n' "$GEN_EMU"
1916 printf '\n'
1917 printf 'Setting Defaults\n'
1918 printf '================\n'
1919 printf 'bc.banner=%s\n' "$bc_default_banner"
1920 printf 'bc.sigint_reset=%s\n' "$bc_default_sigint_reset"
1921 printf 'dc.sigint_reset=%s\n' "$dc_default_sigint_reset"
1922 printf 'bc.tty_mode=%s\n' "$bc_default_tty_mode"
1923 printf 'dc.tty_mode=%s\n' "$dc_default_tty_mode"
1924 printf 'bc.prompt=%s\n' "$bc_default_prompt"
1925 printf 'dc.prompt=%s\n' "$dc_default_prompt"
1926 printf 'bc.expr_exit=%s\n' "$bc_default_expr_exit"
1927 printf 'dc.expr_exit=%s\n' "$dc_default_expr_exit"
1928 printf 'bc.digit_clamp=%s\n' "$bc_default_digit_clamp"
1929 printf 'dc.digit_clamp=%s\n' "$dc_default_digit_clamp"
1931 # This code outputs a warning. The warning is to not surprise users when locales
1932 # are installed outside of the prefix. This warning is suppressed when the
1933 # default prefix is used, as well, so as not to panic users just installing by
1934 # hand. I believe this will be okay because NLSPATH is usually in /usr and the
1935 # default prefix is /usr/local, so they'll be close that way.
1936 if [ "$nls" -ne 0 ] && [ "${NLSPATH#$PREFIX}" = "${NLSPATH}" ] && [ "$defprefix" -eq 0 ]; then
1937 printf '\n********************************************************************************\n\n'
1938 printf 'WARNING: Locales will *NOT* be installed in $PREFIX (%s).\n' "$PREFIX"
1939 printf '\n'
1940 printf ' This is because they *MUST* be installed at a fixed location to even\n'
1941 printf ' work, and that fixed location is $NLSPATH (%s).\n' "$NLSPATH"
1942 printf '\n'
1943 printf ' This location is *outside* of $PREFIX. If you do not wish to install\n'
1944 printf ' locales outside of $PREFIX, you must disable NLS with the -N or the\n'
1945 printf ' --disable-nls options.\n'
1946 printf '\n'
1947 printf ' The author apologizes for the inconvenience, but the need to install\n'
1948 printf ' the locales at a fixed location is mandated by POSIX, and it is not\n'
1949 printf ' possible for the author to change that requirement.\n'
1950 printf '\n********************************************************************************\n'
1953 # This is where the real work begins. This is the point at which the Makefile.in
1954 # template is edited and output to the Makefile.
1956 contents=$(cat "$scriptdir/Makefile.in")
1958 needle="WARNING"
1959 replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***'
1961 contents=$(replace "$contents" "$needle" "$replacement")
1963 # The contents are edited to have the list of files to build.
1964 contents=$(gen_file_list "$contents" $unneeded)
1966 SRC_TARGETS=""
1968 # This line and loop generates the individual targets for source files. I used
1969 # to just use an implicit target, but that was found to be inadequate when I
1970 # added the library.
1971 src_files=$(find_src_files $unneeded)
1973 for f in $src_files; do
1974 o=$(replace_ext "$f" "c" "o")
1975 o=$(basename "$o")
1976 SRC_TARGETS=$(printf '%s\n\nsrc/%s: src %s %s\n\t$(CC) $(CFLAGS) -o src/%s -c %s\n' \
1977 "$SRC_TARGETS" "$o" "$headers" "$f" "$o" "$f")
1978 done
1980 # Replace all the placeholders.
1981 contents=$(replace "$contents" "ROOTDIR" "$scriptdir")
1982 contents=$(replace "$contents" "BUILDDIR" "$builddir")
1984 contents=$(replace "$contents" "HEADERS" "$headers")
1986 contents=$(replace "$contents" "BC_ENABLED" "$bc")
1987 contents=$(replace "$contents" "DC_ENABLED" "$dc")
1989 contents=$(replace "$contents" "BC_ALL_TESTS" "$bc_test")
1990 contents=$(replace "$contents" "BC_ALL_TESTS_NP" "$bc_test_np")
1991 contents=$(replace "$contents" "BC_TESTS" "$bc_tests")
1992 contents=$(replace "$contents" "BC_SCRIPT_TESTS" "$bc_script_tests")
1993 contents=$(replace "$contents" "BC_ERROR_TESTS" "$bc_err_tests")
1994 contents=$(replace "$contents" "BC_TEST_EXEC" "$bc_test_exec")
1995 contents=$(replace "$contents" "TIMECONST_ALL_TESTS" "$timeconst")
1997 contents=$(replace "$contents" "DC_ALL_TESTS" "$dc_test")
1998 contents=$(replace "$contents" "DC_ALL_TESTS_NP" "$dc_test_np")
1999 contents=$(replace "$contents" "DC_TESTS" "$dc_tests")
2000 contents=$(replace "$contents" "DC_SCRIPT_TESTS" "$dc_script_tests")
2001 contents=$(replace "$contents" "DC_ERROR_TESTS" "$dc_err_tests")
2002 contents=$(replace "$contents" "DC_TEST_EXEC" "$dc_test_exec")
2004 contents=$(replace "$contents" "BCL_TEST_EXEC" "$bcl_test_exec")
2006 contents=$(replace "$contents" "BUILD_TYPE" "$manpage_args")
2007 contents=$(replace "$contents" "EXCLUDE_EXTRA_MATH" "$exclude_extra_math")
2009 contents=$(replace "$contents" "LIBRARY" "$library")
2010 contents=$(replace "$contents" "HISTORY" "$hist")
2011 contents=$(replace "$contents" "EXTRA_MATH" "$extra_math")
2012 contents=$(replace "$contents" "NLS" "$nls")
2013 contents=$(replace "$contents" "FUZZ" "$fuzz")
2014 contents=$(replace "$contents" "MEMCHECK" "$memcheck")
2016 contents=$(replace "$contents" "BC_LIB_O" "$bc_lib")
2017 contents=$(replace "$contents" "BC_HELP_O" "$bc_help")
2018 contents=$(replace "$contents" "DC_HELP_O" "$dc_help")
2019 contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O")
2020 contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len")
2022 contents=$(replace "$contents" "NLSPATH" "$NLSPATH")
2023 contents=$(replace "$contents" "DESTDIR" "$destdir")
2024 contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX")
2025 contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX")
2026 contents=$(replace "$contents" "BINDIR" "$BINDIR")
2027 contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR")
2028 contents=$(replace "$contents" "LIBDIR" "$LIBDIR")
2029 contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR")
2030 contents=$(replace "$contents" "MAN3DIR" "$MAN3DIR")
2031 contents=$(replace "$contents" "CFLAGS" "$CFLAGS")
2032 contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS")
2033 contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS")
2034 contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS")
2035 contents=$(replace "$contents" "CC" "$CC")
2036 contents=$(replace "$contents" "HOSTCC" "$HOSTCC")
2037 contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT")
2038 contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS")
2039 contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs")
2040 contents=$(replace "$contents" "INSTALL_MAN_PREREQS" "$install_man_prereqs")
2041 contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales")
2042 contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs")
2043 contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs")
2044 contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs")
2045 contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs")
2047 contents=$(replace "$contents" "PC_PATH" "$PC_PATH")
2048 contents=$(replace "$contents" "PKG_CONFIG_INSTALL" "$pkg_config_install")
2049 contents=$(replace "$contents" "PKG_CONFIG_UNINSTALL" "$pkg_config_uninstall")
2051 contents=$(replace "$contents" "DEFAULT_TARGET" "$default_target")
2052 contents=$(replace "$contents" "DEFAULT_TARGET_PREREQS" "$default_target_prereqs")
2053 contents=$(replace "$contents" "DEFAULT_TARGET_CMD" "$default_target_cmd")
2054 contents=$(replace "$contents" "SECOND_TARGET" "$second_target")
2055 contents=$(replace "$contents" "SECOND_TARGET_PREREQS" "$second_target_prereqs")
2056 contents=$(replace "$contents" "SECOND_TARGET_CMD" "$second_target_cmd")
2058 contents=$(replace "$contents" "ALL_PREREQ" "$ALL_PREREQ")
2059 contents=$(replace "$contents" "BC_EXEC_PREREQ" "$bc_exec_prereq")
2060 contents=$(replace "$contents" "BC_EXEC_CMD" "$bc_exec_cmd")
2061 contents=$(replace "$contents" "DC_EXEC_PREREQ" "$dc_exec_prereq")
2062 contents=$(replace "$contents" "DC_EXEC_CMD" "$dc_exec_cmd")
2064 contents=$(replace "$contents" "EXECUTABLES" "$executables")
2065 contents=$(replace "$contents" "MAIN_EXEC" "$main_exec")
2066 contents=$(replace "$contents" "EXEC" "$executable")
2067 contents=$(replace "$contents" "TESTS" "$tests")
2069 contents=$(replace "$contents" "BC_HISTORY_TEST_PREREQS" "$test_bc_history_prereqs")
2070 contents=$(replace "$contents" "DC_HISTORY_TEST_PREREQS" "$test_dc_history_prereqs")
2071 contents=$(replace "$contents" "HISTORY_TESTS" "$history_tests")
2073 contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test")
2074 contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test")
2076 contents=$(replace "$contents" "TIMECONST" "$timeconst")
2078 contents=$(replace "$contents" "KARATSUBA" "$karatsuba")
2079 contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test")
2081 contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE")
2083 contents=$(replace "$contents" "GEN_DIR" "$GEN_DIR")
2084 contents=$(replace "$contents" "GEN" "$GEN")
2085 contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET")
2086 contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS")
2087 contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU")
2089 contents=$(replace "$contents" "BSD" "$bsd")
2090 contents=$(replace "$contents" "APPLE" "$apple")
2092 contents=$(replace "$contents" "BC_DEFAULT_BANNER" "$bc_default_banner")
2093 contents=$(replace "$contents" "BC_DEFAULT_SIGINT_RESET" "$bc_default_sigint_reset")
2094 contents=$(replace "$contents" "DC_DEFAULT_SIGINT_RESET" "$dc_default_sigint_reset")
2095 contents=$(replace "$contents" "BC_DEFAULT_TTY_MODE" "$bc_default_tty_mode")
2096 contents=$(replace "$contents" "DC_DEFAULT_TTY_MODE" "$dc_default_tty_mode")
2097 contents=$(replace "$contents" "BC_DEFAULT_PROMPT" "$bc_default_prompt")
2098 contents=$(replace "$contents" "DC_DEFAULT_PROMPT" "$dc_default_prompt")
2099 contents=$(replace "$contents" "BC_DEFAULT_EXPR_EXIT" "$bc_default_expr_exit")
2100 contents=$(replace "$contents" "DC_DEFAULT_EXPR_EXIT" "$dc_default_expr_exit")
2101 contents=$(replace "$contents" "BC_DEFAULT_DIGIT_CLAMP" "$bc_default_digit_clamp")
2102 contents=$(replace "$contents" "DC_DEFAULT_DIGIT_CLAMP" "$dc_default_digit_clamp")
2104 # Do the first print to the Makefile.
2105 printf '%s\n%s\n\n' "$contents" "$SRC_TARGETS" > "Makefile"
2107 # Generate the individual test targets.
2108 if [ "$bc" -ne 0 ]; then
2109 gen_std_tests bc "$extra_math" "$time_tests" $bc_test_exec
2110 gen_script_tests bc "$extra_math" "$generate_tests" "$time_tests" $bc_test_exec
2111 gen_err_tests bc $bc_test_exec
2114 if [ "$dc" -ne 0 ]; then
2115 gen_std_tests dc "$extra_math" "$time_tests" $dc_test_exec
2116 gen_script_tests dc "$extra_math" "$generate_tests" "$time_tests" $dc_test_exec
2117 gen_err_tests dc $dc_test_exec
2120 # Copy the correct manuals to the expected places.
2121 mkdir -p manuals
2122 cp -f "$scriptdir/manuals/bc/$manpage_args.1.md" manuals/bc.1.md
2123 cp -f "$scriptdir/manuals/bc/$manpage_args.1" manuals/bc.1
2124 cp -f "$scriptdir/manuals/dc/$manpage_args.1.md" manuals/dc.1.md
2125 cp -f "$scriptdir/manuals/dc/$manpage_args.1" manuals/dc.1
2127 make clean > /dev/null