X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Filasm%2FDriver.cs;h=af7120dce5878fc481227faf290c186e52eb5662;hb=11961e0202d0a11ed40a8bf72f9f0a3b478e6dde;hp=73eacc9a21272efbc8503bc6f9e0b0d984766d93;hpb=24a7667524f1e4b5d722c45cd69e63a9f905cf56;p=mono.git diff --git a/mcs/ilasm/Driver.cs b/mcs/ilasm/Driver.cs index 73eacc9a212..af7120dce58 100644 --- a/mcs/ilasm/Driver.cs +++ b/mcs/ilasm/Driver.cs @@ -6,12 +6,15 @@ // Jackson Harper (Jackson@LatitudeGeo.com) // // (C) 2003 Jackson Harper, All rights reserved +// Copyright (C) 2004 Novell, Inc (http://www.novell.com) // using System; using System.IO; using System.Reflection; using System.Collections; +using System.Security.Cryptography; +using Mono.Security; namespace Mono.ILASM { @@ -28,59 +31,82 @@ namespace Mono.ILASM { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; DriverMain driver = new DriverMain (args); - try { - if (!driver.Run ()) { - Console.WriteLine (); - Console.WriteLine ("***** FAILURE *****"); - return 1; - } - } catch (Exception e) { - Console.WriteLine (e); - Console.WriteLine ("Error while compiling."); + if (!driver.Run ()) return 1; - } - Console.WriteLine ("Operation completed successfully"); + Report.Message ("Operation completed successfully"); return 0; } private class DriverMain { private ArrayList il_file_list; - private Report report; private string output_file; private Target target = Target.Exe; private string target_string = "exe"; - private bool quiet = false; private bool show_tokens = false; - private bool show_method_def = false; - private bool show_method_ref = false; +// private bool show_method_def = false; +// private bool show_method_ref = false; private bool show_parser = false; private bool scan_only = false; + private bool debugging_info = false; private CodeGen codegen; + private bool keycontainer = false; + private string keyname; + private StrongName sn; public DriverMain (string[] args) { il_file_list = new ArrayList (); ParseArgs (args); - report = new Report (quiet); } public bool Run () { + if (il_file_list.Count == 0) + Usage (); + if (output_file == null) + output_file = CreateOutputFilename (); try { - if (il_file_list.Count == 0) - Usage (); - if (output_file == null) - output_file = CreateOutputFile (); - codegen = new CodeGen (output_file, target == Target.Dll, true, report); - foreach (string file_path in il_file_list) + codegen = new CodeGen (output_file, target == Target.Dll, debugging_info); + foreach (string file_path in il_file_list) { + Report.FilePath = file_path; ProcessFile (file_path); + } if (scan_only) return true; - if (report.ErrorCount > 0) + if (Report.ErrorCount > 0) return false; - codegen.Write (); + + if (target != Target.Dll && !codegen.HasEntryPoint) + Report.Error ("No entry point found."); + + // if we have a key and aren't assembling a netmodule + if ((keyname != null) && !codegen.IsThisAssembly (null)) { + LoadKey (); + // this overrides any attribute or .publickey directive in the source + codegen.ThisAssembly.SetPublicKey (sn.PublicKey); + } + + try { + codegen.Write (); + } catch { + File.Delete (output_file); + throw; + } + } catch (ILAsmException e) { + Error (e.ToString ()); + return false; + } catch (PEAPI.PEFileException pe) { + Error ("Error : " + pe.Message); + return false; + } + + try { + if (sn != null) { + Report.Message ("Signing assembly with the specified strongname keypair"); + return Sign (output_file); + } } catch { return false; } @@ -88,6 +114,38 @@ namespace Mono.ILASM { return true; } + private void Error (string message) + { + Console.WriteLine (message + "\n"); + Console.WriteLine ("***** FAILURE *****\n"); + } + + private void LoadKey () + { + if (keycontainer) { + CspParameters csp = new CspParameters (); + csp.KeyContainerName = keyname; + RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (csp); + sn = new StrongName (rsa); + } else { + byte[] data = null; + using (FileStream fs = File.OpenRead (keyname)) { + data = new byte [fs.Length]; + fs.Read (data, 0, data.Length); + fs.Close (); + } + sn = new StrongName (data); + } + } + + private bool Sign (string filename) + { + // note: if the file cannot be signed (no public key in it) then + // we do not show an error, or a warning, if the key file doesn't + // exists + return sn.Sign (filename); + } + private void ProcessFile (string file_path) { if (!File.Exists (file_path)) { @@ -95,7 +153,7 @@ namespace Mono.ILASM { file_path); Environment.Exit (2); } - report.AssembleFile (file_path, null, + Report.AssembleFile (file_path, null, target_string, output_file); StreamReader reader = File.OpenText (file_path); ILTokenizer scanner = new ILTokenizer (reader); @@ -116,6 +174,7 @@ namespace Mono.ILASM { } ILParser parser = new ILParser (codegen, scanner); + codegen.BeginSourceFile (file_path); try { if (show_parser) parser.yyparse (new ScannerAdapter (scanner), @@ -123,12 +182,19 @@ namespace Mono.ILASM { else parser.yyparse (new ScannerAdapter (scanner), null); } catch (ILTokenizingException ilte) { - report.Error (file_path + "(" + ilte.Location.line + ") : error : " + - "syntax error at token '" + ilte.Token + "'."); - } catch { - Console.WriteLine ("Error at: " + scanner.Reader.Location); + Report.Error (ilte.Location, "syntax error at token '" + ilte.Token + "'"); + } catch (Mono.ILASM.yyParser.yyException ye) { + Report.Error (scanner.Reader.Location, ye.Message); + } catch (ILAsmException ie) { + ie.FilePath = file_path; + ie.Location = scanner.Reader.Location; throw; - } + } catch (Exception){ + Console.Write ("{0} ({1}, {2}): ",file_path, scanner.Reader.Location.line, scanner.Reader.Location.column); + throw; + } finally { + codegen.EndSourceFile (); + } } public void ShowToken (object sender, NewTokenEventArgs args) @@ -179,21 +245,31 @@ namespace Mono.ILASM { target_string = "dll"; break; case "quiet": - quiet = true; + Report.Quiet = true; break; + case "debug": + case "deb": + debugging_info = true; + break; // Stubs to stay commandline compatible with MS case "listing": case "nologo": - case "debug": case "clock": case "error": case "subsystem": case "flags": case "alignment": case "base": - case "key": case "resource": break; + case "key": + if (command_arg.Length > 0) + keycontainer = (command_arg [0] == '@'); + if (keycontainer) + keyname = command_arg.Substring (1); + else + keyname = command_arg; + break; case "scan_only": scan_only = true; break; @@ -201,10 +277,10 @@ namespace Mono.ILASM { show_tokens = true; break; case "show_method_def": - show_method_def = true; +// show_method_def = true; break; case "show_method_ref": - show_method_ref = true; +// show_method_ref = true; break; case "show_parser": show_parser = true; @@ -246,7 +322,7 @@ namespace Mono.ILASM { /// /// Get the first file name and makes it into an output file name /// - private string CreateOutputFile () + private string CreateOutputFilename () { string file_name = (string)il_file_list[0]; int ext_index = file_name.LastIndexOf ('.'); @@ -267,6 +343,9 @@ namespace Mono.ILASM { " /output:file_name Specifies output file.\n" + " /exe Compile to executable.\n" + " /dll Compile to library.\n" + + " /debug Include debug information.\n" + + " /key:keyfile Strongname using the specified key file\n" + + " /key:@container Strongname using the specified key container\n" + "Options can be of the form -option or /option\n"); Environment.Exit (1); } @@ -281,7 +360,7 @@ namespace Mono.ILASM { private void Version () { - string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString (); + string version = System.Reflection.Assembly.GetExecutingAssembly ().GetName ().Version.ToString (); Console.WriteLine ("Mono ILasm compiler version {0}", version); Environment.Exit (0); }