- relaced updaterevision.c with a python script

Sat, 21 Feb 2015 18:50:11 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 21 Feb 2015 18:50:11 +0200
changeset 926
e1ae2dbcae8e
parent 920
147497785496
child 927
409b82a4765e

- relaced updaterevision.c with a python script
- add 'release' and 'debug' to .hgignore

.hgignore file | annotate | diff | comparison | revisions
CMakeLists.txt file | annotate | diff | comparison | revisions
src/version.cc file | annotate | diff | comparison | revisions
updaterevision.py file | annotate | diff | comparison | revisions
updaterevision/CMakeLists.txt file | annotate | diff | comparison | revisions
updaterevision/updaterevision.c file | annotate | diff | comparison | revisions
--- a/.hgignore	Mon Dec 01 05:10:22 2014 +0200
+++ b/.hgignore	Sat Feb 21 18:50:11 2015 +0200
@@ -3,6 +3,8 @@
 build_shared
 build_debug
 build_release
+debug
+release
 *.kdev4
 Makefile
 Makefile.*
--- a/CMakeLists.txt	Mon Dec 01 05:10:22 2014 +0200
+++ b/CMakeLists.txt	Sat Feb 21 18:50:11 2015 +0200
@@ -6,7 +6,6 @@
 ######################################################################
 
 project (ldforge)
-add_subdirectory (updaterevision)
 add_subdirectory (codegen)
 cmake_minimum_required (VERSION 2.6)
 
@@ -25,14 +24,7 @@
 
 find_package (OpenGL REQUIRED)
 
-get_target_property (UPDATEREVISION_EXE updaterevision LOCATION)
 get_target_property (CODEGEN_EXE codegen LOCATION)
