build: Changing the way version is managed

Now control over the version number is moved to wscript.  In addition to
that, a new NDN_CXX_VERSION_BUILD_STRING macro is set to include more
detailed information, including commit ID (e.g., "0.1.0-rc1-1-g5c86570").

Change-Id: I89ddd4e2f1248bca4f828865ec6d3c3921fa2822
diff --git a/src/version.hpp b/src/version.hpp
deleted file mode 100644
index 2fa652c..0000000
--- a/src/version.hpp
+++ /dev/null
@@ -1,45 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (c) 2013-2014,  Regents of the University of California.
- * All rights reserved.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * This file licensed under New BSD License.  See COPYING for detailed information about
- * ndn-cxx library copyright, permissions, and redistribution restrictions.
- *
- * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
- */
-
-#ifndef NDN_VERSION_HPP
-#define NDN_VERSION_HPP
-
-namespace nfd {
-
-/** ndn-cxx version follows Semantic Versioning 2.0.0 specification
- *  http://semver.org/
- */
-
-/** \brief ndn-cxx version represented as an integer
- *
- *  MAJOR*1000000 + MINOR*1000 + PATCH
- */
-#define NDN_CXX_VERSION 1000
-
-/** \brief ndn-cxx version represented as a string
- *
- *  MAJOR.MINOR.PATCH
- */
-#define NDN_CXX_VERSION_STRING "0.1.0"
-
-/// MAJOR version
-#define NDN_CXX_VERSION_MAJOR (NDN_CXX_VERSION / 1000000)
-/// MINOR version
-#define NDN_CXX_VERSION_MINOR (NDN_CXX_VERSION % 1000000 / 1000)
-/// PATCH version
-#define NDN_CXX_VERSION_PATCH (NDN_CXX_VERSION % 1000)
-
-} // namespace nfd
-
-#endif // NDN_VERSION_HPP
diff --git a/src/version.hpp.in b/src/version.hpp.in
new file mode 100644
index 0000000..dee1660
--- /dev/null
+++ b/src/version.hpp.in
@@ -0,0 +1,61 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (c) 2013-2014,  Regents of the University of California.
+ * All rights reserved.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * This file licensed under New BSD License.  See COPYING for detailed information about
+ * ndn-cxx library copyright, permissions, and redistribution restrictions.
+ *
+ * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
+ */
+
+#ifndef NDN_VERSION_HPP
+#define NDN_VERSION_HPP
+
+namespace ndn {
+
+/** ndn-cxx version follows Semantic Versioning 2.0.0 specification
+ *  http://semver.org/
+ */
+
+/** \brief ndn-cxx version represented as an integer
+ *
+ *  MAJOR*1000000 + MINOR*1000 + PATCH
+ */
+#define NDN_CXX_VERSION @VERSION@
+
+/** \brief ndn-cxx version represented as a string
+ *
+ *  MAJOR.MINOR.PATCH
+ */
+#define NDN_CXX_VERSION_STRING "@VERSION_STRING@"
+
+/** \brief ndn-cxx version string, including git commit information, if ndn-cxx is build from
+ *         specific git commit
+ *
+ * NDN_CXX_VERSION_BUILD_STRING is obtained using the following command (`ndn-cxx-` prefix is
+ * afterwards removed):
+ *
+ *    `git describe --match 'ndn-cxx-*'`
+ *
+ * When ndn-cxx is built not from git, NDN_CXX_VERSION_BUILD_STRING equals NDN_CXX_VERSION_STRING
+ *
+ * MAJOR.MINOR.PATCH(-release-candidate-tag)(-(number-of-commits-since-tag)-COMMIT-HASH)
+ *
+ * Example, 0.1.0-rc1-1-g5c86570
+ */
+#define NDN_CXX_VERSION_BUILD_STRING "@VERSION_BUILD@"
+
+/// MAJOR version
+#define NDN_CXX_VERSION_MAJOR @VERSION_MAJOR@
+/// MINOR version
+#define NDN_CXX_VERSION_MINOR @VERSION_MINOR@
+/// PATCH version
+#define NDN_CXX_VERSION_PATCH @VERSION_PATCH@
+
+} // namespace ndn
+
+#endif // NDN_VERSION_HPP
diff --git a/tools/ndnsec.cpp b/tools/ndnsec.cpp
index 1c62981..1e72ac0 100644
--- a/tools/ndnsec.cpp
+++ b/tools/ndnsec.cpp
@@ -12,6 +12,7 @@
  * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
  */
 
+#include "version.hpp"
 #include "ndnsec-util.hpp"
 #include "ndnsec-list.hpp"
 #include "ndnsec-get-default.hpp"
