Bug 1377216 - Discover version control info in configure; r?glandium draft
authorGregory Szorc <gps@mozilla.com>
Tue, 18 Jul 2017 18:07:29 -0700
changeset 610965 e50d6f4d3a8123bc62fbcdcb9252d4a1d1d36d0e
parent 610964 212f03efaa5d4a10f288ba69711d99f934225949
child 610966 f8f2b26d11f0c6f5058620adc8b3f2633a8448fd
push id69070
push userbmo:gps@mozilla.com
push dateWed, 19 Jul 2017 01:11:03 +0000
reviewersglandium
bugs1377216
milestone56.0a1
Bug 1377216 - Discover version control info in configure; r?glandium For reasons unknown to me, Windows CI is periodically failing to find the Mercurial binary. In addition, we've also reimplemented various VCS logic throughout the build system. There is room to cut down on code complexity by e.g. recording VCS info in configure instead of determining it at run-time. Also, for forensic purposes it is sometimes desirable to know which VCS tool is in use by a build and which version of that tool is being used. This commit adds VCS type detection, binary searching, and version resolution to configure. substs now contains VCS_CHECKOUT_TYPE, HG, and GIT, which can be consulted by downstream consumers. If the Mercurial or Git versions could not be resolved, all variables are not set. Otherwise, VCS_CHECKOUT_TYPE and one of HG or GIT is set. If MOZ_AUTOMATION is set, we require that the VCS info be resolved. This helps prevents weirdness in automation due to unexpected environment configuration. MozReview-Commit-ID: AMLy0Hfx5rD
build/moz.configure/init.configure
--- a/build/moz.configure/init.configure
+++ b/build/moz.configure/init.configure
@@ -304,16 +304,96 @@ def shell(value, mozillabuild):
     shell = 'sh'
     if mozillabuild:
         shell = mozillabuild[0] + '/msys/bin/sh'
     if sys.platform == 'win32':
         shell = shell + '.exe'
     return find_program(shell)
 
 
+# Source checkout and version control integration.
+# ================================================
+
+@depends(check_build_environment, 'MOZ_AUTOMATION')
+@checking('for vcs source checkout')
+@imports('os')
+def vcs_checkout_type(build_env, automation):
+    if os.path.exists(os.path.join(build_env.topsrcdir, '.hg')):
+        return 'hg'
+    elif os.path.exists(os.path.join(build_env.topsrcdir, '.git')):
+        return 'git'
+    elif automation:
+        raise FatalCheckError('unable to resolve VCS type; must run '
+                              'from a source checkout when MOZ_AUTOMATION '
+                              'is set')
+
+# Resolve VCS binary for detected repository type.
+hg = check_prog('HG', ('hg',), allow_missing=True,
+                when=depends(vcs_checkout_type)(lambda x: x == 'hg'))
+git = check_prog('GIT', ('git',), allow_missing=True,
+                 when=depends(vcs_checkout_type)(lambda x: x == 'git'))
+
+@depends_if(hg)
+@checking('for Mercurial version')
+@imports('os')
+@imports('re')
+def hg_version(hg):
+    # HGPLAIN in Mercurial 1.5+ forces stable output, regardless of set
+    # locale or encoding.
+    env = dict(os.environ)
+    env['HGPLAIN'] = '1'
+
+    out = check_cmd_output(hg, '--version', env=env)
+
+    match = re.search(r'Mercurial Distributed SCM \(version ([^\)]+)', out)
+
+    if not match:
+        raise FatalCheckError('unable to determine Mercurial version: %s' % out)
+
+    # The version string may be "unknown" for Mercurial run out of its own
+    # source checkout or for bad builds. But LooseVersion handles it.
+
+    return Version(match.group(1))
+
+@depends_if(git)
+@checking('for Git version')
+@imports('re')
+def git_version(git):
+    out = check_cmd_output(git, '--version').rstrip()
+
+    match = re.search('git version (.*)$', out)
+
+    if not match:
+        raise FatalCheckError('unable to determine Git version: %s' % out)
+
+    return Version(match.group(1))
+
+# Only set VCS_CHECKOUT_TYPE if we resolved the VCS binary.
+# Require resolved VCS info when running in automation so automation's
+# environment is more well-defined.
+@depends(vcs_checkout_type, hg_version, git_version, 'MOZ_AUTOMATION')
+def exposed_vcs_checkout_type(vcs_checkout_type, hg, git, automation):
+    if vcs_checkout_type == 'hg':
+        if hg:
+            return 'hg'
+
+        if automation:
+            raise FatalCheckError('could not resolve Mercurial binary info')
+
+    elif vcs_checkout_type == 'git':
+        if git:
+            return 'git'
+
+        if automation:
+            raise FatalCheckError('could not resolve Git binary info')
+    elif vcs_checkout_type:
+        raise FatalCheckError('unhandled VCS type: %s' % vcs_checkout_type)
+
+set_config('VCS_CHECKOUT_TYPE', exposed_vcs_checkout_type)
+
 # Host and target systems
 # ==============================================================
 option('--host', nargs=1, help='Define the system type performing the build')
 
 option('--target', nargs=1,
        help='Define the system type where the resulting executables will be '
             'used')