-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.sh
390 lines (324 loc) · 7.94 KB
/
config.sh
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
## config.sh: 3211cef, see https://github.com/jmesmon/cninja.git
# ex: sts=8 sw=8 ts=8 noet
set -eu
D="$(dirname "$0")"
: ${SRC_DIR:="$D"}
: ${OBJ_DIR:="."}
: ${HOST_CC:=cc}
: ${HOST_LDFLAGS:=}
: ${CROSS_COMPILER:=}
: ${CC:=${CROSS_COMPILER}cc}
: ${OBJCOPY:=${CROSS_COMPILER}objcopy}
# FIXME: do we need a env wrapper here? (yes)
: ${HOST_PKGCONFIG:=pkg-config}
: ${PKGCONFIG:=pkg-config}
# XXX: convenience only
: ${GIT_VER:=$(${GIT:-git} describe --dirty=+ --always 2>/dev/null || echo "+")}
: ${WARN_FLAGS_C:="-Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wmissing-declarations -Wbad-function-cast"}
: ${WARN_FLAGS:="-Wall -Wundef -Wshadow -Wcast-align -Wwrite-strings -Wextra -Werror=attributes -Wno-missing-field-initializers ${WARN_FLAGS_C}"}
# XXX: handle for HOST too
# XXX: consider parallelizing invocation here?
if [ -n "${PKGCONFIG_LIBS:=}" ]; then
PKGCONFIG_CFLAGS="$(${PKGCONFIG} --cflags ${PKGCONFIG_LIBS})"
PKGCONFIG_LDFLAGS="$(${PKGCONFIG} --libs ${PKGCONFIG_LIBS})"
else
PKGCONFIG_CFLAGS=""
PKGCONFIG_LDFLAGS=""
fi
LIB_CFLAGS="${LIB_CFLAGS:-} ${PKGCONFIG_CFLAGS} "
LIB_LDFLAGS="${LIB_LDFLAGS:-} ${PKGCONFIG_LDFLAGS}"
ALL_CFLAGS="${WARN_FLAGS}"
if_runs () {
local y="$1"
local n="$2"
shift 2
"$@" >/dev/null 2>&1 && printf "%s" "$y" || printf "%s" "$n"
}
# Given a series of flags for CC, echo (space seperated) the ones that the
# compiler is happy with.
# XXX: Note that this does mean flags with spaces in them won't work.
EMPTY_MAIN="int main(void) { return 0; }"
cflag_x () {
local cc=$(eval printf "%s" "\${$1CC}")
local cflags=$(eval printf "%s" "\${$1CFLAGS:-}")
shift
for i in "$@"; do
printf "%s" "$EMPTY_MAIN" | if_runs "$i " "" $cc $cflags -x c "$i" - -o /dev/null
done
}
try_run() {
"$@" >/dev/null 2>&1
}
cflag_first () {
local cc=$(eval printf "%s" "\${$1CC}")
local cflags=$(eval printf "%s" "\${$1CFLAGS:-}")
shift
for i in "$@"; do
if try_run $cc $cflags -x c "$i" /dev/null -o /dev/null; then
echo "$i"
return
fi
done
}
die () {
>&2 echo "Error: $*"
exit 1
}
: ${EXTRA_FLAGS=}
: ${SANITIZE_FLAGS="$(cflag_x "" -fsanitize=address -fsanitize=undefined)"}
: ${DEBUG_FLAGS="$(cflag_x "" -ggdb3 -fvar-tracking-assignments)"}
: ${LTO_FLAGS="$(cflag_x "" -flto)"}
: ${OPT_FLAGS="$(cflag_first "" -Og -Os -O2)"}
COMMON_FLAGS="${SANITIZE_FLAGS} ${DEBUG_FLAGS} ${LTO_FLAGS}"
: ${CFLAGS="${ALL_CFLAGS} ${COMMON_FLAGS} ${OPT_FLAGS} ${EXTRA_FLAGS}"}
if [ -n "${LTO_FLAGS}" ]; then
COMMON_FLAGS="${COMMON_FLAGS} ${OPT_FLAGS}"
fi
# Without LIB_CFLAGS
# FIXME: may not be entirely correct, sometimes we'll want libraries in host binaries
: ${HOST_CFLAGS:=${CFLAGS:-}}
CFLAGS="-DCFG_GIT_VERSION=${GIT_VER} -I. ${LIB_CFLAGS} ${CFLAGS:-}"
: ${LDFLAGS:="${COMMON_FLAGS}"}
LDFLAGS="${LDFLAGS} ${DEBUG_FLAGS}"
CONFIG_H_GEN=./config_h_gen
CONFIGS=""
: ${CPPFLAGS=}
# Check if compiler likes -MMD -MF
if $CC $CFLAGS -MMD -MF /dev/null -c -x c /dev/null -o /dev/null >/dev/null 2>&1; then
DEP_LINE=" depfile = \$out.d"
DEP_FLAGS="-MMD -MF \$out.d"
else
DEP_LINE=""
DEP_FLAGS=""
fi
exec 5>build.ninja
>&5 echo "# generated by config.sh"
>&5 cat <<EOF
host_cc = $HOST_CC
host_cflags = $HOST_CFLAGS
host_ldflags = $HOST_LDFLAGS
cc = $CC
objcopy = $OBJCOPY
cflags = $CFLAGS
ldflags = $LDFLAGS
cppflags = $CPPFLAGS
libs = $LIB_LDFLAGS
rule cc
command = \$cc \$cflags $DEP_FLAGS -c \$in -o \$out
$DEP_LINE
rule cc_fail
command = ! \$cc \$cflags $DEP_FLAGS -c \$in -o \$out
$DEP_LINE
rule cc_host
command = \$host_cc \$host_cflags $DEP_FLAGS -c \$in -o \$out
$DEP_LINE
rule ccld_host
command = \$host_cc \$host_ldflags -o \$out \$in
rule ccld
command = \$cc \$ldflags -o \$out \$in \$libs
rule config_h_frag
command = ${CONFIG_H_GEN} \$in \$cc \$cflags \$ldflags > \$out
rule combine
command = cat \$in > \$out
rule ninja_gen
command = $0
generator = yes
EOF
CONFIGURE_DEPS="$0"
# <target>
target_dir() {
local target="$1"
printf "%s" ".build-$target"
}
# <target>
target_ldflags() {
local target="$1"
local v="$(var_name "$target")"
_ev "ldflags_$v:-"
}
var_name() {
echo "$1" | sed -e 's/-/_/'
}
# <target>
target_cflags() {
local target="$1"
local v="$(var_name "$target")"
_ev "cflags_$v:-"
_ev "cppflags_$v:-"
}
# <target> <src-file>...
to_obj () {
local target="$1"
shift
for i in "$@"; do
printf "%s " "$(target_dir "$target")/$i.o"
done
}
_ev () {
eval printf "%s " "\${$1}"
}
config () {
local configs=""
for i in "$D"/config_h/*.c; do
local name=".config.h-$i-frag"
>&5 echo "build $name : config_h_frag $i | "$D"/if_compiles ${CONFIG_H_GEN}"
configs="$configs $name"
done
>&5 echo "build config.h : combine $D/config_h/prefix.h $configs $D/config_h/suffix.h"
}
if [ -e "config_h" ]; then
CONFIG_H=true
else
CONFIG_H=false
fi
# If any files in config_h change, we need to re-generate build.ninja
if $CONFIG_H; then
CONFIGURE_DEPS="$CONFIGURE_DEPS config_h/ ${CONFIG_H_GEN}"
fi
e_if() {
local v=$1
shift
if $v; then
echo "$@"
fi
}
# <target> <src> [<act>]
# uses: CONFIG_H
obj() {
local target=$1
local s=$2
shift
shift
local act=cc
if [ $# -ne 0 ]; then
act=$1
shift
fi
>&5 cat <<EOF
build $(to_obj "$target" "$s"): $act $s | $(e_if $CONFIG_H config.h)
cflags = \$cflags -I$(target_dir "$target") $(target_cflags "$target")
EOF
}
# <target> <obj>...
bin_base () {
local target="$1"
shift
>&5 cat <<EOF
build $target : ccld $@
ldflags = -L$(target_dir "$target") \$ldflags $(target_ldflags "$target")
EOF
}
# <target> <src>...
bin() {
if [ "$#" -lt 2 ]; then
die "'bin $1' has to have some source"
fi
local target="$1"
shift
for s in "$@"; do
obj "$target" "$s"
done
bin_base "$target" $(to_obj "$target" "$@")
>&5 echo "default $target"
}
# <target> <src>
host_obj() {
local target="$1"
>&5 cat <<EOF
build $(to_obj "$target" "$s"): cc_host $s | $(e_if $CONFIG_H config.h)
EOF
}
# <target> <src>...
host_bin() {
local target="$1"
shift
for s in "$@"; do
host_obj "$target" "$s"
done
>&5 cat <<EOF
build $target : ccld_host $@
EOF
}
# <target>
host_run() {
>&2 echo "warning: NOT running $1"
}
# <target> <file>
add_run_test() {
local target=$1
local f=$2
local b=$(basename $f .c)
host_bin "$target/$b" "$f"
host_run "$target/$b"
}
# Add a set of tests based on source files in a given directory
#
# The type of test is determined by the first part of the file name.
#
# Types:
# - compile tests: build the source file with the target compiler. Build
# success is test success. Only the single file is compiled, nothing additional
# is linked to it. (other than as specified by target link flags)
# - compile_fail tests: the same as compile tests, except build failure is test
# success.
# - run tests: compile tests, but additionally run the test. (XXX: resolve
# running target tests)
# - api tests: compile tests, but link the module we're a part of to the test object.
#
#
#
#
# <target-name> <path-to-dir>
add_test_dir() {
local target="$1/test"
local f="$2"
for f in "$f"/*; do
b="$(basename "$f")"
case "$b" in
compile*.c)
obj "$target" "$f"
>&5 echo "default $(to_obj "$target" "$f")"
;;
compile_fail*.c)
obj "$target" "$f" cc_fail
>&5 echo "default $(to_obj "$target" "$f")"
;;
run*.c)
obj "$target" "$f"
>&5 echo "default $(to_obj "$target" "$f")"
add_run_test "$target" "$f"
;;
api*.c)
>&2 echo "api test not supported, skipped $f"
;;
esac
done
}
# A module is a directory with a particular layout that gives us a library
# which can be linked against
#
# ./tests is a test dir as specified by `add_test_dir()`
# ./*.c are the source files compiled into objects and included in the library
# ./*.h are headers that are used as the library's public interface
#
# <directory>
add_module() {
local m="$1"
local f
for f in "$D/$1"/*; do
b="$(basename "$f")"
if [ -d "$f" ]; then
if [ "$b" = test ]; then
add_test_dir "$m" "$f"
else
>&2 echo unknown dir $f
fi
else
>&2 echo file $f
fi
done
}
end_of_ninja () {
>&5 echo build build.ninja : ninja_gen $CONFIGURE_DEPS
}
trap end_of_ninja EXIT