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 modeldump;
043:
044: import java.io.IOException;
045: import modelutils.FileCodeModel;
046: import java.io.File;
047: import java.io.InputStream;
048: import java.io.PrintStream;
049: import java.util.ArrayList;
050: import java.util.Iterator;
051: import java.util.List;
052: import modelutils.Config;
053: import org.netbeans.modules.cnd.api.model.CsmFile;
054: import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
055: import org.netbeans.modules.cnd.api.model.CsmProject;
056: import org.netbeans.modules.cnd.apt.impl.support.APTFileMacroMap;
057: import org.netbeans.modules.cnd.apt.impl.support.APTIncludeHandlerImpl;
058: import org.netbeans.modules.cnd.makeproject.api.compilers.BasicCompiler;
059: import org.netbeans.modules.cnd.makeproject.api.compilers.GNUCCCompiler;
060: import org.netbeans.modules.cnd.makeproject.api.compilers.GNUCCompiler;
061: import org.netbeans.modules.cnd.makeproject.api.platforms.Platform;
062: import org.netbeans.modules.cnd.makeproject.api.platforms.Platforms;
063: import org.netbeans.modules.cnd.modelimpl.parser.apt.APTPreprocStateImpl;
064: import org.netbeans.modules.cnd.modelimpl.parser.apt.APTSystemStorage;
065: import org.netbeans.modules.cnd.apt.support.APTIncludeHandler;
066: import org.netbeans.modules.cnd.apt.support.APTMacroMap;
067: import org.netbeans.modules.cnd.apt.support.APTPreprocState;
068: import org.netbeans.modules.cnd.apt.utils.APTMacroUtils;
069: import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl;
070: import org.netbeans.modules.cnd.modelimpl.csm.core.ModelImpl;
071: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectBase;
072:
073: //import org.openide.util.NotImplementedException;
074:
075: /**
076: *
077: * @author ak119685
078: */
079: public class ModelDump {
080: PrintStream log = null;
081: ModelImpl model = null;
082: FileCodeModelReader modelReader = new FileCodeModelReader();
083: boolean modelStarted = false;
084: ProjectBase project = null;
085:
086: static ArrayList<String> sys_c_includes = new ArrayList<String>();
087: static ArrayList<String> sys_c_defines = new ArrayList<String>();
088: static ArrayList<String> sys_cpp_includes = new ArrayList<String>();
089: static ArrayList<String> sys_cpp_defines = new ArrayList<String>();
090:
091: static {
092: getSystemPredefines("c", sys_c_includes, sys_c_defines); // NOI18N
093: getSystemPredefines("c++", sys_cpp_includes, sys_cpp_defines); // NOI18N
094: }
095:
096: public ModelDump(PrintStream log) {
097: this .log = log;
098: model = (ModelImpl) CsmModelAccessor.getModel(); //new ModelImpl();
099: }
100:
101: public void setLog(PrintStream log) {
102: this .log = log;
103: }
104:
105: // Get predefined includes and defines...
106:
107: private static void getSystemPredefines(String lang,
108: ArrayList<String> includes, ArrayList<String> defines) {
109: BasicCompiler compiler;
110: if ("c".equals(lang)) { // NOI18N
111: compiler = new GNUCCompiler();
112: } else if ("c++".equals(lang)) { // NOI18N
113: compiler = new GNUCCCompiler();
114: } else {
115: return;
116: }
117: Platform platform = Platforms.getPlatform(Platform
118: .getDefaultPlatform());
119: for (Object o : compiler.getSystemIncludeDirectories(platform)) {
120: includes.add((String) o);
121: }
122: for (Object o : compiler.getSystemPreprocessorSymbols(platform)) {
123: defines.add((String) o);
124: }
125: }
126:
127: //
128: // NB!!!: The function below "eats" dashes ("-" symbols)
129: //
130: // private static void getSystemPredefines(String lang, ArrayList<String> includes, ArrayList<String> defines) {
131: // Process gcc = null;
132: // boolean inSearchList = false;
133: // boolean waitingDefinitions = true;
134: //
135: // try {
136: // gcc = Runtime.getRuntime().exec("gcc -v -E -x " + lang + " /dev/null"); // NOI18N
137: //
138: // InputStream in = gcc.getErrorStream();
139: // StringBuffer str = new StringBuffer();
140: // int i;
141: // char c;
142: //
143: // while((i = in.read()) != -1) {
144: // c = (char)i;
145: //
146: // if (c == '-' && (char)in.read() == 'D') {
147: // do {
148: // str = new StringBuffer();
149: // while ((c = (char)in.read()) != ' ') {
150: // str.append(c);
151: // }
152: // defines.add(str.toString());
153: //
154: // } while ((char)in.read() == '-' && (char)in.read() == 'D');
155: // } else {
156: // if (c == '\n') {
157: // String string = str.toString();
158: // if (string.matches("#include <.*")) { // NOI18N
159: // inSearchList = true;
160: // } else if (string.matches("End of search list.*")) { // NOI18N
161: // inSearchList = false;
162: // } else if (inSearchList) {
163: // includes.add(string.trim());
164: // }
165: // str = new StringBuffer();
166: // } else {
167: // str.append(c);
168: // }
169: // }
170: // }
171: // } catch (IOException ex) {
172: // ex.printStackTrace();
173: // }
174: // }
175:
176: public void startModel() {
177: if (!modelStarted) {
178: log.println("Starting model..."); // NOI18N
179: model.startup();
180: project = model.addProject("DummyProjectID",
181: "Dummy Project"); // NOI18N
182: modelStarted = true;
183: }
184: }
185:
186: public void stopModel() {
187: if (modelStarted) {
188: log.println("Stopping model..."); // NOI18N
189: model.shutdown();
190: modelStarted = false;
191: }
192: }
193:
194: public static void main(String[] args) {
195: try {
196: Config config = new Config("l:i:d:vh", args); // NOI18N
197:
198: if (config.flagSet("-h")) { // NOI18N
199: outUsage();
200: return;
201: }
202:
203: String logFile = config.getParameterFor("-l"); // NOI18N
204: PrintStream log = (logFile == null) ? System.out
205: : new PrintStream(logFile);
206:
207: String sourceFile = config.getArgument();
208: List<String> includes = config.getParametersFor("-i"); // NOI18N
209: List<String> defines = config.getParametersFor("-d"); // NOI18N
210:
211: if (includes == null) {
212: includes = new ArrayList<String>();
213: }
214:
215: if (defines == null) {
216: defines = new ArrayList<String>();
217: }
218:
219: ModelDump modelDump = new ModelDump(log);
220: CsmFile csmFile = modelDump.process(sourceFile, includes,
221: defines);
222: FileCodeModel codeModel = modelDump.modelReader
223: .getModelFor(csmFile);
224:
225: if (config.flagSet("-v")) { // NOI18N
226: codeModel.dump();
227: }
228:
229: modelDump.stopModel();
230:
231: } catch (Exception ex) {
232: System.err.println("Fatal error: " + ex.getMessage());
233: ex.printStackTrace(System.err);
234: }
235: }
236:
237: public FileImpl process(String fileName, List<String> includes,
238: List<String> defines) {
239: log.println("\nGetting model data for " + fileName + " ..."); // NOI18N
240:
241: if (!modelStarted) {
242: startModel();
243: }
244:
245: ArrayList<String> sysIncludes = null;
246: if (fileName.endsWith(".c")) { // NOI18N
247: sysIncludes = sys_c_includes;
248: defines.addAll(sys_c_defines);
249: } else {
250: sysIncludes = sys_cpp_includes;
251: defines.addAll(sys_cpp_defines);
252: }
253:
254: FileImpl fileImpl = parseFile(fileName, sysIncludes, includes,
255: defines);
256:
257: log.println("\t... done.\n"); // NOI18N
258:
259: return fileImpl;
260: }
261:
262: private FileImpl parseFile(String fileName,
263: List<String> sysIncludes, List<String> quoteIncludes,
264: List<String> defines) {
265: File file = new File(fileName);
266: FileImpl fileImpl = null;
267:
268: log.println("Processing file: " + fileName); // NOI18N
269: log.println("System includes: " + sysIncludes); // NOI18N
270: log.println("Quote includes: " + quoteIncludes); // NOI18N
271: log.println("Definitions: " + defines); // NOI18N
272:
273: APTSystemStorage aptSystemStorage = new APTSystemStorage();
274: APTMacroMap map = new APTFileMacroMap();
275: APTMacroUtils.fillMacroMap(map, defines);
276:
277: List checkedSysIncludes = aptSystemStorage.getIncludes(
278: "TraceModelSysIncludes", sysIncludes); // NOI18N
279: APTIncludeHandler aptIncludeHandler = new APTIncludeHandlerImpl(
280: file.getAbsolutePath(), quoteIncludes,
281: checkedSysIncludes);
282:
283: APTPreprocState preprocState = new APTPreprocStateImpl(map,
284: aptIncludeHandler, true);
285: APTPreprocState.State state = preprocState.getState();
286: fileImpl = (FileImpl) project.testAPTParseFile(file
287: .getAbsolutePath(), preprocState);
288:
289: try {
290: fileImpl.scheduleParsing(true, state);
291: } catch (InterruptedException e) {
292: e.printStackTrace(log);
293: }
294:
295: for (Iterator it = project.getLibraries().iterator(); it
296: .hasNext();) {
297: CsmProject lib = (CsmProject) it.next();
298: lib.waitParse();
299: }
300:
301: project.waitParse();
302:
303: return fileImpl;
304: }
305:
306: private static void outUsage() {
307: System.err.println("ModelDump usage:");
308: System.err.println("ant -f build_cli.xml -Dargs=\"args\"");
309: System.err.println("Where args can be: l:i:d:vh");
310: }
311:
312: }
|