[PATCH] qmake: use a dedicated build script to generate ssrf-version.h

Lubomir I. Ivanov neolit123 at gmail.com
Fri Jan 30 13:18:29 PST 2015


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

- added ./scripts/write-version
- subsurface-gen-version.pri is much simpler now
- .git/HEAD is no longer used explicitly in .pro/.pri files
- the version_h rule is called on each 'make' invocation
but recompilation will occur only if ssrf-version.h
is updated by ./scripts/write-version
- qmake now depends on the existence of ssrf-version.h
so it creates an empty one on Makefile generation so
that a warning is not shown

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---

this is the result of having a fun time with qmake (not).
let me know if it's missing something or if it's wrong in some
way, so that i can take another look at it.

if the changes are cosmetic i'd suggest we apply this one first
and apply on top of it.
---
 scripts/write-version      | 61 ++++++++++++++++++++++++++++++++++++++++++++++
 subsurface-gen-version.pri | 47 +++++++++--------------------------
 subsurface.pro             |  4 +++
 3 files changed, 76 insertions(+), 36 deletions(-)
 create mode 100644 scripts/write-version

diff --git a/scripts/write-version b/scripts/write-version
new file mode 100644
index 0000000..42505ae
--- /dev/null
+++ b/scripts/write-version
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# arguments:
+#	$1 - target H file
+#	$2 - fallback version string
+#	$3 - os name {linux|darwin|win}
+#
+# !!! has no error checking ATM !!!
+# should be started from where .git is!
+#
+
+# set OS and TARGET
+TARGET=$1
+TARGET_TEMP=$TARGET.tmp
+VERSION=$2
+OS=$3
+
+# check if git is installed
+GIT_ERROR=1 
+HAS_GIT=`git --version 2> NUL`
+if [ "$HAS_GIT" ]; then
+	# check if .git exists
+	if [ -d ".git" ]; then
+		FULL_VER=`sh ./scripts/get-version linux`
+		GIT_ERROR=0
+	else
+		echo "WARNING: not a git repository"
+	fi
+else
+	echo "WARNING: git command not found"
+fi
+
+#if there was an error, fallback to .gitversion or use the dummy version
+if [ "$GIT_ERROR" == "1" ]; then
+	if [ -f .gitversion ]; then
+		FULL_VER=`cat .gitversion`
+	else
+		FULL_VER=$VERSION
+	fi
+fi
+
+# grab some strings from get-version
+CANONICAL_VER=`sh ./scripts/get-version full $FULL_VER`
+OS_USABLE_VER=`sh ./scripts/get-version $OS $FULL_VER`
+
+# write to a temp file 
+echo "#define VERSION_STRING \"$OS_USABLE_VER\"" > $TARGET_TEMP
+echo "#define GIT_VERSION_STRING \"$FULL_VER\"" >> $TARGET_TEMP
+echo "#define CANONICAL_VERSION_STRING \"$CANONICAL_VER\"" >> $TARGET_TEMP
+
+# if the target file is missing create it
+if [ ! -f $TARGET ]; then
+	CMD=`touch $TARGET`
+fi
+
+# if the temp file and the target file differ, replace the target file with the temp file
+CMD=`diff -q $TARGET $TARGET_TEMP || cp $TARGET_TEMP $TARGET`
+
+# remove the temp file
+CMD=`rm -f $TARGET_TEMP`
diff --git a/subsurface-gen-version.pri b/subsurface-gen-version.pri
index 7d4cda5..8af4404 100644
--- a/subsurface-gen-version.pri
+++ b/subsurface-gen-version.pri
@@ -1,40 +1,15 @@
 # Generate the ssrf-version.h file
-VERSION_FILE = ssrf-version.h
 macx: VER_OS = darwin
 unix: !macx: VER_OS = linux
 win32: VER_OS = win
