Bug 1264482 - Move compiler invocation for preprocessing to a separate function. r?ted draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 06 Apr 2016 11:32:03 +0900
changeset 350675 3469be0504dbcefa776784b49926c55ae12cd5a9
parent 350674 31d971391da2345d77a794d6a45f5df8ea5fd6dc
child 350676 4c4bebbaa2345f92ceb2f156eda137d162553224
push id15385
push userbmo:mh+mozilla@glandium.org
push dateThu, 14 Apr 2016 04:41:28 +0000
reviewersted
bugs1264482
milestone48.0a1
Bug 1264482 - Move compiler invocation for preprocessing to a separate function. r?ted
build/moz.configure/toolchain.configure
--- a/build/moz.configure/toolchain.configure
+++ b/build/moz.configure/toolchain.configure
@@ -147,18 +147,54 @@ add_old_configure_assignment('TOOLCHAIN_
 
 
 # Compilers
 # ==============================================================
 @imports('os')
 @imports('subprocess')
 @imports(_from='mozbuild.configure.util', _import='LineIO')
 @imports(_from='mozbuild.shellutil', _import='quote')
+@imports(_from='tempfile', _import='mkstemp')
+def try_preprocess(compiler, language, source):
+    suffix = {
+        'C': '.c',
+        'C++': '.cpp',
+    }[language]
+
+    fd, path = mkstemp(prefix='conftest.', suffix=suffix)
+    try:
+        source = source.encode('ascii', 'replace')
+
+        log.debug('Creating `%s` with content:', path)
+        with LineIO(lambda l: log.debug('| %s', l)) as out:
+            out.write(source)
+
+        os.write(fd, source)
+        os.close(fd)
+
+        cmd = compiler + ['-E', path]
+        log.debug('Executing: `%s`', quote(*cmd))
+        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE)
+        stdout, stderr = proc.communicate()
+        retcode = proc.wait()
+        if retcode == 0:
+            return stdout
+
+        log.debug('The command returned non-zero exit status %d.', retcode)
+        for out, desc in ((stdout, 'output'), (stderr, 'error output')):
+            if out:
+                log.debug('Its %s was:', desc)
+                with LineIO(lambda l: log.debug('| %s', l)) as o:
+                    o.write(out)
+    finally:
+        os.remove(path)
+
+
 @imports(_from='mozbuild.util', _import='LimitedString')
-@imports(_from='tempfile', _import='mkstemp')
 @imports(_from='textwrap', _import='dedent')
 def check_compiler(compiler, language):
     class CompilerType(LimitedString):
         POSSIBLE_VALUES = ('msvc', 'clang-cl', 'clang', 'gcc')
 
     check = dedent('''\
         #if defined(_MSC_VER)
         #if defined(__clang__)
@@ -168,54 +204,24 @@ def check_compiler(compiler, language):
         #endif
         #elif defined(__clang__)
         COMPILER clang __clang_major__.__clang_minor__.__clang_patchlevel__
         #elif defined(__GNUC__)
         COMPILER gcc __GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__
         #endif
     ''')
 
-    suffix = {
-        'C': '.c',
-        'C++': '.cpp',
-    }[language]
-
-    fd, path = mkstemp(prefix='conftest.', suffix=suffix)
-    try:
-        source = check.encode('ascii', 'replace')
-
-        log.debug('Creating `%s` with content:', path)
-        with LineIO(lambda l: log.debug('| %s', l)) as out:
-            out.write(source)
-
-        os.write(fd, source)
-        os.close(fd)
+    result = try_preprocess(compiler, language, check)
 
-        cmd = compiler + ['-E', path]
-        log.debug('Executing: `%s`', quote(*cmd))
-        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                                stderr=subprocess.PIPE)
-        stdout, stderr = proc.communicate()
-        retcode = proc.wait()
-        if retcode == 0:
-            for line in stdout.splitlines():
-                if line.startswith('COMPILER '):
-                    _, type, version = line.split(None, 2)
-                    version = version.replace(' ', '')
-                    return CompilerType(type), version
-            return
-
-        log.debug('The command returned non-zero exit status %d.', retcode)
-        for out, desc in ((stdout, 'output'), (stderr, 'error output')):
-            if out:
-                log.debug('Its %s was:', desc)
-                with LineIO(lambda l: log.debug('| %s', l)) as o:
-                    o.write(out)
-    finally:
-        os.remove(path)
+    if result:
+        for line in result.splitlines():
+            if line.startswith('COMPILER '):
+                _, type, version = line.split(None, 2)
+                version = version.replace(' ', '')
+                return CompilerType(type), version
 
 
 @template
 def default_c_compilers(host_or_target):
     '''Template defining the set of default C compilers for the host and
     target platforms.
     `host_or_target` is either `host` or `target` (the @depends functions
     from init.configure.