Bug 1143932 - Factor out sources processing code from emit_from_context; r=gps
authorMs2ger <ms2ger@gmail.com>
Tue, 17 Mar 2015 10:32:10 +0100
changeset 251097 371a2c961926e86bb29d8bbfe10a4c5dca48eca0
parent 251096 cf13977aa4fa8896b7b1b0eeb31c610682906824
child 251098 6670f5380a4067c6d58e8ccee328756d699c87e7
push id1096
push userbcampen@mozilla.com
push dateTue, 17 Mar 2015 18:28:18 +0000
reviewersgps
bugs1143932
milestone39.0a1
Bug 1143932 - Factor out sources processing code from emit_from_context; r=gps
python/mozbuild/mozbuild/frontend/emitter.py
--- a/python/mozbuild/mozbuild/frontend/emitter.py
+++ b/python/mozbuild/mozbuild/frontend/emitter.py
@@ -540,22 +540,16 @@ class TreeMetadataEmitter(LoggingMixin):
             self.log(logging.WARN, 'mozbuild_warning', dict(
                 path=context.main_path),
                 '{path}: NO_DIST_INSTALL has no effect on XPIDL_SOURCES.')
 
         for idl in context['XPIDL_SOURCES']:
             yield XPIDLFile(context, mozpath.join(context.srcdir, idl),
                 xpidl_module)
 
