shake
minimal build system that generates Ninja build files
git clone https://9o.is/git/shake.git
commit 7ef77cd50c48828cbd93ec41e25d9357f9d6491a parent 930c3af82ed243332bfa99fda9fd064aa1796ffb Author: Jul <jul@9o.is> Date: Fri, 13 Mar 2026 03:15:25 +0800 handle prefixing and build formatting in awk Diffstat:
| M | shake | | | 16 | ++++++++-------- |
| M | shakeout.awk | | | 108 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
2 files changed, 81 insertions(+), 43 deletions(-)
diff --git a/shake b/shake @@ -95,7 +95,7 @@ shakeout() { } sub() { - printf 'shake:subninja $dir/%s.ninja\n' "$1" + printf 'shake+subninja $dir/%s.ninja\n' "$1" { [ "${2-}" ] && eval "$2 $1" $1 @@ -104,7 +104,7 @@ sub() { } shake() { - printf 'shake:subninja $dir/%s/%s.ninja\n' "$1" "$NINJA_FN" + printf 'shake+subninja $dir/%s/%s.ninja\n' "$1" "$NINJA_FN" NINJA_FILES="$NINJA_FILES $dir/$1/ninja" if in_target_route $1; then @@ -127,12 +127,12 @@ shake() { } var() { - printf 'shake:var %s %s\n' "$1" "${*:2}" + printf 'shake+var %s %s\n' "$1" "${*:2}" eval "$1='\$$1'" } bind() { - printf 'shake:bind %s %s\n' "$1" "${*:2}" + printf 'shake+bind %s %s\n' "$1" "${*:2}" } build() { @@ -141,7 +141,7 @@ build() { shift 2 prefix _vs $dir $* build_parse $_vs - printf 'shake:build %s: %s %s %s %s\n' "$_v2" "$_v1" "$_vs" "$_vd" "$_vo" + printf 'shake+build %s: %s %s %s %s\n' "$_v2" "$_v1" "$_vs" "$_vd" "$_vo" } build_parse() { @@ -168,7 +168,7 @@ rule() { _rule=$1 _cmd="$2" - printf 'shake:rule %s\n' "$_rule" + printf 'shake+rule %s\n' "$_rule" bind command "$_cmd" _d= @@ -182,13 +182,13 @@ rule() { } default() { - printf 'shake:default %s\n' "$*" + printf 'shake+default %s\n' "$*" } phony() { prefix _v $dir $1 shift - printf 'shake:build %s: phony %s\n' "$_v" "$*" + printf 'shake+build %s: phony %s\n' "$_v" "$*" } prefix() { diff --git a/shakeout.awk b/shakeout.awk @@ -1,71 +1,109 @@ -function print_tail(start, i, ch) { - for(i=start; i<=NF; i++) { - if (prefix != "" && $i !~ /^($|.\/|\/)/) - printf " %s/%s", prefix, $i - else - printf " %s", $i - } +BEGIN { + state = "" } -function reset() { - state = "" - prefix = "" +function reset(newstate) { + state = newstate printf "\n" } -BEGIN { - state = "" - prefix = "" +function add_prefix(str) { + if (str ~ /^(\$|\.\/|\.\.\/|\/)/) return str + if (state == "VAR") return str + if (state == "BRUL") return str + if (state == "BOUT") return "$outdir/" str + if (state == "BIN") return "$dir/" str + if (state == "BDEP") return "$dir/" str + if (state == "BORD") return "$dir/" str + + printf "shake: error add_prefix()\n" > "/dev/stderr" + exit 1 } -/^shake:build / { - reset() - printf "build %s", $2 - print_tail(3) - state = "inline" +function print_tail(start, i) { + for(i=start; i<=NF; i++) { + printf " %s", add_prefix($i) + } +} + +function process(cmd, str) { + while (match(str, /(:|\|\||\||[ \t]+)/)) { + tok = substr(str, 1, RSTART - 1) + sep = substr(str, RSTART, RLENGTH) + + if (tok != "" && tok != cmd) { + printf " %s", add_prefix(tok) + if (state == "BRUL") state = "BIN" + } + + if (sep ~ /:/) { + state = "BRUL" + printf ":" + } + + else if (sep ~ /\|\|/ && state != "BORD") { + state = "BORD" + printf " ||" + } + + else if (sep ~ /\|/ && state != "BDEP") { + state = "BDEP" + printf " |" + } + + str = substr(str, RSTART + RLENGTH) + } + + if (str != "") printf " %s", add_prefix(str) +} + +/^shake\+build / { + reset("BOUT") + printf "build" + process("shake+build", $0) next } -/^shake:var / { - reset() +/^shake\+var / { + reset("VAR") printf "%s =", $2 print_tail(3) - state = "inline" next } -/^shake:bind / { - reset() +/^shake\+bind / { + reset("VAR") printf " %s =", $2 print_tail(3) - state = "inline" next } -/^shake:default / { - reset() +/^shake\+default / { + reset("BIN") printf "default" - state = "inline" - prefix = "$dir" print_tail(2) next } -/^shake:(rule|subninja) / { - reset() - split($1, parts, ":") - printf "%s %s", parts[2], $2 +/^shake\+subninja / { + reset("BIN") + printf "subninja %s", add_prefix($2) + next +} + +/^shake\+rule / { + reset("") + printf "rule %s", $2 next } -state == "inline" { +state == "VAR" { print_tail(1) next } { - printf "shake: unknown command: %s\n", $0 > "/dev/stderr" - exit 1 + process("", $0) } END {