[PATCH] Replace build date with Git hash and commit date

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Jan 9 08:24:47 UTC 2019


The usage of a build date prevents reproducible builds.
---
 common/conf.py | 25 +------------------------
 common/waf.py  | 22 ++--------------------
 wscript        | 39 ++++++++++++++++++++++++++++++++++++---
 3 files changed, 39 insertions(+), 47 deletions(-)

diff --git a/common/conf.py b/common/conf.py
index 60db066..8d5e6b8 100644
--- a/common/conf.py
+++ b/common/conf.py
@@ -1,26 +1,3 @@
-import datetime
-
-def build_date():
-    now = datetime.date.today()
-    m = now.strftime('%B')
-    y = now.strftime('%Y')
-    if now.day == 11:
-        s = 'th'
-    elif now.day % 10 == 1:
-        s = 'st'
-    elif now.day == 12:
-        s = 'th'
-    elif now.day % 10 == 2:
-        s = 'nd'
-    elif now.day == 13:
-        s = 'th'
-    elif now.day == 3:
-        s = 'rd'
-    else:
-        s = 'th'
-    d = '%2d%s' % (now.day, s)
-    return '%s %s %s' % (d, m, y)
-
 extensions = [
 	"sphinx.ext.autodoc",
 	"sphinx.ext.coverage",
@@ -47,7 +24,7 @@ master_doc = 'index'
 
 # General information about the project.
 project = u'RTEMS Documentation Project'
-copyright = u'2018, RTEMS Project (built %s)' % (build_date())
+copyright = u'2018, RTEMS Project'
 
 # The version info for the project you're documenting, acts as replacement for
 # |version| and |release|, also used in various other places throughout the
diff --git a/common/waf.py b/common/waf.py
index d2cb432..01dd24f 100644
--- a/common/waf.py
+++ b/common/waf.py
@@ -17,24 +17,8 @@ import latex
 
 sphinx_min_version = (1, 3)
 
-def build_date():
-    import datetime
-    now = datetime.date.today()
-    m = now.strftime('%B')
-    y = now.strftime('%Y')
-    if now.day % 10 == 1:
-        s = 'st'
-    elif now.day % 10 == 2:
-        s = 'nd'
-    elif now.day == 3:
-        s = 'rd'
-    else:
-        s = 'th'
-    d = '%2d%s' % (now.day, s)
-    return '%s %s %s' % (d, m, y)
-
 def version_cmdline(ctx):
-    return '-Drelease="%s" -Dversion="%s"' % (ctx.env.VERSION, ctx.env.VERSION)
+    return '-Drelease="%s" -Dversion="%s"' % (ctx.env.RELEASE, ctx.env.VERSION)
 
 def sphinx_cmdline(ctx, build_type, conf_dir, doctrees,
                    source_dir, output_dir, configs = []):
@@ -207,8 +191,6 @@ def check_sphinx_extension(ctx, extension):
 def cmd_configure(ctx):
     check_sphinx = not ctx.env.BIN_SPHINX_BUILD
     if check_sphinx:
-        ctx.msg('Checking version', ctx.env.VERSION)
-
         ctx.find_program("sphinx-build", var="BIN_SPHINX_BUILD", mandatory = True)
         ctx.find_program("aspell", var = "BIN_ASPELL", mandatory = False)
 
@@ -502,7 +484,7 @@ def xml_catalogue(ctx, building):
     cat = xml.Document()
 
     root = cat.createElement('rtems-docs')
-    root.setAttribute('date', build_date())
+    root.setAttribute('date', ctx.env.DATE)
     cat.appendChild(root)
 
     heading = cat.createElement('catalogue')
diff --git a/wscript b/wscript
index 82d5eaa..1488d3a 100644
--- a/wscript
+++ b/wscript
@@ -9,8 +9,6 @@ path.append(abspath('common'))
 import waflib
 import waf as docs_waf
 
-version = '5.0.0 (master)'
-
 build_all = ['user',
              'rsb',
              'c-user',
@@ -31,7 +29,6 @@ def options(opt):
     docs_waf.cmd_options(opt)
 
 def configure(conf):
-    conf.env.VERSION = version
     for b in building:
         conf.recurse(b)
     conf.env['BUILD_FROM_TOP'] = 'yes'
@@ -53,7 +50,43 @@ def coverpage_js(ctx):
     with open(ctx.outputs[0].abspath(), 'w') as o:
         o.write(js.replace('@CATALOGUE', xml))
 
+def pretty_day(day):
+    if day == 3:
+        s = 'rd'
+    elif day == 11:
+        s = 'th'
+    elif day == 12:
+        s = 'th'
+    elif day == 13:
+        s = 'th'
+    elif day % 10 == 1:
+        s = 'st'
+    elif day % 10 == 2:
+        s = 'nd'
+    else:
+        s = 'th'
+    return str(day) + s
+
 def build(ctx):
+    #
+    # Get date and version from Git
+    #
+    version = '5.0.0'
+    if ctx.exec_command(['git', 'diff-index', '--quiet', 'HEAD']) == 0:
+        modified = ''
+    else:
+        modified = '-modified'
+    try:
+        out = ctx.cmd_and_log(['git', 'log', '-1', '--format=%H,%cd', '--date=format:%e,%B,%Y'])
+        f = out.strip('\n').split(',')
+        version = version + '.' + f[0] + modified
+        date = pretty_day(int(f[1])) + ' ' + f[2] + ' ' + f[3]
+    except waflib.Build.Errors.WafError:
+        date = 'unknown date'
+    ctx.env.DATE = date
+    ctx.env.RELEASE = version + ' (' + date + ')'
+    ctx.env.VERSION = version
+
     #
     # Generate any PlantUML images if enabled.
     #
-- 
2.16.4



More information about the devel mailing list