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