3177da2741dfc03cf74949d055bcd235cf7cf113
[jump.git] / dol / src / dol / main / Options.java
1 /* $Id: Options.java 203 2010-10-11 08:59:47Z dchokshi $ */
2 package dol.main;
3
4 /**
5  * This class handles the command line options. It checks if an option is
6  * valid, and if so, it calls the appropriate method in the UserInterface,
7  * that reflects the global setting of DOL.
8  */
9 public class Options {
10
11     /**
12      * Parse the command-line arguments, creating models as specified. Then
13      * execute each model that contains a manager.
14      *
15      * @param args
16      *            The command-line arguments.
17      * @exception IllegalArgumentException
18      *            MyException If such and such occurs
19      * @throws IllegalArgumentException
20      *             if an illegal argument is found on the command line.
21      */
22     public Options(String args[]) throws IllegalArgumentException, NumberFormatException {
23         _ui = UserInterface.getInstance();
24         if(args != null) {
25             _parseArgs(args);
26         }
27     }
28
29     /**
30      * Parse the command-line arguments.
31      *
32      * @param args The arguments to be parsed
33      *
34      * @throws IllegalArgumentException
35      *             if an illegal argument is found on the command line.
36      */
37     protected void _parseArgs(String args[]) throws IllegalArgumentException,
38         NumberFormatException {
39         if(args.length > 0) {
40             for(int i = 0; i < args.length; i++ ) {
41                 String arg = args[i];
42                 if(_parseArg(arg) == false ) {
43                     if(arg.startsWith("-") && i < args.length - 1 ) {
44                         if(arg.equals("--platform") || arg.equals("-p") ) {
45                             _ui.setPlatformFileName(args[++i]);
46                         } else if(arg.equals("--processnetwork") || arg.equals("-P") ) {
47                             _ui.setNetworkFileName(args[++i]);
48                         } else if(arg.equals("--mapping") || arg.equals("-m") ) {
49                             _ui.setMappingFileName(args[++i]);
50                         } else if(arg.equals("--scheduler") || arg.equals("-s") ) {
51                             _ui.setSchedulerFileName(args[++i]);
52                         } else if(arg.equals("--systemC") || arg.equals("-C") ) {
53                             _ui.setCodeDirectoryName(args[++i]);
54                             _ui.setSystemCFlag();
55                         } else if(arg.equals("--HdS") || arg.equals("-H") ) {
56                             _ui.setHdsCodeDirectoryName(args[++i]);
57                             _ui.setHdsFlag();
58                         } else if(arg.startsWith("--rtems") || arg.startsWith("-R") ) {
59                             String bsp = arg;
60                             bsp = bsp.replaceAll("--rtems" , "");
61                             bsp = bsp.replaceAll("-R", "");
62                             if (bsp.equals("")) {
63                                 _ui.setRtemsBSP("pc386");
64                             } else if (bsp.equals("pc386") || bsp.equals("mparm")) {
65                                 _ui.setRtemsBSP(bsp);
66                             } else {
67                                 throw new IllegalArgumentException(
68                                         "Board support package \"" + bsp
69                                         + "\" not supported by code generation.");
70                             }
71                             _ui.setRtemsCodeDirectoryName(args[++i]);
72                             _ui.setRtemsFlag();
73                         } else if(arg.equals("--PaF") || arg.equals("-PF") ) {
74                             _ui.setPipeAndFilterCodeDirectoryName(args[++i]);
75                             _ui.setPipeAndFilterFlag();
76                         } else if(arg.equals("--epiphany") || arg.equals("-E") ) {
77                             _ui.setEpiphanyCodeDirectoryName(args[++i]);
78                             _ui.setEpiphanyFlag();
79                         } else if(arg.equals("--protothread") || arg.equals("-PT") ) {
80                             _ui.setProtothreadCodeDirectoryName(args[++i]);
81                             _ui.setProtothreadFlag();
82                         } else if(arg.equals("--dotty") || arg.equals("-D")) {
83                             _ui.setDottyFileName(args[++i]);
84                         } else if(arg.equals("--profiling") || arg.equals("-T")) {
85                             _ui.setTraceName(args[++i]);
86                             _ui.setProfilingFlag();
87                         } else if (arg.equals("--vsplog") || arg.equals("-L")) {
88                             _ui.setVspLogFileName(args[++i]);
89                             _ui.setVspLogFlag();
90                         } else if (arg.equals("--workload") || arg.equals("-W")) {
91                             _ui.setWorkloadFileName(args[++i]);
92                             _ui.setWorkloadFlag();
93                         } else if(arg.equals("--xmlGen") || arg.equals("-G")) {
94                             _ui.setOutputFileName(args[++i]);
95                             _ui.setXmlGenFlag();
96                         } else if(arg.equals("--cbe") || arg.equals("-CBE")) {
97                                 _ui.setCbeCodeDirectoryName(args[++i]); 
98                                 _ui.setCbeFlag(); 
99                         } else if(arg.equals("--yapi") || arg.equals("-Y")) {
100                             _ui.setYapiCodeDirectoryName(args[++i]); 
101                             _ui.setYapiFlag(); 
102                         } else {
103                             throw new IllegalArgumentException("Unrecognized option: " + arg);
104                         }
105                     } else {
106                         throw new IllegalArgumentException("Unrecognized option: " + arg);
107                     }
108                 }
109             }
110         } else {
111             throw new IllegalArgumentException(_usage());
112         }
113     }
114
115     /**
116      * Parse a single commandline argument.
117      *
118      * @param arg commandline argument
119      * @return true if the argument is understood, false otherwise
120      * @throws IllegalArgumentExcecption thrown if an illegal argument is
121      *                                   found on the command line.
122      */
123     protected boolean _parseArg(String arg) throws IllegalArgumentException {
124         if(arg.equals("--help") || arg.equals("-h") ) {
125             //throw new IllegalArgumentException(_usage());
126             System.out.println(_usage() );
127             System.exit(0);
128         } else if(arg.equals("--version") || arg.equals("-V") ) {
129             System.out.println("DOL version 0.0.1\n");
130             System.exit(0);
131         } else if(arg.equals("--verbose") || arg.equals("-v") ) {
132             _ui.setVerboseFlag();
133         } else if(arg.equals("--check") || arg.equals("-c")) {
134             _ui.setCheckFlag();
135         } else if(arg.equals("--debug")) {
136             _ui.setDebugFlag();
137         } else if(arg.equals("")) { // Ignore blank argument.
138         } else { // Argument not recognized.
139             return false;
140         }
141         return true;
142     }
143
144     /**
145      * Return a string summarizing the command-line arguments.
146      *
147      * @return A usage string.
148      */
149     protected String _usage() {
150         String result = "Usage: " + _commandTemplate + "\n\n"
151             + "Options:\n";
152
153         int i;
154         for(i = 0; i < _commandOptions.length; i++) {
155             result += " " + _commandOptions[i][0] + "\tabbr["
156                 + _commandOptions[i][1] + " " + _commandOptions[i][2]
157                 + "]\n";
158         }
159         result += "\nBoolean flags:\n";
160         for(i = 0; i < _commandFlags.length; i++) {
161             result += " " + _commandFlags[i][0] + "\tabbr["
162                 + _commandFlags[i][1] + "]\n";
163         }
164         return result;
165     }
166
167     /**
168      * The command-line options that are either present or not. Give the full
169      * name preceded with '--' and abbreviated version.
170      */
171     protected String _commandFlags[][] = {
172         { "--check     ", "-c" },
173         { "--flatten   ", "-F" },
174         { "--help      ", "-h" },
175         { "--version   ", "-V" },
176         { "--verbose   ", "-v" },
177         { "--debug     ", "none" },
178     };
179
180     /**
181      * The command-line options that take arguments.
182      */
183     protected String _commandOptions[][] = {
184         { "--platform      ", "-p ",  "<FileName>" },
185         { "--processnetwork", "-P ",  "<FileName>" },
186         { "--mapping       ", "-m ",  "<FileName>" },
187         { "--scheduler     ", "-s ",  "<FileName>" },
188         { "--dotty         ", "-D ",  "<FileName>" },
189         { "--xmlGen        ", "-G",   "<FileName>" },
190         { "--systemC       ", "-C ",  "<DirectoryName>" },
191         { "--HdS           ", "-H ",  "<DirectoryName>" },
192         { "--PaF           ", "-PF",  "<DirectoryName>" },
193         { "--rtems         ", "-R",   "<DirectoryName>" },
194         { "--protothread   ", "-PT",  "<DirectoryName>" },
195         { "--profiling     ", "-T",   "<trace FileName>" },
196         { "--vsplog        ", "-L",   "<log FileName>" },
197         { "--cbe           ", "-CBE", "<DirectoryName>" },
198         { "--yapi          ", "-Y",   "<DirectoryName>" },
199         { "--epiphany      ", "-E",   "<DirectoryName>" }
200     };
201
202     /**
203      * The form of the command line.
204      */
205     protected String _commandTemplate = "dol [ options ]";
206
207     /**
208      * The UserInterface object.
209      */
210     protected UserInterface _ui = null;
211 }