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