Bug 1296530 - Add a `when` argument to set_config() and set_define(). r?chmanchester draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 12 Oct 2016 14:24:28 +0900
changeset 425038 7c76e28bb8a26928168910d86f283ac518c5aa4a
parent 425037 a542279b7c654e698237a0f6093f4917b6e09748
child 425039 161b19d821c066b805ec2ba5284cdd2a3cd481d3
push id32321
push userbmo:mh+mozilla@glandium.org
push dateFri, 14 Oct 2016 02:53:47 +0000
reviewerschmanchester
bugs1296530
milestone52.0a1
Bug 1296530 - Add a `when` argument to set_config() and set_define(). r?chmanchester
python/mozbuild/mozbuild/configure/__init__.py
python/mozbuild/mozbuild/test/configure/test_configure.py
--- a/python/mozbuild/mozbuild/configure/__init__.py
+++ b/python/mozbuild/mozbuild/configure/__init__.py
@@ -666,54 +666,62 @@ class ConfigureSandbox(dict):
         if '.' in what:
             _from, what = what.rsplit('.', 1)
             import_line += 'from %s ' % _from
         import_line += 'import %s as imported' % what
         glob = {}
         exec_(import_line, {}, glob)
         return glob['imported']
 
-    def _resolve_and_set(self, data, name, value):
+    def _resolve_and_set(self, data, name, value, when=None):
         # Don't set anything when --help was on the command line
         if self._help:
             return
+        if when and not self._value_for(when):
+            return
         name = self._resolve(name, need_help_dependency=False)
         if name is None:
             return
         if not isinstance(name, types.StringTypes):
             raise TypeError("Unexpected type: '%s'" % type(name).__name__)
         if name in data:
             raise ConfigureError(
                 "Cannot add '%s' to configuration: Key already "
                 "exists" % name)
         value = self._resolve(value, need_help_dependency=False)
         if value is not None:
             data[name] = value
 
-    def set_config_impl(self, name, value):
+    def set_config_impl(self, name, value, when=None):
         '''Implementation of set_config().
         Set the configuration items with the given name to the given value.
         Both `name` and `value` can be references to @depends functions,
         in which case the result from these functions is used. If the result
         of either function is None, the configuration item is not set.
         '''
+        if when is not None:
+            when = self._dependency(when, 'set_config', 'when')
+
         self._execution_queue.append((
-            self._resolve_and_set, (self._config, name, value)))
+            self._resolve_and_set, (self._config, name, value, when)))
 
-    def set_define_impl(self, name, value):
+    def set_define_impl(self, name, value, when=None):
         '''Implementation of set_define().
         Set the define with the given name to the given value. Both `name` and
         `value` can be references to @depends functions, in which case the
         result from these functions is used. If the result of either function
         is None, the define is not set. If the result is False, the define is
         explicitly undefined (-U).
         '''
+        if when is not None:
+            when = self._dependency(when, 'set_define', 'when')
+
         defines = self._config.setdefault('DEFINES', {})
         self._execution_queue.append((
-            self._resolve_and_set, (defines, name, value)))
+            self._resolve_and_set, (defines, name, value, when)))
 
     def imply_option_impl(self, option, value, reason=None):
         '''Implementation of imply_option().
         Injects additional options as if they had been passed on the command
         line. The `option` argument is a string as in option()'s `name` or
         `env`. The option must be declared after `imply_option` references it.
         The `value` argument indicates the value to pass to the option.
         It can be:
--- a/python/mozbuild/mozbuild/test/configure/test_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_configure.py
@@ -428,16 +428,39 @@ class TestConfigure(unittest.TestCase):
         config = get_config([])
         self.assertEquals(config, {'BAR': False})
 
         with self.assertRaises(ConfigureError):
             # Both --set-foo and --set-name=FOO are going to try to
             # set_config('FOO'...)
             get_config(['--set-foo', '--set-name=FOO'])
 
+    def test_set_config_when(self):
+        with self.moz_configure('''
+            @depends('--help')
+            def always(_):
+                return True
+            @depends('--help')
+            def never(_):
+                return False
+            option('--with-qux', help='qux')
+            set_config('FOO', 'foo', when=always)
+            set_config('BAR', 'bar', when=never)
+            set_config('QUX', 'qux', when='--with-qux')
+        '''):
+            config = self.get_config()
+            self.assertEquals(config, {
+                'FOO': 'foo',
+            })
+            config = self.get_config(['--with-qux'])
+            self.assertEquals(config, {
+                'FOO': 'foo',
+                'QUX': 'qux',
+            })
+
     def test_set_define(self):
         def get_config(*args):
             return self.get_config(*args, configure='set_define.configure')
 
         help, config = get_config(['--help'])
         self.assertEquals(config, {'DEFINES': {}})
 
         config = get_config(['--set-foo'])
@@ -460,16 +483,39 @@ class TestConfigure(unittest.TestCase):
         config = get_config([])
         self.assertEquals(config['DEFINES'], {'BAR': False})
 
         with self.assertRaises(ConfigureError):
             # Both --set-foo and --set-name=FOO are going to try to
             # set_define('FOO'...)
             get_config(['--set-foo', '--set-name=FOO'])
 
+    def test_set_define_when(self):
+        with self.moz_configure('''
+            @depends('--help')
+            def always(_):
+                return True
+            @depends('--help')
+            def never(_):
+                return False
+            option('--with-qux', help='qux')
+            set_define('FOO', 'foo', when=always)
+            set_define('BAR', 'bar', when=never)
+            set_define('QUX', 'qux', when='--with-qux')
+        '''):
+            config = self.get_config()
+            self.assertEquals(config['DEFINES'], {
+                'FOO': 'foo',
+            })
+            config = self.get_config(['--with-qux'])
+            self.assertEquals(config['DEFINES'], {
+                'FOO': 'foo',
+                'QUX': 'qux',
+            })
+
     def test_imply_option_simple(self):
         def get_config(*args):
             return self.get_config(
                 *args, configure='imply_option/simple.configure')
 
         help, config = get_config(['--help'])
         self.assertEquals(config, {})