Skip to content

Commit

Permalink
chore: Avoid calling GCC for tests when C code is unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
vain0x committed Feb 14, 2021
1 parent c3717f3 commit a904bdb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
23 changes: 15 additions & 8 deletions build-template.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ build $milone_dotnet_release: $

rule build_milone
description = build_milone out=$out
command = MILONE_HOME=$$PWD $milone compile $verbosity MiloneLang >$out
command = MILONE_HOME=$$PWD $milone compile $verbosity MiloneLang | scripts/write-if-change $out
restat = 1

rule build_cc
description = build_cc in=$in
Expand Down Expand Up @@ -92,7 +93,8 @@ build target/milone_gen3.c: $

rule diff
description = diff out=$out
command = diff $in >$out
command = diff $in | scripts/write-if-change $out
restat = 1

build target/milone_gen3.diff: $
diff $
Expand Down Expand Up @@ -128,12 +130,14 @@ build test_self: $
rule take_snapshot
description = take_snapshot $project
command = $
MILONE_HOME=$$PWD $milone compile -q $project >$out || $
echo 'milone-lang compile error.' >$out_file
MILONE_HOME=$$PWD $milone compile -q $project | scripts/write-if-change $out || $
echo 'milone-lang compile error.' | scripts/write-if-change $out_file
restat = 1

# Print snapshot diff.
rule git_diff_snapshot_stats
command = git diff --stat -- 'tests/**/*.c' >$out
command = git diff --stat -- 'tests/**/*.c' | scripts/write-if-change $out
restat = 1

build snapshot_diff: $
git_diff_snapshot_stats $
Expand All @@ -154,10 +158,13 @@ rule execute_test
description = execute_test $c_file
command = $
test ! -z "$$(grep '#error' -- $c_file)" && $
echo 'milone-lang compile error.' >$out || { $
$in >$out ; $
echo '$$? = '$$? >>$out ; $
echo 'milone-lang compile error.' | scripts/write-if-change $out || { $
$in >$out.tmp ; $
echo '$$? = '$$? >>$out.tmp ; $
cat $out.tmp | scripts/write-if-change $out ; $
rm -f $out.tmp ; $
}
restat = 1

rule diff_test_output
description = diff_test_output $test
Expand Down
30 changes: 30 additions & 0 deletions scripts/write-if-change
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
# USAGE:
# ... | write-if-change $FILENAME
#
# Write standard input to a file unless the input is same as current contents.
# That is, timestamp changes only if its contents change.

set -eu

FILENAME="$1"

if test -r "$FILENAME" && test ! -f "$FILENAME"
then
echo "ERROR: $FILENAME is not a regular file."
exit 1
fi

if test ! -f "$FILENAME"
then
cat >$FILENAME
exit 0
fi

OLD_CONTENTS="$(cat $FILENAME)"
NEW_CONTENTS="$(cat)"

if test "$OLD_CONTENTS" != "$NEW_CONTENTS"
then
echo "$NEW_CONTENTS" >$FILENAME
fi

0 comments on commit a904bdb

Please sign in to comment.