Skip to content

Commit

Permalink
Meta: Provide a way to only update a file if the output changes
Browse files Browse the repository at this point in the history
This is only useful for build commands that update their destination in all cases
and thus sometimes confuse cmake into rebuilding everything needlessly.
  • Loading branch information
BenWiederhake authored and awesomekling committed Aug 4, 2020
1 parent 98e18d7 commit aaa13e5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Meta/write-only-on-difference.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

set -e

if [ "$#" -lt "2" ]; then
echo "USAGE: $0 <file> <cmd...>"
exit 1
fi

DST_FILE="$1"
shift

# Just in case:
mkdir -p -- "$(dirname -- "${DST_FILE}")"

cleanup()
{
rm -f -- "${DST_FILE}.tmp"
}
trap cleanup 0 1 2 3 6

"$@" > "${DST_FILE}.tmp"
# If we get here, the command was successful, and we can overwrite the destination.

if ! cmp --quiet -- "${DST_FILE}.tmp" "${DST_FILE}"; then
# File changed, need to overwrite:
mv -f -- "${DST_FILE}.tmp" "${DST_FILE}"
fi

0 comments on commit aaa13e5

Please sign in to comment.