-exists(.git/HEAD): {
-	win32: SET_GIT_DIR = set GIT_DIR
-	else: SET_GIT_DIR = GIT_DIR
-	GIT_HEAD = .git/HEAD
-	VERSION_SCRIPT = $$PWD/scripts/get-version
-	# always use linux here -------------------vvv	so we get the true full version
-	FULL_VERSION = "`$$VERSION_SCRIPT linux`"
-	VERSION = $$system("sh scripts/get-version full || echo $${VERSION}")
-	PRODVERSION_STRING = $$system("sh scripts/get-version win $$FULL_VERSION || echo $${VERSION}.0.0-git")
-	VERSION_STRING = $$system("sh scripts/get-version linux $$FULL_VERSION || echo $${VERSION}-git")
-	version_h.depends = $$VERSION_SCRIPT $$PWD/.git/$$system("$$SET_GIT_DIR=$$PWD/.git git rev-parse --symbolic-full-name HEAD")
-	version_h.commands = echo \\$${LITERAL_HASH}define VERSION_STRING \\\"`GIT_DIR=$$PWD/.git $$VERSION_SCRIPT $$VER_OS || echo $$VERSION-git`\\\" > ${QMAKE_FILE_OUT}
-	version_h.commands += $$escape_expand(\\n)$$escape_expand(\\t)
-	version_h.commands += echo \\$${LITERAL_HASH}define GIT_VERSION_STRING \\\"`GIT_DIR=$$PWD/.git $$VERSION_SCRIPT linux || echo $$VERSION-git`\\\" >> ${QMAKE_FILE_OUT}
-	version_h.commands += $$escape_expand(\\n)$$escape_expand(\\t)
-	version_h.commands += echo \\$${LITERAL_HASH}define CANONICAL_VERSION_STRING \\\"`GIT_DIR=$$PWD/.git $$VERSION_SCRIPT full || echo $$VERSION-git`\\\" >> ${QMAKE_FILE_OUT}
-	version_h.input = GIT_HEAD
-	version_h.output = $$VERSION_FILE
-	version_h.variable_out = GENERATED_FILES
-	version_h.CONFIG = ignore_no_exist
-	QMAKE_EXTRA_COMPILERS += version_h
-} else {
-	# This is probably a package
-	exists(.gitversion): {
-		FULL_VERSION = $$system("cat .gitversion")
-	} else {
-		FULL_VERSION = $$VERSION
-	}
-	CANONICAL_VERSION = $$system("sh scripts/get-version full $$FULL_VERSION")
-	OS_USABLE_VERSION = $$system("sh scripts/get-version $$VER_OS $$FULL_VERSION")
-	system(echo \\$${LITERAL_HASH}define VERSION_STRING \\\"$$OS_USABLE_VERSION\\\" > $$VERSION_FILE)
-	system(echo \\$${LITERAL_HASH}define GIT_VERSION_STRING \\\"$$FULL_VERSION\\\" >> $$VERSION_FILE)
-	system(echo \\$${LITERAL_HASH}define CANONICAL_VERSION_STRING \\\"$$CANONICAL_VERSION\\\" >> $$VERSION_FILE)
-	QMAKE_CLEAN += $$VERSION_FILE
-}
+
+# use a compiler target that has a phony input and is forced on each `make` invocation
+# the resulted file is not a link target and is cleared with `make clean`
+PHONY_DEPS = .
+version_h.input = PHONY_DEPS
+version_h.depends = FORCE
+version_h.output = $$VERSION_FILE
+version_h.commands = @echo checking $$VERSION_FILE ... && sh ./scripts/write-version $$VERSION_FILE $$VERSION $$VER_OS
+version_h.CONFIG += no_link
+QMAKE_EXTRA_COMPILERS += version_h
+QMAKE_CLEAN += $$VERSION_FILE
diff --git a/subsurface.pro b/subsurface.pro
index 71fef0f..0821448 100644
--- a/subsurface.pro
+++ b/subsurface.pro
@@ -20,8 +20,12 @@ else: TARGET = subsurface
 QMAKE_CLEAN += $$TARGET
 
 VERSION = 4.3
+VERSION_FILE = ssrf-version.h
+# create a blank VERSION_FILE if missing
+system(cat $$VERSION_FILE > /dev/null 2>&1 || touch $$VERSION_FILE)
 
 HEADERS = \
+	ssrf-version.h \
 	cochran.h \
 	color.h \
 	deco.h \
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list