2002-09-19 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / mcs / driver.cs
index f7b5f55ac39fb698bc5295c60e7f02ac985d6153..33c575812db183dd66372a22f5f1e5261e5ed767 100755 (executable)
@@ -15,6 +15,7 @@ namespace Mono.CSharp
        using System.Reflection.Emit;
        using System.Collections;
        using System.IO;
+       using System.Text;
        using System.Globalization;
        using Mono.Languages;
 
@@ -33,6 +34,13 @@ namespace Mono.CSharp
                // mscorlib.dll here.
                static ArrayList references;
 
+               //
+               // If any of these fail, we ignore the problem.  This is so
+               // that we can list all the assemblies in Windows and not fail
+               // if they are missing on Linux.
+               //
+               static ArrayList soft_references;
+
                // Lookup paths
                static ArrayList link_paths;
 
@@ -42,16 +50,22 @@ namespace Mono.CSharp
                // Whether we want to only run the tokenizer
                static bool tokenize = false;
                
-               static int error_count = 0;
-
                static string first_source;
 
                static Target target = Target.Exe;
                static string target_ext = ".exe";
 
+               static bool want_debugging_support = false;
+               static ArrayList debug_arglist = new ArrayList ();
+
                static bool parse_only = false;
                static bool timestamps = false;
 
+               //
+               // Whether to load the initial config file (what CSC.RSP has by default)
+               // 
+               static bool load_default_config = true;
+
                static Hashtable response_file_list;
                static Hashtable source_files = new Hashtable ();
 
@@ -59,41 +73,75 @@ namespace Mono.CSharp
                // A list of resource files
                //
                static ArrayList resources;
+               static ArrayList embedded_resources;
                
                //
                // An array of the defines from the command line
                //
                static ArrayList defines;
 
+               //
+               // Output file
+               //
+               static string output_file = null;
+
                //
                // Last time we took the time
                //
-               static DateTime last_time;
-               static void ShowTime (string msg)
+               static DateTime last_time, first_time;
+
+               //
+               // Encoding: ISO-Latin1 is 28591
+               //
+               static Encoding encoding = Encoding.GetEncoding (28591);
+
+               //
+               // Whether the user has specified a different encoder manually
+               //
+               static bool using_default_encoder = true;
+               
+               public static void ShowTime (string msg)
                {
+                       if (!timestamps)
+                               return;
+
                        DateTime now = DateTime.Now;
                        TimeSpan span = now - last_time;
                        last_time = now;
-                       
+
                        Console.WriteLine (
                                "[{0:00}:{1:000}] {2}",
-                               span.Seconds, span.Milliseconds, msg);
+                               (int) span.TotalSeconds, span.Milliseconds, msg);
                }