@@ -34,6 +35,7 @@
 
 std::string ndnsec_helper("\
   help         Show all commands.\n\
+  version      Show version and exit.\n\
   list         Display information in PublicInfo.\n\
   get-default  Get default setting info.\n\
   set-default  Configure default setting.\n\
@@ -64,7 +66,9 @@
 
   try
     {
-      if (command == "help")              { std::cerr << ndnsec_helper << std::endl; }
+      if (command == "help")              { std::cout << ndnsec_helper << std::endl; }
+      else if (command == "version")      { std::cout << NDN_CXX_VERSION_BUILD_STRING
+                                                      << std::endl; }
       else if (command == "list")         { return ndnsec_list(argc - 1, argv + 1); }
       else if (command == "get-default")  { return ndnsec_get_default(argc - 1, argv + 1); }
       else if (command == "set-default")  { return ndnsec_set_default(argc - 1, argv + 1); }
diff --git a/wscript b/wscript
index 175ea12..2e46ff5 100644
--- a/wscript
+++ b/wscript
@@ -1,13 +1,12 @@
 # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
 
-from waflib import Logs, Utils
-import re
+from waflib import Logs, Utils, Context
+import os
 
-VERSION = re.search('^#define NDN_CXX_VERSION_STRING\\s+"(.*)"',
-                    open("src/version.hpp").read(), re.M).group(1)
+VERSION = "0.1.0"
 APPNAME = "ndn-cxx"
 PACKAGE_BUGREPORT = "http://redmine.named-data.net/projects/ndn-cxx"
-PACKAGE_URL = "https://github.com/named-data/ndn-cxx"
+PACKAGE_URL = "http://named-data.net/doc/ndn-cxx/"
 
 def options(opt):
     opt.load(['compiler_cxx', 'gnu_dirs', 'c_osx'])
@@ -102,14 +101,31 @@
     conf.write_config_header('src/ndn-cxx-config.hpp', define_prefix='NDN_CXX_')
 
 def build(bld):
+    version(bld)
+
+    bld(features="subst",
+        name='version',
+        source='src/version.hpp.in',
+        target='src/version.hpp',
+        install_path=None,
+        VERSION_STRING=VERSION_BASE,
+        VERSION_BUILD=VERSION,
+        VERSION=int(VERSION_SPLIT[0]) * 1000000 +
+                int(VERSION_SPLIT[1]) * 1000 +
+                int(VERSION_SPLIT[2]),
+        VERSION_MAJOR=VERSION_SPLIT[0],
+        VERSION_MINOR=VERSION_SPLIT[1],
+        VERSION_PATCH=VERSION_SPLIT[2],
+        )
+
     libndn_cxx = bld(
         features=['cxx', 'cxxstlib'], # 'cxxshlib',
-        # vnum="0.3.0",
+        # vnum=VERSION,
         target="ndn-cxx",
         name="ndn-cxx",
         source=bld.path.ant_glob('src/**/*.cpp',
                                    excl=['src/**/*-osx.cpp', 'src/**/*-sqlite3.cpp']),
-        use='BOOST OPENSSL LOG4CXX CRYPTOPP SQLITE3 RT PIC PTHREAD',
+        use='version BOOST OPENSSL LOG4CXX CRYPTOPP SQLITE3 RT PIC PTHREAD',
         includes=". src",
         export_includes="src",
         install_path='${LIBDIR}',
@@ -156,7 +172,7 @@
          source="libndn-cxx.pc.in",
          target="libndn-cxx.pc",
          install_path="${LIBDIR}/pkgconfig",
-         VERSION=VERSION,
+         VERSION=VERSION_BASE,
 
          # This probably not the right thing to do, but to simplify life of apps
          # that use the library
@@ -185,6 +201,9 @@
     bld.install_files("%s/ndn-cxx" % bld.env['INCLUDEDIR'],
                       bld.path.find_resource('src/ndn-cxx-config.hpp'))
 
+    bld.install_files("%s/ndn-cxx" % bld.env['INCLUDEDIR'],
+                      bld.path.find_resource('src/version.hpp'))
+
     bld.install_files("${SYSCONFDIR}/ndn", "client.conf.sample")
 
     if bld.env['SPHINX_BUILD']:
@@ -201,6 +220,8 @@
     Options.commands = ['doxygen', 'sphinx'] + Options.commands
 
 def doxygen(bld):
+    version(bld)
+
     if not bld.env.DOXYGEN:
         Logs.error("ERROR: cannot build documentation (`doxygen' is not found in $PATH)")
     else:
@@ -222,6 +243,8 @@
             use="doxygen-conf")
 
 def sphinx(bld):
+    version(bld)
+
     if not bld.env.SPHINX_BUILD:
         bld.fatal("ERROR: cannot build documentation (`sphinx-build' is not found in $PATH)")
     else:
@@ -230,3 +253,27 @@
             source=bld.path.ant_glob("docs/**/*.rst"),
             config="docs/conf.py",
             VERSION=VERSION)
+
+
+def version(ctx):
+    if getattr(Context.g_module, 'VERSION_BASE', None):
+        return
+
+    Context.g_module.VERSION_BASE = Context.g_module.VERSION
+    Context.g_module.VERSION_SPLIT = [v for v in VERSION_BASE.split('.')]
+
+    try:
+        cmd = ['git', 'describe', '--match', 'ndn-cxx-*']
+        p = Utils.subprocess.Popen(cmd, stdout=Utils.subprocess.PIPE,
+                                   stderr=None, stdin=None)
+        out = p.communicate()[0].strip()
+        if p.returncode == 0 and out != "":
+            Context.g_module.VERSION = out[8:]
+    except:
+        pass
+
+def dist(ctx):
+    version(ctx)
+
+def distcheck(ctx):
+    version(ctx)