harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

config.sh (4465B)


      1 outdir=${OUTDIR:-.build}
      2 srcdir=${SRCDIR:-$(dirname "$0")}
      3 AR=${AR:-ar}
      4 AS=${AS:-as}
      5 CC=${CC:-cc}
      6 CFLAGS=${CFLAGS:-}
      7 LDFLAGS=${LDFLAGS:-}
      8 LD=${LD:-ld}
      9 QBE=${QBE:-qbe}
     10 
     11 if [ `uname -s` = "OpenBSD" ]
     12 then
     13 	LD="${LD:-ld} -nopie"
     14 fi
     15 
     16 for arg
     17 do
     18 	case "$arg" in
     19 		--bindir=*)
     20 			BINDIR=${arg#*=}
     21 			;;
     22 		--libdir=*)
     23 			LIBDIR=${arg#*=}
     24 			;;
     25 		--mandir=*)
     26 			MANDIR=${arg#*=}
     27 			;;
     28 		--prefix=*)
     29 			PREFIX=${arg#*=}
     30 			if [ "$PREFIX" = "/usr" ]
     31 			then
     32 				SYSCONFDIR=/etc
     33 			fi
     34 			;;
     35 		--sharedir=*)
     36 			SHAREDIR=${arg#*=}
     37 			;;
     38 		--sysconfdir=*)
     39 			SYSCONFDIR=${arg#*=}
     40 			;;
     41 		--help)
     42 			cat >&2 <<'EOF'
     43 Usage: configure [options...]
     44 
     45 Options:
     46 --help
     47 --prefix=<path>
     48 	default: /usr/local
     49 --bindir=<path>
     50 	default: (prefix)/bin
     51 --libdir=<path>
     52 	default: (prefix)/lib
     53 --mandir=<path>
     54 	default: (prefix)/share/man
     55 --sharedir=<path>
     56 	default: (prefix)/share
     57 --sysconfdir=<path>
     58 	default: /etc if prefix is /usr, otherwise (prefix)/etc
     59 
     60 Environment variables:
     61 AR	(default: "ar")
     62 AS	(default: "as")
     63 CC	(default: "cc")
     64 CFLAGS	(default: "")
     65 LD	(default: "ld")
     66 LDFLAGS	(default: "")
     67 OUTDIR	(default: ".build")
     68 QBE	(default: "qbe")
     69 SRCDIR	(default: directory of configure script)
     70 EOF
     71 			exit
     72 			;;
     73 		*)
     74 			printf 'Error: unknown configure option %s\n' "$arg" >&2
     75 			exit 1
     76 			;;
     77 	esac
     78 done
     79 
     80 subdir() {
     81 	eval ". $srcdir/$1/configure"
     82 }
     83 
     84 headers() {
     85 	printf 'HEADERS=\\\n'
     86 	while [ $# -ne 0 ]
     87 	do
     88 		printf '\tinclude/%s \\\n' "$1"
     89 		shift
     90 	done
     91 }
     92 
     93 genrules() {
     94 	target="$1"
     95 	shift
     96 	printf '# Begin generated rules for %s\n' "$target"
     97 	for file in "$@"
     98 	do
     99 		ext="${file#*.}"
    100 		file="${file%.*}"
    101 		deps=
    102 		if [ $ext = "ha" ]
    103 		then
    104 			deps=" harec"
    105 		fi
    106 		printf '%s.o: %s.%s%s $(HEADERS)\n' "$file" "$file" "$ext" "$deps"
    107 	done
    108 	printf '%s_objects=\\\n' "$target"
    109 	n=0
    110 	for file in "$@"
    111 	do
    112 		file="${file%.*}"
    113 		n=$((n+1))
    114 		if [ $n -eq $# ]
    115 		then
    116 			printf '\t%s.o\n' "$file"
    117 		else
    118 			printf '\t%s.o \\\n' "$file"
    119 		fi
    120 	done
    121 	printf '# End generated rules for %s\n' "$target"
    122 }
    123 
    124 append_cflags() {
    125 	for flag
    126 	do
    127 		CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")"
    128 	done
    129 }
    130 
    131 test_cflags() {
    132 	[ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c
    133 	int main(void) { return 0; }
    134 	EOF
    135 	werror=""
    136 	case "$CFLAGS" in
    137 		*-Werror*)
    138 			werror="-Werror"
    139 			;;
    140 	esac
    141 	if $CC $werror "$@" -o /dev/null "$outdir"/check.c >/dev/null 2>&1
    142 	then
    143 		append_cflags "$@"
    144 	else
    145 		return 1
    146 	fi
    147 }
    148 
    149 find_library() {
    150 	name="$1"
    151 	pc="$2"
    152 	printf "Checking for %s... " "$name"
    153 	if ! pkg-config "$pc" 2>/dev/null
    154 	then
    155 		printf "NOT FOUND\n"
    156 		printf "Tried pkg-config %s\n" "$pc"
    157 		return 1
    158 	fi
    159 	printf "OK\n"
    160 	CFLAGS="$CFLAGS $(pkg-config --cflags "$pc")"
    161 	LIBS="$LIBS $(pkg-config --libs "$pc")"
    162 }
    163 
    164 run_configure() {
    165 	mkdir -p "$outdir"
    166 
    167 	for flag in -g -std=c11 -D_XOPEN_SOURCE=700 \
    168 		-Wall -Wextra -Werror -pedantic \
    169 		-Wno-unused-parameter
    170 	do
    171 		printf "Checking for %s... " "$flag"
    172 		if test_cflags "$flag"
    173 		then
    174 			echo yes
    175 		else
    176 			echo no
    177 		fi
    178 	done
    179 
    180 	printf "Checking for qbe... "
    181 	if $QBE -h > /dev/null 2>&1
    182 	then
    183 		echo yes
    184 	else
    185 		echo no
    186 		echo "Error: no qbe binary found"
    187 		exit 1
    188 	fi
    189 
    190 	printf "Creating %s/config.mk... " "$outdir"
    191 	cat <<EOF > "$outdir"/config.mk
    192 CC=$CC
    193 AS=$AS
    194 LD=$LD
    195 AR=$AR
    196 QBE=$QBE
    197 LIBS=$LIBS
    198 PREFIX=${PREFIX:-/usr/local}
    199 OUTDIR=${outdir}
    200 SRCDIR=${srcdir}
    201 BINDIR=${BINDIR:-\$(PREFIX)/bin}
    202 SHAREDIR=${SHAREDIR:-\$(PREFIX)/share}
    203 SYSCONFDIR=${SYSCONFDIR:-\$(PREFIX)/etc}
    204 LIBDIR=${LIBDIR:-\$(PREFIX)/lib}
    205 MANDIR=${MANDIR:-\$(PREFIX)/share/man}
    206 VARLIBDIR=${MANDIR:-\$(PREFIX)/var/lib}
    207 CACHE=\$(OUTDIR)/cache
    208 CFLAGS=${CFLAGS} \\
    209 	-Iinclude -I\$(OUTDIR) \\
    210 	-DPREFIX='"\$(PREFIX)"' \\
    211 	-DLIBDIR='"\$(LIBDIR)"' \\
    212 	-DVARLIBDIR='"\$(VARLIBDIR)"' \\
    213 	-DSYSCONFDIR='"\$(SYSCONFDIR)"'
    214 LDFLAGS=${LDFLAGS}
    215 HARECACHE=./mod
    216 
    217 all: ${all}
    218 EOF
    219 
    220 	for target in $all
    221 	do
    222 		$target >>"$outdir"/config.mk
    223 	done
    224 	echo done
    225 
    226 	cat <<-EOF >harec.sh
    227 	export PATH="$(pwd)":\$PATH
    228 	EOF
    229 
    230 	if [ "$srcdir" = "." ]
    231 	then
    232 		return
    233 	fi
    234 
    235 	populate() (
    236 		path="$1"
    237 		mkdir -p "${path#$srcdir/}"
    238 		fullpath() ( cd "$1" && pwd )
    239 		for orig in "$path"/*
    240 		do
    241 			link="${orig#$srcdir/}"
    242 			if [ -d "$orig" ]
    243 			then
    244 				mkdir -p $link
    245 				populate "$orig"
    246 			elif [ -f "$orig" ]
    247 			then
    248 				ln -sf "$(fullpath "$path")"/"$(basename "$orig")" "$link"
    249 			fi
    250 		done
    251 	)
    252 
    253 	printf "Populating build dir... "
    254 	populate "$srcdir/doc"
    255 	populate "$srcdir/include"
    256 	populate "$srcdir/rt"
    257 	populate "$srcdir/src"
    258 	populate "$srcdir/tests"
    259 	populate "$srcdir/testmod"
    260 	ln -sf "$srcdir"/Makefile ./
    261 	echo done
    262 }