2003-02-03 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / ilasm / Driver.cs
1 //
2 // Mono.ILASM.Driver
3 //    Main Command line interface for Mono ILasm Compiler
4 //
5 // Author(s):
6 //  Jackson Harper (Jackson@LatitudeGeo.com)
7 //
8 // (C) 2003 Jackson Harper, All rights reserved
9 //
10
11 using System;
12 using System.IO;
13 using System.Collections;
14
15 namespace Mono.ILASM {
16
17         public class Driver {
18
19                 enum Target {
20                         Dll, 
21                         Exe
22                 }
23
24                 public static int Main (string[] args)
25                 {
26                         DriverMain driver = new DriverMain (args);
27                         driver.Run ();
28                         return 0;
29                 }
30
31                 private class DriverMain {
32                         
33                         private ArrayList il_file_list;
34                         private string output_file;
35                         private Target target = Target.Exe;
36                         private bool scan_only = false;
37                         private CodeGen codegen;
38
39                         public DriverMain (string[] args)
40                         {
41                                 il_file_list = new ArrayList ();
42                                 ParseArgs (args);
43                         }
44                         
45                         public void Run () 
46                         {
47                                 if (il_file_list.Count == 0) {
48                                         Usage ();
49                                         return;
50                                 }
51                                 if (output_file == null)
52                                         output_file = CreateOutputFile ();
53                                 codegen = new CodeGen (output_file);
54                                 foreach (string file_path in il_file_list)
55                                         ProcessFile (file_path);
56                                 if (scan_only)
57                                         return;
58                                 codegen.Emit ();
59                         }
60
61                         private void ProcessFile (string file_path)
62                         {
63                                 StreamReader reader = File.OpenText (file_path);
64                                 ILTokenizer scanner = new ILTokenizer (reader);
65                                 
66                                 if (scan_only) {
67                                         ILToken tok;
68                                         while ((tok = scanner.NextToken) != ILToken.EOF) {
69                                                 Console.WriteLine (tok);
70                                         }
71                                         return;
72                                 }
73
74                                 ILParser parser = new ILParser (codegen);
75                                 parser.yyparse (new ScannerAdapter (scanner), null);
76                         }
77
78                         private void ParseArgs (string[] args)
79                         {
80                                 string command_arg;
81                                 foreach (string str in args) {
82                                         if ((str[0] != '-') && (str[0] != '/')) {
83                                                 il_file_list.Add (str);
84                                                 continue;
85                                         } 
86                                         switch (GetCommand (str, out command_arg)) {
87                                                 case "out":
88                                                         Console.WriteLine ("Setting output: {0}", command_arg);
89                                                         output_file = command_arg;
90                                                         break;
91                                                 case "exe":
92                                                         target = Target.Exe;
93                                                         break;
94                                                 case "dll":
95                                                         target = Target.Dll;
96                                                         break;
97                                                 case "scan_only":
98                                                         scan_only = true;
99                                                         break;
100                                                 case "-about":
101                                                         About ();
102                                                         break;
103                                         }
104                                 }
105                         }
106                         
107                         private string GetCommand (string str, out string command_arg)
108                         {
109                                 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
110                                 string command = str.Substring (1, 
111                                         end_index == -1 ? str.Length - 1 : end_index - 1);
112                                 
113                                 if (end_index != -1) {
114                                         command_arg = str.Substring (end_index+1);
115                                 } else {
116                                         command_arg = null;
117                                 }
118
119                                 return command.ToLower ();
120                         }
121                         
122                         /// <summary>
123                         ///   Get the first file name and make it into an output file name
124                         /// </summary>
125                         private string CreateOutputFile () 
126                         {
127                                 string file_name = (string)il_file_list[0];
128                                 int ext_index = file_name.LastIndexOf ('.');                    
129         
130                                 if (ext_index == -1)
131                                         ext_index = file_name.Length;
132                                 
133                                 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
134                                         target.ToString ().ToLower ());                 
135                         }
136
137                         private void Usage ()
138                         {
139                                 Console.WriteLine ("Mono ILasm compiler\n" +
140                                         "ilasm [options] source-files\n" +
141                                         "   --about            About the Mono ILasm compiler\n" +
142                                         "   /out:file_name     Specifies output file.\n" +
143                                         "   /exe               Compile to executable.\n" +
144                                         "   /dll               Compile to library.\n" +
145                                         "Options can be of the form -option or /option\n");
146                         }
147
148                         private void About ()
149                         {       
150                                 Console.WriteLine (
151                                         "For more information on Mono, visit the project Web site\n" +
152                                         "   http://www.go-mono.com\n\n");
153                                 Environment.Exit (0);
154                         }
155
156                 }
157         }
158 }
159