-               
-               static int tokenize_file (string input_file)
+
+               public static void ShowTotalTime (string msg)
+               {
+                       if (!timestamps)
+                               return;
+
+                       DateTime now = DateTime.Now;
+                       TimeSpan span = now - first_time;
+                       last_time = now;
+
+                       Console.WriteLine (
+                               "[{0:00}:{1:000}] {2}",
+                               (int) span.TotalSeconds, span.Milliseconds, msg);
+               }              
+              
+               static void tokenize_file (string input_file)
                {
                        Stream input;
 
                        try {
                                input = File.OpenRead (input_file);
-
                        } catch {
                                Report.Error (2001, "Source file '" + input_file + "' could not be opened");
-                               return 1;
+                               return;
                        }
 
                        using (input){
-                               Tokenizer lexer = new Tokenizer (input, input_file, defines);
+                               StreamReader reader = new StreamReader (input, encoding, using_default_encoder);
+                               Tokenizer lexer = new Tokenizer (reader, input_file, defines);
                                int token, tokens = 0, errors = 0;
 
                                while ((token = lexer.token ()) != Token.EOF){
@@ -105,150 +153,168 @@ namespace Mono.CSharp
                                Console.WriteLine ("Tokenized: " + tokens + " found " + errors + " errors");
                        }
                        
-                       return 0;
+                       return;
                }
-               
-               static int parse (string input_file)
+
+               // MonoTODO("Change error code for aborted compilation to something reasonable")]               
+               static void parse (string input_file)
                {
                        CSharpParser parser;
                        Stream input;
-                       int errors;
 
                        try {
                                input = File.OpenRead (input_file);
                        } catch {
                                Report.Error (2001, "Source file '" + input_file + "' could not be opened");
-                               return 1;
+                               return;
                        }
 
-                       parser = new CSharpParser (input_file, input, defines);
+                       StreamReader reader = new StreamReader (input, encoding, using_default_encoder);
+                               
+                       parser = new CSharpParser (reader, input_file, defines);
                        parser.yacc_verbose = yacc_verbose;
                        try {
-                               errors = parser.parse ();
+                               parser.parse ();
                        } catch (Exception ex) {
-                               Console.WriteLine (ex);
-                               Console.WriteLine ("Compilation aborted");
-                               return 1;
+                               Report.Error(666, "Compilation aborted: " + ex);
                        } finally {
                                input.Close ();
                        }
-                       
-                       return errors;
                }
                
-               static void Usage (bool is_error)
+               static void Usage ()
                {
                        Console.WriteLine (
                                "Mono C# compiler, (C) 2001 Ximian, Inc.\n" +
                                "mcs [options] source-files\n" +
-                               "   --about         About the Mono C# compiler\n" +
-                               "   --checked       Set default context to checked\n" +
-                               "   --define SYM    Defines the symbol SYM\n" + 
-                               "   --fatal         Makes errors fatal\n" +
-                               "   -L PATH         Adds PATH to the assembly link path\n" +
-                               "   --nostdlib      Does not load core libraries\n" +
-                               "   --nowarn XXX    Ignores warning number XXX\n" +
-                               "   -o FNAME        Specifies output file\n" +
-                               "   --optimize      Optimizes\n" +
-                               "   --parse         Only parses the source file\n" +
-                               "   --probe X       Probes for the source to generate code X on line L\n" +
-                               "   --recurse SPEC  Recursively compiles the files in SPEC ([dir]/file)\n" + 
-                               "   --resource FILE Addds FILE as a resource\n" + 
-                               "   --stacktrace    Shows stack trace at error location\n" +
-                               "   --target KIND   Specifies the target (KIND is one of: exe, winexe, " +
-                                                   "library, module)\n" +
-                               "   --timestamp     Displays time stamps of various compiler events\n" +
-                               "   --unsafe        Allows unsafe code\n" +
-                               "   --werror        Treat warnings as errors\n" +
-                               "   --wlevel LEVEL  Sets warning level (the highest is 4, the default)\n" +
-                               "   -r              References an assembly\n" +
-                               "   -v              Verbose parsing (for debugging the parser)\n" +
-                                "   @file           Read response file for more options");
-                       if (is_error)
-                               error_count++;
+                               "   --about            About the Mono C# compiler\n" +
+                               "   -checked[+|-]      Set default context to checked\n" +
+                               "   -codepage:ID       Sets code page to the one in ID\n" +
+                               "                      (number, `utf8' or `reset')" +
+                               "   -define:S1[;S2]    Defines one or more symbols (short: /d:)\n" +
+                               "   -debug[+-]         Generate debugging information\n" + 
+                               "   -g                 Generate debugging information\n" +
+                               "   --debug-args X     Specify additional arguments for the\n" +
+                               "                      symbol writer.\n" +
+                               "   --fatal            Makes errors fatal\n" +
+                               "   -lib:PATH1,PATH2   Adds the paths to the assembly link path\n" +
+                               "   -main:class        Specified the class that contains the entry point\n" +
+                               "   -noconfig[+|-]     Disables implicit references to assemblies\n" +
+                               "   -nostdlib[+|-]     Does not load core libraries\n" +
+                               "   -nowarn:W1[,W2]    Disables one or more warnings\n" + 
+                               "   -out:FNAME         Specifies output file\n" +
+                               "   --parse            Only parses the source file\n" +
+                               "   --expect-error X   Expect that error X will be encountered\n" +
+                               "   -recurse:SPEC      Recursively compiles the files in SPEC ([dir]/file)\n" + 
+                               "   -reference:ASS     References the specified assembly (-r:ASS)\n" +
+                               "   --stacktrace       Shows stack trace at error location\n" +
+                               "   -target:KIND       Specifies the target (KIND is one of: exe, winexe, " +
+                                                      "library, module), (short: /t:)\n" +
+                               "   --timestamp        Displays time stamps of various compiler events\n" +
+                               "   -unsafe[+|-]       Allows unsafe code\n" +
+                               "   -warnaserror[+|-]  Treat warnings as errors\n" +
+                               "   -warn:LEVEL        Sets warning level (the highest is 4, the default)\n" +
+                               "   -v                 Verbose parsing (for debugging the parser)\n" +
+                               "\n" +
+                               "Resources:" +
+                               "   -linkresource:FILE[,ID] Links FILE as a resource\n" +
+                               "   -resource:FILE[,ID]     Embed FILE as a resource\n" +
+                               
+#if MCS_DEBUG                                         
+                               "   --mcs-debug X      Sets MCS debugging level to X\n" +
+#endif                                                
+                                "   @file              Read response file for more options\n\n" +
+                               "Options can be of the form -option or /option");
                }
 
                static void About ()
                {
                        Console.WriteLine (
-                               "The Mono C# compiler is (C) 2001 Ximian, Inc.\n\n" +
+                               "The Mono C# compiler is (C) 2001, 2002 Ximian, Inc.\n\n" +
                                "The compiler source code is released under the terms of the GNU GPL\n\n" +
 
                                "For more information on Mono, visit the project Web site\n" +
                                "   http://www.go-mono.com\n\n" +
 
-                               "The compiler was written by Miguel de Icaza and Ravi Pratap");
-               }
-               
-               static void error (string msg)
-               {
-                       Console.WriteLine ("Error: " + msg);
-               }
-
-               static void notice (string msg)
-               {
-                       Console.WriteLine (msg);
+                               "The compiler was written by Miguel de Icaza, Ravi Pratap and Martin Baulig");
+                       Environment.Exit (0);
                }
                
                public static int Main (string[] args)
                {
-                       MainDriver (args);
+                       bool ok = MainDriver (args);
                        
-                       return (error_count + Report.Errors) != 0 ? 1 : 0;
+                       if (ok && Report.Errors == 0) {
+                               Console.Write("Compilation succeeded");
+                               if (Report.Warnings > 0) {
+                                       Console.Write(" - {0} warning(s)", Report.Warnings);
+                               } 
+                               Console.WriteLine();
+                               return 0;
+                       } else {
+                               Console.WriteLine("Compilation failed: {0} error(s), {1} warnings",
+                                       Report.Errors, Report.Warnings);
+                               return 1;
+                       }
                }
 
-               static public int LoadAssembly (string assembly)
+               static public void LoadAssembly (string assembly, bool soft)
                {
                        Assembly a;
                        string total_log = "";
 
                        try {
-                               a = Assembly.Load (assembly);
-                               RootContext.TypeManager.AddAssembly (a);
-                               return 0;
+                               char[] path_chars = { '/', '\\', '.' };
+
+                               if (assembly.IndexOfAny (path_chars) != -1) {
+                                       a = Assembly.LoadFrom (assembly);
+                               } else {
+                                       a = Assembly.Load (assembly);
+                               }
+                               TypeManager.AddAssembly (a);
+
                        } catch (FileNotFoundException){
                                foreach (string dir in link_paths){
-                                       string full_path = dir + "/" + assembly + ".dll";
+                                       string full_path;
+                                       if (assembly.EndsWith (".dll"))
+                                               full_path = dir + "/" + assembly;
+                                       else
+                                               full_path = dir + "/" + assembly + ".dll";
 
                                        try {
                                                a = Assembly.LoadFrom (full_path);
-                                               RootContext.TypeManager.AddAssembly (a);
-                                               return 0;
+                                               TypeManager.AddAssembly (a);
+                                               return;
                                        } catch (FileNotFoundException ff) {
                                                total_log += ff.FusionLog;
                                                continue;
                                        }
                                }
+                               if (!soft) {
+                                       Report.Error (6, "Cannot find assembly `" + assembly + "'" );
+                                       Console.WriteLine ("Log: \n" + total_log);
+                               }
                        } catch (BadImageFormatException f) {
-                               error ("// Bad file format while loading assembly");
-                               error ("Log: " + f.FusionLog);
-                               return 1;
+                               Report.Error(6, "Cannot load assembly (bad file format)" + f.FusionLog);
                        } catch (FileLoadException f){
-                               error ("// File Load Exception: ");
-                               error ("Log: " + f.FusionLog);
-                               return 1;
+                               Report.Error(6, "Cannot load assembly " + f.FusionLog);
                        } catch (ArgumentNullException){
-                               error ("// Argument Null exception ");
-                               return 1;
+                               Report.Error(6, "Cannot load assembly (null argument)");
                        }
-                       
-                       Report.Error (6, "Can not find assembly `" + assembly + "'" );
-                       Console.WriteLine ("Log: \n" + total_log);
-
-                       return 0;
                }
 
                /// <summary>
                ///   Loads all assemblies referenced on the command line
                /// </summary>
-               static public int LoadReferences ()
+               static public void LoadReferences ()
                {
-                       int errors = 0;
-
                        foreach (string r in references)
-                               errors += LoadAssembly (r);
+                               LoadAssembly (r, false);
 
-                       return errors;
+                       foreach (string r in soft_references)
+                               LoadAssembly (r, true);
+                       
+                       return;
                }
 
                static void SetupDefaultDefines ()
@@ -268,11 +334,36 @@ namespace Mono.CSharp
                                return null;
                        }
 
+                       StringBuilder sb = new StringBuilder ();
+                       
                        while ((line = f.ReadLine ()) != null){
-                               string [] line_args = line.Split (new char [] { ' ' });
+                               int t = line.Length;
+
+                               for (int i = 0; i < t; i++){
+                                       char c = line [i];
+                                       
+                                       if (c == '"' || c == '\''){
+                                               char end = c;
+                                               
+                                               for (i++; i < t; i++){
+                                                       c = line [i];
 
-                               foreach (string arg in line_args)
-                                       args.Add (arg);
+                                                       if (c == end)
+                                                               break;
+                                                       sb.Append (c);
+                                               }
+                                       } else if (c == ' '){
+                                               if (sb.Length > 0){
+                                                       args.Add (sb.ToString ());
+                                                       sb.Length = 0;
+                                               }
+                                       } else
+                                               sb.Append (c);
+                               }
+                               if (sb.Length > 0){
+                                       args.Add (sb.ToString ());
+                                       sb.Length = 0;
+                               }
                        }
 
                        string [] ret_value = new string [args.Count];
@@ -331,7 +422,7 @@ namespace Mono.CSharp
                        pattern = spec;
                }
 
-               static int ProcessFile (string f)
+               static void ProcessFile (string f)
                {
                        if (first_source == null)
                                first_source = f;
@@ -344,32 +435,39 @@ namespace Mono.CSharp
                        } else
                                source_files.Add (f, f);
                                        
-                       if (tokenize)
+                       if (tokenize) {
                                tokenize_file (f);
-                       else
-                               return parse (f);
-                       return 0;
+                       } else {
+                               parse (f);
+                       }
                }
 
-               static int CompileFiles (string spec, bool recurse)
+               static void CompileFiles (string spec, bool recurse)
                {
                        string path, pattern;
-                       int errors = 0;
 
                        SplitPathAndPattern (spec, out path, out pattern);
+                       if (pattern.IndexOf ("*") == -1){
+                               ProcessFile (spec);
+                               return;
+                       }
+
                        string [] files = null;
                        try {
                                files = Directory.GetFiles (path, pattern);
                        } catch (System.IO.DirectoryNotFoundException) {
                                Report.Error (2001, "Source file `" + spec + "' could not be found");
-                               return 1;
+                               return;
+                       } catch (System.IO.IOException){
+                               Report.Error (2001, "Source file `" + spec + "' could not be found");
+                               return;
+                       }
+                       foreach (string f in files) {
+                               ProcessFile (f);
                        }
-                               
-                       foreach (string f in files)
-                               errors += ProcessFile (f);
 
                        if (!recurse)
-                               return errors;
+                               return;
                        
                        string [] dirs = null;
 
@@ -378,10 +476,578 @@ namespace Mono.CSharp
                        } catch {
                        }
                        
-                       foreach (string d in dirs)
-                               errors += CompileFiles (path + "\\" + d + "\\" + pattern, true);
+                       foreach (string d in dirs) {
+                                       
+                               // Don't include path in this string, as each
+                               // directory entry already does
+                               CompileFiles (d + "/" + pattern, true);
+                       }
+               }
 
-                       return errors;
+               static void DefineDefaultConfig ()
+               {
+                       //
+                       // For now the "default config" is harcoded into the compiler
+                       // we can move this outside later
+                       //
+                       string [] default_config = {
+                               "System",
+                               "System.Xml",
+#if false
+                               //
+                               // Is it worth pre-loading all this stuff?
+                               //
+                               "Accessibility",
+                               "System.Configuration.Install",
+                               "System.Data",
+                               "System.Design",
+                               "System.DirectoryServices",
+                               "System.Drawing.Design",
+                               "System.Drawing",
+                               "System.EnterpriseServices",
+                               "System.Management",
+                               "System.Messaging",
+                               "System.Runtime.Remoting",
+                               "System.Runtime.Serialization.Formatters.Soap",
+                               "System.Security",
+                               "System.ServiceProcess",
+                               "System.Web",
+                               "System.Web.RegularExpressions",
+                               "System.Web.Services",
+                               "System.Windows.Forms"
+#endif
+                       };
+                       
+                       int p = 0;
+                       foreach (string def in default_config)
+                               soft_references.Insert (p++, def);
+               }
+
+               static void SetOutputFile (string name)
+               {
+                       output_file = name;
+                       string bname = CodeGen.Basename (output_file);
+                       if (bname.IndexOf (".") == -1)
+                               output_file += ".exe";
+               }
+
+               static void SetWarningLevel (string s)
+               {
+                       int level = 0;
+
+                       try {
+                               level = Int32.Parse (s);
+                       } catch {
+                               Report.Error (
+                                       1900,
+                                       "--wlevel requires an value from 0 to 4");
+                               Environment.Exit (1);
+                       }
+                       if (level < 0 || level > 4){
+                               Report.Error (1900, "Warning level must be 0 to 4");
+                               Environment.Exit (1);
+                       } else
+                               RootContext.WarningLevel = level;
+               }
+               //
+               // Currently handles the Unix-like command line options, but will be
+               // deprecated in favor of the CSCParseOption, which will also handle the
+               // options that start with a dash in the future.
+               //
+               static bool UnixParseOption (string arg, ref string [] args, ref int i)
+               {
+                       switch (arg){
+                       case "-v":
+                               yacc_verbose = true;
+                               return true;
+                               
+                       case "--parse":
+                               parse_only = true;
+                               return true;
+                               
+                       case "--main": case "-m":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               RootContext.MainClass = args [++i];
+                               return true;
+                               
+                       case "--unsafe":
+                               RootContext.Unsafe = true;
+                               return true;
+                               
+                       case "/?": case "/h": case "/help":
+                       case "--help":
+                               Usage ();
+                               Environment.Exit (0);
+                               return true;
+                               
+                       case "--define":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               defines.Add (args [++i]);
+                               return true;
+                               
+                       case "--expect-error": {
+                               int code = 0;
+                               
+                               try {
+                                       code = Int32.Parse (
+                                               args [++i], NumberStyles.AllowLeadingSign);
+                                       Report.ExpectedError = code;
+                               } catch {
+                                       Report.Error (-14, "Invalid number specified");
+                               } 
+                               return true;
+                       }
+                               
+                       case "--tokenize": 
+                               tokenize = true;
+                               return true;
+                               
+                       case "-o": 
+                       case "--output":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               SetOutputFile (args [++i]);
+                               return true;
+                               
+                       case "--checked":
+                               RootContext.Checked = true;
+                               return true;
+                               
+                       case "--stacktrace":
+                               Report.Stacktrace = true;
+                               return true;
+                               
+                       case "--linkresource":
+                       case "--linkres":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Console.WriteLine("Missing argument to --linkres"); 
+                                       Environment.Exit (1);
+                               }
+                               if (resources == null)
+                                       resources = new ArrayList ();
+                               
+                               resources.Add (args [++i]);
+                               return true;
+                               
+                       case "--resource":
+                       case "--res":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Console.WriteLine("Missing argument to --resource"); 
+                                       Environment.Exit (1);
+                               }
+                               if (embedded_resources == null)
+                                       embedded_resources = new ArrayList ();
+                               
+                               embedded_resources.Add (args [++i]);
+                               return true;
+                               
+                       case "--target":
+                               if ((i + 1) >= args.Length){
+                                       Environment.Exit (1);
+                                       return true;
+                               }
+                               
+                               string type = args [++i];
+                               switch (type){
+                               case "library":
+                                       target = Target.Library;
+                                       target_ext = ".dll";
+                                       break;
+                                       
+                               case "exe":
+                                       target = Target.Exe;
+                                       break;
+                                       
+                               case "winexe":
+                                       target = Target.WinExe;
+                                       break;
+                                       
+                               case "module":
+                                       target = Target.Module;
+                                       target_ext = ".dll";
+                                       break;
+                               default:
+                                       Usage ();
+                                       Environment.Exit (1);
+                                       break;
+                               }
+                               return true;
+                               
+                       case "-r":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               
+                               references.Add (args [++i]);
+                               return true;
+                               
+                       case "-L":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();       
+                                       Environment.Exit (1);
+                               }
+                               link_paths.Add (args [++i]);
+                               return true;
+                               
+                       case "--nostdlib":
+                               RootContext.StdLib = false;
+                               return true;
+                               
+                       case "--fatal":
+                               Report.Fatal = true;
+                               return true;
+                               
+                       case "--werror":
+                               Report.WarningsAreErrors = true;
+                               return true;
+                               
+                       case "--nowarn":
+                               if ((i + 1) >= args.Length){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               int warn = 0;
+                               
+                               try {
+                                       warn = Int32.Parse (args [++i]);
+                               } catch {
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               Report.SetIgnoreWarning (warn);
+                               return true;
+                               
+                       case "--wlevel":
+                               if ((i + 1) >= args.Length){
+                                       Report.Error (
+                                               1900,
+                                               "--wlevel requires an value from 0 to 4");
+                                       Environment.Exit (1);
+                               }
+
+                               SetWarningLevel (args [++i]);
+                               return true;
+
+#if MCS_DEBUG
+                       case "--mcs-debug":
+                               if ((i + 1) >= args.Length){
+                                       Console.WriteLine ("--mcs-debug requires an argument");
+                                       Environment.Exit (1);
+                               }
+
+                               try {
+                                       Report.DebugFlags = Int32.Parse (args [++i]);
+                               } catch {
+                                       Console.WriteLine ("Invalid argument to --mcs-debug");
+                                       Environment.Exit (1);
+                               }
+                               return true;
+#endif
+                               
+                       case "--about":
+                               About ();
+                               return true;
+                               
+                       case "--recurse":
+                               if ((i + 1) >= args.Length){
+                                       Console.WriteLine ("--recurse requires an argument");
+                                       Environment.Exit (1);
+                               }
+                               CompileFiles (args [++i], true); 
+                               return true;
+                               
+                       case "--timestamp":
+                               timestamps = true;
+                               last_time = first_time = DateTime.Now;
+                               debug_arglist.Add ("timestamp");
+                               return true;
+                               
+                       case "--debug": case "-g":
+                               want_debugging_support = true;
+                               return true;
+                               
+                       case "--debug-args":
+                               if ((i + 1) >= args.Length){
+                                       Console.WriteLine ("--debug-args requires an argument");
+                                       Environment.Exit (1);
+                               }
+                               char[] sep = { ',' };
+                               debug_arglist.AddRange (args [++i].Split (sep));
+                               return true;
+                               
+                       case "--noconfig":
+                               load_default_config = false;
+                               return true;
+                       }
+
+                       return false;
+               }
+
+               //
+               // Currently it is very basic option parsing, but eventually, this will
+               // be the complete option parser
+               //
+               static bool CSCParseOption (string option, ref string [] args, ref int i)
+               {
+                       int idx = option.IndexOf (":");
+                       string arg, value;
+
+                       if (idx == -1){
+                               arg = option;
+                               value = "";
+                       } else {
+                               arg = option.Substring (0, idx);
+
+                               value = option.Substring (idx + 1);
+                       }
+
+                       switch (arg){
+                       case "/nologo":
+                               return true;
+
+                       case "/t":
+                       case "/target":
+                               switch (value){
+                               case "exe":
+                                       target = Target.Exe;
+                                       break;
+
+                               case "winexe":
+                                       target = Target.WinExe;
+                                       break;
+
+                               case "library":
+                                       target = Target.Library;
+                                       target_ext = ".dll";
+                                       break;
+
+                               case "module":
+                                       target = Target.Module;
+                                       target_ext = ".dll";
+                                       break;
+
+                               default:
+                                       Usage ();
+                                       Environment.Exit (1);
+                                       break;
+                               }
+                               return true;
+
+                       case "/out":
+                               if (value == ""){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+                               SetOutputFile (value);
+                               return true;
+
+                       case "/optimize":
+                       case "/optimize+":
+                       case "/optimize-":
+                       case "/incremental":
+                       case "/incremental+":
+                       case "/incremental-":
+                               // nothing.
+                               return true;
+
+                       case "/d":
+                       case "/define": {
+                               string [] defs;
+
+                               if (value == ""){
+                                       Usage ();
+                                       Environment.Exit (1);
+                               }
+
+                               defs = value.Split (new Char [] {';', ','});
+                               foreach (string d in defs){
+                                       defines.Add (d);
+                               }
+                               return true;
+                       }
+
+                       case "/linkres":
+                       case "/linkresource":
+                               if (value == ""){
+                                       Console.WriteLine ("{0} requires an argument", arg);
+                                       Environment.Exit (1);
+                               }
+                               if (resources == null)
+                                       resources = new ArrayList ();
+                               
+                               resources.Add (value);
+                               return true;
+                               
+                       case "/res":
+                       case "/resource":
+                               if (value == ""){
+                                       Console.WriteLine ("{0} requires an argument", arg);
+                                       Environment.Exit (1);
+                               }
+                               if (embedded_resources == null)
+                                       embedded_resources = new ArrayList ();
+                               
+                               embedded_resources.Add (value);
+                               return true;
+                               
+                       case "/recurse":
+                               if (value == ""){
+                                       Console.WriteLine ("/recurse requires an argument");
+                                       Environment.Exit (1);
+                               }
+                               CompileFiles (value, true); 
+                               return true;
+
+                       case "/r":
+                       case "/reference":
+                               if (value == ""){
+                                       Console.WriteLine ("/reference requires an argument");
+                                       Environment.Exit (1);
+                               }
+                               references.Add (value);
+                               return true;
+
+                       case "/lib": {
+                               string [] libdirs;
+                               
+                               if (value == ""){
+                                       Console.WriteLine ("/lib requires an argument");
+                                       Environment.Exit (1);
+                               }
+
+                               libdirs = value.Split (new Char [] { ',' });
+                               foreach (string dir in libdirs)
+                                       link_paths.Add (dir);
+                               return true;
+                       }
+                               
+                       case "/debug":
+                       case "/debug+":
+                               want_debugging_support = true;
+                               return true;
+
+                       case "/checked":
+                       case "/checked+":
+                               RootContext.Checked = true;
+                               return true;
+
+                       case "/checked-":
+                               RootContext.Checked = false;
+                               return true;
+
+                       case "/unsafe":
+                       case "/unsafe+":
+                               RootContext.Unsafe = true;
+                               return true;
+
+                       case "/unsafe-":
+                               RootContext.Unsafe = false;
+                               return true;
+
+                       case "/warnaserror":
+                       case "/warnaserror+":
+                               Report.WarningsAreErrors = true;
+                               return true;
+
+                       case "/warnaserror-":
+                               Report.WarningsAreErrors = false;
+                               return true;
+
+                       case "/warn":
+                               SetWarningLevel (value);
+                               return true;
+
+                       case "/nowarn": {
+                               string [] warns;
+
+                               if (value == ""){
+                                       Console.WriteLine ("/nowarn requires an argument");
+                                       Environment.Exit (1);
+                               }
+                               
+                               warns = value.Split (new Char [] {','});
+                               foreach (string wc in warns){
+                                       int warn = 0;
+                                       
+                                       try {
+                                               warn = Int32.Parse (wc);
+                                       } catch {
+                                               Usage ();
+                                               Environment.Exit (1);
+                                       }
+                                       Report.SetIgnoreWarning (warn);
+                               }
+                               return true;
+                       }
+
+                       case "/noconfig":
+                               load_default_config = false;
+                               return true;
+
+                       case "/help":
+                       case "/?":
+                               Usage ();
+                               Environment.Exit (0);
+                               return true;
+
+                       case "/main":
+                       case "/m":
+                               if (value == ""){
+                                       Console.WriteLine ("/main requires an argument");                                       
+                                       Environment.Exit (1);
+                               }
+                               RootContext.MainClass = value;
+                               return true;
+
+                       case "/nostdlib":
+                       case "/nostdlib+":
+                               RootContext.StdLib = false;
+                               return true;
+
+                       case "/nostdlib-":
+                               RootContext.StdLib = true;
+                               return true;
+
+                       case "/codepage":
+                               int cp = -1;
+
+                               if (value == "utf8")
+                                       cp = (new UTF8Encoding()).WindowsCodePage;
+                               if (value == "reset"){
+                                       cp = 28591;
+                                       using_default_encoder = true;
+                               }
+                               
+                               try {
+                                       cp = Int32.Parse (value);
+                               } catch { }
+                               
+                               if (cp == -1){
+                                       Console.WriteLine ("Invalid code-page requested");
+                                       Usage ();
+                               }
+
+                               try {
+                                       encoding = Encoding.GetEncoding (cp);
+                                       using_default_encoder = false;
+                               } catch {
+                                       Console.WriteLine ("Code page: {0} not supported", cp);
+                               }
+                               return true;
+
+                       }
+                       return false;
                }
                
                /// <summary>
@@ -393,13 +1059,14 @@ namespace Mono.CSharp
                ///    TODO: Mostly structured to debug the compiler
                ///    now, needs to be turned into a real driver soon.
                /// </remarks>
-               static void MainDriver (string [] args)
+               // [MonoTODO("Change error code for unknown argument to something reasonable")]
+               static bool MainDriver (string [] args)
                {
-                       int errors = 0, i;
-                       string output_file = null;
+                       int i;
                        bool parsing_options = true;
                        
                        references = new ArrayList ();
+                       soft_references = new ArrayList ();
                        link_paths = new ArrayList ();
 
                        SetupDefaultDefines ();
@@ -410,7 +1077,6 @@ namespace Mono.CSharp
                        // This is not required because Assembly.Load knows about this
                        // path.
                        //
-                       link_paths.Add (GetSystemDir ());
 
                        int argc = args.Length;
                        for (i = 0; i < argc; i++){
@@ -436,7 +1102,7 @@ namespace Mono.CSharp
                                        if (extra_args == null){
                                                Report.Error (2011, "Unable to open response file: " +
                                                              response_file);
-                                               return;
+                                               return false;
                                        }
 
                                        new_args = new string [extra_args.Length + argc];
@@ -447,218 +1113,25 @@ namespace Mono.CSharp
                                        continue;
                                }
 
-                               //
-                               // Prepare to recurse
-                               //
-                               
-                               if (parsing_options && (arg.StartsWith ("-") || arg.StartsWith ("/"))){
-                                       switch (arg){
-                                       case "-v":
-                                               yacc_verbose = true;
-                                               continue;
-
-                                       case "--":
+                               if (parsing_options){
+                                       if (arg == "--"){
                                                parsing_options = false;
                                                continue;
-
-                                       case "--parse":
-                                               parse_only = true;
-                                               continue;
-
-                                       case "--main": case "-m":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
-                                               }
-                                               RootContext.MainClass = args [++i];
-                                               continue;
-
-                                       case "--unsafe":
-                                               RootContext.Unsafe = true;
-                                               continue;
-                                               
-                                       case "--optimize":
-                                               RootContext.Optimize = true;
-                                               continue;
-
-                                       case "/?": case "/h": case "/help":
-                                       case "--help":
-                                               Usage (false);
-                                               return;
-
-                                       case "--define":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
-                                               }
-                                               defines.Add (args [++i]);
-                                               continue;
-                                               
-                                       case "--probe": {
-                                               int code = 0;
-
-                                               try {
-                                                       code = Int32.Parse (
-                                                               args [++i], NumberStyles.AllowLeadingSign);
-                                                       Report.SetProbe (code);
-                                               } catch {
-                                                       Report.Error (-14, "Invalid number specified");
-                                               } 
-                                               continue;
-                                       }
-
-                                       case "--tokenize": {
-                                               tokenize = true;
-                                               continue;
                                        }
                                        
-                                       case "-o": 
-                                       case "--output":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
+                                       if (arg.StartsWith ("-")){
+                                               if (UnixParseOption (arg, ref args, ref i))
+                                                       continue;
+
+                                               // Try a -CSCOPTION
+                                               string csc_opt = "/" + arg.Substring (1);
+                                               if (CSCParseOption (csc_opt, ref args, ref i))
+                                                       continue;
+                                       } else {
+                                               if (arg.StartsWith ("/")){
+                                                       if (CSCParseOption (arg, ref args, ref i))
+                                                               continue;
                                                }
-                                               output_file = args [++i];
-                                               string bname = CodeGen.Basename (output_file);
-                                               if (bname.IndexOf (".") == -1)
-                                                       output_file += ".exe";
-                                               continue;
-
-                                       case "--checked":
-                                               RootContext.Checked = true;
-                                               continue;
-
-                                       case "--stacktrace":
-                                               Report.Stacktrace = true;
-                                               continue;
-
-                                       case "--resource":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       Console.WriteLine("Missing argument to --resource"); 
-                                                       return;
-                                               }
-                                               if (resources == null)
-                                                       resources = new ArrayList ();
-                                               
-                                               resources.Add (args [++i]);
-                                               continue;
-                                                       
-                                       case "--target":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
-                                               }
-
-                                               string type = args [++i];
-                                               switch (type){
-                                               case "library":
-                                                       target = Target.Library;
-                                                       target_ext = ".dll";
-                                                       break;
-                                                       
-                                               case "exe":
-                                                       target = Target.Exe;
-                                                       break;
-                                                       
-                                               case "winexe":
-                                                       target = Target.WinExe;
-                                                       break;
-                                                       
-                                               case "module":
-                                                       target = Target.Module;
-                                                       target_ext = ".dll";
-                                                       break;
-                                               }
-                                               continue;
-
-                                       case "-r":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
-                                               }
-                                               
-                                               references.Add (args [++i]);
-                                               continue;
-                                               
-                                       case "-L":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
-                                               }
-                                               link_paths.Add (args [++i]);
-                                               continue;
-                                               
-                                       case "--nostdlib":
-                                               RootContext.StdLib = false;
-                                               continue;
-                                               
-                                       case "--fatal":
-                                               Report.Fatal = true;
-                                               continue;
-
-                                       case "--werror":
-                                               Report.WarningsAreErrors = true;
-                                               continue;
-
-                                       case "--nowarn":
-                                               if ((i + 1) >= argc){
-                                                       Usage (true);
-                                                       return;
-                                               }
-                                               int warn;
-                                               
-                                               try {
-                                                       warn = Int32.Parse (args [++i]);
-                                               } catch {
-                                                       Usage (true);
-                                                       return;
-                                               }
-                                               Report.SetIgnoreWarning (warn);
-                                               continue;
-
-                                       case "--wlevel":
-                                               if ((i + 1) >= argc){
-                                                       Report.Error (
-                                                               1900,
-                                                               "--wlevel requires an value from 0 to 4");
-                                                       error_count++;
-                                                       return;
-                                               }
-                                               int level;
-                                               
-                                               try {
-                                                       level = Int32.Parse (args [++i]);
-                                               } catch {
-                                                       Report.Error (
-                                                               1900,
-                                                               "--wlevel requires an value from 0 to 4");
-                                                       return;
-                                               }
-                                               if (level < 0 || level > 4){
-                                                       Report.Error (1900, "Warning level must be 0 to 4");
-                                                       return;
-                                               } else
-                                                       RootContext.WarningLevel = level;
-                                               continue;
-                                               
-                                       case "--about":
-                                               About ();
-                                               return;
-
-                                       case "--recurse":
-                                               if ((i + 1) >= argc){
-                                                       Console.WriteLine ("--recurse requires an argument");
-                                                       error_count++;
-                                                       return;
-                                               }
-                                               errors += CompileFiles (args [++i], true);
-                                               continue;
-                                               
-                                       case "--timestamp":
-                                               timestamps = true;
-                                               last_time = DateTime.Now;
-                                               continue;
                                        }
                                }
 
@@ -666,30 +1139,30 @@ namespace Mono.CSharp
                        }
 
                        if (tokenize)
-                               return;
+                               return true;
                        
                        if (first_source == null){
                                Report.Error (2008, "No files to compile were specified");
-                               return;
+                               return false;
                        }
 
                        if (Report.Errors > 0)
-                               return;
+                               return false;
                        
                        if (parse_only)
-                               return;
+                               return true;
                        
                        //
                        // Load Core Library for default compilation
                        //
-                       if (RootContext.StdLib){
+                       if (RootContext.StdLib)
                                references.Insert (0, "mscorlib");
-                               references.Insert (1, "System");
-                       }
 
-                       if (errors > 0){
-                               error ("Parsing failed");
-                               return;
+                       if (load_default_config)
+                               DefineDefaultConfig ();
+
+                       if (Report.Errors > 0){
+                               return false;
                        }
 
                        //
@@ -697,17 +1170,16 @@ namespace Mono.CSharp
                        //
                        if (timestamps)
                                ShowTime ("Loading references");
-                       errors += LoadReferences ();
+                       link_paths.Add (GetSystemDir ());
+                       LoadReferences ();
+                       
                        if (timestamps)
                                ShowTime ("   References loaded");
                        
-                       if (errors > 0){
-                               error ("Could not load one or more assemblies");
-                               return;
+                       if (Report.Errors > 0){
+                               return false;
                        }
 
-                       error_count = errors;
-
                        //
                        // Quick hack
                        //
@@ -720,9 +1192,11 @@ namespace Mono.CSharp
                                        output_file = first_source + target_ext;
                        }
 
-                       RootContext.CodeGen = new CodeGen (output_file, output_file);
+                       string[] debug_args = new string [debug_arglist.Count];
+                       debug_arglist.CopyTo (debug_args);
+                       CodeGen.Init (output_file, output_file, want_debugging_support, debug_args);
 
-                       RootContext.TypeManager.AddModule (RootContext.CodeGen.ModuleBuilder);
+                       TypeManager.AddModule (CodeGen.ModuleBuilder);
 
                        //
                        // Before emitting, we need to get the core
@@ -731,7 +1205,13 @@ namespace Mono.CSharp
                        //
                        if (timestamps)
                                ShowTime ("Initializing Core Types");
-                       RootContext.TypeManager.InitCoreTypes ();
+                       if (!RootContext.StdLib){
+                               RootContext.ResolveCore ();
+                               if (Report.Errors > 0)
+                                       return false;
+                       }
+                       
+                       TypeManager.InitCoreTypes ();
                        if (timestamps)
                                ShowTime ("   Core Types done");
 
@@ -743,11 +1223,15 @@ namespace Mono.CSharp
                        RootContext.ResolveTree ();
                        if (timestamps)
                                ShowTime ("Populate tree");
+                       if (!RootContext.StdLib)
+                               RootContext.BootCorlib_PopulateCoreTypes ();
                        RootContext.PopulateTypes ();
+                       RootContext.DefineTypes ();
                        
+                       TypeManager.InitCodeHelpers ();
+                               
                        if (Report.Errors > 0){
-                               error ("Compilation failed");
-                               return;
+                               return false;
                        }
                        
                        //
@@ -755,17 +1239,21 @@ namespace Mono.CSharp
                        //
                        if (timestamps)
                                ShowTime ("Emitting code");
+                       ShowTotalTime ("Total so far");
                        RootContext.EmitCode ();
                        if (timestamps)
                                ShowTime ("   done");
 
                        if (Report.Errors > 0){
-                               error ("Compilation failed");
-                               return;
+                               return false;
                        }
 
                        if (timestamps)
                                ShowTime ("Closing types");
+
+                       if (RootContext.WarningLevel >= 4)
+                               if (!Namespace.VerifyUsing ())
+                                       return false;
                        
                        RootContext.CloseTypes ();
 
@@ -782,33 +1270,77 @@ namespace Mono.CSharp
                                MethodInfo ep = RootContext.EntryPoint;
 
                                if (ep == null){
-                                       Report.Error (5001, "Program " + output_file +
+                                       if (Report.Errors == 0)
+                                               Report.Error (5001, "Program " + output_file +
                                                              " does not have an entry point defined");
-                                       return;
+                                       return false;
                                }
                                
-                               RootContext.CodeGen.AssemblyBuilder.SetEntryPoint (ep, k);
+                               CodeGen.AssemblyBuilder.SetEntryPoint (ep, k);
                        }
 
                        //
                        // Add the resources
                        //
                        if (resources != null){
-                               foreach (string file in resources)
-                                       RootContext.CodeGen.AssemblyBuilder.AddResourceFile (file, file);
+                               foreach (string spec in resources){
+                                       string file, res;
+                                       int cp;
+                                       
+                                       cp = spec.IndexOf (',');
+                                       if (cp != -1){
+                                               file = spec.Substring (0, cp);
+                                               res = spec.Substring (cp + 1);
+                                       } else
+                                               file = res = spec;
+
+                                       CodeGen.AssemblyBuilder.AddResourceFile (res, file);
+                               }
                        }
                        
-                       RootContext.CodeGen.Save (output_file);
-                       if (timestamps)
+                       if (embedded_resources != null){
+                               object[] margs = new object [2];
+                               Type[] argst = new Type [2];
+                               argst [0] = argst [1] = typeof (string);
+                               MethodInfo embed_res = typeof (AssemblyBuilder).GetMethod ("EmbedResourceFile", argst);
+                               if (embed_res == null) {
+                                       Report.Warning (0, new Location (-1), "Cannot embed resources on this runtime: try the Mono runtime instead.");
+                               } else {
+                                       foreach (string spec in embedded_resources) {
+                                               int cp;
+
+                                               cp = spec.IndexOf (',');
+                                               if (cp != -1){
+                                                       margs [0] = spec.Substring (cp + 1);
+                                                       margs [1] = spec.Substring (0, cp);
+                                               } else
+                                                       margs [0] = margs [1] = spec;
+                                               embed_res.Invoke (CodeGen.AssemblyBuilder, margs);
+                                       }
+                               }
+                       }
+                       
+                       CodeGen.Save (output_file);
+                       if (timestamps) {
                                ShowTime ("Saved output");
+                               ShowTotalTime ("Total");
+                       }
 
-                       if (Report.Errors > 0){
-                               error ("Compilation failed");
-                               return;
-                       } else if (Report.ProbeCode != 0){
-                               error ("Failed to report code " + Report.ProbeCode);
-                               Environment.Exit (124);
+                       Timer.ShowTimers ();
+                       
+                       if (want_debugging_support) {
+                               CodeGen.SaveSymbols ();
+                               if (timestamps)
+                                       ShowTime ("Saved symbols");
                        }
+
+                       if (Report.ExpectedError != 0){
+                               Console.WriteLine("Failed to report expected error " + Report.ExpectedError);
+                               Environment.Exit (1);
+                               return false;
+                       }
+
+                       return (Report.Errors == 0);
                }
 
        }