autodoc/build/autodoc.sh

103 lines
2.2 KiB
Bash
Raw Normal View History

2019-09-19 16:11:47 +00:00
#!/bin/bash
# $1:WATCHROOT (default: ".")
g_watchroot="$(readlink -f "${1:-.}")"
# compile using bare make command
do_make() { # $1:DIR $2:MAKEFILE $3:OBJECT
2019-09-19 16:11:47 +00:00
# extract params
local dir="$1"
local makefile="$2"
local object="$3"
2019-09-19 16:11:47 +00:00
2019-09-24 09:37:39 +00:00
# check Makefile 'source pattern'
local srcpat="$(grep -E "^#@SRCPAT" "${dir}/${makefile}" | tail -n 1 | sed -r "s/^#@SRCPAT\s+//")"
2019-09-19 16:11:47 +00:00
if [ -z "${srcpat}" ]; then
2019-09-24 09:37:39 +00:00
echo -n "Empty source pattern, check '#@SRCPAT' annotation! "
return 1
2019-09-20 09:02:01 +00:00
2019-09-19 16:11:47 +00:00
elif [[ "${object}" =~ ${srcpat} ]]; then
# check for autodoc target
local target="$(grep -E "^autodoc:" "${dir}/${makefile}" | sed -r "s/:.*$//")"
2019-09-24 09:37:39 +00:00
if [ -z "${target}" ]; then
echo "Running 'make'. "
else
echo "Making '${target}'. "
fi
make --no-print-directory -C "${dir}" -j ${target}
2019-09-20 09:02:01 +00:00
2019-09-19 16:11:47 +00:00
else
2019-09-24 09:37:39 +00:00
echo -n "SRCPAT '${srcpat}' mismatch. "
return 1
2019-09-19 16:11:47 +00:00
fi
2019-09-24 09:37:39 +00:00
return 0
2019-09-19 16:11:47 +00:00
}
# compile a directory
2019-09-20 09:02:01 +00:00
do_compile() { # $1:DIR $2:OBJECT $3:DONE
2019-09-19 16:11:47 +00:00
# extract params
local dir="$1"
local object="$2"
2019-09-20 09:02:01 +00:00
local done="${3:-0}"
2019-09-19 16:11:47 +00:00
2019-09-20 09:02:01 +00:00
# build systems
if [ -r "${dir}/Makefile" ]; then
# Makefile found
2019-09-24 09:37:39 +00:00
echo -n "Found '${dir}/Makefile': "
do_make "${dir}" "Makefile" "${object}" \
&& local done="1"
2019-09-20 09:02:01 +00:00
fi
2019-09-19 16:11:47 +00:00
# never leave $g_watchroot
2019-09-20 09:02:01 +00:00
if [ "${dir}" != "${g_watchroot}" ]; then
# search parent dir for more build instructions
do_compile "$(dirname "${dir}")" "${object}" "${done}"
2019-09-19 16:11:47 +00:00
2019-09-20 09:02:01 +00:00
elif [ "${done}" == "0" ]; then
# hit $g_watchroot
echo "No build instructions found!"
2019-09-19 16:11:47 +00:00
fi
}
# process an inotify event
do_handle() { # $1:FLAGS $2:OBJECT
# extract params
local flags="$1"
shift 1
local dir="$(dirname "$*")"
local object="$(basename "$*")"
if [[ "${flags}" =~ "ISDIR" ]]; then
2019-09-20 07:48:30 +00:00
# object refers to directory
2019-09-19 16:11:47 +00:00
local dir="${dir}/${object}"
local object="."
fi
2019-09-20 07:48:30 +00:00
# start using toolchain
2019-09-20 09:02:01 +00:00
echo -n "'${object}': '${flags}' in '${dir}'. "
2019-09-19 16:11:47 +00:00
do_compile "${dir}" "${object}"
}
#
# MAIN
#
2019-09-20 07:48:30 +00:00
echo "Booting ${0} in '${g_watchroot}'."
2019-09-19 16:11:47 +00:00
# setup inotify:
# -mrq monitor, recursive, quiet
# -e events
# --format %e eventlist csv, %w workdir, %f filename
inotifywait -mrq \
-e create -e delete -e moved_to -e close_write \
--format '%e %w%f' \
"${g_watchroot}" | \
\
while read FILE; do
do_handle ${FILE}
done