Update
[mono.git] / mcs / ilasm / Driver.cs
index 73eacc9a21272efbc8503bc6f9e0b0d984766d93..5df97902989bffc6bbe67f1f5ee5ab41db05b11f 100644 (file)
@@ -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,17 +31,11 @@ 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.");
-                                return 1;
-                        }
                         Console.WriteLine ("Operation completed successfully");
                         return 0;
                 }
@@ -56,7 +53,10 @@ namespace Mono.ILASM {
                         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;
 
                         public DriverMain (string[] args)
                         {
@@ -67,12 +67,11 @@ namespace Mono.ILASM {
 
                         public bool Run ()
                         {
-                                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);
+                                        codegen = new CodeGen (output_file, target == Target.Dll, true, debugging_info, report);
                                         foreach (string file_path in il_file_list)
                                                 ProcessFile (file_path);
                                         if (scan_only)
@@ -81,6 +80,12 @@ namespace Mono.ILASM {
                                         if (report.ErrorCount > 0)
                                                 return false;
                                         codegen.Write ();
+
+                                try {
+                                       if (keyname != null) {
+                                               Console.WriteLine ("Signing assembly with the specified strongname keypair");
+                                               return Sign (output_file);
+                                       }
                                 } catch {
                                         return false;
                                 }
@@ -88,6 +93,29 @@ namespace Mono.ILASM {
                                 return true;
                         }
 
+                       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
+                               StrongName sn = null;
+                               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);
+                               }
+                               return sn.Sign (filename);
+                       }
+
                         private void ProcessFile (string file_path)
                         {
                                 if (!File.Exists (file_path)) {
@@ -116,6 +144,7 @@ namespace Mono.ILASM {
                                 }
 
                                 ILParser parser = new ILParser (codegen, scanner);
+                               codegen.BeginSourceFile (file_path);
                                 try {
                                         if (show_parser)
                                                 parser.yyparse (new ScannerAdapter (scanner),
@@ -128,7 +157,9 @@ namespace Mono.ILASM {
                                 } catch {
                                         Console.WriteLine ("Error at: " + scanner.Reader.Location);
                                         throw;
-                                }
+                                } finally {
+                                       codegen.EndSourceFile ();
+                               }
                         }
 
                         public void ShowToken (object sender, NewTokenEventArgs args)
@@ -181,19 +212,31 @@ namespace Mono.ILASM {
                                         case "quiet":
                                                 quiet = true;
                                                 break;
+                                        case "debug":
+                                        case "deb":
+                                               if (str[0] != '-')
+                                                       break;
+                                               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;
@@ -267,6 +310,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);
                         }