#!/bin/sh set -e CONFIGDIR="$(dirname $0)/config" show_help() { cat <<-EOF usage: ./configure [options...] Options: -a arch Choose the underlying architecture to build for: $(awk '{ printf("\t%s\n", $0); }' ${CONFIGDIR}/architecture) -c version Conform to: $(awk '{ printf("\t%s\n", $0); }' ${CONFIGDIR}/versions.c) -p version Conform to: $(awk '{ printf("\t%s\n", $0); }' ${CONFIGDIR}/versions.posix) -w wordsize Choose a word size for the architecuture: $(awk '{ printf("\t%s\n", $0); }' ${CONFIGDIR}/wordsize) -x version Conform to: $(awk '{ printf("\t%s\n", $0); }' ${CONFIGDIR}/versions.xopen) EOF } validate_option() { option="${CONFIGDIR}/$1" for opt in $(awk '/^[0-9]/ { print $1 }' "${option}"); do if [ "$2" = "${opt}" ]; then echo "$2" return 0 fi done printf 'Invalid option '%s'. Valid options are:\n' "$2" >&2 cat "${option}" >&2 exit 1 } option= architecture=x86 wordsize=64 standard_c= posix= xopen= while getopts hc:p:x: option; do case ${option} in a) architecture=$(validate_option architecture ${OPTARG}) ;; c) standard_c=$(validate_option c ${OPTARG}) ;; p) posix=$(validate_option posix ${OPTARG}) ;; x) xopen=$(validate_option xopen ${OPTARG}) ;; h) show_help exit 0 ;; ?) printf 'Unknown option "%c". Try "-h" for help.\n' $option exit 1 ;; esac done if [ -z "${standard_c}" ] && [ -z "${posix}" ] && [ -z "${xopen}" ]; then xopen=700 fi if [ -n "${posix}" ] && [ -n "${xopen}" ]; then printf 'Specify only one of "-x" or "-p".\n' exit 1 fi if [ -n "${xopen}" ]; then if [ $xopen -eq 700 ]; then posix=200809 elif [ $xopen -eq 600 ]; then posix=200112 elif [ $xopen -eq 500 ]; then posix=199506 else posix=2 fi fi if [ -z "${standard_c}" ] && [ -n "${posix}" ]; then if [ $posix -ge 200112 ]; then standard_c=199901 else standard_c=1 fi fi if [ ${posix:-0} -ge 200112 ] && [ $standard_c -lt 199901 ]; then printf 'POSIX.1-2001 and newer require C99 or newer.\n' exit 1 fi printf 'C library configured to conform to the following standards:\n' printf 'ISO C: %d\n' ${standard_c} test -n "${posix}" && printf 'POSIX: %d\n' ${posix} test -n "${xopen}" && printf 'X/OPEN: %d\n' ${xopen} printf 'Building for %d-bit %s architecture\n' "${wordsize}" "${architecture}" exec > Makefile cat <<-EOF .POSIX: .DEFAULT:;\$(MAKE) all ARCHITECTURE=${architecture} WORDSIZE=${WORDSIZE} SRCDIR=$(dirname $0)/src INCDIR=$(dirname $0)/include OBJDIR=$(pwd)/obj EOF if [ ${standard_c:-1} -lt 199901 ]; then printf 'CC=c89\n' else printf 'CC=c%02d\n' $(( (standard_c / 100) % 100 )) fi if [ -n "${xopen}" ]; then if [ $xopen -eq 400 ]; then printf 'CFLAGS=-D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1\n' else printf 'CFLAGS=-D_XOPEN_SOURCE=%d\n' "${xopen}" fi elif [ -n "${posix}" ]; then printf 'CFLAGS=-D_POSIX_C_SOURCE=%d\n' "${posix}" fi cat <