shake
minimal build system that generates Ninja build files
git clone https://9o.is/git/shake.git
commit be28ad60ce28d10b34e2ec35b302b49f881a31c9 parent 2731d5bc1edbd44d8a3c8a534bea5e836a5ef3bd Author: Jul <jul@9o.is> Date: Thu, 12 Mar 2026 14:39:40 +0800 remove write function - pipe stdout to ninja file Diffstat:
| M | shake | | | 116 | +++++++++++++++++++++++++++++++++---------------------------------------------- |
1 file changed, 49 insertions(+), 67 deletions(-)
diff --git a/shake b/shake @@ -8,10 +8,10 @@ TARGET_ROUTE= BUILDDIR=.shake DIR=. OUTDIR=. -NINJA_FILE=local.ninja -NINJA_FILES=$DIR/$NINJA_FILE +NINJA_FN=local +NINJA_FILES=$DIR/$NINJA_FN.ninja GEN_FILES="./Shakefile $SHAKE_BIN" -GEN_OUTS=$DIR/$NINJA_FILE +GEN_OUTS=$DIR/$NINJA_FN.ninja usage() { printf "usage: shake [options] [target] @@ -58,29 +58,9 @@ while [ $# -gt 0 ]; do esac done -init_file() { - if persist && [ -e $DIR/$NINJA_FILE.tmp ]; then - rm -f $DIR/$NINJA_FILE.tmp - fi -} - -fini_file() { - _f=$DIR/$NINJA_FILE - if persist && [ -e $_f.tmp ]; then - if cmp -s $_f.tmp $_f; then - rm -f $_f.tmp - else - mv $_f.tmp $_f - fi - fi -} - fini_gen() { gen "$GEN_OUTS" '|' $GEN_FILES phony ninja $NINJA_FILES - phony build.ninja ninja - bind generator 1 - fini_file wait } @@ -99,52 +79,50 @@ in_target_route() { [ ! "$TARGET" ] || has $DIR/$1 $TARGET_ROUTE } -persist() { - [ ! "$TARGET" ] || [ $TARGET = $DIR ] -} - -write() { - if persist; then - printf '%s\n' "$@" >> $DIR/$NINJA_FILE.tmp +shakeout() { + _f=$3 + if [ ! "$TARGET" ] || [ $TARGET = "$1" ]; then + printf '' >$_f.tmp + $(type -P cat) >$_f.tmp + if cmp -s $_f.tmp $_f; then + rm -f $_f.tmp + else + mv $_f.tmp $_f + fi + else + $(type -P cat) >/dev/null fi } -sub() ( - write "subninja $dir/$1.ninja" - NINJA_FILE=$1.ninja - init_file - - [ "${2-}" ] && eval "$2 $1" - $1 - [ "${3-}" ] && eval "$3 $1" - fini_file -) +sub() { + printf 'subninja $dir/%s.ninja\n' "$1" + { + [ "${2-}" ] && eval "$2 $1" + $1 + [ "${3-}" ] && eval "$3 $1" + } | shakeout "$DIR" "$OUTDIR" "$DIR/$1.ninja" +} shake() { - write "subninja $dir/$1/$NINJA_FILE" + printf 'subninja $dir/%s/%s.ninja\n' "$1" "$NINJA_FN" NINJA_FILES="$NINJA_FILES $dir/$1/ninja" if in_target_route $1; then - ( + { DIR=$DIR/$1 OUTDIR=$OUTDIR/$1 - NINJA_FILES=$DIR/local.ninja + NINJA_FILES=$DIR/$NINJA_FN.ninja GEN_FILES="$GEN_FILES $DIR/Shakefile" - GEN_OUTS=$DIR/$NINJA_FILE - - init_file + GEN_OUTS=$DIR/$NINJA_FN.ninja var dir $dir/$1 var outdir $outdir/$1 [ "${2-}" ] && eval "$2 $1" - . $DIR/Shakefile - [ "${2-}" ] && eval "$3 $1" - fini_gen - ) & + } | shakeout "$DIR/$1" "$OUTDIR/$1" "$DIR/$1/$NINJA_FN.ninja" & fi } @@ -152,7 +130,7 @@ var() { _v1=$1 shift inline _vs $* - write "$_v1 = $_vs" + printf '%s = %s\n' "$_v1" "$_vs" eval "$_v1='\$$_v1'" } @@ -160,7 +138,7 @@ bind() { _v1=$1 shift inline _vs $* - write " $_v1 = $_vs" + printf ' %s = %s\n' "$_v1" "$_vs" } build() { @@ -169,7 +147,7 @@ build() { shift 2 prefix _vs $dir $* build_parse $_vs - write "build $_v2: $_v1 $_vs $_vd $_vo" + printf 'build %s: %s %s %s %s\n' "$_v2" "$_v1" "$_vs" "$_vd" "$_vo" } build_parse() { @@ -196,7 +174,7 @@ rule() { _rule=$1 _cmd="$2" - write "rule $_rule" + printf 'rule %s\n' "$_rule" bind command "$_cmd" _d= @@ -211,13 +189,13 @@ rule() { default() { prefix _v $dir $1 - write "default $_v" + printf 'default %s\n' "$_v" } phony() { prefix _v $dir $1 shift - write "build $_v: phony $*" + printf 'build %s: phony %s\n' "$_v" "$*" } prefix() { @@ -320,21 +298,25 @@ case "$TARGET" in esac mkdir -p $BUILDDIR -init_file -var ninja_required_version 1.8 -var builddir $BUILDDIR -var dir $DIR -var outdir $OUTDIR +{ + var ninja_required_version 1.8 + var builddir $BUILDDIR + var dir $DIR + var outdir $OUTDIR -rule gen "$SHAKE_BIN $dir" -bind description SHAKE $dir -bind generator 1 -bind restat 1 + rule gen "$SHAKE_BIN $dir" + bind description SHAKE $dir + bind generator 1 + bind restat 1 -trap fini_gen EXIT + trap fini_gen EXIT -. $DIR/Shakefile + . $DIR/Shakefile + + phony build.ninja ninja + bind generator 1 +} | shakeout "$DIR" "$OUTDIR" "$DIR/$NINJA_FN.ninja" if [ ! -L build.ninja ]; then rm -f build.ninja