Generate version information as part of the build.
authorIain Patterson <me@iain.cx>
Thu, 26 Dec 2013 19:30:47 +0000 (19:30 +0000)
committerIain Patterson <me@iain.cx>
Fri, 27 Dec 2013 11:12:45 +0000 (11:12 +0000)
Use git (if it's available) to create a major and minor version number
based on the most recent tag name.  If we are not building from a
tagged commit, use the commit count since the last tag as the subminor
version.  If Jenkins is being used for the build, use the build number
as the subsubminor version.

nssm.h
nssm.vcproj
version.cmd [new file with mode: 0644]

diff --git a/nssm.h b/nssm.h
index 46308d5..636524d 100644 (file)
--- a/nssm.h
+++ b/nssm.h
@@ -21,9 +21,7 @@ void strip_basename(TCHAR *);
 int usage(int);\r
 \r
 #define NSSM _T("nssm")\r
-#define NSSM_VERSION _T("2.21")\r
-#define NSSM_VERSIONINFO 2,21,0,0\r
-#define NSSM_DATE _T("2013-11-24")\r
+#include "version.h"\r
 \r
 /*\r
   Throttle the restart of the service if it stops before this many\r
index 803bb43..3b52894 100755 (executable)
@@ -30,6 +30,8 @@
                        >\r
                        <Tool\r
                                Name="VCPreBuildEventTool"\r
+                               Description="Setting version information"\r
+                               CommandLine="version.cmd"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
                        >\r
                        <Tool\r
                                Name="VCPreBuildEventTool"\r
+                               Description="Setting version information"\r
+                               CommandLine="version.cmd"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
                        >\r
                        <Tool\r
                                Name="VCPreBuildEventTool"\r
+                               Description="Setting version information"\r
+                               CommandLine="version.cmd"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
                        >\r
                        <Tool\r
                                Name="VCPreBuildEventTool"\r
+                               Description="Setting version information"\r
+                               CommandLine="version.cmd"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
diff --git a/version.cmd b/version.cmd
new file mode 100644 (file)
index 0000000..536c3e6
--- /dev/null
@@ -0,0 +1,33 @@
+@rem Set default version in case git isn't available.\r
+set description=0.0-0-prerelease\r
+@rem Get canonical version from git tags, eg v2.21-24-g2c60e53.\r
+for /f %%v in ('git describe --tags --long') do set description=%%v\r
+\r
+@rem Strip leading v if present, eg 2.21-24-g2c60e53.\r
+set description=%description:v=%\r
+set version=%description%\r
+\r
+@rem Get the number of commits and commit hash, eg 24-g2c60e53.\r
+set n=%version:*-=%\r
+set commit=%n:*-=%\r
+call set n=%%n:%commit%=%%\r
+set n=%n:~0,-1%\r
+\r
+@rem Strip n and commit, eg 2.21.\r
+call set version=%%version:%n%-%commit%=%%\r
+set version=%version:~0,-1%\r
+\r
+@rem Find major and minor.\r
+set minor=%version:*.=%\r
+call set major=%%version:.%minor%=%%\r
+\r
+@rem Don't include n and commit if we match a tag exactly.\r
+if "%n%" == "0" set description=%major%.%minor%\r
+\r
+@rem Ignore the build number if this isn't Jenkins.\r
+if "%BUILD_NUMBER%" == "" set BUILD_NUMBER=0\r
+\r
+@rem Create version.h.\r
+@echo>version.h #define NSSM_VERSION _T("%description%")\r
+@echo>>version.h #define NSSM_VERSIONINFO %major%,%minor%,%n%,%BUILD_NUMBER%\r
+@echo>>version.h #define NSSM_DATE _T("%DATE%")\r