Update
[mono.git] / mcs / ilasm / Driver.cs
index 3eee91790efb83383873aadd7041876e02f35fda..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;
                 }
@@ -58,6 +55,8 @@ namespace Mono.ILASM {
                         private bool scan_only = false;
                        private bool debugging_info = false;
                         private CodeGen codegen;
+                       private bool keycontainer = false;
+                       private string keyname;
 
                         public DriverMain (string[] args)
                         {
@@ -68,7 +67,6 @@ namespace Mono.ILASM {
 
                         public bool Run ()
                         {
-                                try {
                                         if (il_file_list.Count == 0)
                                                 Usage ();
                                         if (output_file == null)
@@ -82,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;
                                 }
@@ -89,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)) {
@@ -185,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;
@@ -223,11 +262,6 @@ namespace Mono.ILASM {
                                                         break;
                                                 Version ();
                                                 break;
-                                       case "-debug":
-                                               if (str[0] != '-')
-                                                       break;
-                                               debugging_info = true;
-                                               break;
                                         default:
                                                 if (str [0] == '-')
                                                         break;
@@ -276,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);
                         }