001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.dwarfdiscovery.provider;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.HashMap;
049: import java.util.List;
050: import java.util.Map;
051: import org.netbeans.modules.cnd.discovery.api.DiscoveryProvider;
052: import org.netbeans.modules.cnd.discovery.api.ItemProperties;
053: import org.netbeans.modules.cnd.discovery.api.ProjectProperties;
054: import org.netbeans.modules.cnd.discovery.api.ProjectProxy;
055: import org.netbeans.modules.cnd.discovery.api.ProjectUtil;
056: import org.netbeans.modules.cnd.discovery.api.ProviderProperty;
057: import org.netbeans.modules.cnd.discovery.api.SourceFileProperties;
058: import org.netbeans.modules.cnd.dwarfdump.CompilationUnit;
059: import org.netbeans.modules.cnd.dwarfdump.Dwarf;
060: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.LANG;
061: import org.netbeans.modules.cnd.dwarfdump.exception.WrongFileFormatException;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.util.Utilities;
064:
065: /**
066: *
067: * @author Alexander Simon
068: */
069: public abstract class BaseDwarfProvider implements DiscoveryProvider {
070:
071: private static final boolean TRACE_READ_EXCEPTIONS = Boolean
072: .getBoolean("cnd.dwarfdiscovery.trace.read.errors"); // NOI18N
073: private static final boolean FULL_TRACE = Boolean
074: .getBoolean("cnd.dwarfdiscovery.trace.read.source"); // NOI18N
075: public static final String RESTRICT_SOURCE_ROOT = "restrict_source_root"; // NOI18N
076: public static final String RESTRICT_COMPILE_ROOT = "restrict_compile_root"; // NOI18N
077: protected boolean isStoped = false;
078:
079: public BaseDwarfProvider() {
080: }
081:
082: public boolean isApplicable(ProjectProxy project) {
083: return true;
084: }
085:
086: public void stop() {
087: isStoped = true;
088: }
089:
090: protected List<ProjectProperties> divideByLanguage(
091: List<SourceFileProperties> sources) {
092: DwarfProject cProp = null;
093: DwarfProject cppProp = null;
094: for (SourceFileProperties source : sources) {
095: ItemProperties.LanguageKind lang = source.getLanguageKind();
096: DwarfProject current = null;
097: if (lang == ItemProperties.LanguageKind.C) {
098: if (cProp == null) {
099: cProp = new DwarfProject(lang);
100: }
101: current = cProp;
102: } else {
103: if (cppProp == null) {
104: cppProp = new DwarfProject(lang);
105: }
106: current = cppProp;
107: }
108: current.update(source);
109: }
110: List<ProjectProperties> languages = new ArrayList<ProjectProperties>();
111: if (cProp != null) {
112: languages.add(cProp);
113: }
114: if (cppProp != null) {
115: languages.add(cppProp);
116: }
117: return languages;
118: }
119:
120: protected List<SourceFileProperties> getSourceFileProperties(
121: String[] objFileName) {
122: try {
123: HashMap<String, SourceFileProperties> map = new HashMap<String, SourceFileProperties>();
124: for (String file : objFileName) {
125: if (isStoped) {
126: break;
127: }
128: for (SourceFileProperties f : getSourceFileProperties(
129: file, map)) {
130: if (isStoped) {
131: break;
132: }
133: ProviderProperty p = getProperty(RESTRICT_SOURCE_ROOT);
134: if (p != null) {
135: String s = (String) p.getValue();
136: if (!f.getItemPath().startsWith(s)) {
137: continue;
138: }
139: }
140: p = getProperty(RESTRICT_COMPILE_ROOT);
141: if (p != null) {
142: String s = (String) p.getValue();
143: if (!f.getCompilePath().startsWith(s)) {
144: continue;
145: }
146: }
147:
148: String name = f.getItemPath();
149: if (new File(name).exists()) {
150: SourceFileProperties existed = map.get(name);
151: if (existed == null) {
152: map.put(name, f);
153: } else {
154: // Duplicated
155: if (existed.getUserInludePaths().size() < f
156: .getUserInludePaths().size()) {
157: map.put(name, f);
158: }
159: }
160: }
161: }
162: }
163: List<SourceFileProperties> list = new ArrayList<SourceFileProperties>();
164: list.addAll(map.values());
165: return list;
166: } finally {
167: PathCache.dispose();
168: }
169: }
170:
171: protected int sizeComilationUnit(String objFileName) {
172: int res = 0;
173: Dwarf dump = null;
174: try {
175: dump = new Dwarf(objFileName);
176: List<CompilationUnit> units = dump.getCompilationUnits();
177: if (units != null && units.size() > 0) {
178: for (CompilationUnit cu : units) {
179: if (cu.getRoot() == null
180: || cu.getSourceFileName() == null) {
181: continue;
182: }
183: String lang = cu.getSourceLanguage();
184: if (lang == null) {
185: continue;
186: }
187: if (LANG.DW_LANG_C.toString().equals(lang)
188: || LANG.DW_LANG_C89.toString().equals(lang)
189: || LANG.DW_LANG_C99.toString().equals(lang)) {
190: res++;
191: } else if (LANG.DW_LANG_C_plus_plus.toString()
192: .equals(lang)) {
193: res++;
194: }
195: }
196: }
197: } catch (FileNotFoundException ex) {
198: // Skip Exception
199: } catch (WrongFileFormatException ex) {
200: // Skip Exception
201: } catch (IOException ex) {
202: // Skip Exception
203: } catch (Exception ex) {
204: // Skip Exception
205: } finally {
206: if (dump != null) {
207: dump.dispose();
208: }
209: }
210: return res;
211: }
212:
213: private List<SourceFileProperties> getSourceFileProperties(
214: String objFileName,
215: HashMap<String, SourceFileProperties> map) {
216: List<SourceFileProperties> list = new ArrayList<SourceFileProperties>();
217: Dwarf dump = null;
218: try {
219: if (FULL_TRACE)
220: System.out.println("Process file " + objFileName); // NOI18N
221: dump = new Dwarf(objFileName);
222: List<CompilationUnit> units = dump.getCompilationUnits();
223: if (units != null && units.size() > 0) {
224: for (CompilationUnit cu : units) {
225: if (isStoped) {
226: break;
227: }
228: if (cu.getRoot() == null
229: || cu.getSourceFileName() == null) {
230: if (TRACE_READ_EXCEPTIONS)
231: System.out
232: .println("Compilation unit has broken name in file "
233: + objFileName); // NOI18N
234: continue;
235: }
236: String lang = cu.getSourceLanguage();
237: if (lang == null) {
238: if (TRACE_READ_EXCEPTIONS)
239: System.out
240: .println("Compilation unit has unresolved language in file "
241: + objFileName); // NOI18N
242: continue;
243: }
244: DwarfSource source = null;
245: if (LANG.DW_LANG_C.toString().equals(lang)
246: || LANG.DW_LANG_C89.toString().equals(lang)
247: || LANG.DW_LANG_C99.toString().equals(lang)) {
248: source = new DwarfSource(cu, false,
249: getCommpilerSettings(), grepBase);
250: } else if (LANG.DW_LANG_C_plus_plus.toString()
251: .equals(lang)) {
252: source = new DwarfSource(cu, true,
253: getCommpilerSettings(), grepBase);
254: } else {
255: if (FULL_TRACE)
256: System.out.println("Unknown language: "
257: + lang); // NOI18N
258: // Ignore other languages
259: }
260: if (source != null) {
261: String name = source.getItemPath();
262: SourceFileProperties old = map.get(name);
263: if (old != null
264: && old.getUserInludePaths().size() > 0) {
265: if (FULL_TRACE)
266: System.out
267: .println("Compilation unit already exist. Skip "
268: + name); // NOI18N
269: // do not process processed item
270: continue;
271: }
272: source.process(cu);
273: list.add(source);
274: }
275: }
276: } else {
277: if (TRACE_READ_EXCEPTIONS)
278: System.out
279: .println("There are no compilation units in file "
280: + objFileName); // NOI18N
281: }
282: } catch (FileNotFoundException ex) {
283: // Skip Exception
284: if (TRACE_READ_EXCEPTIONS)
285: System.out.println("File not found " + objFileName
286: + ": " + ex.getMessage()); // NOI18N
287: } catch (WrongFileFormatException ex) {
288: if (TRACE_READ_EXCEPTIONS)
289: System.out.println("Unsuported format of file "
290: + objFileName + ": " + ex.getMessage()); // NOI18N
291: ProviderProperty p = getProperty(RESTRICT_COMPILE_ROOT);
292: String root = "";
293: if (p != null) {
294: root = (String) p.getValue();
295: }
296: list = new LogReader(objFileName, root).getResults();
297: } catch (IOException ex) {
298: if (TRACE_READ_EXCEPTIONS) {
299: System.err.println("Exception in file " + objFileName); // NOI18N
300: ex.printStackTrace();
301: }
302: } catch (Exception ex) {
303: if (TRACE_READ_EXCEPTIONS) {
304: System.err.println("Exception in file " + objFileName); // NOI18N
305: ex.printStackTrace();
306: }
307: } finally {
308: if (dump != null) {
309: dump.dispose();
310: }
311: }
312: return list;
313: }
314:
315: private Map<String, List<String>> grepBase = new HashMap<String, List<String>>();
316:
317: public CompilerSettings getCommpilerSettings() {
318: return myCommpilerSettings;
319: }
320:
321: public void setCommpilerSettings(ProjectProxy project) {
322: myCommpilerSettings = new CompilerSettings(project);
323: }
324:
325: private CompilerSettings myCommpilerSettings;
326:
327: public static class CompilerSettings {
328: private List<String> systemIncludePathsC;
329: private List<String> systemIncludePathsCpp;
330: private Map<String, String> systemMacroDefinitionsC;
331: private Map<String, String> systemMacroDefinitionsCpp;
332: private Map<String, String> normalizedPaths = new HashMap<String, String>();
333: private String compileFlavor;
334: private String compileDirectory;
335:
336: public CompilerSettings(ProjectProxy project) {
337: systemIncludePathsCpp = ProjectUtil.getSystemIncludePaths(
338: project, true);
339: systemIncludePathsC = ProjectUtil.getSystemIncludePaths(
340: project, false);
341: systemMacroDefinitionsCpp = ProjectUtil
342: .getSystemMacroDefinitions(project, true);
343: systemMacroDefinitionsC = ProjectUtil
344: .getSystemMacroDefinitions(project, false);
345: compileFlavor = ProjectUtil.getCompilerFlavor(project);
346: compileDirectory = ProjectUtil
347: .getCompilerDirectory(project);
348: }
349:
350: public List<String> getSystemIncludePaths(boolean isCPP) {
351: if (isCPP) {
352: return systemIncludePathsCpp;
353: } else {
354: return systemIncludePathsC;
355: }
356: }
357:
358: public Map<String, String> getSystemMacroDefinitions(
359: boolean isCPP) {
360: if (isCPP) {
361: return systemMacroDefinitionsCpp;
362: } else {
363: return systemMacroDefinitionsC;
364: }
365: }
366:
367: public String getNormalizedPath(String path) {
368: String res = normalizedPaths.get(path);
369: if (res == null) {
370: res = normalizePath(path);
371: normalizedPaths.put(path, res);
372: }
373: return res;
374: }
375:
376: private String normalizePath(String path) {
377: path = FileUtil.normalizeFile(new File(path))
378: .getAbsolutePath();
379: if (Utilities.isWindows()) {
380: path = path.replace('\\', '/');
381: }
382: return path;
383: }
384:
385: public String getCompileFlavor() {
386: return compileFlavor;
387: }
388:
389: public String getCompileDirectory() {
390: return compileDirectory;
391: }
392: }
393: }
|