Bug 1296530 - Add a `when` argument to imply_option(). r?chmanchester draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 12 Oct 2016 14:48:47 +0900
changeset 425039 161b19d821c066b805ec2ba5284cdd2a3cd481d3
parent 425038 7c76e28bb8a26928168910d86f283ac518c5aa4a
child 425040 753c1e3022c343e4988e6b773d457c5709451fef
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 imply_option(). 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
@@ -367,16 +367,20 @@ class ConfigureSandbox(dict):
     @memoize
     def _value_for_option(self, option):
         implied = {}
         for implied_option in self._implied_options[:]:
             if implied_option.name not in (option.name, option.env):
                 continue
             self._implied_options.remove(implied_option)
 
+            if (implied_option.when and
+                not self._value_for(implied_option.when)):
+                continue
+
             value = self._resolve(implied_option.value,
                                   need_help_dependency=False)
 
             if value is not None:
                 if isinstance(value, OptionValue):
                     pass
                 elif value is True:
                     value = PositiveOptionValue()
@@ -713,17 +717,17 @@ class ConfigureSandbox(dict):
         '''
         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, when)))
 
-    def imply_option_impl(self, option, value, reason=None):
+    def imply_option_impl(self, option, value, reason=None, when=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:
         - True. In this case `imply_option` injects the positive option
           (--enable-foo/--with-foo).
@@ -779,27 +783,31 @@ class ConfigureSandbox(dict):
             reason = "imply_option at %s:%s" % (filename, line)
 
         if not reason:
             raise ConfigureError(
                 "Cannot infer what implies '%s'. Please add a `reason` to "
                 "the `imply_option` call."
                 % option)
 
+        if when is not None:
+            when = self._dependency(when, 'imply_option', 'when')
+
         prefix, name, values = Option.split_option(option)
         if values != ():
             raise ConfigureError("Implied option must not contain an '='")
 
         self._implied_options.append(ReadOnlyNamespace(
             option=option,
             prefix=prefix,
             name=name,
             value=value,
             caller=inspect.stack()[1],
             reason=reason,
+            when=when,
         ))
 
     def _prepare_function(self, func):
         '''Alter the given function global namespace with the common ground
         for @depends, and @template.
         '''
         if not inspect.isfunction(func):
             raise TypeError("Unexpected type: '%s'" % type(func).__name__)
--- a/python/mozbuild/mozbuild/test/configure/test_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_configure.py
@@ -672,16 +672,33 @@ class TestConfigure(unittest.TestCase):
                 def foo(value):
                     return value
             '''):
                 self.get_config()
 
         self.assertEquals(e.exception.message,
                           "Unexpected type: 'int'")
 
+    def test_imply_option_when(self):
+        with self.moz_configure('''
+            option('--with-foo', help='foo')
+            imply_option('--with-qux', True, when='--with-foo')
+            option('--with-qux', help='qux')
+            set_config('QUX', depends('--with-qux')(lambda x: x))
+        '''):
+            config = self.get_config()
+            self.assertEquals(config, {
+                'QUX': NegativeOptionValue(),
+            })
+
+            config = self.get_config(['--with-foo'])
+            self.assertEquals(config, {
+                'QUX': PositiveOptionValue(),
+            })
+
     def test_option_failures(self):
         with self.assertRaises(ConfigureError) as e:
             with self.moz_configure('option("--with-foo", help="foo")'):
                 self.get_config()
 
         self.assertEquals(
             e.exception.message,
             'Option `--with-foo` is not handled ; reference it with a @depends'