3 // Main Command line interface for Mono ILasm Compiler
6 // Jackson Harper (Jackson@LatitudeGeo.com)
8 // (C) 2003 Jackson Harper, All rights reserved
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 using System.Reflection;
15 using System.Collections;
16 using System.Security.Cryptography;
19 namespace Mono.ILASM {
28 public static int Main (string[] args)
30 // Do everything in Invariant
31 System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
33 DriverMain driver = new DriverMain (args);
36 Console.WriteLine ("***** FAILURE *****");
39 Console.WriteLine ("Operation completed successfully");
43 private class DriverMain {
45 private ArrayList il_file_list;
46 private Report report;
47 private string output_file;
48 private Target target = Target.Exe;
49 private string target_string = "exe";
50 private bool quiet = false;
51 private bool show_tokens = false;
52 private bool show_method_def = false;
53 private bool show_method_ref = false;
54 private bool show_parser = false;
55 private bool scan_only = false;
56 private bool debugging_info = false;
57 private CodeGen codegen;
58 private bool keycontainer = false;
59 private string keyname;
61 public DriverMain (string[] args)
63 il_file_list = new ArrayList ();
65 report = new Report (quiet);
70 if (il_file_list.Count == 0)
72 if (output_file == null)
73 output_file = CreateOutputFile ();
74 codegen = new CodeGen (output_file, target == Target.Dll, true, debugging_info, report);
75 foreach (string file_path in il_file_list)
76 ProcessFile (file_path);
80 if (report.ErrorCount > 0)
85 if (keyname != null) {
86 Console.WriteLine ("Signing assembly with the specified strongname keypair");
87 return Sign (output_file);
96 private bool Sign (string filename)
98 // note: if the file cannot be signed (no public key in it) then
99 // we do not show an error, or a warning, if the key file doesn't
101 StrongName sn = null;
103 CspParameters csp = new CspParameters ();
104 csp.KeyContainerName = keyname;
105 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (csp);
106 sn = new StrongName (rsa);
109 using (FileStream fs = File.OpenRead (keyname)) {
110 data = new byte [fs.Length];
111 fs.Read (data, 0, data.Length);
114 sn = new StrongName (data);
116 return sn.Sign (filename);
119 private void ProcessFile (string file_path)
121 if (!File.Exists (file_path)) {
122 Console.WriteLine ("File does not exist: {0}",
124 Environment.Exit (2);
126 report.AssembleFile (file_path, null,
127 target_string, output_file);
128 StreamReader reader = File.OpenText (file_path);
129 ILTokenizer scanner = new ILTokenizer (reader);
132 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
133 //if (show_method_def)
134 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
135 //if (show_method_ref)
136 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
140 while ((tok = scanner.NextToken) != ILToken.EOF) {
141 Console.WriteLine (tok);
146 ILParser parser = new ILParser (codegen, scanner);
147 codegen.BeginSourceFile (file_path);
150 parser.yyparse (new ScannerAdapter (scanner),
151 new yydebug.yyDebugSimple ());
153 parser.yyparse (new ScannerAdapter (scanner), null);
154 } catch (ILTokenizingException ilte) {
155 report.Error (file_path + "(" + ilte.Location.line + ") : error : " +
156 "syntax error at token '" + ilte.Token + "'.");
158 Console.WriteLine ("Error at: " + scanner.Reader.Location);
161 codegen.EndSourceFile ();
165 public void ShowToken (object sender, NewTokenEventArgs args)
167 Console.WriteLine ("token: '{0}'", args.Token);
170 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
172 Console.WriteLine ("***** Method defined *****");
173 Console.WriteLine ("-- signature: {0}", args.Signature);
174 Console.WriteLine ("-- name: {0}", args.Name);
175 Console.WriteLine ("-- return type: {0}", args.ReturnType);
176 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
177 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
178 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
179 Console.WriteLine ("-- call conv: {0}", args.CallConv);
182 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
184 Console.WriteLine ("***** Method referenced *****");
185 Console.WriteLine ("-- signature: {0}", args.Signature);
186 Console.WriteLine ("-- name: {0}", args.Name);
187 Console.WriteLine ("-- return type: {0}", args.ReturnType);
188 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
191 private void ParseArgs (string[] args)
194 foreach (string str in args) {
195 if ((str[0] != '-') && (str[0] != '/')) {
196 il_file_list.Add (str);
199 switch (GetCommand (str, out command_arg)) {
202 output_file = command_arg;
206 target_string = "exe";
210 target_string = "dll";
219 debugging_info = true;
221 // Stubs to stay commandline compatible with MS
233 if (command_arg.Length > 0)
234 keycontainer = (command_arg [0] == '@');
236 keyname = command_arg.Substring (1);
238 keyname = command_arg;
246 case "show_method_def":
247 show_method_def = true;
249 case "show_method_ref":
250 show_method_ref = true;
268 il_file_list.Add (str);
274 private string GetCommand (string str, out string command_arg)
276 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
277 string command = str.Substring (1,
278 end_index == -1 ? str.Length - 1 : end_index - 1);
280 if (end_index != -1) {
281 command_arg = str.Substring (end_index+1);
286 return command.ToLower ();
290 /// Get the first file name and makes it into an output file name
292 private string CreateOutputFile ()
294 string file_name = (string)il_file_list[0];
295 int ext_index = file_name.LastIndexOf ('.');
298 ext_index = file_name.Length;
300 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
304 private void Usage ()
306 Console.WriteLine ("Mono ILasm compiler\n" +
307 "ilasm [options] source-files\n" +
308 " --about About the Mono ILasm compiler\n" +
309 " --version Print the version number of the Mono ILasm compiler\n" +
310 " /output:file_name Specifies output file.\n" +
311 " /exe Compile to executable.\n" +
312 " /dll Compile to library.\n" +
313 " /debug Include debug information.\n" +
314 " /key:keyfile Strongname using the specified key file\n" +
315 " /key:@container Strongname using the specified key container\n" +
316 "Options can be of the form -option or /option\n");
317 Environment.Exit (1);
320 private void About ()
323 "For more information on Mono, visit the project Web site\n" +
324 " http://www.go-mono.com\n\n");
325 Environment.Exit (0);
328 private void Version ()
330 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
331 Console.WriteLine ("Mono ILasm compiler version {0}", version);
332 Environment.Exit (0);