summaryrefslogtreecommitdiff
path: root/Meta/write-only-on-difference.sh
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2020-08-02 22:05:45 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-04 11:27:14 +0200
commitaaa13e57396ba5eecf31ef5f925a73f4cf31518a (patch)
tree848037bcc1aae1a543d3648b3837a4d7a564a843 /Meta/write-only-on-difference.sh
parent98e18d73392aa997450fe7e823f906f87f47997a (diff)
downloadserenity-aaa13e57396ba5eecf31ef5f925a73f4cf31518a.zip
Meta: Provide a way to only update a file if the output changes
This is only useful for build commands that update their destination in all cases and thus sometimes confuse cmake into rebuilding everything needlessly.
Diffstat (limited to 'Meta/write-only-on-difference.sh')
-rwxr-xr-xMeta/write-only-on-difference.sh28
1 files changed, 28 insertions, 0 deletions
diff --git a/Meta/write-only-on-difference.sh b/Meta/write-only-on-difference.sh
new file mode 100755
index 0000000000..a6485e79a9
--- /dev/null
+++ b/Meta/write-only-on-difference.sh
@@ -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