KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > Main


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26 package soot;
27
28
29 import soot.toolkits.astmetrics.ClassData;
30 import java.util.*;
31 import soot.options.Options;
32
33 import java.io.*;
34
35 /** Main class for Soot; provides Soot's command-line user interface. */
36 public class Main {
37     public Main(Singletons.Global g) {
38     }
39     public static Main v() {
40         return G.v().soot_Main();
41     }
42     // TODO: the following string should be updated by the source control
43
// No it shouldn't. Prcs is horribly borken in this respect, and causes
44
// the code to not compile all the time.
45
public final String JavaDoc versionString = "2.2.3";
46
47     private Date start;
48     private Date finish;
49
50     private void printVersion() {
51         G.v().out.println("Soot version " + versionString);
52
53         G.v().out.println(
54             "Copyright (C) 1997-2003 Raja Vallee-Rai and others.");
55         G.v().out.println("All rights reserved.");
56         G.v().out.println("");
57         G.v().out.println(
58             "Contributions are copyright (C) 1997-2003 by their respective contributors.");
59         G.v().out.println("See the file 'credits' for a list of contributors.");
60         G.v().out.println("See individual source files for details.");
61         G.v().out.println("");
62         G.v().out.println(
63             "Soot comes with ABSOLUTELY NO WARRANTY. Soot is free software,");
64         G.v().out.println(
65             "and you are welcome to redistribute it under certain conditions.");
66         G.v().out.println(
67             "See the accompanying file 'COPYING-LESSER.txt' for details.");
68         G.v().out.println();
69         G.v().out.println("Visit the Soot website:");
70         G.v().out.println(" http://www.sable.mcgill.ca/soot/");
71         G.v().out.println();
72         G.v().out.println("For a list of command line options, enter:");
73         G.v().out.println(" java soot.Main --help");
74     }
75
76     private void processCmdLine(String JavaDoc[] args) {
77
78         if (!Options.v().parse(args))
79             throw new CompilationDeathException(
80                 CompilationDeathException.COMPILATION_ABORTED,
81                 "Option parse error");
82
83         if( PackManager.v().onlyStandardPacks() ) {
84             for( Iterator packIt = PackManager.v().allPacks().iterator(); packIt.hasNext(); ) {
85                 final Pack pack = (Pack) packIt.next();
86                 Options.v().warnForeignPhase(pack.getPhaseName());
87                 for( Iterator trIt = pack.iterator(); trIt.hasNext(); ) {
88                     final Transform tr = (Transform) trIt.next();
89                     Options.v().warnForeignPhase(tr.getPhaseName());
90                 }
91             }
92         }
93         Options.v().warnNonexistentPhase();
94
95         if (Options.v().help()) {
96             G.v().out.println(Options.v().getUsage());
97             throw new CompilationDeathException(CompilationDeathException.COMPILATION_SUCCEEDED);
98         }
99
100         if (Options.v().phase_list()) {
101             G.v().out.println(Options.v().getPhaseList());
102             throw new CompilationDeathException(CompilationDeathException.COMPILATION_SUCCEEDED);
103         }
104
105         if(!Options.v().phase_help().isEmpty()) {
106             for( Iterator phaseIt = Options.v().phase_help().iterator(); phaseIt.hasNext(); ) {
107                 final String JavaDoc phase = (String JavaDoc) phaseIt.next();
108                 G.v().out.println(Options.v().getPhaseHelp(phase));
109             }
110             throw new CompilationDeathException(CompilationDeathException.COMPILATION_SUCCEEDED);
111         }
112
113         if (args.length == 0 || Options.v().version()) {
114             printVersion();
115             throw new CompilationDeathException(CompilationDeathException.COMPILATION_SUCCEEDED);
116         }
117
118         postCmdLineCheck();
119     }
120
121     private void exitCompilation(int status) {
122         exitCompilation(status, "");
123     }
124
125     private void exitCompilation(int status, String JavaDoc msg) {
126         if(status == CompilationDeathException.COMPILATION_ABORTED) {
127                 G.v().out.println("compilation failed: "+msg);
128         }
129     }
130
131     private void postCmdLineCheck() {
132         if (Options.v().classes().isEmpty()
133         && Options.v().process_dir().isEmpty()) {
134             throw new CompilationDeathException(
135                 CompilationDeathException.COMPILATION_ABORTED,
136                 "No main class specified!");
137         }
138     }
139
140     public String JavaDoc[] cmdLineArgs;
141     /**
142      * Entry point for cmd line invocation of soot.
143      */

144     public static void main(String JavaDoc[] args) {
145         try {
146             Main.v().run(args);
147         } catch( OutOfMemoryError JavaDoc e ) {
148             G.v().out.println( "Soot has run out of the memory allocated to it by the Java VM." );
149             G.v().out.println( "To allocate more memory to Soot, use the -Xmx switch to Java." );
150             G.v().out.println( "For example (for 400MB): java -Xmx400m soot.Main ..." );
151             throw e;
152         }
153     }
154
155     /**
156      * Entry point to the soot's compilation process.
157      */

158     public int run(String JavaDoc[] args) {
159         cmdLineArgs = args;
160
161         start = new Date();
162
163         try {
164             Timers.v().totalTimer.start();
165
166             processCmdLine(cmdLineArgs);
167
168             G.v().out.println("Soot started on " + start);
169
170             Scene.v().loadNecessaryClasses();
171
172             /*
173              * By this all the java to jimple has occured so we just check ast-metrics flag
174              *
175              * If it is set......print the astMetrics.xml file and stop executing soot
176              */

177             if(Options.v().ast_metrics()){
178                 try{
179                     OutputStream streamOut = new FileOutputStream("../astMetrics.xml");
180                     PrintWriter writerOut = new PrintWriter(new OutputStreamWriter(streamOut));
181                     writerOut.println("<?xml version='1.0'?>");
182                     writerOut.println("<ASTMetrics>");
183                     
184                     Iterator it = G.v().ASTMetricsData.iterator();
185                     while(it.hasNext()){
186                         //each is a classData object
187
ClassData cData = (ClassData)it.next();
188                         writerOut.println(cData.toString());
189                     }
190
191                     writerOut.println("</ASTMetrics>");
192                     writerOut.flush();
193                     streamOut.close();
194                 } catch (IOException e) {
195                     throw new CompilationDeathException("Cannot output file astMetrics");
196                 }
197                 exitCompilation(CompilationDeathException.COMPILATION_SUCCEEDED);
198                 return CompilationDeathException.COMPILATION_SUCCEEDED;
199
200             }
201             
202             
203             PackManager.v().runPacks();
204             PackManager.v().writeOutput();
205
206             Timers.v().totalTimer.end();
207
208             // Print out time stats.
209
if (Options.v().time())
210                 Timers.v().printProfilingInformation();
211
212         } catch (CompilationDeathException e) {
213             Timers.v().totalTimer.end();
214             exitCompilation(e.getStatus(), e.getMessage());
215             return e.getStatus();
216         }
217
218         finish = new Date();
219
220         G.v().out.println("Soot finished on " + finish);
221         long runtime = finish.getTime() - start.getTime();
222         G.v().out.println(
223             "Soot has run for "
224                 + (runtime / 60000)
225                 + " min. "
226                 + ((runtime % 60000) / 1000)
227                 + " sec.");
228
229         exitCompilation(CompilationDeathException.COMPILATION_SUCCEEDED);
230         return CompilationDeathException.COMPILATION_SUCCEEDED;
231     }
232 }
233
Popular Tags