Bug 1269517 - Factor Python configure's try_preprocess into an invocation of a lower-level function. r=glandium
MozReview-Commit-ID: LQ1dqs9ChyZ
--- a/build/moz.configure/toolchain.configure
+++ b/build/moz.configure/toolchain.configure
@@ -163,41 +163,18 @@ def toolchain_prefix(value, target, host
return '%s-' % target.toolchain
set_config('TOOLCHAIN_PREFIX', toolchain_prefix)
add_old_configure_assignment('TOOLCHAIN_PREFIX', toolchain_prefix)
# Compilers
# ==============================================================
-@imports('os')
-@imports('subprocess')
-@imports(_from='mozbuild.configure.util', _import='LineIO')
-@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]
- return check_cmd_output(*cmd)
- finally:
- os.remove(path)
-
+ return try_invoke_compiler(compiler, language, source, ['-E'])
@imports(_from='mozbuild.configure.constants', _import='CompilerType')
@imports(_from='textwrap', _import='dedent')
def get_compiler_info(compiler, language):
'''Returns information about the given `compiler` (command line in the
form of a list or tuple), in the given `language`.
The returned information includes:
--- a/build/moz.configure/util.configure
+++ b/build/moz.configure/util.configure
@@ -79,16 +79,47 @@ def find_program(file, paths=None):
die("Paths provided to find_program must be a list of strings, "
"not %r", paths)
paths = list(itertools.chain(
*(p.split(pathsep) for p in paths if p)))
return normsep(which(file, path=paths))
except WhichError:
return None
+@imports('os')
+@imports('subprocess')
+@imports(_from='mozbuild.configure.util', _import='LineIO')
+@imports(_from='tempfile', _import='mkstemp')
+def try_invoke_compiler(compiler, language, source, flags=None, onerror=None):
+ flags = flags or []
+
+ if not isinstance(flags, (list, tuple)):
+ die("Flags provided to try_compile must be a list of strings, "
+ "not %r", paths)
+
+ 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 + list(flags) + [path]
+ kwargs = {'onerror': onerror}
+ return check_cmd_output(*cmd, **kwargs)
+ finally:
+ os.remove(path)
def unique_list(l):
result = []
for i in l:
if l not in result:
result.append(i)
return result