Bug 1257823 - Move set_define() to the sandbox draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 23 Mar 2016 11:05:54 +0900
changeset 343860 7f92b9b6aacf40e21abd7119856203faa0680e45
parent 343859 9f7c4f40087a4391e68768c0f85bbe6881301a6c
child 343861 0e6907fd8f6c716fb47afd0cf7a03829a8c874cf
push id13691
push userbmo:mh+mozilla@glandium.org
push dateWed, 23 Mar 2016 10:00:34 +0000
bugs1257823
milestone48.0a1
Bug 1257823 - Move set_define() to the sandbox In order to make the transition to global set_define easier, move its current definition from inside the sandbox to the sandbox itself.
build/moz.configure/util.configure
python/mozbuild/mozbuild/configure/__init__.py
--- a/build/moz.configure/util.configure
+++ b/build/moz.configure/util.configure
@@ -56,36 +56,16 @@ def find_program(file):
         return os.path.abspath(file) if os.path.isfile(file) else None
     from which import which, WhichError
     try:
         return normsep(which(file))
     except WhichError:
         return None
 
 
-@depends('--help')
-def _defines(help):
-    ret = {}
-    set_config('DEFINES', ret)
-    return ret
-
-
-@template
-def set_define(name, value):
-    @depends(_defines)
-    @advanced
-    def _add_define(defines):
-        from mozbuild.configure import ConfigureError
-        if name in defines:
-            raise ConfigureError("'%s' is already defined" % name)
-        defines[name] = value
-
-del _defines
-
-
 @template
 def unique_list(l):
     result = []
     for i in l:
         if l not in result:
             result.append(i)
     return result
 
--- a/python/mozbuild/mozbuild/configure/__init__.py
+++ b/python/mozbuild/mozbuild/configure/__init__.py
@@ -285,20 +285,22 @@ class ConfigureSandbox(dict):
         references. The decorated function is called as soon as the decorator
         is called, and the arguments it receives are the OptionValue or
         function results corresponding to each of the arguments to @depends.
         As an exception, when a HelpFormatter is attached, only functions that
         have '--help' in their @depends argument list are called.
 
         The decorated function is altered to use a different global namespace
         for its execution. This different global namespace exposes a limited
-        set of functions from os.path, and two additional functions:
-        `imply_option` and `set_config`. The former allows to inject additional
-        options as if they had been passed on the command line. The latter
-        declares new configuration items for consumption by moz.build.
+        set of functions from os.path, and three additional functions:
+        `imply_option`, `set_config` and `set_define`. The first allows to
+        inject additional options as if they had been passed on the command
+        line. The second declares new configuration items for consumption by
+        moz.build. The last declares new defines, stored in a DEFINES
+        configuration item.
         '''
         if not args:
             raise ConfigureError('@depends needs at least one argument')
 
         with_help = False
         resolved_args = []
         for arg in args:
             if isinstance(arg, types.StringTypes):
@@ -329,16 +331,17 @@ class ConfigureSandbox(dict):
             if inspect.isgeneratorfunction(func):
                 raise ConfigureError(
                     'Cannot decorate generator functions with @depends')
             func, glob = self._prepare_function(func)
             result = DependsOutput()
             glob.update(
                 imply_option=result.imply_option,
                 set_config=result.__setitem__,
+                set_define=self._set_define,
             )
             dummy = wraps(func)(DummyFunction())
             self._depends[dummy] = func
             func.with_help = with_help
             if with_help:
                 for arg in args:
                     if (isinstance(arg, DummyFunction) and
                             not self._depends[arg].with_help):
@@ -417,16 +420,22 @@ class ConfigureSandbox(dict):
         '''Implementation of @advanced.
         This function gives the decorated function access to the complete set
         of builtins, allowing the import keyword as an expected side effect.
         '''
         func, glob = self._prepare_function(func)
         glob.update(__builtins__=__builtins__)
         return func
 
+    def _set_define(self, name, value):
+        defines = self._config.setdefault('DEFINES', {})
+        if name in defines:
+            raise ConfigureError("'%s' is already defined" % name)
+        defines[name] = value
+
     def _prepare_function(self, func):
         '''Alter the given function global namespace with the common ground
         for @depends, @template and @advanced.
         '''
         if not inspect.isfunction(func):
             raise TypeError("Unexpected type: '%s'" % type(func))
         if func in self._prepared_functions:
             return func, func.func_globals