blob: 00ff56973be04a109ea06bb7b9d8011a1c1392f8 (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
#!/bin/sh

# Build cgit for development and debugging. The sanitizers the compiler offers
# stop the program at a memory error or undefined behaviour, which makes it
# slow, so the result is for testing rather than serving. With clang it also
# leaves a compilation database in build/ for a language server, and with
# ctags a tags file there.
#
# Any arguments go on to make, the way Lua flags do where pkg-config cannot
# find Lua:
#
#     ./tools/build-development.sh LUA_PKGCONFIG=luajit
#
# Going back to a normal build takes make cleanall first.

set -eu

cd "$(dirname "$0")/.."

# The build starts from clean so the bundled git objects pick up the same
# flags, and cleanall rather than clean because only cleanall descends into
# vendor/git.
make cleanall

# Named once, so the probe and the build use the same compiler. CC goes on
# make's command line because git's Makefile sets it, which would override
# the one from the environment.
CC=${CC:-cc}

# OpenBSD's compiler has no AddressSanitizer, so the build falls back to the
# undefined behaviour checks alone, and to no sanitizer where neither links.
probe=$(mktemp "${TMPDIR:-/tmp}/cgit-probe.XXXXXX")
trap 'rm -f "$probe" "$probe.c"' EXIT
printf 'int main(void){return 0;}\n' >"$probe.c"
sanitize=""
for checks in address,undefined undefined
do
	if $CC -fsanitize="$checks" -o "$probe" "$probe.c" >/dev/null 2>&1
	then
		sanitize="SANITIZE=$checks"
		break
	fi
done
make "$@" CC="$CC" CFLAGS="-O0 -g3" DEVELOPER=1 $sanitize \
	GENERATE_COMPILATION_DATABASE=yes

# clang writes one entry per object under vendor/git/compile_commands, each
# ending in a comma, so they are joined into one array. cgit's are named after
# ../../build, which a shell glob would pass over as hidden.
if test -d vendor/git/compile_commands
then
	find vendor/git/compile_commands -name '*.json' -exec cat {} + |
		sed -e '1s/^/[/' -e '$s/,$/]/' >build/compile_commands.json
	test -s build/compile_commands.json || rm -f build/compile_commands.json
fi

if command -v ctags >/dev/null
then
	make tags
fi