diff options
author | Cem Keylan <cem@ckyln.com> | 2020-12-26 02:39:36 +0300 |
---|---|---|
committer | Cem Keylan <cem@ckyln.com> | 2020-12-26 03:12:25 +0300 |
commit | d1590234d19724f14e8ba94582986963fce47267 (patch) | |
tree | 1bb6273414ef57443b3cdda21f0329b0dc8ae742 /lib.rc | |
parent | 06aca27d4b504ea5f42f4611fa3b44d9429bea75 (diff) | |
download | docs-d1590234d19724f14e8ba94582986963fce47267.tar.gz |
docs: switch to redo build system.20201226
Diffstat (limited to 'lib.rc')
-rw-r--r-- | lib.rc | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -0,0 +1,84 @@ +# Various helper functions for redo +# URL: https://github.com/cemkeylan/redo-lib +# LICENSE: CC0 (Public Domain) + +# 'basename' is not used by the functions here, but it doesn't mean that it +# cannot be used at all. +# shellcheck disable=2034 +target=$1 basename=$2 dest=$3 + +# Add dependency to these files as well. +redo-ifchange lib.rc config.rc + +setv() { + # Usage: setv [variable = [key...]] + # + # Variable setting function that somewhat imitates the Makefile syntax. + # - Using = sets the variable. + # - Using ?= sets the variable if it is unset. + # - Using += increments to a variable. + [ "$3" ] || { + printf '%s\n' "Faulty variable syntax" >&2 + exit 1 + } + var=$1 sym=$2; shift 2 + case "$sym" in + ==) export "$var=$*" ;; + =) eval "[ \"\$$var\" ]" || export "$var=$*" ;; + +=) eval export "$var=\$$var $*" + esac +} + +targcheck() { + # Usage: targcheck [target...] + # + # Check if current target is one of the given arguments of this function. + # Returns 0 if target is one of the arguments, returns 1 if not. + for arg; do + [ "$arg" = "$target" ] && return 0 + done; return 1 +} + +PHONY() { + # Usage: PHONY [target...] + # + # Function that resembles the .PHONY: target on the classic 'make' system. + # You can either use it without an argument on a single target, or specify + # multiple targets. + if [ -z "$1" ] || targcheck "$@"; then + # There is no guarantee that the value of dest will not be modified + # during the operation, we want to evaluate the value of $dest as soon + # as possible + # shellcheck disable=2064 + trap "rm -f $dest" EXIT INT + fi +} + +expsuf() { + # Usage: expsuf [suffix [item...]] + # + # Expand suffix for the given list. + suffix=$1 buf=; shift + for i; do buf="$buf $i$suffix "; done; printf %s "$buf" +} + +repsuf() { + # Usage: repsuf [old-suffix new-suffix [item...]] + # + # Replace old-suffix with new-suffix on list. + oldsuffix=$1 newsuffix=$2 buf=; shift 2 + for i; do buf="$buf ${i%$oldsuffix}$newsuffix "; done; printf %s "$buf" +} + +redo_clean() { + # Clean function for various redo implementations + [ -r .do_built ] && { + while read -r file; do + [ -d "$file" ] || rm -f "$file" + done < .do_built + } + find . -type f \( -name '*.tmp' -o -name '*.did' -o -name '.dep*' -o -name '.target*' \) \ + -exec rm -f -- {} + + [ "$DO_BUILT" ] || find . -name '.do_built*' -exec rm -rf -- {} + + [ "$REDO_BASE" ] || rm -rf -- .redo +} |