2003-02-10 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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                                                         output_file = command_arg;
89                                                         break;
90                                                 case "exe":
91                                                         target = Target.Exe;
92                                                         break;
93                                                 case "dll":
94                                                         target = Target.Dll;
95                                                         break;
96                                                 case "scan_only":
97                                                         scan_only = true;
98                                                         break;
99                                                 case "-about":
100                                                         About ();
101                                                         break;
102                                         }
103                                 }
104                         }
105                         
106                         private string GetCommand (string str, out string command_arg)
107                         {
108                                 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
109                                 string command = str.Substring (1, 
110                                         end_index == -1 ? str.Length - 1 : end_index - 1);
111                                 
112                                 if (end_index != -1) {
113                                         command_arg = str.Substring (end_index+1);
114                                 } else {
115                                         command_arg = null;
116                                 }
117
118                                 return command.ToLower ();
119                         }
120                         
121                         /// <summary>
122                         ///   Get the first file name and make it into an output file name
123                         /// </summary>
124                         private string CreateOutputFile () 
125                         {
126                                 string file_name = (string)il_file_list[0];
127                                 int ext_index = file_name.LastIndexOf ('.');                    
128         
129                                 if (ext_index == -1)
130                                         ext_index = file_name.Length;
131                                 
132                                 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
133                                         target.ToString ().ToLower ());                 
134                         }
135
136                         private void Usage ()
137                         {
138                                 Console.WriteLine ("Mono ILasm compiler\n" +
139                                         "ilasm [options] source-files\n" +
140                                         "   --about            About the Mono ILasm compiler\n" +
141                                         "   /out:file_name     Specifies output file.\n" +
142                                         "   /exe               Compile to executable.\n" +
143                                         "   /dll               Compile to library.\n" +
144                                         "Options can be of the form -option or /option\n");
145                         }
146
147                         private void About ()
148                         {       
149                                 Console.WriteLine (
150                                         "For more information on Mono, visit the project Web site\n" +
151                                         "   http://www.go-mono.com\n\n");
152                                 Environment.Exit (0);
153                         }
154
155                 }
156         }
157 }
158