xref: /freebsd/contrib/bc/configure.sh (revision e17f5b1d)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2020 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14#   this list of conditions and the following disclaimer in the documentation
15#   and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30script="$0"
31scriptdir=$(dirname "$script")
32script=$(basename "$script")
33
34. "$scriptdir/functions.sh"
35
36usage() {
37
38	if [ $# -gt 0 ]; then
39
40		_usage_val=1
41
42		printf "%s\n\n" "$1"
43
44	else
45		_usage_val=0
46	fi
47
48	printf 'usage: %s -h\n' "$script"
49	printf '       %s --help\n' "$script"
50	printf '       %s [-bD|-dB|-c] [-EfgGHlMNPT] [-O OPT_LEVEL] [-k KARATSUBA_LEN]\n' "$script"
51	printf '       %s \\\n' "$script"
52	printf '           [--bc-only --disable-dc|--dc-only --disable-bc|--coverage]      \\\n'
53	printf '           [--debug --disable-extra-math --disable-generated-tests]        \\\n'
54	printf '           [--disable-history --disable-man-pages --disable-nls]           \\\n'
55	printf '           [--disable-prompt --disable-strip] [--install-all-locales]      \\\n'
56	printf '           [--opt=OPT_LEVEL] [--karatsuba-len=KARATSUBA_LEN]               \\\n'
57	printf '           [--prefix=PREFIX] [--bindir=BINDIR] [--datarootdir=DATAROOTDIR] \\\n'
58	printf '           [--datadir=DATADIR] [--mandir=MANDIR] [--man1dir=MAN1DIR]       \\\n'
59	printf '           [--force]                                                       \\\n'
60	printf '\n'
61	printf '    -b, --bc-only\n'
62	printf '        Build bc only. It is an error if "-d", "--dc-only", "-B", or "--disable-bc"\n'
63	printf '        are specified too.\n'
64	printf '    -B, --disable-bc\n'
65	printf '        Disable bc. It is an error if "-b", "--bc-only", "-D", or "--disable-dc"\n'
66	printf '        are specified too.\n'
67	printf '    -c, --coverage\n'
68	printf '        Generate test coverage code. Requires gcov and regcovr.\n'
69	printf '        It is an error if either "-b" ("-D") or "-d" ("-B") is specified.\n'
70	printf '        Requires a compiler that use gcc-compatible coverage options\n'
71	printf '    -d, --dc-only\n'
72	printf '        Build dc only. It is an error if "-b", "--bc-only", "-D", or "--disable-dc"\n'
73	printf '        are specified too.\n'
74	printf '    -D, --disable-dc\n'
75	printf '        Disable dc. It is an error if "-d", "--dc-only" "-B", or "--disable-bc"\n'
76	printf '        are specified too.\n'
77	printf '    -E, --disable-extra-math\n'
78	printf '        Disable extra math. This includes: "$" operator (truncate to integer),\n'
79	printf '        "@" operator (set number of decimal places), and r(x, p) (rounding\n'
80	printf '        function). Additionally, this option disables the extra printing\n'
81	printf '        functions in the math library.\n'
82	printf '    -f, --force\n'
83	printf '        Force use of all enabled options, even if they do not work. This\n'
84	printf '        option is to allow the maintainer a way to test that certain options\n'
85	printf '        are not failing invisibly. (Development only.)'
86	printf '    -g, --debug\n'
87	printf '        Build in debug mode. Adds the "-g" flag, and if there are no\n'
88	printf '        other CFLAGS, and "-O" was not given, this also adds the "-O0"\n'
89	printf '        flag. If this flag is *not* given, "-DNDEBUG" is added to CPPFLAGS\n'
90	printf '        and a strip flag is added to the link stage.\n'
91	printf '    -G, --disable-generated-tests\n'
92	printf '        Disable generating tests. This is for platforms that do not have a\n'
93	printf '        GNU bc-compatible bc to generate tests.\n'
94	printf '    -h, --help\n'
95	printf '        Print this help message and exit.\n'
96	printf '    -H, --disable-history\n'
97	printf '        Disable history.\n'
98	printf '    -k KARATSUBA_LEN, --karatsuba-len KARATSUBA_LEN\n'
99	printf '        Set the karatsuba length to KARATSUBA_LEN (default is 64).\n'
100	printf '        It is an error if KARATSUBA_LEN is not a number or is less than 16.\n'
101	printf '    -l, --install-all-locales\n'
102	printf '        Installs all locales, regardless of how many are on the system. This\n'
103	printf '        option is useful for package maintainers who want to make sure that\n'
104	printf '        a package contains all of the locales that end users might need.\n'
105	printf '    -M, --disable-man-pages\n'
106	printf '        Disable installing manpages.\n'
107	printf '    -N, --disable-nls\n'
108	printf '        Disable POSIX locale (NLS) support.\n'
109	printf '    -O OPT_LEVEL, --opt OPT_LEVEL\n'
110	printf '        Set the optimization level. This can also be included in the CFLAGS,\n'
111	printf '        but it is provided, so maintainers can build optimized debug builds.\n'
112	printf '        This is passed through to the compiler, so it must be supported.\n'
113	printf '    -P, --disable-prompt\n'
114	printf '        Disables the prompt in the built bc. The prompt will never show up,\n'
115	printf '        or in other words, it will be permanently disabled and cannot be\n'
116	printf '        enabled.\n'
117	printf '    -T, --disable-strip\n'
118	printf '        Disable stripping symbols from the compiled binary or binaries.\n'
119	printf '        Stripping symbols only happens when debug mode is off.\n'
120	printf '    --prefix PREFIX\n'
121	printf '        The prefix to install to. Overrides "$PREFIX" if it exists.\n'
122	printf '        If PREFIX is "/usr", install path will be "/usr/bin".\n'
123	printf '        Default is "/usr/local".\n'
124	printf '    --bindir BINDIR\n'
125	printf '        The directory to install binaries. Overrides "$BINDIR" if it exists.\n'
126	printf '        Default is "$PREFIX/bin".\n'
127	printf '    --datarootdir DATAROOTDIR\n'
128	printf '        The root location for data files. Overrides "$DATAROOTDIR" if it exists.\n'
129	printf '        Default is "$PREFIX/share".\n'
130	printf '    --datadir DATADIR\n'
131	printf '        The location for data files. Overrides "$DATADIR" if it exists.\n'
132	printf '        Default is "$DATAROOTDIR".\n'
133	printf '    --mandir MANDIR\n'
134	printf '        The location to install manpages to. Overrides "$MANDIR" if it exists.\n'
135	printf '        Default is "$DATADIR/man".\n'
136	printf '    --man1dir MAN1DIR\n'
137	printf '        The location to install Section 1 manpages to. Overrides "$MAN1DIR" if\n'
138	printf '        it exists. Default is "$MANDIR/man1".\n'
139	printf '\n'
140	printf 'In addition, the following environment variables are used:\n'
141	printf '\n'
142	printf '    CC           C compiler. Must be compatible with POSIX c99. If there is a\n'
143	printf '                 space in the basename of the compiler, the items after the\n'
144	printf '                 first space are assumed to be compiler flags, and in that case,\n'
145	printf '                 the flags are automatically moved into CFLAGS. Default is\n'
146	printf '                 "c99".\n'
147	printf '    HOSTCC       Host C compiler. Must be compatible with POSIX c99. If there is\n'
148	printf '                 a space in the basename of the compiler, the items after the\n'
149	printf '                 first space are assumed to be compiler flags, and in the case,\n'
150	printf '                 the flags are automatically moved into HOSTCFLAGS. Default is\n'
151	printf '                 "$CC".\n'
152	printf '    HOST_CC      Same as HOSTCC. If HOSTCC also exists, it is used.\n'
153	printf '    CFLAGS       C compiler flags.\n'
154	printf '    HOSTCFLAGS   CFLAGS for HOSTCC. Default is "$CFLAGS".\n'
155	printf '    HOST_CFLAGS  Same as HOST_CFLAGS. If HOST_CFLAGS also exists, it is used.\n'
156	printf '    CPPFLAGS     C preprocessor flags. Default is "".\n'
157	printf '    LDFLAGS      Linker flags. Default is "".\n'
158	printf '    PREFIX       The prefix to install to. Default is "/usr/local".\n'
159	printf '                 If PREFIX is "/usr", install path will be "/usr/bin".\n'
160	printf '    BINDIR       The directory to install binaries. Default is "$PREFIX/bin".\n'
161	printf '    DATAROOTDIR  The root location for data files. Default is "$PREFIX/share".\n'
162	printf '    DATADIR      The location for data files. Default is "$DATAROOTDIR".\n'
163	printf '    MANDIR       The location to install manpages to. Default is "$DATADIR/man".\n'
164	printf '    MAN1DIR      The location to install Section 1 manpages to. Default is\n'
165	printf '                 "$MANDIR/man1".\n'
166	printf '    NLSPATH      The location to install locale catalogs to. Must be an absolute\n'
167	printf '                 path (or contain one). This is treated the same as the POSIX\n'
168	printf '                 definition of $NLSPATH (see POSIX environment variables for\n'
169	printf '                 more information). Default is "/usr/share/locale/%%L/%%N".\n'
170	printf '    EXECSUFFIX   The suffix to append to the executable names, used to not\n'
171	printf '                 interfere with other installed bc executables. Default is "".\n'
172	printf '    EXECPREFIX   The prefix to append to the executable names, used to not\n'
173	printf '                 interfere with other installed bc executables. Default is "".\n'
174	printf '    DESTDIR      For package creation. Default is "". If it is empty when\n'
175	printf '                 `%s` is run, it can also be passed to `make install`\n' "$script"
176	printf '                 later as an environment variable. If both are specified,\n'
177	printf '                 the one given to `%s` takes precedence.\n' "$script"
178	printf '    LONG_BIT     The number of bits in a C `long` type. This is mostly for the\n'
179	printf '                 embedded space since this `bc` uses `long`s internally for\n'
180	printf '                 overflow checking. In C99, a `long` is required to be 32 bits.\n'
181	printf '                 For most normal desktop systems, setting this is unnecessary,\n'
182	printf '                 except that 32-bit platforms with 64-bit longs may want to set\n'
183	printf '                 it to `32`. Default is the default of `LONG_BIT` for the target\n'
184	printf '                 platform. Minimum allowed is `32`. It is a build time error if\n'
185	printf '                 the specified value of `LONG_BIT` is greater than the default\n'
186	printf '                 value of `LONG_BIT` for the target platform.\n'
187	printf '    GEN_HOST     Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to\n'
188	printf '                 produce the C files that contain the help texts as well as the\n'
189	printf '                 math libraries. By default, `gen/strgen.c` is used, compiled by\n'
190	printf '                 "$HOSTCC" and run on the host machine. Using `gen/strgen.sh`\n'
191	printf '                 removes the need to compile and run an executable on the host\n'
192	printf '                 machine since `gen/strgen.sh` is a POSIX shell script. However,\n'
193	printf '                 `gen/lib2.bc` is perilously close to 4095 characters, the max\n'
194	printf '                 supported length of a string literal in C99 (and it could be\n'
195	printf '                 added to in the future), and `gen/strgen.sh` generates a string\n'
196	printf '                 literal instead of an array, as `gen/strgen.c` does. For most\n'
197	printf '                 production-ready compilers, this limit probably is not\n'
198	printf '                 enforced, but it could be. Both options are still available for\n'
199	printf '                 this reason. If you are sure your compiler does not have the\n'
200	printf '                 limit and do not want to compile and run a binary on the host\n'
201	printf '                 machine, set this variable to "0". Any other value, or a\n'
202	printf '                 non-existent value, will cause the build system to compile and\n'
203	printf '                 run `gen/strgen.c`. Default is "".\n'
204	printf '    GEN_EMU      Emulator to run string generator code under (leave empty if not\n'
205	printf '                 necessary). This is not necessary when using `gen/strgen.sh`.\n'
206	printf '                 Default is "".\n'
207	printf '\n'
208	printf 'WARNING: even though `configure.sh` supports both option types, short and\n'
209	printf 'long, it does not support handling both at the same time. Use only one type.\n'
210
211	exit "$_usage_val"
212}
213
214replace_ext() {
215
216	if [ "$#" -ne 3 ]; then
217		err_exit "Invalid number of args to $0"
218	fi
219
220	_replace_ext_file="$1"
221	_replace_ext_ext1="$2"
222	_replace_ext_ext2="$3"
223
224	_replace_ext_result=${_replace_ext_file%.$_replace_ext_ext1}.$_replace_ext_ext2
225
226	printf '%s\n' "$_replace_ext_result"
227}
228
229replace_exts() {
230
231	if [ "$#" -ne 3 ]; then
232		err_exit "Invalid number of args to $0"
233	fi
234
235	_replace_exts_files="$1"
236	_replace_exts_ext1="$2"
237	_replace_exts_ext2="$3"
238
239	for _replace_exts_file in $_replace_exts_files; do
240		_replace_exts_new_name=$(replace_ext "$_replace_exts_file" "$_replace_exts_ext1" "$_replace_exts_ext2")
241		_replace_exts_result="$_replace_exts_result $_replace_exts_new_name"
242	done
243
244	printf '%s\n' "$_replace_exts_result"
245}
246
247replace() {
248
249	if [ "$#" -ne 3 ]; then
250		err_exit "Invalid number of args to $0"
251	fi
252
253	_replace_str="$1"
254	_replace_needle="$2"
255	_replace_replacement="$3"
256
257	substring_replace "$_replace_str" "%%$_replace_needle%%" "$_replace_replacement"
258}
259
260gen_file_lists() {
261
262	if [ "$#" -lt 3 ]; then
263		err_exit "Invalid number of args to $0"
264	fi
265
266	_gen_file_lists_contents="$1"
267	shift
268
269	_gen_file_lists_filedir="$1"
270	shift
271
272	_gen_file_lists_typ="$1"
273	shift
274
275	# If there is an extra argument, and it
276	# is zero, we keep the file lists empty.
277	if [ "$#" -gt 0 ]; then
278		_gen_file_lists_use="$1"
279	else
280		_gen_file_lists_use="1"
281	fi
282
283	_gen_file_lists_needle_src="${_gen_file_lists_typ}SRC"
284	_gen_file_lists_needle_obj="${_gen_file_lists_typ}OBJ"
285	_gen_file_lists_needle_gcda="${_gen_file_lists_typ}GCDA"
286	_gen_file_lists_needle_gcno="${_gen_file_lists_typ}GCNO"
287
288	if [ "$_gen_file_lists_use" -ne 0 ]; then
289
290		_gen_file_lists_replacement=$(cd "$_gen_file_lists_filedir" && find . ! -name . -prune -name "*.c" | cut -d/ -f2 | sed "s@^@$_gen_file_lists_filedir/@g" | tr '\n' ' ')
291		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_src" "$_gen_file_lists_replacement")
292
293		_gen_file_lists_replacement=$(replace_exts "$_gen_file_lists_replacement" "c" "o")
294		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_obj" "$_gen_file_lists_replacement")
295
296		_gen_file_lists_replacement=$(replace_exts "$_gen_file_lists_replacement" "o" "gcda")
297		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_gcda" "$_gen_file_lists_replacement")
298
299		_gen_file_lists_replacement=$(replace_exts "$_gen_file_lists_replacement" "gcda" "gcno")
300		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_gcno" "$_gen_file_lists_replacement")
301
302	else
303		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_src" "")
304		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_obj" "")
305		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_gcda" "")
306		_gen_file_lists_contents=$(replace "$_gen_file_lists_contents" "$_gen_file_lists_needle_gcno" "")
307	fi
308
309	printf '%s\n' "$_gen_file_lists_contents"
310}
311
312bc_only=0
313dc_only=0
314coverage=0
315karatsuba_len=32
316debug=0
317hist=1
318extra_math=1
319optimization=""
320generate_tests=1
321install_manpages=1
322nls=1
323prompt=1
324force=0
325strip_bin=1
326all_locales=0
327
328while getopts "bBcdDEfgGhHk:lMNO:PST-" opt; do
329
330	case "$opt" in
331		b) bc_only=1 ;;
332		B) dc_only=1 ;;
333		c) coverage=1 ;;
334		d) dc_only=1 ;;
335		D) bc_only=1 ;;
336		E) extra_math=0 ;;
337		f) force=1 ;;
338		g) debug=1 ;;
339		G) generate_tests=0 ;;
340		h) usage ;;
341		H) hist=0 ;;
342		k) karatsuba_len="$OPTARG" ;;
343		l) all_locales=1 ;;
344		M) install_manpages=0 ;;
345		N) nls=0 ;;
346		O) optimization="$OPTARG" ;;
347		P) prompt=0 ;;
348		T) strip_bin=0 ;;
349		-)
350			arg="$1"
351			arg="${arg#--}"
352			LONG_OPTARG="${arg#*=}"
353			case $arg in
354				help) usage ;;
355				bc-only) bc_only=1 ;;
356				dc-only) dc_only=1 ;;
357				coverage) coverage=1 ;;
358				debug) debug=1 ;;
359				force) force=1 ;;
360				prefix=?*) PREFIX="$LONG_OPTARG" ;;
361				prefix)
362					if [ "$#" -lt 2 ]; then
363						usage "No argument given for '--$arg' option"
364					fi
365					PREFIX="$2"
366					shift ;;
367				bindir=?*) BINDIR="$LONG_OPTARG" ;;
368				bindir)
369					if [ "$#" -lt 2 ]; then
370						usage "No argument given for '--$arg' option"
371					fi
372					BINDIR="$2"
373					shift ;;
374				datarootdir=?*) DATAROOTDIR="$LONG_OPTARG" ;;
375				datarootdir)
376					if [ "$#" -lt 2 ]; then
377						usage "No argument given for '--$arg' option"
378					fi
379					DATAROOTDIR="$2"
380					shift ;;
381				datadir=?*) DATADIR="$LONG_OPTARG" ;;
382				datadir)
383					if [ "$#" -lt 2 ]; then
384						usage "No argument given for '--$arg' option"
385					fi
386					DATADIR="$2"
387					shift ;;
388				mandir=?*) MANDIR="$LONG_OPTARG" ;;
389				mandir)
390					if [ "$#" -lt 2 ]; then
391						usage "No argument given for '--$arg' option"
392					fi
393					MANDIR="$2"
394					shift ;;
395				man1dir=?*) MAN1DIR="$LONG_OPTARG" ;;
396				man1dir)
397					if [ "$#" -lt 2 ]; then
398						usage "No argument given for '--$arg' option"
399					fi
400					MAN1DIR="$2"
401					shift ;;
402				localedir=?*) LOCALEDIR="$LONG_OPTARG" ;;
403				localedir)
404					if [ "$#" -lt 2 ]; then
405						usage "No argument given for '--$arg' option"
406					fi
407					LOCALEDIR="$2"
408					shift ;;
409				karatsuba-len=?*) karatsuba_len="$LONG_OPTARG" ;;
410				karatsuba-len)
411					if [ "$#" -lt 2 ]; then
412						usage "No argument given for '--$arg' option"
413					fi
414					karatsuba_len="$1"
415					shift ;;
416				opt=?*) optimization="$LONG_OPTARG" ;;
417				opt)
418					if [ "$#" -lt 2 ]; then
419						usage "No argument given for '--$arg' option"
420					fi
421					optimization="$1"
422					shift ;;
423				disable-bc) dc_only=1 ;;
424				disable-dc) bc_only=1 ;;
425				disable-extra-math) extra_math=0 ;;
426				disable-generated-tests) generate_tests=0 ;;
427				disable-history) hist=0 ;;
428				disable-man-pages) install_manpages=0 ;;
429				disable-nls) nls=0 ;;
430				disable-prompt) prompt=0 ;;
431				disable-strip) strip_bin=0 ;;
432				install-all-locales) all_locales=1 ;;
433				help* | bc-only* | dc-only* | coverage* | debug*)
434					usage "No arg allowed for --$arg option" ;;
435				disable-bc* | disable-dc* | disable-extra-math*)
436					usage "No arg allowed for --$arg option" ;;
437				disable-generated-tests* | disable-history*)
438					usage "No arg allowed for --$arg option" ;;
439				disable-man-pages* | disable-nls* | disable-strip*)
440					usage "No arg allowed for --$arg option" ;;
441				install-all-locales*)
442					usage "No arg allowed for --$arg option" ;;
443				'') break ;; # "--" terminates argument processing
444				* ) usage "Invalid option $LONG_OPTARG" ;;
445			esac
446			shift
447			OPTIND=1 ;;
448		?) usage "Invalid option $opt" ;;
449	esac
450
451done
452
453if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then
454	usage "Can only specify one of -b(-D) or -d(-B)"
455fi
456
457case $karatsuba_len in
458	(*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;;
459	(*) ;;
460esac
461
462if [ "$karatsuba_len" -lt 16 ]; then
463	usage "KARATSUBA_LEN is less than 16"
464fi
465
466set -e
467
468if [ -z "${LONG_BIT+set}" ]; then
469	LONG_BIT_DEFINE=""
470elif [ "$LONG_BIT" -lt 32 ]; then
471	usage "LONG_BIT is less than 32"
472else
473	LONG_BIT_DEFINE="-DBC_LONG_BIT=\$(BC_LONG_BIT)"
474fi
475
476if [ -z "$CC" ]; then
477	CC="c99"
478else
479	ccbase=$(basename "$CC")
480	suffix=" *"
481	prefix="* "
482
483	if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
484		ccflags="${ccbase#$prefix}"
485		cc="${ccbase%%$suffix}"
486		ccdir=$(dirname "$CC")
487		if [ "$ccdir" = "." ] && [ "${CC#.}" = "$CC" ]; then
488			ccdir=""
489		else
490			ccdir="$ccdir/"
491		fi
492		CC="${ccdir}${cc}"
493		CFLAGS="$CFLAGS $ccflags"
494	fi
495fi
496
497if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then
498	HOSTCC="$CC"
499elif [ -z "$HOSTCC" ]; then
500	HOSTCC="$HOST_CC"
501fi
502
503if [ "$HOSTCC" != "$CC" ]; then
504	ccbase=$(basename "$HOSTCC")
505	suffix=" *"
506	prefix="* "
507
508	if [ "${ccbase%%$suffix}" != "$ccbase" ]; then
509		ccflags="${ccbase#$prefix}"
510		cc="${ccbase%%$suffix}"
511		ccdir=$(dirname "$HOSTCC")
512		if [ "$ccdir" = "." ] && [ "${HOSTCC#.}" = "$HOSTCC" ]; then
513			ccdir=""
514		else
515			ccdir="$ccdir/"
516		fi
517		HOSTCC="${ccdir}${cc}"
518		HOSTCFLAGS="$HOSTCFLAGS $ccflags"
519	fi
520fi
521
522if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then
523	HOSTCFLAGS="$CFLAGS"
524elif [ -z "${HOSTCFLAGS+set}" ]; then
525	HOSTCFLAGS="$HOST_CFLAGS"
526fi
527
528link="@printf 'No link necessary\\\\n'"
529main_exec="BC"
530executable="BC_EXEC"
531
532bc_test="@tests/all.sh bc $extra_math 1 $generate_tests 0 \$(BC_EXEC)"
533bc_time_test="@tests/all.sh bc $extra_math 1 $generate_tests 1 \$(BC_EXEC)"
534
535dc_test="@tests/all.sh dc $extra_math 1 $generate_tests 0 \$(DC_EXEC)"
536dc_time_test="@tests/all.sh dc $extra_math 1 $generate_tests 1 \$(DC_EXEC)"
537
538timeconst="@tests/bc/timeconst.sh tests/bc/scripts/timeconst.bc \$(BC_EXEC)"
539
540# In order to have cleanup at exit, we need to be in
541# debug mode, so don't run valgrind without that.
542if [ "$debug" -ne 0 ]; then
543	vg_bc_test="@tests/all.sh bc $extra_math 1 $generate_tests 0 valgrind \$(VALGRIND_ARGS) \$(BC_EXEC)"
544	vg_dc_test="@tests/all.sh dc $extra_math 1 $generate_tests 0 valgrind \$(VALGRIND_ARGS) \$(DC_EXEC)"
545else
546	vg_bc_test="@printf 'Cannot run valgrind without debug flags\\\\n'"
547	vg_dc_test="@printf 'Cannot run valgrind without debug flags\\\\n'"
548fi
549
550karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
551karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
552
553bc_lib="\$(GEN_DIR)/lib.o"
554bc_help="\$(GEN_DIR)/bc_help.o"
555dc_help="\$(GEN_DIR)/dc_help.o"
556
557if [ "$bc_only" -eq 1 ]; then
558
559	bc=1
560	dc=0
561
562	dc_help=""
563
564	executables="bc"
565
566	dc_test="@printf 'No dc tests to run\\\\n'"
567	dc_time_test="@printf 'No dc tests to run\\\\n'"
568	vg_dc_test="@printf 'No dc tests to run\\\\n'"
569
570	install_prereqs=" install_bc_manpage"
571	uninstall_prereqs=" uninstall_bc"
572	uninstall_man_prereqs=" uninstall_bc_manpage"
573
574elif [ "$dc_only" -eq 1 ]; then
575
576	bc=0
577	dc=1
578
579	bc_lib=""
580	bc_help=""
581
582	executables="dc"
583
584	main_exec="DC"
585	executable="DC_EXEC"
586
587	bc_test="@printf 'No bc tests to run\\\\n'"
588	bc_time_test="@printf 'No bc tests to run\\\\n'"
589	vg_bc_test="@printf 'No bc tests to run\\\\n'"
590
591	timeconst="@printf 'timeconst cannot be run because bc is not built\\\\n'"
592
593	install_prereqs=" install_dc_manpage"
594	uninstall_prereqs=" uninstall_dc"
595	uninstall_man_prereqs=" uninstall_dc_manpage"
596
597else
598
599	bc=1
600	dc=1
601
602	executables="bc and dc"
603
604	link="\$(LINK) \$(BIN) \$(EXEC_PREFIX)\$(DC)"
605
606	karatsuba="@\$(KARATSUBA) 30 0 \$(BC_EXEC)"
607	karatsuba_test="@\$(KARATSUBA) 1 100 \$(BC_EXEC)"
608
609	install_prereqs=" install_bc_manpage install_dc_manpage"
610	uninstall_prereqs=" uninstall_bc uninstall_dc"
611	uninstall_man_prereqs=" uninstall_bc_manpage uninstall_dc_manpage"
612
613fi
614
615if [ "$debug" -eq 1 ]; then
616
617	if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then
618		CFLAGS="-O0"
619	fi
620
621	CFLAGS="-g $CFLAGS"
622
623else
624	CPPFLAGS="-DNDEBUG $CPPFLAGS"
625	if [ "$strip_bin" -ne 0 ]; then
626		LDFLAGS="-s $LDFLAGS"
627	fi
628fi
629
630if [ -n "$optimization" ]; then
631	CFLAGS="-O$optimization $CFLAGS"
632fi
633
634if [ "$coverage" -eq 1 ]; then
635
636	if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then
637		usage "Can only specify -c without -b or -d"
638	fi
639
640	CFLAGS="-fprofile-arcs -ftest-coverage -g -O0 $CFLAGS"
641	CPPFLAGS="-DNDEBUG $CPPFLAGS"
642
643	COVERAGE_OUTPUT="@gcov -pabcdf \$(GCDA) \$(BC_GCDA) \$(DC_GCDA) \$(HISTORY_GCDA) \$(RAND_GCDA)"
644	COVERAGE_OUTPUT="$COVERAGE_OUTPUT;\$(RM) -f \$(GEN)*.gc*"
645	COVERAGE_OUTPUT="$COVERAGE_OUTPUT;gcovr --html-details --output index.html"
646	COVERAGE_PREREQS=" test coverage_output"
647
648else
649	COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'"
650	COVERAGE_PREREQS=""
651fi
652
653if [ -z "${DESTDIR+set}" ]; then
654	destdir=""
655else
656	destdir="DESTDIR = $DESTDIR"
657fi
658
659if [ -z "${PREFIX+set}" ]; then
660	PREFIX="/usr/local"
661fi
662
663if [ -z "${BINDIR+set}" ]; then
664	BINDIR="$PREFIX/bin"
665fi
666
667if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then
668	if [ -z "${DATAROOTDIR+set}" ]; then
669		DATAROOTDIR="$PREFIX/share"
670	fi
671fi
672
673if [ "$install_manpages" -ne 0 ]; then
674
675	if [ -z "${DATADIR+set}" ]; then
676		DATADIR="$DATAROOTDIR"
677	fi
678
679	if [ -z "${MANDIR+set}" ]; then
680		MANDIR="$DATADIR/man"
681	fi
682
683	if [ -z "${MAN1DIR+set}" ]; then
684		MAN1DIR="$MANDIR/man1"
685	fi
686
687else
688	install_prereqs=""
689	uninstall_man_prereqs=""
690fi
691
692if [ "$nls" -ne 0 ]; then
693
694	set +e
695
696	printf 'Testing NLS...\n'
697
698	flags="-DBC_ENABLE_NLS=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
699	flags="$flags -DBC_ENABLE_HISTORY=$hist"
700	flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I./include/"
701	flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
702
703	"$CC" $CPPFLAGS $CFLAGS $flags -c "src/vm.c" -o "$scriptdir/vm.o" > /dev/null 2>&1
704
705	err="$?"
706
707	rm -rf "$scriptdir/vm.o"
708
709	# If this errors, it is probably because of building on Windows,
710	# and NLS is not supported on Windows, so disable it.
711	if [ "$err" -ne 0 ]; then
712		printf 'NLS does not work.\n'
713		if [ $force -eq 0 ]; then
714			printf 'Disabling NLS...\n\n'
715			nls=0
716		else
717			printf 'Forcing NLS...\n\n'
718		fi
719	else
720		printf 'NLS works.\n\n'
721
722		printf 'Testing gencat...\n'
723		gencat "$scriptdir/en_US.cat" "$scriptdir/locales/en_US.msg" > /dev/null 2>&1
724
725		err="$?"
726
727		rm -rf "$scriptdir/en_US.cat"
728
729		if [ "$err" -ne 0 ]; then
730			printf 'gencat does not work.\n'
731			if [ $force -eq 0 ]; then
732				printf 'Disabling NLS...\n\n'
733				nls=0
734			else
735				printf 'Forcing NLS...\n\n'
736			fi
737		else
738
739			printf 'gencat works.\n\n'
740
741			if [ "$HOSTCC" != "$CC" ]; then
742				printf 'Cross-compile detected.\n\n'
743				printf 'WARNING: Catalog files generated with gencat may not be portable\n'
744				printf '         across different architectures.\n\n'
745			fi
746
747			if [ -z "$NLSPATH" ]; then
748				NLSPATH="/usr/share/locale/%L/%N"
749			fi
750
751			install_locales_prereqs=" install_locales"
752			uninstall_locales_prereqs=" uninstall_locales"
753
754		fi
755
756	fi
757
758	set -e
759
760else
761	install_locales_prereqs=""
762	uninstall_locales_prereqs=""
763	all_locales=0
764fi
765
766if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then
767	install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
768else
769	install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
770fi
771
772if [ "$hist" -eq 1 ]; then
773
774	set +e
775
776	printf 'Testing history...\n'
777
778	flags="-DBC_ENABLE_HISTORY=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc"
779	flags="$flags -DBC_ENABLE_NLS=$nls"
780	flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I./include/"
781	flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"
782
783	"$CC" $CPPFLAGS $CFLAGS $flags -c "src/history/history.c" -o "$scriptdir/history.o" > /dev/null 2>&1
784
785	err="$?"
786
787	rm -rf "$scriptdir/history.o"
788
789	# If this errors, it is probably because of building on Windows,
790	# and history is not supported on Windows, so disable it.
791	if [ "$err" -ne 0 ]; then
792		printf 'History does not work.\n'
793		if [ $force -eq 0 ]; then
794			printf 'Disabling history...\n\n'
795			hist=0
796		else
797			printf 'Forcing history...\n\n'
798		fi
799	else
800		printf 'History works.\n\n'
801	fi
802
803	set -e
804
805fi
806
807if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ]; then
808	BC_LIB2_O="\$(GEN_DIR)/lib2.o"
809else
810	BC_LIB2_O=""
811fi
812
813GEN="strgen"
814GEN_EXEC_TARGET="\$(HOSTCC) \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)"
815CLEAN_PREREQS=" clean_gen"
816
817if [ -z "${GEN_HOST+set}" ]; then
818	GEN_HOST=1
819else
820	if [ "$GEN_HOST" -eq 0 ]; then
821		GEN="strgen.sh"
822		GEN_EXEC_TARGET="@printf 'Do not need to build gen/strgen.c\\\\n'"
823		CLEAN_PREREQS=""
824	fi
825fi
826
827manpage_args=""
828
829if [ "$extra_math" -eq 0 ]; then
830	manpage_args="E"
831fi
832
833if [ "$hist" -eq 0 ]; then
834	manpage_args="${manpage_args}H"
835fi
836
837if [ "$nls" -eq 0 ]; then
838	manpage_args="${manpage_args}N"
839fi
840
841if [ "$prompt" -eq 0 ]; then
842	manpage_args="${manpage_args}P"
843fi
844
845if [ "$manpage_args" = "" ]; then
846	manpage_args="A"
847fi
848
849# Print out the values; this is for debugging.
850if [ "$bc" -ne 0 ]; then
851	printf 'Building bc\n'
852else
853	printf 'Not building bc\n'
854fi
855if [ "$dc" -ne 0 ]; then
856	printf 'Building dc\n'
857else
858	printf 'Not building dc\n'
859fi
860printf '\n'
861printf 'BC_ENABLE_HISTORY=%s\n' "$hist"
862printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math"
863printf 'BC_ENABLE_NLS=%s\n' "$nls"
864printf 'BC_ENABLE_PROMPT=%s\n' "$prompt"
865printf '\n'
866printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len"
867printf '\n'
868printf 'CC=%s\n' "$CC"
869printf 'CFLAGS=%s\n' "$CFLAGS"
870printf 'HOSTCC=%s\n' "$HOSTCC"
871printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS"
872printf 'CPPFLAGS=%s\n' "$CPPFLAGS"
873printf 'LDFLAGS=%s\n' "$LDFLAGS"
874printf 'PREFIX=%s\n' "$PREFIX"
875printf 'BINDIR=%s\n' "$BINDIR"
876printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR"
877printf 'DATADIR=%s\n' "$DATADIR"
878printf 'MANDIR=%s\n' "$MANDIR"
879printf 'MAN1DIR=%s\n' "$MAN1DIR"
880printf 'NLSPATH=%s\n' "$NLSPATH"
881printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX"
882printf 'EXECPREFIX=%s\n' "$EXECPREFIX"
883printf 'DESTDIR=%s\n' "$DESTDIR"
884printf 'LONG_BIT=%s\n' "$LONG_BIT"
885printf 'GEN_HOST=%s\n' "$GEN_HOST"
886printf 'GEN_EMU=%s\n' "$GEN_EMU"
887
888contents=$(cat "$scriptdir/Makefile.in")
889
890needle="WARNING"
891replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***'
892
893contents=$(replace "$contents" "$needle" "$replacement")
894
895contents=$(gen_file_lists "$contents" "$scriptdir/src" "")
896contents=$(gen_file_lists "$contents" "$scriptdir/src/bc" "BC_" "$bc")
897contents=$(gen_file_lists "$contents" "$scriptdir/src/dc" "DC_" "$dc")
898contents=$(gen_file_lists "$contents" "$scriptdir/src/history" "HISTORY_" "$hist")
899contents=$(gen_file_lists "$contents" "$scriptdir/src/rand" "RAND_" "$extra_math")
900
901contents=$(replace "$contents" "BC_ENABLED" "$bc")
902contents=$(replace "$contents" "DC_ENABLED" "$dc")
903contents=$(replace "$contents" "LINK" "$link")
904
905contents=$(replace "$contents" "HISTORY" "$hist")
906contents=$(replace "$contents" "EXTRA_MATH" "$extra_math")
907contents=$(replace "$contents" "NLS" "$nls")
908contents=$(replace "$contents" "PROMPT" "$prompt")
909contents=$(replace "$contents" "BC_LIB_O" "$bc_lib")
910contents=$(replace "$contents" "BC_HELP_O" "$bc_help")
911contents=$(replace "$contents" "DC_HELP_O" "$dc_help")
912contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O")
913contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len")
914
915contents=$(replace "$contents" "NLSPATH" "$NLSPATH")
916contents=$(replace "$contents" "DESTDIR" "$destdir")
917contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX")
918contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX")
919contents=$(replace "$contents" "BINDIR" "$BINDIR")
920contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR")
921contents=$(replace "$contents" "CFLAGS" "$CFLAGS")
922contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS")
923contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS")
924contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS")
925contents=$(replace "$contents" "CC" "$CC")
926contents=$(replace "$contents" "HOSTCC" "$HOSTCC")
927contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT")
928contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS")
929contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs")
930contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales")
931contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs")
932contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs")
933contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs")
934contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs")
935
936contents=$(replace "$contents" "EXECUTABLES" "$executables")
937contents=$(replace "$contents" "MAIN_EXEC" "$main_exec")
938contents=$(replace "$contents" "EXEC" "$executable")
939
940contents=$(replace "$contents" "BC_TEST" "$bc_test")
941contents=$(replace "$contents" "BC_TIME_TEST" "$bc_time_test")
942
943contents=$(replace "$contents" "DC_TEST" "$dc_test")
944contents=$(replace "$contents" "DC_TIME_TEST" "$dc_time_test")
945
946contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test")
947contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test")
948
949contents=$(replace "$contents" "TIMECONST" "$timeconst")
950
951contents=$(replace "$contents" "KARATSUBA" "$karatsuba")
952contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test")
953
954contents=$(replace "$contents" "LONG_BIT" "$LONG_BIT")
955contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE")
956
957contents=$(replace "$contents" "GEN" "$GEN")
958contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET")
959contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS")
960contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU")
961
962printf '%s\n' "$contents" > "$scriptdir/Makefile"
963
964cd "$scriptdir"
965
966cp -f manuals/bc/$manpage_args.1.md manuals/bc.1.md
967cp -f manuals/bc/$manpage_args.1 manuals/bc.1
968cp -f manuals/dc/$manpage_args.1.md manuals/dc.1.md
969cp -f manuals/dc/$manpage_args.1 manuals/dc.1
970
971make clean > /dev/null
972