aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/mk
blob: 051235a1f27720d7100eee64c7e0a3372d0cf303 (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
83
84
85
86
87
#!/bin/sh
progname="$(basename "$0")"
showmode() {
	! [ -n "$FROM_PROMPT" ] && return
	! [ -t 1 ] && return

	mode=" ($1)"
	col=$(( $(tput cols) - $(echo "$mode" | wc -L) + 1 ))
	printf '\e[1A\e['$col'G\e[1;30m%s\e[0m\n' "$mode"
}

make_tags() {
	build_dir="${1-build}"

	if [ -e "$build_dir/domains.yaml" ] ; then
		yaml2json "$build_dir/domains.yaml" | jq --raw-output '. as $r | $r.flash_order[] as $n | $r.domains[] | select(.name == $n) | .build_dir' | while IFS= read -r build_dir; do
    echo "Processing: $build_dir"
		done
	fi

}

mode_west() {
	showmode 'west'
	if [ -z "$*" ] ; then
		set -- build
		[ -e sysbuild ] || [ -e sysbuild.conf ] && set -- "$@" --sysbuild
	fi
	west "$@"
	ec=$?

	mktags build &

	exit $ec
}
path="."
for x in $(seq 10) ; do
	[ -e "$path/.west" ] && mode_west "$@"
	[ "$path" -ef "$path/.." ] && break
	path="$path/.."
done

mode_cmake() {
	showmode 'cmake'
	builddir="build"

	[ "$*" = "clean" ] && exec rm -rf "$builddir"

	export CMAKE_GENERATOR="Ninja"

	# re-run configuration if fresh or CMakeLists was changed
	if [ ! -e "$builddir/build.ninja" ] ||
		[ "CMakeLists.txt" -nt "$builddir/build.ninja" ] ; then
		cmake --log-level WARNING -B "$builddir" || exit $?
	fi

	# build
	cmake --build "$builddir" -- "$@"
	ec=$?

	# generate vim tags (continue after mk exits)
	mktags "$builddir" &

	exit $ec
}
[ -e "CMakeLists.txt" ] && mode_cmake "$@"

mode_make() {
	showmode 'make'
	exec make "$@"
}
[ -e "makefile" ] && mode_make "$@"
[ -e "Makefile" ] && mode_make "$@"

mode_latexmk() {
	showmode 'latexmk'

	[ "$*" = "clean" ] && exec latexmk -c

	exec latexmk "$@"
}
[ -e "latexmkrc" ] && mode_latexmk "$@"
[ -e ".latexmkrc" ] && mode_latexmk "$@"

echo "$progname: unable to determine project build system!" >&2
exit 1