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 ("Operation completed successfully");
40 private class DriverMain {
42 private ArrayList il_file_list;
43 private string output_file;
44 private Target target = Target.Exe;
45 private string target_string = "exe";
46 private bool show_tokens = false;
47 private bool show_method_def = false;
48 private bool show_method_ref = false;
49 private bool show_parser = false;
50 private bool scan_only = false;
51 private bool debugging_info = false;
52 private CodeGen codegen;
53 private bool keycontainer = false;
54 private string keyname;
56 public DriverMain (string[] args)
58 il_file_list = new ArrayList ();
64 if (il_file_list.Count == 0)
66 if (output_file == null)
67 output_file = CreateOutputFilename ();
69 codegen = new CodeGen (output_file, target == Target.Dll, debugging_info);
70 foreach (string file_path in il_file_list)
71 ProcessFile (file_path);
75 if (Report.ErrorCount > 0)
78 if (target != Target.Dll && !codegen.HasEntryPoint)
79 Report.Error ("No entry point found.");
84 File.Delete (output_file);
87 } catch (ILAsmException e) {
90 } catch (PEAPI.PEFileException pe) {
96 if (keyname != null) {
97 Console.WriteLine ("Signing assembly with the specified strongname keypair");
98 return Sign (output_file);
107 private void Error (string message)
109 Console.WriteLine ("Error : " + message + "\n");
110 Console.WriteLine ("***** FAILURE *****\n");
113 private bool Sign (string filename)
115 // note: if the file cannot be signed (no public key in it) then
116 // we do not show an error, or a warning, if the key file doesn't
118 StrongName sn = null;
120 CspParameters csp = new CspParameters ();
121 csp.KeyContainerName = keyname;
122 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (csp);
123 sn = new StrongName (rsa);
126 using (FileStream fs = File.OpenRead (keyname)) {
127 data = new byte [fs.Length];
128 fs.Read (data, 0, data.Length);
131 sn = new StrongName (data);
133 return sn.Sign (filename);
136 private void ProcessFile (string file_path)
138 if (!File.Exists (file_path)) {
139 Console.WriteLine ("File does not exist: {0}",
141 Environment.Exit (2);
143 Report.AssembleFile (file_path, null,
144 target_string, output_file);
145 StreamReader reader = File.OpenText (file_path);
146 ILTokenizer scanner = new ILTokenizer (reader);
149 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
150 //if (show_method_def)
151 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
152 //if (show_method_ref)
153 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
157 while ((tok = scanner.NextToken) != ILToken.EOF) {
158 Console.WriteLine (tok);
163 ILParser parser = new ILParser (codegen, scanner);
164 codegen.BeginSourceFile (file_path);
167 parser.yyparse (new ScannerAdapter (scanner),
168 new yydebug.yyDebugSimple ());
170 parser.yyparse (new ScannerAdapter (scanner), null);
171 } catch (ILTokenizingException ilte) {
172 Report.Error (file_path + "(" + ilte.Location.line + ") : error : " +
173 "syntax error at token '" + ilte.Token + "'.");
174 } catch (Mono.ILASM.yyParser.yyException) {
175 Report.Error ("Error at: " + scanner.Reader.Location);
177 Console.WriteLine ("Error at: " + scanner.Reader.Location);
180 codegen.EndSourceFile ();
184 public void ShowToken (object sender, NewTokenEventArgs args)
186 Console.WriteLine ("token: '{0}'", args.Token);
189 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
191 Console.WriteLine ("***** Method defined *****");
192 Console.WriteLine ("-- signature: {0}", args.Signature);
193 Console.WriteLine ("-- name: {0}", args.Name);
194 Console.WriteLine ("-- return type: {0}", args.ReturnType);
195 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
196 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
197 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
198 Console.WriteLine ("-- call conv: {0}", args.CallConv);
201 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
203 Console.WriteLine ("***** Method referenced *****");
204 Console.WriteLine ("-- signature: {0}", args.Signature);
205 Console.WriteLine ("-- name: {0}", args.Name);
206 Console.WriteLine ("-- return type: {0}", args.ReturnType);
207 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
210 private void ParseArgs (string[] args)
213 foreach (string str in args) {
214 if ((str[0] != '-') && (str[0] != '/')) {
215 il_file_list.Add (str);
218 switch (GetCommand (str, out command_arg)) {
221 output_file = command_arg;
225 target_string = "exe";
229 target_string = "dll";
238 debugging_info = true;
240 // Stubs to stay commandline compatible with MS
252 if (command_arg.Length > 0)
253 keycontainer = (command_arg [0] == '@');
255 keyname = command_arg.Substring (1);
257 keyname = command_arg;
265 case "show_method_def":
266 show_method_def = true;
268 case "show_method_ref":
269 show_method_ref = true;
287 il_file_list.Add (str);
293 private string GetCommand (string str, out string command_arg)
295 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
296 string command = str.Substring (1,
297 end_index == -1 ? str.Length - 1 : end_index - 1);
299 if (end_index != -1) {
300 command_arg = str.Substring (end_index+1);
305 return command.ToLower ();
309 /// Get the first file name and makes it into an output file name
311 private string CreateOutputFilename ()
313 string file_name = (string)il_file_list[0];
314 int ext_index = file_name.LastIndexOf ('.');
317 ext_index = file_name.Length;
319 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
323 private void Usage ()
325 Console.WriteLine ("Mono ILasm compiler\n" +
326 "ilasm [options] source-files\n" +
327 " --about About the Mono ILasm compiler\n" +
328 " --version Print the version number of the Mono ILasm compiler\n" +
329 " /output:file_name Specifies output file.\n" +
330 " /exe Compile to executable.\n" +
331 " /dll Compile to library.\n" +
332 " /debug Include debug information.\n" +
333 " /key:keyfile Strongname using the specified key file\n" +
334 " /key:@container Strongname using the specified key container\n" +
335 "Options can be of the form -option or /option\n");
336 Environment.Exit (1);
339 private void About ()
342 "For more information on Mono, visit the project Web site\n" +
343 " http://www.go-mono.com\n\n");
344 Environment.Exit (0);
347 private void Version ()
349 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
350 Console.WriteLine ("Mono ILasm compiler version {0}", version);
351 Environment.Exit (0);