Bug 1060210 - Automatically import new CppEclipse project into the workspace . r=gps
authorBenoit Girard <b56girard@gmail.com>
Wed, 03 Sep 2014 15:12:18 -0400
changeset 203462 decfb59e691c37e0615862f4fefdaaedd3ffef06
parent 203461 00275cee2708cf01ca8738e04215245fd20d4429
child 203463 1b4ba4d597ed7f58cf50c0f6f9bda063c789445f
push id27428
push usercbook@mozilla.com
push dateThu, 04 Sep 2014 13:00:04 +0000
treeherdermozilla-central@7bfd030e8fc8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1060210
milestone35.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 1060210 - Automatically import new CppEclipse project into the workspace . r=gps
python/mozbuild/mozbuild/backend/cpp_eclipse.py
--- a/python/mozbuild/mozbuild/backend/cpp_eclipse.py
+++ b/python/mozbuild/mozbuild/backend/cpp_eclipse.py
@@ -1,16 +1,17 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+import errno
 import random
-import errno
+import os
+import subprocess
 import types
-import os
 import xml.etree.ElementTree as ET
 from .common import CommonBackend
 
 from ..frontend.data import (
     Defines,
 )
 
 # TODO Have ./mach eclipse generate the workspace and index it:
@@ -23,32 +24,31 @@ class CppEclipseBackend(CommonBackend):
     """
 
     def _init(self):
         CommonBackend._init(self)
 
         self._paths_to_defines = {}
         self._workspace_dir = self.get_workspace_path()
         self._project_dir = os.path.join(self._workspace_dir, 'gecko')
+        self._overwriting_workspace = os.path.isdir(self._workspace_dir)
 
         self._macbundle = self.environment.substs['MOZ_MACBUNDLE_NAME']
         self._appname = self.environment.substs['MOZ_APP_NAME']
         self._bin_suffix = self.environment.substs['BIN_SUFFIX']
         self._cxx = self.environment.substs['CXX']
         # Note: We need the C Pre Processor (CPP) flags, not the CXX flags
         self._cppflags = self.environment.substs.get('CPPFLAGS', '')
 
         def detailed(summary):
-            return ('\n' + \
-                   'Generated Cpp Eclipse workspace in "%s".\n' + \
-                   'OPTIONAL: Setup & index the project using: eclipse -application org.eclipse.cdt.managedbuilder.core.headlessbuild -data %s -importAll %s\n' + \
-                   'NOTE: This will take about 10 minutes.\n\n' \
-                   'Run with: eclipse -data %s\n' \
-                   'Import the project using File > Import > General > Existing Project into workspace') \
-                   % (self._workspace_dir, self._workspace_dir, self._project_dir, self._workspace_dir)
+            return ('Generated Cpp Eclipse workspace in "%s".\n' + \
+                   'If missing, import the project using File > Import > General > Existing Project into workspace\n' + \
+                   '\n' + \
+                   'Run with: eclipse -data %s\n') \
+                   % (self._workspace_dir, self._workspace_dir)
 
         self.summary.backend_detailed_summary = types.MethodType(detailed,
             self.summary)
 
     def get_workspace_path(self):
         # Eclipse doesn't support having the workspace inside the srcdir.
         # Since most people have their objdir inside their srcdir it's easier
         # and more consistent to just put the workspace along side the srcdir
@@ -104,16 +104,46 @@ class CppEclipseBackend(CommonBackend):
         formatter_prefs_path = os.path.join(settings_dir, 'org.eclipse.cdt.core.prefs')
         with open(formatter_prefs_path, 'wb') as fh:
             fh.write(FORMATTER_SETTINGS);
 
         editor_prefs_path = os.path.join(workspace_settings_dir, "org.eclipse.ui.editors.prefs");
         with open(editor_prefs_path, 'wb') as fh:
             fh.write(EDITOR_SETTINGS);
 
+        # Now import the project into the workspace
+        self._import_project()
+
+    def _import_project(self):
+        # If the workspace already exists then don't import the project again because
+        # eclipse doesn't handle this properly
+        if self._overwriting_workspace:
+            return
+
+        # We disable the indexer otherwise we're forced to index
+        # the whole codebase when importing the project. Indexing the project can take 20 minutes.
+        self._write_noindex()
+
+        try:
+            process = subprocess.check_call(
+                             ["eclipse", "-application", "-nosplash",
+                              "org.eclipse.cdt.managedbuilder.core.headlessbuild",
+                              "-data", self._workspace_dir, "-importAll", self._project_dir])
+        finally:
+            self._remove_noindex()
+
+    def _write_noindex(self):
+        noindex_path = os.path.join(self._project_dir, '.settings/org.eclipse.cdt.core.prefs')
+        with open(noindex_path, 'wb') as fh:
+            fh.write(NOINDEX_TEMPLATE);
+
+    def _remove_noindex(self):
+        noindex_path = os.path.join(self._project_dir, '.settings/org.eclipse.cdt.core.prefs')
+        os.remove(noindex_path)
+
     def _define_entry(self, name, value):
         define = ET.Element('entry')
         define.set('kind', 'macro')
         define.set('name', name)
         define.set('value', value)
         return ET.tostring(define)
 
     def _write_language_settings(self, fh):
@@ -645,8 +675,12 @@ org.eclipse.cdt.core.formatter.keep_impl
 org.eclipse.cdt.core.formatter.keep_then_statement_on_same_line=false
 org.eclipse.cdt.core.formatter.lineSplit=80
 org.eclipse.cdt.core.formatter.number_of_empty_lines_to_preserve=1
 org.eclipse.cdt.core.formatter.put_empty_statement_on_new_line=true
 org.eclipse.cdt.core.formatter.tabulation.char=space
 org.eclipse.cdt.core.formatter.tabulation.size=2
 org.eclipse.cdt.core.formatter.use_tabs_only_for_leading_indentations=false
 """
+
+NOINDEX_TEMPLATE = """eclipse.preferences.version=1
+indexer/indexerId=org.eclipse.cdt.core.nullIndexer
+"""