aboutsummaryrefslogtreecommitdiffstats
path: root/configure
blob: 431dc4a2a7e3642e0e8ecc615999177a7f43e59d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash

CFLAGS=(-std=c11 -fPIC)
CPPFLAGS=()
LDLIBS=()
LDFLAGS=()

warnings=(
	-Wall -Wcast-align -Wcast-qual -Wextra -Wpedantic -Wformat=2
	-Winit-self -Wmissing-prototypes -Wpointer-arith -Wshadow
	-Wstrict-prototypes -Wsuggest-attribute=format
	-Wsuggest-attribute=noreturn
)

usage () { echo "Usage: $0 [-h|options...]"; }

help () {
	cat <<EOF
Options:
  -B ldlib    Append ldlib to LDLIBS
  -C cflag    Append cflag to CFLAGS
  -L ldflag   Append ldflag to LDFLAGS
  -P cppflag  Append cppflag to CPPFLAGS
  -c when     Enable compiler colours (always|auto|off) [default: auto]
  -d          Enable debugging flags
  -e          Enable -Werror
  -f          Generate a compile_flags.txt
  -h          Show this help
  -o          Enable optimisation flags
  -v          Print results of configuration
  -w          Enable warning flags
Environment:
  CC          C compiler
EOF
}

check() {
	what=$1
	shift
	for p do
		if command -v "$p" >/dev/null 2>&1; then
			echo "$p"
			return
		fi
	done
	echo "$what not set or found" >&2
	return 1
}

CC=$(check '$CC, cc, gcc or clang' "$CC" cc gcc clang) || exit

colour=auto
while getopts B:C:L:P:c:defhovw opt; do
	case $opt in
	B) LDLIBS+=("$OPTARG");;
	C) CFLAGS+=("$OPTARG");;
	L) LDFLAGS+=("$OPTARG");;
	P) CPPFLAGS+=("$OPTARG");;
	c) colour="$OPTARG";;
	d) CFLAGS+=(-Og -g); LDFLAGS+=(-Og -g);;
	e) CFLAGS+=(-Werror);;
	f) gen_flags=1;;
	h) usage; help; exit;;
	o) CFLAGS+=(-O2 -flto); LDFLAGS+=(-O2 -flto);;
	v) verbose=1;;
	w) CFLAGS+=("${warnings[@]}");;
	?) usage >&2; exit 1;;
	esac
done
if [[ $colour == auto && -t 1 ]]; then
	colour=always
fi
if [[ $colour == always ]]; then
	CFLAGS+=(-fdiagnostics-color)
fi

{
	echo "# generated using $0 $@"
	declare -p CC CFLAGS CPPFLAGS LDFLAGS LDLIBS
} >.config.rc
[[ $verbose ]] && cat .config.rc
[[ $gen_flags ]] && printf '%s\n' "${CFLAGS[@]}" "${CPPFLAGS[@]}" >compile_flags.txt