]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/configure.sh
MFC: 362681, 362697, 362914, 362984, 362986, 362987, 363091, 363172, 363809,
[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] [-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
214 replace_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
229 replace_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
247 replace() {
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
260 gen_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
312 bc_only=0
313 dc_only=0
314 coverage=0
315 karatsuba_len=32
316 debug=0
317 hist=1
318 extra_math=1
319 optimization=""
320 generate_tests=1
321 install_manpages=1
322 nls=1
323 prompt=1
324 force=0
325 strip_bin=1
326 all_locales=0
327
328 while 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
451 done
452
453 if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then
454         usage "Can only specify one of -b(-D) or -d(-B)"
455 fi
456
457 case $karatsuba_len in
458         (*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;;
459         (*) ;;
460 esac
461
462 if [ "$karatsuba_len" -lt 16 ]; then
463         usage "KARATSUBA_LEN is less than 16"
464 fi
465
466 set -e
467
468 if [ -z "${LONG_BIT+set}" ]; then
469         LONG_BIT_DEFINE=""
470 elif [ "$LONG_BIT" -lt 32 ]; then
471         usage "LONG_BIT is less than 32"
472 else
473         LONG_BIT_DEFINE="-DBC_LONG_BIT=\$(BC_LONG_BIT)"
474 fi
475
476 if [ -z "$CC" ]; then
477         CC="c99"
478 else
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
495 fi
496
497 if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then
498         HOSTCC="$CC"
499 elif [ -z "$HOSTCC" ]; then
500         HOSTCC="$HOST_CC"
501 fi
502
503 if [ "$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
520 fi
521
522 if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then
523         HOSTCFLAGS="$CFLAGS"
524 elif [ -z "${HOSTCFLAGS+set}" ]; then
525         HOSTCFLAGS="$HOST_CFLAGS"
526 fi
527
528 link="@printf 'No link necessary\\\\n'"
529 main_exec="BC"
530 executable="BC_EXEC"
531
532 bc_test="@tests/all.sh bc $extra_math 1 $generate_tests 0 \$(BC_EXEC)"
533 bc_time_test="@tests/all.sh bc $extra_math 1 $generate_tests 1 \$(BC_EXEC)"
534
535 dc_test="@tests/all.sh dc $extra_math 1 $generate_tests 0 \$(DC_EXEC)"
536 dc_time_test="@tests/all.sh dc $extra_math 1 $generate_tests 1 \$(DC_EXEC)"
537
538 timeconst="@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.
542 if [ "$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)"
545 else
546         vg_bc_test="@printf 'Cannot run valgrind without debug flags\\\\n'"
547         vg_dc_test="@printf 'Cannot run valgrind without debug flags\\\\n'"
548 fi
549
550 karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
551 karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'"
552
553 bc_lib="\$(GEN_DIR)/lib.o"
554 bc_help="\$(GEN_DIR)/bc_help.o"
555 dc_help="\$(GEN_DIR)/dc_help.o"
556
557 if [ "$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
574 elif [ "$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
597 else
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
613 fi
614
615 if [ "$debug" -eq 1 ]; then
616
617         if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then
618                 CFLAGS="-O0"
619         fi
620
621         CFLAGS="-g $CFLAGS"
622
623 else
624         CPPFLAGS="-DNDEBUG $CPPFLAGS"
625         if [ "$strip_bin" -ne 0 ]; then
626                 LDFLAGS="-s $LDFLAGS"
627         fi
628 fi
629
630 if [ -n "$optimization" ]; then
631         CFLAGS="-O$optimization $CFLAGS"
632 fi
633
634 if [ "$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
648 else
649         COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'"
650         COVERAGE_PREREQS=""
651 fi
652
653 if [ -z "${DESTDIR+set}" ]; then
654         destdir=""
655 else
656         destdir="DESTDIR = $DESTDIR"
657 fi
658
659 if [ -z "${PREFIX+set}" ]; then
660         PREFIX="/usr/local"
661 fi
662
663 if [ -z "${BINDIR+set}" ]; then
664         BINDIR="$PREFIX/bin"
665 fi
666
667 if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then
668         if [ -z "${DATAROOTDIR+set}" ]; then
669                 DATAROOTDIR="$PREFIX/share"
670         fi
671 fi
672
673 if [ "$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
687 else
688         install_prereqs=""
689         uninstall_man_prereqs=""
690 fi
691
692 if [ "$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
760 else
761         install_locales_prereqs=""
762         uninstall_locales_prereqs=""
763         all_locales=0
764 fi
765
766 if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then
767         install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
768 else
769         install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)"
770 fi
771
772 if [ "$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
805 fi
806
807 if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ]; then
808         BC_LIB2_O="\$(GEN_DIR)/lib2.o"
809 else
810         BC_LIB2_O=""
811 fi
812
813 GEN="strgen"
814 GEN_EXEC_TARGET="\$(HOSTCC) \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)"
815 CLEAN_PREREQS=" clean_gen"
816
817 if [ -z "${GEN_HOST+set}" ]; then
818         GEN_HOST=1
819 else
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
825 fi
826
827 manpage_args=""
828
829 if [ "$extra_math" -eq 0 ]; then
830         manpage_args="E"
831 fi
832
833 if [ "$hist" -eq 0 ]; then
834         manpage_args="${manpage_args}H"
835 fi
836
837 if [ "$nls" -eq 0 ]; then
838         manpage_args="${manpage_args}N"
839 fi
840
841 if [ "$prompt" -eq 0 ]; then
842         manpage_args="${manpage_args}P"
843 fi
844
845 if [ "$manpage_args" = "" ]; then
846         manpage_args="A"
847 fi
848
849 # Print out the values; this is for debugging.
850 if [ "$bc" -ne 0 ]; then
851         printf 'Building bc\n'
852 else
853         printf 'Not building bc\n'
854 fi
855 if [ "$dc" -ne 0 ]; then
856         printf 'Building dc\n'
857 else
858         printf 'Not building dc\n'
859 fi
860 printf '\n'
861 printf 'BC_ENABLE_HISTORY=%s\n' "$hist"
862 printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math"
863 printf 'BC_ENABLE_NLS=%s\n' "$nls"
864 printf 'BC_ENABLE_PROMPT=%s\n' "$prompt"
865 printf '\n'
866 printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len"
867 printf '\n'
868 printf 'CC=%s\n' "$CC"
869 printf 'CFLAGS=%s\n' "$CFLAGS"
870 printf 'HOSTCC=%s\n' "$HOSTCC"
871 printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS"
872 printf 'CPPFLAGS=%s\n' "$CPPFLAGS"
873 printf 'LDFLAGS=%s\n' "$LDFLAGS"
874 printf 'PREFIX=%s\n' "$PREFIX"
875 printf 'BINDIR=%s\n' "$BINDIR"
876 printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR"
877 printf 'DATADIR=%s\n' "$DATADIR"
878 printf 'MANDIR=%s\n' "$MANDIR"
879 printf 'MAN1DIR=%s\n' "$MAN1DIR"
880 printf 'NLSPATH=%s\n' "$NLSPATH"
881 printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX"
882 printf 'EXECPREFIX=%s\n' "$EXECPREFIX"
883 printf 'DESTDIR=%s\n' "$DESTDIR"
884 printf 'LONG_BIT=%s\n' "$LONG_BIT"
885 printf 'GEN_HOST=%s\n' "$GEN_HOST"
886 printf 'GEN_EMU=%s\n' "$GEN_EMU"
887
888 contents=$(cat "$scriptdir/Makefile.in")
889
890 needle="WARNING"
891 replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***'
892
893 contents=$(replace "$contents" "$needle" "$replacement")
894
895 contents=$(gen_file_lists "$contents" "$scriptdir/src" "")
896 contents=$(gen_file_lists "$contents" "$scriptdir/src/bc" "BC_" "$bc")
897 contents=$(gen_file_lists "$contents" "$scriptdir/src/dc" "DC_" "$dc")
898 contents=$(gen_file_lists "$contents" "$scriptdir/src/history" "HISTORY_" "$hist")
899 contents=$(gen_file_lists "$contents" "$scriptdir/src/rand" "RAND_" "$extra_math")
900
901 contents=$(replace "$contents" "BC_ENABLED" "$bc")
902 contents=$(replace "$contents" "DC_ENABLED" "$dc")
903 contents=$(replace "$contents" "LINK" "$link")
904
905 contents=$(replace "$contents" "HISTORY" "$hist")
906 contents=$(replace "$contents" "EXTRA_MATH" "$extra_math")
907 contents=$(replace "$contents" "NLS" "$nls")
908 contents=$(replace "$contents" "PROMPT" "$prompt")
909 contents=$(replace "$contents" "BC_LIB_O" "$bc_lib")
910 contents=$(replace "$contents" "BC_HELP_O" "$bc_help")
911 contents=$(replace "$contents" "DC_HELP_O" "$dc_help")
912 contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O")
913 contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len")
914
915 contents=$(replace "$contents" "NLSPATH" "$NLSPATH")
916 contents=$(replace "$contents" "DESTDIR" "$destdir")
917 contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX")
918 contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX")
919 contents=$(replace "$contents" "BINDIR" "$BINDIR")
920 contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR")
921 contents=$(replace "$contents" "CFLAGS" "$CFLAGS")
922 contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS")
923 contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS")
924 contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS")
925 contents=$(replace "$contents" "CC" "$CC")
926 contents=$(replace "$contents" "HOSTCC" "$HOSTCC")
927 contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT")
928 contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS")
929 contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs")
930 contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales")
931 contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs")
932 contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs")
933 contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs")
934 contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs")
935
936 contents=$(replace "$contents" "EXECUTABLES" "$executables")
937 contents=$(replace "$contents" "MAIN_EXEC" "$main_exec")
938 contents=$(replace "$contents" "EXEC" "$executable")
939
940 contents=$(replace "$contents" "BC_TEST" "$bc_test")
941 contents=$(replace "$contents" "BC_TIME_TEST" "$bc_time_test")
942
943 contents=$(replace "$contents" "DC_TEST" "$dc_test")
944 contents=$(replace "$contents" "DC_TIME_TEST" "$dc_time_test")
945
946 contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test")
947 contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test")
948
949 contents=$(replace "$contents" "TIMECONST" "$timeconst")
950
951 contents=$(replace "$contents" "KARATSUBA" "$karatsuba")
952 contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test")
953
954 contents=$(replace "$contents" "LONG_BIT" "$LONG_BIT")
955 contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE")
956
957 contents=$(replace "$contents" "GEN" "$GEN")
958 contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET")
959 contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS")
960 contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU")
961
962 printf '%s\n' "$contents" > "$scriptdir/Makefile"
963
964 cd "$scriptdir"
965
966 cp -f manuals/bc/$manpage_args.1.md manuals/bc.1.md
967 cp -f manuals/bc/$manpage_args.1 manuals/bc.1
968 cp -f manuals/dc/$manpage_args.1.md manuals/dc.1.md
969 cp -f manuals/dc/$manpage_args.1 manuals/dc.1
970
971 make clean > /dev/null