-
-add_custom_target (revision_check ALL
-	COMMAND ${UPDATEREVISION_EXE} ${CMAKE_SOURCE_DIR} hginfo.h
-	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
-	DEPENDS updaterevision)
-
 include_directories (${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
 
 set (LDFORGE_SOURCES
@@ -173,20 +165,20 @@
 	qt5_generate_moc (LDFORGE_MOC ${LDFORGE_HEADERS})
 	qt5_add_resources (LDFORGE_QRC ${LDFORGE_RESOURCES})
 	qt5_wrap_ui (LDFORGE_FORMS_HEADERS ${LDFORGE_FORMS})
-	add_executable (ldforge WIN32 ${LDFORGE_SOURCES} ${LDFORGE_MOC}
+	add_executable (${PROJECT_NAME} WIN32 ${LDFORGE_SOURCES} ${LDFORGE_MOC}
 		${LDFORGE_QRC} ${LDFORGE_FORMS_HEADERS})
 else()
 	qt4_wrap_cpp (LDFORGE_MOC ${LDFORGE_HEADERS})
 	qt4_wrap_ui (LDFORGE_FORMS_HEADERS ${LDFORGE_FORMS})
 	qt4_add_resources (LDFORGE_RCC ${LDFORGE_RESOURCES})
-	add_executable (ldforge WIN32 ${LDFORGE_SOURCES} ${LDFORGE_RCC}
+	add_executable (${PROJECT_NAME} WIN32 ${LDFORGE_SOURCES} ${LDFORGE_RCC}
 		${LDFORGE_FORMS_HEADERS} ${LDFORGE_MOC})
 endif()
 
 if (USE_QT5)
-	target_link_libraries (ldforge Qt5::Widgets Qt5::Network Qt5::OpenGL ${OPENGL_LIBRARIES})
+	target_link_libraries (${PROJECT_NAME} Qt5::Widgets Qt5::Network Qt5::OpenGL ${OPENGL_LIBRARIES})
 else()
-	target_link_libraries (ldforge
+	target_link_libraries (${PROJECT_NAME}
 		${QT_QTCORE_LIBRARY}
 		${QT_QTGUI_LIBRARY}
 		${QT_QTNETWORK_LIBRARY}
@@ -195,5 +187,11 @@
 	)
 endif()
 
-add_dependencies (ldforge revision_check codegeneration)
-install (TARGETS ldforge RUNTIME DESTINATION bin)
+add_dependencies (${PROJECT_NAME} revision_check codegeneration)
+install (TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
+
+add_custom_target (make_hginfo_h
+	COMMAND python
+		"${CMAKE_SOURCE_DIR}/updaterevision.py"
+		"${CMAKE_CURRENT_BINARY_DIR}/hginfo.h")
+add_dependencies (${PROJECT_NAME} make_hginfo_h)
\ No newline at end of file
--- a/src/version.cc	Mon Dec 01 05:10:22 2014 +0200
+++ b/src/version.cc	Sat Feb 21 18:50:11 2015 +0200
@@ -52,10 +52,10 @@
 {
 	if (g_fullVersionString[0] == '\0')
 	{
-#if BUILD_ID != BUILD_RELEASE and defined (SVN_REVISION_STRING)
-		sprintf (g_fullVersionString, "%s-" SVN_REVISION_STRING, VersionString());
+#if BUILD_ID != BUILD_RELEASE and defined (HG_NODE)
+		sprintf (g_fullVersionString, "%s-" HG_NODE, VersionString());
 #else
-		sprintf (g_fullVersionString, "%s", versionString());
+		sprintf (g_fullVersionString, "%s", VersionString());
 #endif
 	}
 
@@ -66,10 +66,10 @@
 //
 const char* CommitTimeString()
 {
-#ifdef SVN_REVISION_NUMBER
+#ifdef HG_DATE_TIME
 	if (g_buildTime[0] == '\0')
 	{
-		time_t timestamp = SVN_REVISION_NUMBER;
+		time_t timestamp = HG_DATE_TIME;
 		strftime (g_buildTime, sizeof g_buildTime, "%d %b %Y", localtime (&timestamp));
 	}
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/updaterevision.py	Sat Feb 21 18:50:11 2015 +0200
@@ -0,0 +1,73 @@
+#
+#	Copyright 2014 Teemu Piippo
+#	All rights reserved.
+#
+#	Redistribution and use in source and binary forms, with or without
+#	modification, are permitted provided that the following conditions
+#	are met:
+#
+#	1. Redistributions of source code must retain the above copyright
+#	   notice, this list of conditions and the following disclaimer.
+#	2. Redistributions in binary form must reproduce the above copyright
+#	   notice, this list of conditions and the following disclaimer in the
+#	   documentation and/or other materials provided with the distribution.
+#	3. Neither the name of the copyright holder nor the names of its
+#	   contributors may be used to endorse or promote products derived from
+#	   this software without specific prior written permission.
+#
+#	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+#	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+#	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+#	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
+#	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+#	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+#	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+#	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+#	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+#	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+#	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+import sys
+import subprocess
+from datetime import datetime
+
+if len (sys.argv) != 2:
+    print 'usage: %s <output>' % sys.argv[0]
+    quit (1)
+
+oldrev = ''
+
+try:
+    with open (sys.argv[1]) as fp:
+        oldrev = fp.readline().replace ('\n', '').replace ('// ', '')
+except IOError:
+    pass
+
+data = subprocess.check_output (['hg', 'log', '-r.', '--template',
+    '{node|short} {branch} {date|hgdate}']).replace ('\n', '').split (' ')
+
+rev = data[0]
+branch = data[1]
+timestamp = int (data[2])
+date = datetime.utcfromtimestamp (timestamp)
+datestring = date.strftime ('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000'
+
+if len(rev) > 7:
+    rev = rev[0:7]
+
+if subprocess.check_output (['hg', 'id', '-n']).replace ('\n', '')[-1] == '+':
+    rev += '+'
+
+if rev == oldrev:
+    print "%s is up to date at %s" % (sys.argv[1], rev)
+    quit (0)
+
+with open (sys.argv[1], 'w') as fp:
+    fp.write ('// %s\n' % rev)
+    fp.write ('#define HG_NODE "%s"\n' % rev)
+    fp.write ('#define HG_BRANCH "%s"\n' % branch)
+    fp.write ('#define HG_DATE_VERSION "%s"\n' % datestring)
+    fp.write ('#define HG_DATE_STRING "%s"\n' % date.strftime ('%d %b %Y'))
+    fp.write ('#define HG_DATE_TIME %d\n' % int (timestamp))
+    print '%s updated to %s' % (sys.argv[1], rev)
--- a/updaterevision/CMakeLists.txt	Mon Dec 01 05:10:22 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-cmake_minimum_required (VERSION 2.4)
-add_executable (updaterevision updaterevision.c)
\ No newline at end of file
--- a/updaterevision/updaterevision.c	Mon Dec 01 05:10:22 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,192 +0,0 @@
-/* updaterevision.c
- *
- * Public domain. This program uses the svnversion command to get the
- * repository revision for a particular directory and writes it into
- * a header file so that it can be used as a project's build number.
- */
-
-#define _CRT_SECURE_NO_DEPRECATE
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <errno.h>
-// [BB] New #includes.
-#include <sys/stat.h>
-#include <time.h>
-
-int main(int argc, char **argv)
-{
-    const char *name = "svnout";
-	char currev[64], lastrev[64], run[256], *rev;
-	unsigned long urev;
-	FILE *stream = NULL;
-	int gotrev = 0, needupdate = 1;
-	// [BB] Are we working with a SVN checkout?
-	int svnCheckout = 0;
-	char hgdateString[64];
-	time_t hgdate = 0;
-	char hgHash[13];
-	hgHash[0] = '\0';
-
-	if (argc != 3)
-	{
-		fprintf (stderr, "Usage: %s <repository directory> <path to svnrevision.h>\n", argv[0]);
-		return 1;
-	}
-
-	// [BB] Try to figure out whether this is a SVN or a Hg checkout.
-	{
-		struct stat st;
-		char filename[1024];
-		sprintf ( filename, "%s/.svn/entries", argv[1] );
-		if ( stat ( filename, &st ) == 0 )
-			svnCheckout = 1;
-		// [BB] If stat failed we have to manually clear errno, otherwise the code below doesn't work.
-		else
-			errno = 0;
-	}
-
-	// Use svnversion to get the revision number. If that fails, pretend it's
-	// revision 0. Note that this requires you have the command-line svn tools installed.
-	// [BB] Depending on whether this is a SVN or Hg checkout we have to use the appropriate tool.
-	if ( svnCheckout )
-		sprintf (run, "svnversion -cn %s", argv[1]);
-	else
-		sprintf (run, "hg identify -n"); 
-    // if ((name = tempnam(NULL, "svnout")) != NULL)
-	{
-#ifdef __APPLE__
-		// tempnam will return errno of 2 even though it is successful for our purposes.
-		errno = 0;
-#endif
-		if((stream = freopen(name, "w+b", stdout)) != NULL &&
-		   system(run) == 0 &&
-#ifndef __FreeBSD__
-		   errno == 0 &&
-#endif
-		   fseek(stream, 0, SEEK_SET) == 0 &&
-		   fgets(currev, sizeof currev, stream) == currev &&
-		   (isdigit(currev[0]) || (currev[0] == '-' && currev[1] == '1')))
-		{
-			gotrev = 1;
-			// [BB] Find the date the revision of the working copy was created.
-			if ( ( svnCheckout == 0 ) &&
-				( system("hg log -r. --template \"{date|hgdate} {node|short}\"") == 0 ) &&
-				( fseek(stream, strlen(currev), SEEK_SET) == 0 ) &&
-				( fgets(hgdateString, sizeof ( hgdateString ), stream) == hgdateString ) )
-			{
-				// [BB] Find the hash in the output and store it.
-				char *p = strrchr ( hgdateString, ' ' );
-				strncpy ( hgHash, p ? ( p+1 ) : "hashnotfound" , sizeof( hgHash ) - 1 );
-				hgHash[ sizeof ( hgHash ) - 1 ] = '\0';
-				// [BB] Extract the date from the output and store it.
-				hgdate = atoi ( hgdateString );
-			}
-		}
-	}
-	if (stream != NULL)
-	{
-		fclose (stream);
-		remove (name);
-	}
-    /*if (name != NULL)
-	{
-		free (name);
-    }*/
-
-	if (!gotrev)
-	{
-		fprintf (stderr, "Failed to get current revision: %s\n", strerror(errno));
-		strcpy (currev, "0");
-		rev = currev;
-	}
-	else
-	{
-		rev = strchr (currev, ':');
-		if (rev == NULL)
-		{
-			rev = currev;
-		}
-		else
-		{
-			rev += 1;
-		}
-	}
-
-	// [BB] Create date version string.
-	if ( gotrev && ( svnCheckout == 0 ) )
-	{
-		char *endptr;
-		unsigned long parsedRev = strtoul(rev, &endptr, 10);
-		unsigned int localChanges = ( *endptr == '+' );
-		struct tm	*lt = gmtime( &hgdate );
-		if ( localChanges )
-			sprintf ( rev, "%d%02d%02d-%02d%02dM", lt->tm_year - 100, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min);
-		else
-			sprintf ( rev, "%d%02d%02d-%02d%02d", lt->tm_year - 100, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min);
-	}
-
-	stream = fopen (argv[2], "r");
-	if (stream != NULL)
-	{
-		if (!gotrev)
-		{ // If we didn't get a revision but the file does exist, leave it alone.
-			fprintf( stderr, "No revision found.\n" );
-			fclose (stream);
-			return 0;
-		}
-		// Read the revision that's in this file already. If it's the same as
-		// what we've got, then we don't need to modify it and can avoid rebuilding
-		// dependant files.
-		if (fgets(lastrev, sizeof lastrev, stream) == lastrev)
-		{
-			if (lastrev[0] != '\0')
-			{ // Strip trailing \n
-				lastrev[strlen(lastrev) - 1] = '\0';
-			}
-			if (strcmp(rev, lastrev + 3) == 0)
-			{
-				needupdate = 0;
-			}
-		}
-		fclose (stream);
-	}
-
-	if (needupdate)
-	{
-		stream = fopen (argv[2], "w");
-		if (stream == NULL)
-		{
-			return 1;
-		}
-		// [BB] Use hgdate as revision number.
-		if ( hgdate )
-			urev = hgdate;
-		else
-			urev = strtoul(rev, NULL, 10);
-		fprintf (stream,
-"// %s\n"
-"//\n"
-"// This file was automatically generated by the\n"
-"// updaterevision tool. Do not edit by hand.\n"
-"\n"
-"#define SVN_REVISION_STRING \"%s\"\n"
-"#define SVN_REVISION_NUMBER %lu\n",
-			rev, rev, urev);
-
-		// [BB] Also save the hg hash.
-		if ( svnCheckout == 0 )
-			fprintf (stream, "#define HG_REVISION_HASH_STRING \"%s\"\n", hgHash);
-
-		fclose (stream);
-		fprintf (stderr, "%s updated to revision %s.\n", argv[2], rev);
-	}
-	else
-	{
-		fprintf (stderr, "%s is up to date at revision %s.\n", argv[2], rev);
-	}
-
-	return 0;
-}

mercurial