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;
55 private StrongName sn;
57 public DriverMain (string[] args)
59 il_file_list = new ArrayList ();
65 if (il_file_list.Count == 0)
67 if (output_file == null)
68 output_file = CreateOutputFilename ();
70 codegen = new CodeGen (output_file, target == Target.Dll, debugging_info);
71 foreach (string file_path in il_file_list) {
72 Report.FilePath = file_path;
73 ProcessFile (file_path);
78 if (Report.ErrorCount > 0)
81 if (target != Target.Dll && !codegen.HasEntryPoint)
82 Report.Error ("No entry point found.");
84 // if we have a key and aren't assembling a netmodule
85 if ((keyname != null) && !codegen.IsThisAssembly (null)) {
87 // this overrides any attribute or .publickey directive in the source
88 codegen.ThisAssembly.SetPublicKey (sn.PublicKey);
94 File.Delete (output_file);
97 } catch (ILAsmException e) {
98 Error (e.ToString ());
100 } catch (PEAPI.PEFileException pe) {
101 Error ("Error : " + pe.Message);
107 Console.WriteLine ("Signing assembly with the specified strongname keypair");
108 return Sign (output_file);
117 private void Error (string message)
119 Console.WriteLine (message + "\n");
120 Console.WriteLine ("***** FAILURE *****\n");
123 private void LoadKey ()
126 CspParameters csp = new CspParameters ();
127 csp.KeyContainerName = keyname;
128 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (csp);
129 sn = new StrongName (rsa);
132 using (FileStream fs = File.OpenRead (keyname)) {
133 data = new byte [fs.Length];
134 fs.Read (data, 0, data.Length);
137 sn = new StrongName (data);
141 private bool Sign (string filename)
143 // note: if the file cannot be signed (no public key in it) then
144 // we do not show an error, or a warning, if the key file doesn't
146 return sn.Sign (filename);
149 private void ProcessFile (string file_path)
151 if (!File.Exists (file_path)) {
152 Console.WriteLine ("File does not exist: {0}",
154 Environment.Exit (2);
156 Report.AssembleFile (file_path, null,
157 target_string, output_file);
158 StreamReader reader = File.OpenText (file_path);
159 ILTokenizer scanner = new ILTokenizer (reader);
162 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
163 //if (show_method_def)
164 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
165 //if (show_method_ref)
166 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
170 while ((tok = scanner.NextToken) != ILToken.EOF) {
171 Console.WriteLine (tok);
176 ILParser parser = new ILParser (codegen, scanner);
177 codegen.BeginSourceFile (file_path);
180 parser.yyparse (new ScannerAdapter (scanner),
181 new yydebug.yyDebugSimple ());
183 parser.yyparse (new ScannerAdapter (scanner), null);
184 } catch (ILTokenizingException ilte) {
185 Report.Error (ilte.Location, "syntax error at token '" + ilte.Token + "'");
186 } catch (Mono.ILASM.yyParser.yyException ye) {
187 Report.Error (scanner.Reader.Location, ye.Message);
188 } catch (ILAsmException ie) {
189 ie.FilePath = file_path;
190 ie.Location = scanner.Reader.Location;
192 } catch (Exception e){
193 Console.Write ("{0} ({1}, {2}): ",file_path, scanner.Reader.Location.line, scanner.Reader.Location.column);
196 codegen.EndSourceFile ();
200 public void ShowToken (object sender, NewTokenEventArgs args)
202 Console.WriteLine ("token: '{0}'", args.Token);
205 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
207 Console.WriteLine ("***** Method defined *****");
208 Console.WriteLine ("-- signature: {0}", args.Signature);
209 Console.WriteLine ("-- name: {0}", args.Name);
210 Console.WriteLine ("-- return type: {0}", args.ReturnType);
211 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
212 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
213 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
214 Console.WriteLine ("-- call conv: {0}", args.CallConv);
217 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
219 Console.WriteLine ("***** Method referenced *****");
220 Console.WriteLine ("-- signature: {0}", args.Signature);
221 Console.WriteLine ("-- name: {0}", args.Name);
222 Console.WriteLine ("-- return type: {0}", args.ReturnType);
223 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
226 private void ParseArgs (string[] args)
229 foreach (string str in args) {
230 if ((str[0] != '-') && (str[0] != '/')) {
231 il_file_list.Add (str);
234 switch (GetCommand (str, out command_arg)) {
237 output_file = command_arg;
241 target_string = "exe";
245 target_string = "dll";
254 debugging_info = true;
256 // Stubs to stay commandline compatible with MS
268 if (command_arg.Length > 0)
269 keycontainer = (command_arg [0] == '@');
271 keyname = command_arg.Substring (1);
273 keyname = command_arg;
281 case "show_method_def":
282 show_method_def = true;
284 case "show_method_ref":
285 show_method_ref = true;
303 il_file_list.Add (str);
309 private string GetCommand (string str, out string command_arg)
311 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
312 string command = str.Substring (1,
313 end_index == -1 ? str.Length - 1 : end_index - 1);
315 if (end_index != -1) {
316 command_arg = str.Substring (end_index+1);
321 return command.ToLower ();
325 /// Get the first file name and makes it into an output file name
327 private string CreateOutputFilename ()
329 string file_name = (string)il_file_list[0];
330 int ext_index = file_name.LastIndexOf ('.');
333 ext_index = file_name.Length;
335 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
339 private void Usage ()
341 Console.WriteLine ("Mono ILasm compiler\n" +
342 "ilasm [options] source-files\n" +
343 " --about About the Mono ILasm compiler\n" +
344 " --version Print the version number of the Mono ILasm compiler\n" +
345 " /output:file_name Specifies output file.\n" +
346 " /exe Compile to executable.\n" +
347 " /dll Compile to library.\n" +
348 " /debug Include debug information.\n" +
349 " /key:keyfile Strongname using the specified key file\n" +
350 " /key:@container Strongname using the specified key container\n" +
351 "Options can be of the form -option or /option\n");
352 Environment.Exit (1);
355 private void About ()
358 "For more information on Mono, visit the project Web site\n" +
359 " http://www.go-mono.com\n\n");
360 Environment.Exit (0);
363 private void Version ()
365 string version = System.Reflection.Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
366 Console.WriteLine ("Mono ILasm compiler version {0}", version);
367 Environment.Exit (0);