2 // driver.cs: The compiler command line driver.
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections;
21 /// The compiler driver.
26 Library, Exe, Module, WinExe
30 // Assemblies references to be linked. Initialized with
32 static ArrayList references;
35 static ArrayList link_paths;
37 static bool yacc_verbose = false;
39 static int error_count = 0;
41 static string first_source;
43 static Target target = Target.Exe;
44 static string target_ext = ".exe";
46 static bool parse_only = false;
48 static int parse (string input_file)
51 System.IO.Stream input;
55 input = System.IO.File.OpenRead (input_file);
57 Report.Error (2001, "Source file '" + input_file + "' could not be opened");
61 parser = new CSharpParser (input_file, input);
62 parser.yacc_verbose = yacc_verbose;
64 errors = parser.parse ();
\r
65 } catch (Exception ex) {
66 Console.WriteLine (ex);
67 Console.WriteLine ("Compilation aborted");
74 static void Usage (bool is_error)
77 "Mono C# compiler, (C) 2001 Ximian, Inc.\n" +
78 "mcs [options] source-files\n" +
79 " --about About the Mono C# compiler\n" +
80 " --checked Set default context to checked\n" +
81 " --fatal Makes errors fatal\n" +
82 " -L PATH Adds PATH to the assembly link path\n" +
83 " --nostdlib Does not load core libraries\n" +
84 " --nowarn XXX Ignores warning number XXX\n" +
85 " -o FNAME Specifies output file\n" +
86 " --optimize Optimizes\n" +
87 " --parse Only parses the source file\n" +
88 " --probe X L Probes for the source to generate code X on line L\n" +
89 " --target KIND Specifies the target (KIND is one of: exe, winexe, " +
90 "library, module)\n" +
91 " --unsafe Allows unsafe code\n" +
92 " --werror Treat warnings as errors\n" +
93 " --wlevel LEVEL Sets warning level (the highest is 4, the default)\n" +
94 " -r References an assembly\n" +
95 " -v Verbose parsing (for debugging the parser)\n");
103 "The Mono C# compiler is (C) 2001 Ximian, Inc.\n\n" +
104 "The compiler source code is released under the terms of the GNU GPL\n\n" +
106 "For more information on Mono, visit the project Web site\n" +
107 " http://www.go-mono.com\n\n" +
109 "The compiler was written by Miguel de Icaza and Ravi Pratap");
112 static void error (string msg)
114 Console.WriteLine ("Error: " + msg);
117 static void notice (string msg)
119 Console.WriteLine (msg);
122 public static int Main (string[] args)
129 static public int LoadAssembly (string assembly)
132 string total_log = "";
135 a = Assembly.Load (assembly);
136 RootContext.TypeManager.AddAssembly (a);
138 } catch (FileNotFoundException){
139 foreach (string dir in link_paths){
140 string full_path = dir + "/" + assembly + ".dll";
143 a = Assembly.LoadFrom (full_path);
144 RootContext.TypeManager.AddAssembly (a);
146 } catch (FileNotFoundException ff) {
147 total_log += ff.FusionLog;
151 } catch (BadImageFormatException f) {
152 error ("// Bad file format while loading assembly");
153 error ("Log: " + f.FusionLog);
155 } catch (FileLoadException f){
156 error ("// File Load Exception: ");
157 error ("Log: " + f.FusionLog);
159 } catch (ArgumentNullException){
160 error ("// Argument Null exception ");
164 Report.Error (6, "Can not find assembly `" + assembly + "'" );
165 Console.WriteLine ("Log: \n" + total_log);
171 /// Loads all assemblies referenced on the command line
173 static public int LoadReferences ()
177 foreach (string r in references){
178 errors += LoadAssembly (r);
185 /// Parses the arguments, and drives the compilation
190 /// TODO: Mostly structured to debug the compiler
191 /// now, needs to be turned into a real driver soon.
193 static void MainDriver (string [] args)
196 string output_file = null;
198 references = new ArrayList ();
199 link_paths = new ArrayList ();
204 // This is not required because Assembly.Load knows about this
207 link_paths.Add ("file:///C:/WINNT/Microsoft.NET/Framework/v1.0.2914");
209 int argc = args.Length;
210 for (i = 0; i < argc; i++){
211 string arg = args [i];
213 if (arg.StartsWith ("-")){
223 case "--main": case "-m":
224 if ((i + 1) >= argc){
228 RootContext.MainClass = args [++i];
232 RootContext.Unsafe = true;
236 RootContext.Optimize = true;
246 code = Int32.Parse (args [++i], 0);
247 line = Int32.Parse (args [++i], 0);
248 Report.SetProbe (code, line);
252 case "-o": case "--output":
253 if ((i + 1) >= argc){
257 output_file = args [++i];
261 RootContext.Checked = true;
265 if ((i + 1) >= argc){
270 string type = args [++i];
273 target = Target.Library;
282 target = Target.WinExe;
286 target = Target.Module;
293 if ((i + 1) >= argc){
298 references.Add (args [++i]);
302 if ((i + 1) >= argc){
306 link_paths.Add (args [++i]);
310 RootContext.StdLib = false;
318 Report.WarningsAreErrors = true;
322 if ((i + 1) >= argc){
329 warn = Int32.Parse (args [++i]);
334 Report.SetIgnoreWarning (warn);
338 if ((i + 1) >= argc){
346 level = Int32.Parse (args [++i]);
351 if (level < 0 || level > 4){
352 Report.Error (1900, "Warning level must be 0 to 4");
355 RootContext.WarningLevel = level;
368 if (first_source == null)
371 string [] files = Directory.GetFiles (".", arg);
372 foreach (string f in files){
373 if (!f.ToLower ().EndsWith (".cs")){
374 error ("Do not know how to compile " + arg);
382 if (first_source == null){
383 Report.Error (2008, "No files to compile were specified");
387 if (Report.Errors > 0)
394 // Load Core Library for default compilation
396 if (RootContext.StdLib){
397 references.Insert (0, "mscorlib");
398 references.Insert (1, "System");
402 error ("Parsing failed");
407 // Load assemblies required
409 errors += LoadReferences ();
412 error ("Could not load one or more assemblies");
416 error_count = errors;
421 if (output_file == null){
422 int pos = first_source.LastIndexOf (".");
424 output_file = first_source.Substring (0, pos) + target_ext;
427 RootContext.CodeGen = new CodeGen (output_file, output_file);
430 // Before emitting, we need to get the core
431 // types emitted from the user defined types
432 // or from the system ones.
434 RootContext.TypeManager.InitCoreTypes ();
436 RootContext.TypeManager.AddModule (RootContext.CodeGen.ModuleBuilder);
439 // The second pass of the compiler
441 RootContext.ResolveTree ();
442 RootContext.PopulateTypes ();
444 if (Report.Errors > 0){
445 error ("Compilation failed");
450 // The code generator
452 RootContext.EmitCode ();
454 if (Report.Errors > 0){
455 error ("Compilation failed");
459 RootContext.CloseTypes ();
461 PEFileKinds k = PEFileKinds.ConsoleApplication;
463 if (target == Target.Library || target == Target.Module)
465 else if (target == Target.Exe)
466 k = PEFileKinds.ConsoleApplication;
467 else if (target == Target.WinExe)
468 k = PEFileKinds.WindowApplication;
470 if (target == Target.Exe || target == Target.WinExe){
471 MethodInfo ep = RootContext.EntryPoint;
474 Report.Error (5001, "Program " + output_file +
475 " does not have an entry point defined");
479 RootContext.CodeGen.AssemblyBuilder.SetEntryPoint (ep, k);
482 RootContext.CodeGen.Save (output_file);
484 if (Report.Errors > 0){
485 error ("Compilation failed");
487 } else if (Report.ProbeCode != 0){
488 error ("Failed to report code " + Report.ProbeCode);
489 Environment.Exit (124);