build: only re-generate src/builddata.c when needed
authorFerry Huberts <ferry.huberts@pelagic.nl>
Thu, 11 Jul 2013 10:11:17 +0000 (12:11 +0200)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Thu, 11 Jul 2013 10:11:39 +0000 (12:11 +0200)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
.gitignore
Makefile
make/hash_source.sh

index fff34a6..b09a271 100644 (file)
@@ -20,6 +20,7 @@
 /.project
 /.settings
 /build-stamp
+/builddata.txt
 /configure-stamp
 /olsrd
 /olsrd.exe
index 849f29f..66fe7d4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -84,14 +84,13 @@ switch:
        $(MAKECMDPREFIX)$(MAKECMD) -C $(SWITCHDIR)
 
 # generate it always
-.PHONY: src/builddata.c
-src/builddata.c:
-       $(MAKECMDPREFIX)$(RM) "$@"
-       $(MAKECMDPREFIX)echo "#include \"defs.h\"" >> "$@" 
-       $(MAKECMDPREFIX)echo "const char olsrd_version[] = \"olsr.org -  $(VERS)`./make/hash_source.sh`\";"  >> "$@"
-       $(MAKECMDPREFIX)date +"const char build_date[] = \"%Y-%m-%d %H:%M:%S\";" >> "$@" 
-       $(MAKECMDPREFIX)echo "const char build_host[] = \"$(shell hostname)\";" >> "$@" 
+.PHONY: builddata.txt
+builddata.txt:
+       $(MAKECMDPREFIX)./make/hash_source.sh "$@" "$(VERS)" "$(VERBOSE)"
 
+# only overwrite it when it doesn't exists or when it has changed
+src/builddata.c: builddata.txt
+       $(MAKECMDPREFIX)if [ ! -f "$@" ] || [ -n "$$(diff "$<" "$@")" ]; then cp -a -v "$<" "$@"; fi
 
 .PHONY: help libs clean_libs libs_clean clean distclean uberclean install_libs uninstall_libs libs_install libs_uninstall install_bin uninstall_bin install_olsrd uninstall_olsrd install uninstall build_all install_all uninstall_all clean_all gui clean_gui cfgparser_install cfgparser_clean
 
index 9b41fe6..930a9c3 100755 (executable)
@@ -1,15 +1,54 @@
 #!/bin/sh
 
-sha="`git log -1 --pretty=%h 2> /dev/null`"
-if [ -z "$sha" ]; then
-  sha="0000000"
+if [ $# -ne 3 ]; then
+       echo "ERROR: Supply filename, version and verbosity"
+       echo "       Example: $0 builddata.txt pre-0.6.7 1"
+       exit 1
 fi
-echo -n "-git_$sha"
 
-md5cmd=md5sum
-os=$(uname)
-if [ "xDarwin" = "x$os" ] ; then
-  md5cmd=md5
+buildDataTxt="$1"
+version="$2"
+verbose="$3"
+
+
+md5Command="md5sum"
+osName="$(uname)"
+if [ "x$osName" == "xDarwin" ] ; then
+  md5Command="md5"
+fi
+
+
+gitSha="$(git log -1 --pretty=%h 2> /dev/null)"
+if [ -z "$gitSha" ]; then
+  gitSha="0000000"
 fi
-echo -n "-hash_"
-cat `find . -name *.[ch] | grep -v -E '[/\\]?builddata.c$'`| $md5cmd | awk '{ print $1; }'
+sourceHash="$(cat $(find . -name *.[ch] | grep -v -E '[/\\]?builddata.c$') | "$md5Command" | awk '{ print $1; }')"
+hostName="$(hostname)"
+buildDate="$(date +"%Y-%m-%d %H:%M:%S")"
+
+
+tmpBuildDataTxt="$(mktemp)"
+cat > "$tmpBuildDataTxt" << EOF
+const char olsrd_version[] = "olsr.org - $version-git_$gitSha-hash_$sourceHash";
+const char build_host[]    = "$hostName";
+const char build_date[]    = "$buildDate";
+EOF
+
+
+if [ ! -e "$buildDataTxt" ]; then
+  echo "[CREATE] $buildDataTxt"
+  if [ "$verbose" == "0" ]; then
+    cp -a "$tmpBuildDataTxt" "$buildDataTxt"
+  else
+    cp -a -v "$tmpBuildDataTxt" "$buildDataTxt"
+  fi
+elif [ -n "$(diff -I "^const char build_date\[\].*\$" "$tmpBuildDataTxt" "$buildDataTxt" | sed 's/"/\\"/g')" ]; then
+  echo "[UPDATE] $buildDataTxt"
+  if [ "$verbose" == "0" ]; then
+    cp -a "$tmpBuildDataTxt" "$buildDataTxt"
+  else
+    cp -a -v "$tmpBuildDataTxt" "$buildDataTxt"
+  fi
+fi
+rm -f "$tmpBuildDataTxt"
+