2005-07-24 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / ilasm / Driver.cs
index 7d163bf915b595a9112127b654e5df48abc92fce..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 {
 
@@ -24,46 +27,95 @@ namespace Mono.ILASM {
 
                 public static int Main (string[] args)
                 {
+                        // Do everything in Invariant
+                       System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
+
                         DriverMain driver = new DriverMain (args);
-                        driver.Run ();
+                                if (!driver.Run ()) {
+                                        Console.WriteLine ();
+                                        Console.WriteLine ("***** FAILURE *****");
+                                        return 1;
+                                }
+                        Console.WriteLine ("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_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)
                         {
                                 il_file_list = new ArrayList ();
                                 ParseArgs (args);
+                                report = new Report (quiet);
                         }
 
-                        public void Run ()
+                        public bool Run ()
                         {
-                                if (il_file_list.Count == 0) {
-                                        Usage ();
-                                        return;
+                                        if (il_file_list.Count == 0)
+                                                Usage ();
+                                        if (output_file == null)
+                                                output_file = CreateOutputFile ();
+                                        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)
+                                                return true;
+
+                                        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;
                                 }
-                                if (output_file == null)
-                                        output_file = CreateOutputFile ();
-                                codegen = new CodeGen (output_file, target == Target.Dll, true);
-                                foreach (string file_path in il_file_list)
-                                        ProcessFile (file_path);
-                                if (scan_only)
-                                        return;
 
-                                codegen.Write ();
+                                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)) {
@@ -71,6 +123,8 @@ namespace Mono.ILASM {
                                                 file_path);
                                         Environment.Exit (2);
                                 }
+                                report.AssembleFile (file_path, null,
+                                                target_string, output_file);
                                 StreamReader reader = File.OpenText (file_path);
                                 ILTokenizer scanner = new ILTokenizer (reader);
 
@@ -89,11 +143,23 @@ namespace Mono.ILASM {
                                         return;
                                 }
 
-                                ILParser parser = new ILParser (codegen);
-                                if (show_parser)
-                                        parser.yyparse (new ScannerAdapter (scanner),  new yydebug.yyDebugSimple ());
-                                else
-                                        parser.yyparse (new ScannerAdapter (scanner),  null);
+                                ILParser parser = new ILParser (codegen, scanner);
+                               codegen.BeginSourceFile (file_path);
+                                try {
+                                        if (show_parser)
+                                                parser.yyparse (new ScannerAdapter (scanner),
+                                                                new yydebug.yyDebugSimple ());
+                                        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);
+                                        throw;
+                                } finally {
+                                       codegen.EndSourceFile ();
+                               }
                         }
 
                         public void ShowToken (object sender, NewTokenEventArgs args)
@@ -131,40 +197,76 @@ namespace Mono.ILASM {
                                                 continue;
                                         }
                                         switch (GetCommand (str, out command_arg)) {
-                                                case "out":
-                                                        output_file = command_arg;
-                                                        break;
-                                                case "exe":
-                                                        target = Target.Exe;
+                                        case "out":
+                                        case "output":
+                                                output_file = command_arg;
+                                                break;
+                                        case "exe":
+                                                target = Target.Exe;
+                                                target_string = "exe";
+                                                break;
+                                        case "dll":
+                                                target = Target.Dll;
+                                                target_string = "dll";
+                                                break;
+                                        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 "clock":
+                                        case "error":
+                                        case "subsystem":
+                                        case "flags":
+                                        case "alignment":
+                                        case "base":
+                                        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;
+                                        case "show_tokens":
+                                                show_tokens = true;
+                                                break;
+                                        case "show_method_def":
+                                                show_method_def = true;
+                                                break;
+                                        case "show_method_ref":
+                                                show_method_ref = true;
+                                                break;
+                                        case "show_parser":
+                                                show_parser = true;
+                                                break;
+                                        case "-about":
+                                                if (str[0] != '-')
                                                         break;
-                                                case "dll":
-                                                        target = Target.Dll;
+                                                About ();
+                                                break;
+                                        case "-version":
+                                                if (str[0] != '-')
                                                         break;
-                                                case "scan_only":
-                                                        scan_only = true;
-                                                        break;
-                                                case "show_tokens":
-                                                        show_tokens = true;
-                                                        break;
-                                                case "show_method_def":
-                                                        show_method_def = true;
-                                                        break;
-                                                case "show_method_ref":
-                                                        show_method_ref = true;
-                                                        break;
-                                                case "show_parser":
-                                                        show_parser = true;
-                                                        break;
-                                                case "-about":
-                                                        if (str[0] != '-')
-                                                                break;
-                                                        About ();
-                                                        break;
-                                                case "-version":
-                                                        if (str[0] != '-')
-                                                                break;
-                                                        Version ();
+                                                Version ();
+                                                break;
+                                        default:
+                                                if (str [0] == '-')
                                                         break;
+                                                il_file_list.Add (str);
+                                                break;
                                         }
                                 }
                         }
@@ -196,7 +298,7 @@ namespace Mono.ILASM {
                                         ext_index = file_name.Length;
 
                                 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
-                                        target.ToString ().ToLower ());
+                                        target_string);
                         }
 
                         private void Usage ()
@@ -205,10 +307,14 @@ namespace Mono.ILASM {
                                         "ilasm [options] source-files\n" +
                                         "   --about            About the Mono ILasm compiler\n" +
                                         "   --version          Print the version number of the Mono ILasm compiler\n" +
-                                        "   /out:file_name     Specifies output file.\n" +
+                                        "   /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);
                         }
 
                         private void About ()