Bug 920679 - Improve build environment detection in mach; r=glandium
authorGregory Szorc <gps@mozilla.com>
Tue, 01 Oct 2013 10:55:07 +0200
changeset 149441 fd6ed8459a47018249301b8f47fcba2b37aae165
parent 149440 af4605d0cc6b64214f3b344c12a376a03d43fd13
child 149442 ac2937b4759185cba2d7d41fda4587f43d059331
push id25389
push userryanvm@gmail.com
push dateTue, 01 Oct 2013 20:35:30 +0000
treeherdermozilla-central@4364824a4cab [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs920679
milestone27.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 920679 - Improve build environment detection in mach; r=glandium
python/mozbuild/mozbuild/base.py
python/mozbuild/mozbuild/test/test_base.py
--- a/python/mozbuild/mozbuild/base.py
+++ b/python/mozbuild/mozbuild/base.py
@@ -501,18 +501,24 @@ class MozbuildObject(ProcessExecutionMix
 class MachCommandBase(MozbuildObject):
     """Base class for mach command providers that wish to be MozbuildObjects.
 
     This provides a level of indirection so MozbuildObject can be refactored
     without having to change everything that inherits from it.
     """
 
     def __init__(self, context):
-        MozbuildObject.__init__(self, context.topdir, context.settings,
-            context.log_manager)
+        # Attempt to discover topobjdir through environment detection, as it is
+        # more reliable than mozconfig when cwd is inside an objdir.
+        dummy = MozbuildObject.from_environment(cwd=context.cwd)
+
+        topsrcdir = dummy.topsrcdir or context.topdir
+
+        MozbuildObject.__init__(self, topsrcdir, context.settings,
+            context.log_manager, topobjdir=dummy._topobjdir)
 
         self._mach_context = context
 
         # Incur mozconfig processing so we have unified error handling for
         # errors. Otherwise, the exceptions could bubble back to mach's error
         # handler.
         try:
             self.mozconfig
--- a/python/mozbuild/mozbuild/test/test_base.py
+++ b/python/mozbuild/mozbuild/test/test_base.py
@@ -13,25 +13,24 @@ import unittest
 
 from mozfile.mozfile import NamedTemporaryFile
 
 from mozunit import main
 
 from mach.logging import LoggingManager
 
 from mozbuild.base import (
-    BuildConfig,
+    MachCommandBase,
     MozbuildObject,
     PathArgument,
 )
 
 from mozbuild.backend.configenvironment import ConfigEnvironment
 
 
-
 curdir = os.path.dirname(__file__)
 topsrcdir = os.path.abspath(os.path.join(curdir, '..', '..', '..', '..'))
 log_manager = LoggingManager()
 
 
 class TestMozbuildObject(unittest.TestCase):
     def setUp(self):
         self._old_cwd = os.getcwd()
@@ -170,16 +169,54 @@ class TestMozbuildObject(unittest.TestCa
 
             os.chdir(topobjdir_real)
             obj = MozbuildObject.from_environment(detect_virtualenv_mozinfo=False)
             self.assertEqual(obj.topobjdir, topobjdir_real)
 
         finally:
             shutil.rmtree(d)
 
+    @unittest.skip('Failed on buildbot (bug 853954)')
+    def test_mach_command_base_inside_objdir(self):
+        """Ensure a MachCommandBase constructed from inside the objdir works."""
+
+        d = os.path.realpath(tempfile.mkdtemp())
+
+        try:
+            topobjdir = os.path.join(d, 'objdir')
+            os.makedirs(topobjdir)
+
+            topsrcdir = os.path.join(d, 'srcdir')
+            os.makedirs(topsrcdir)
+
+            mozinfo = os.path.join(topobjdir, 'mozinfo.json')
+            with open(mozinfo, 'wt') as fh:
+                json.dump(dict(
+                    topsrcdir=topsrcdir,
+                ), fh)
+
+            os.chdir(topobjdir)
+
+            class MockMachContext(object):
+                pass
+
+            context = MockMachContext()
+            context.cwd = topobjdir
+            context.topdir = topobjdir
+            context.settings = None
+            context.log_manager = None
+
+            o = MachCommandBase(context)
+
+            self.assertEqual(o.topobjdir, topobjdir)
+            self.assertEqual(o.topsrcdir, topsrcdir)
+
+        finally:
+            shutil.rmtree(d)
+
     def test_config_guess(self):
         # It's difficult to test for exact values from the output of
         # config.guess because they vary depending on platform.
         base = self.get_base()
         result = base._config_guess
 
         self.assertIsNotNone(result)
         self.assertGreater(len(result), 0)