aboutsummaryrefslogtreecommitdiffstats
path: root/.vars.rc.do
blob: 7c4d41722e92674b73a35d8bdb912929ef88a9c8 (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
#!/usr/bin/env bash
set -e

# Ensure variables from the config aren't set before the config is read to
# avoid capturing transient state
unset {,T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} \
      colour debug optimise verbose warn werror extra_targets

CFLAGS=(-std=c11)
TCFLAGS=(-fPIC)

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

declare -a {,T,H}{CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} extra_targets

if [ -e config.rc ]; then
	redo-ifchange config.rc
	. ./config.rc
else
	redo-ifcreate config.rc
fi

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

if [[ $colour ]]; then
	if [[ $colour != 1 ]]; then no=no-; fi
	CFLAGS+=("-f${no}diagnostics-color")
	LDFLAGS+=("-f${no}diagnostics-color")
fi
if [[ $debug ]]; then
	CFLAGS+=(-Og -g)
	LDFLAGS+=(-Og -g)
fi
if [[ $optimise ]]; then
	TCFLAGS+=(-O2 -flto)
	TLDFLAGS+=(-O2 -flto)
fi
if [[ $warn ]]; then
	CFLAGS+=("${warnings[@]}")
	if [[ $warn = 'gcc' ]]; then
		CFLAGS+=(-Wsuggest-attribute=format -Wsuggest-attribute=noreturn)
	fi
fi
if [[ $werror ]]; then
	CFLAGS+=(-Werror)
fi

if [[ ! $TCC ]]; then TCC=$CC; fi
if [[ ! $HCC ]]; then HCC=$CC; fi
for p in T H; do
	for v in CFLAGS CPPFLAGS LDFLAGS LDLIBS; do
		eval "$p$v+=(\"\${$v[@]}\")"
	done
done

{
	echo "# generated by $0"
	declare -p {T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} extra_targets
} >"$3"

if [[ $verbose ]]; then
	echo "# Configuration regenerated:" >&2
	cat "$3" >&2
fi