-        for symbol in ('SOURCES', 'HOST_SOURCES', 'UNIFIED_SOURCES'):
-            for src in (context[symbol] or []):
-                if not os.path.exists(mozpath.join(context.srcdir, src)):
-                    raise SandboxValidationError('File listed in %s does not '
-                        'exist: \'%s\'' % (symbol, src), context)
-
         # Proxy some variables as-is until we have richer classes to represent
         # them. We should aim to keep this set small because it violates the
         # desired abstraction of the build definition away from makefiles.
         passthru = VariablePassthru(context)
         varlist = [
             'ANDROID_GENERATED_RESFILES',
             'ANDROID_RES_DIRS',
             'DISABLE_STL_WRAPPING',
@@ -590,88 +584,18 @@ class TreeMetadataEmitter(LoggingMixin):
         for v in ['CFLAGS', 'CXXFLAGS', 'CMFLAGS', 'CMMFLAGS', 'LDFLAGS']:
             if v in context and context[v]:
                 passthru.variables['MOZBUILD_' + v] = context[v]
 
         # NO_VISIBILITY_FLAGS is slightly different
         if context['NO_VISIBILITY_FLAGS']:
             passthru.variables['VISIBILITY_FLAGS'] = ''
 
-        no_pgo = context.get('NO_PGO')
-        sources = context.get('SOURCES', [])
-        no_pgo_sources = [f for f in sources if sources[f].no_pgo]
-        if no_pgo:
-            if no_pgo_sources:
-                raise SandboxValidationError('NO_PGO and SOURCES[...].no_pgo '
-                    'cannot be set at the same time', context)
-            passthru.variables['NO_PROFILE_GUIDED_OPTIMIZE'] = no_pgo
-        if no_pgo_sources:
-            passthru.variables['NO_PROFILE_GUIDED_OPTIMIZE'] = no_pgo_sources
-
-        # A map from "canonical suffixes" for a particular source file
-        # language to the range of suffixes associated with that language.
-        #
-        # We deliberately don't list the canonical suffix in the suffix list
-        # in the definition; we'll add it in programmatically after defining
-        # things.
-        suffix_map = {
-            '.s': set(['.asm']),
-            '.c': set(),
-            '.m': set(),
-            '.mm': set(),
-            '.cpp': set(['.cc', '.cxx']),
-            '.S': set(),
-        }
-
-        # The inverse of the above, mapping suffixes to their canonical suffix.
-        canonicalized_suffix_map = {}
-        for suffix, alternatives in suffix_map.iteritems():
-            alternatives.add(suffix)
-            for a in alternatives:
-                canonicalized_suffix_map[a] = suffix
-
-        def canonical_suffix_for_file(f):
-            return canonicalized_suffix_map[mozpath.splitext(f)[1]]
-
-        # A map from moz.build variables to the canonical suffixes of file
-        # kinds that can be listed therein.
-        all_suffixes = list(suffix_map.keys())
-        varmap = dict(
-            SOURCES=(Sources, all_suffixes),
-            HOST_SOURCES=(HostSources, ['.c', '.mm', '.cpp']),
-            UNIFIED_SOURCES=(UnifiedSources, ['.c', '.mm', '.cpp']),
-            GENERATED_SOURCES=(GeneratedSources, all_suffixes),
-        )
-
-        for variable, (klass, suffixes) in varmap.items():
-            allowed_suffixes = set().union(*[suffix_map[s] for s in suffixes])
-
-            # First ensure that we haven't been given filetypes that we don't
-            # recognize.
-            for f in context[variable]:
-                ext = mozpath.splitext(f)[1]
-                if ext not in allowed_suffixes:
-                    raise SandboxValidationError(
-                        '%s has an unknown file type.' % f, context)
-                if variable.startswith('GENERATED_'):
-                    l = passthru.variables.setdefault('GARBAGE', [])
-                    l.append(f)
-
-            # Now sort the files to let groupby work.
-            sorted_files = sorted(context[variable], key=canonical_suffix_for_file)
-            for canonical_suffix, files in itertools.groupby(sorted_files, canonical_suffix_for_file):
-                arglist = [context, list(files), canonical_suffix]
-                if variable.startswith('UNIFIED_') and 'FILES_PER_UNIFIED_FILE' in context:
-                    arglist.append(context['FILES_PER_UNIFIED_FILE'])
-                yield klass(*arglist)
-
-        sources_with_flags = [f for f in sources if sources[f].flags]
-        for f in sources_with_flags:
-            ext = mozpath.splitext(f)[1]
-            yield PerSourceFlag(context, f, sources[f].flags)
+        for obj in self._process_sources(context, passthru):
+            yield obj
 
         exports = context.get('EXPORTS')
         if exports:
             yield Exports(context, exports,
                 dist_install=not context.get('NO_DIST_INSTALL', False))
 
         generated_files = context.get('GENERATED_FILES')
         if generated_files:
@@ -866,16 +790,96 @@ class TreeMetadataEmitter(LoggingMixin):
 
         sub = cls(context)
         sub.input_path = mozpath.join(context.srcdir, '%s.in' % path)
         sub.output_path = mozpath.join(context.objdir, path)
         sub.relpath = path
 
         return sub
 
+    def _process_sources(self, context, passthru):
+        for symbol in ('SOURCES', 'HOST_SOURCES', 'UNIFIED_SOURCES'):
+            for src in (context[symbol] or []):
+                if not os.path.exists(mozpath.join(context.srcdir, src)):
+                    raise SandboxValidationError('File listed in %s does not '
+                        'exist: \'%s\'' % (symbol, src), context)
+
+        no_pgo = context.get('NO_PGO')
+        sources = context.get('SOURCES', [])
+        no_pgo_sources = [f for f in sources if sources[f].no_pgo]
+        if no_pgo:
+            if no_pgo_sources:
+                raise SandboxValidationError('NO_PGO and SOURCES[...].no_pgo '
+                    'cannot be set at the same time', context)
+            passthru.variables['NO_PROFILE_GUIDED_OPTIMIZE'] = no_pgo
+        if no_pgo_sources:
+            passthru.variables['NO_PROFILE_GUIDED_OPTIMIZE'] = no_pgo_sources
+
+        # A map from "canonical suffixes" for a particular source file
+        # language to the range of suffixes associated with that language.
+        #
+        # We deliberately don't list the canonical suffix in the suffix list
+        # in the definition; we'll add it in programmatically after defining
+        # things.
+        suffix_map = {
+            '.s': set(['.asm']),
+            '.c': set(),
+            '.m': set(),
+            '.mm': set(),
+            '.cpp': set(['.cc', '.cxx']),
+            '.S': set(),
+        }
+
+        # The inverse of the above, mapping suffixes to their canonical suffix.
+        canonicalized_suffix_map = {}
+        for suffix, alternatives in suffix_map.iteritems():
+            alternatives.add(suffix)
+            for a in alternatives:
+                canonicalized_suffix_map[a] = suffix
+
+        def canonical_suffix_for_file(f):
+            return canonicalized_suffix_map[mozpath.splitext(f)[1]]
+
+        # A map from moz.build variables to the canonical suffixes of file
+        # kinds that can be listed therein.
+        all_suffixes = list(suffix_map.keys())
+        varmap = dict(
+            SOURCES=(Sources, all_suffixes),
+            HOST_SOURCES=(HostSources, ['.c', '.mm', '.cpp']),
+            UNIFIED_SOURCES=(UnifiedSources, ['.c', '.mm', '.cpp']),
+            GENERATED_SOURCES=(GeneratedSources, all_suffixes),
+        )
+
+        for variable, (klass, suffixes) in varmap.items():
+            allowed_suffixes = set().union(*[suffix_map[s] for s in suffixes])
+
+            # First ensure that we haven't been given filetypes that we don't
+            # recognize.
+            for f in context[variable]:
+                ext = mozpath.splitext(f)[1]
+                if ext not in allowed_suffixes:
+                    raise SandboxValidationError(
+                        '%s has an unknown file type.' % f, context)
+                if variable.startswith('GENERATED_'):
+                    l = passthru.variables.setdefault('GARBAGE', [])
+                    l.append(f)
+
+            # Now sort the files to let groupby work.
+            sorted_files = sorted(context[variable], key=canonical_suffix_for_file)
+            for canonical_suffix, files in itertools.groupby(sorted_files, canonical_suffix_for_file):
+                arglist = [context, list(files), canonical_suffix]
+                if variable.startswith('UNIFIED_') and 'FILES_PER_UNIFIED_FILE' in context:
+                    arglist.append(context['FILES_PER_UNIFIED_FILE'])
+                yield klass(*arglist)
+
+        sources_with_flags = [f for f in sources if sources[f].flags]
+        for f in sources_with_flags:
+            ext = mozpath.splitext(f)[1]
+            yield PerSourceFlag(context, f, sources[f].flags)
+
     def _process_test_manifests(self, context):
         # While there are multiple test manifests, the behavior is very similar
         # across them. We enforce this by having common handling of all
         # manifests and outputting a single class type with the differences
         # described inside the instance.
         #
         # Keys are variable prefixes and values are tuples describing how these
         # manifests should be handled: