Bug 1386876 - Do not provide initial values for COMPILE_FLAGS when invoking within a template. r=glandium
authorChris Manchester <cmanchester@mozilla.com>
Mon, 11 Sep 2017 11:37:13 -0700
changeset 429625 ff40df887048eac15a6e2284552495289fe03c7b
parent 429624 2de74e37233164513adc7145871ea56b3fb76166
child 429626 6551197e3467f43e0ffe91d4cd099a6432283a17
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1386876
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1386876 - Do not provide initial values for COMPILE_FLAGS when invoking within a template. r=glandium MozReview-Commit-ID: 9JNEThJvva6
python/mozbuild/mozbuild/frontend/context.py
python/mozbuild/mozbuild/test/frontend/data/compile-flags-templates/moz.build
python/mozbuild/mozbuild/test/frontend/data/compile-flags-templates/test1.c
python/mozbuild/mozbuild/test/frontend/test_emitter.py
--- a/python/mozbuild/mozbuild/frontend/context.py
+++ b/python/mozbuild/mozbuild/frontend/context.py
@@ -301,18 +301,25 @@ class CompileFlags(ContextDerivedValue, 
     def __init__(self, context):
         self.flag_variables = (
             ('STL', context.config.substs.get('STL_FLAGS'), ('CXXFLAGS',)),
             ('VISIBILITY', context.config.substs.get('VISIBILITY_FLAGS'),
              ('CXXFLAGS', 'CFLAGS')),
         )
         self._known_keys = set(k for k, v, _ in self.flag_variables)
 
-        dict.__init__(self,
-                      ((k, TypedList(unicode)(v)) for k, v, _ in self.flag_variables))
+        # Providing defaults here doesn't play well with multiple templates
+        # modifying COMPILE_FLAGS from the same moz.build, because the merge
+        # done after the template runs can't tell which values coming from
+        # a template were set and which were provided as defaults.
+        template_name = getattr(context, 'template', None)
+        if template_name in (None, 'Gyp'):
+            dict.__init__(self, ((k, TypedList(unicode)(v)) for k, v, _ in self.flag_variables))
+        else:
+            dict.__init__(self)
 
     def __setitem__(self, key, value):
         if key not in self._known_keys:
             raise ValueError('Invalid value. `%s` is not a compile flags '
                              'category.' % key)
         if not (isinstance(value, list) and all(isinstance(v, unicode) for v in value)):
             raise ValueError('A list of strings must be provided as a value for a '
                              'compile flags category.')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/compile-flags-templates/moz.build
@@ -0,0 +1,22 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+Library('dummy')
+
+@template
+def DisableStlWrapping():
+    COMPILE_FLAGS['STL'] = []
+
+@template
+def NoVisibilityFlags():
+    COMPILE_FLAGS['VISIBILITY'] = []
+
+UNIFIED_SOURCES += ['test1.c']
+
+DisableStlWrapping()
+NoVisibilityFlags()
new file mode 100644
--- a/python/mozbuild/mozbuild/test/frontend/test_emitter.py
+++ b/python/mozbuild/mozbuild/test/frontend/test_emitter.py
@@ -218,16 +218,23 @@ class TestEmitterBasic(unittest.TestCase
         with self.assertRaisesRegexp(BuildReaderError, 'Invalid value.'):
             self.read_topsrcdir(reader)
 
         reader = self.reader('compile-flags-type-validation')
         with self.assertRaisesRegexp(BuildReaderError,
                                      'A list of strings must be provided'):
             self.read_topsrcdir(reader)
 
+    def test_compile_flags_templates(self):
+        reader = self.reader('compile-flags-templates')
+        sources, flags, lib = self.read_topsrcdir(reader)
+        self.assertIsInstance(flags, ComputedFlags)
+        self.assertEqual(flags.flags['STL'], [])
+        self.assertEqual(flags.flags['VISIBILITY'], [])
+
     def test_disable_stl_wrapping(self):
         reader = self.reader('disable-stl-wrapping')
         sources, flags, lib = self.read_topsrcdir(reader)
         self.assertIsInstance(flags, ComputedFlags)
         self.assertEqual(flags.flags['STL'], [])
 
     def test_visibility_flags(self):
         reader = self.reader('visibility-flags')