# Automatically build labwc
#
-builddir=build-clang
+builddir=build
+g_asan=
+g_wlroots=
-if ! [ -e subprojects/wlroots ]; then
- git clone https://github.com/swaywm/wlroots subprojects/wlroots
-fi
+die () { printf '\033[31mfatal:\033[m %b\n' "$@" >&2 ; exit 1 ; }
+warn () { printf '\033[31mwarn:\033[m %b\n' "$@" >&2 ; }
+say () { printf '\033[32m%s\033[m' "$@" ; }
+usage () {
+ printf "%s\n" \
+"Usage: ./tools/build [<options>]
+Options:
+-w install wlroots as subproject
+--asan run with ASAN and UBSAN
+--clang run with clang
+-h, --help show help"
+ exit 0
+}
-if ! [ -e ${builddir} ]; then
- CC=clang meson -Dwlroots:default_library=static ${builddir}
- # To enable ASAN and UBSAN, run meson with
- # `-Db_sanitize=address,undefined`
-fi
+wlroots_subproject () {
+ if ! [ -e subprojects/wlroots ]; then
+ git clone https://github.com/swaywm/wlroots subprojects/wlroots
+ fi
+}
-ninja -C ${builddir}
+main () {
+ [[ -e src/main.c ]] || die "must be run from top-level directory"
+
+ for arg
+ do
+ opt=${arg%%=*}
+ var=${arg#*=}
+ case "$opt" in
+ -w)
+ wlroots_subproject
+ g_wlroots="-Dwlroots:default_library=static"
+ ;;
+ --asan)
+ g_asan="-Db_sanitize=address,undefined" ;;
+ --clang)
+ export CC=clang
+ ;;
+ -h|--help)
+ usage ;;
+ esac
+ done
+
+ [ -e ${builddir} ] && die "build directory already exists - delete and run again"
+ meson ${g_asan} ${g_wlroots} ${builddir}
+ ninja -C ${builddir}
+ LSAN_OPTIONS=suppressions=../tools/asan_suppressions.txt ninja -C ${builddir} test
+}
+
+main "$@"