2 // driver.cs: The compiler command line driver.
\r
4 // Author: Rafael Teixeira (rafaelteixeirabr@hotmail.com)
\r
5 // Based on mcs by : Miguel de Icaza (miguel@gnu.org)
\r
7 // Licensed under the terms of the GNU GPL
\r
9 // (C) 2002 Rafael Teixeira
\r
12 namespace Mono.Languages
\r
15 using System.Reflection;
\r
16 using System.Reflection.Emit;
\r
17 using System.Collections;
\r
19 using System.Globalization;
\r
23 Library, Exe, Module, WinExe
\r
27 /// The compiler driver.
\r
32 // Assemblies references to be linked. Initialized with
\r
33 // mscorlib.dll elsewhere.
\r
34 static ArrayList references;
\r
37 static ArrayList link_paths;
\r
39 // Whether we want to only run the tokenizer
\r
40 static bool tokenize = false;
\r
42 static int error_count = 0;
\r
44 static string first_source;
\r
46 static Target target = Target.Exe;
\r
47 static string target_ext = ".exe";
\r
49 static bool parse_only = false;
\r
50 static bool timestamps = false;
\r
52 static Hashtable response_file_list;
\r
53 static Hashtable source_files = new Hashtable ();
\r
56 // An array of the defines from the command line
\r
58 static ArrayList defines;
\r
61 // A list of resource files
\r
63 static ArrayList resources;
\r
66 // Last time we took the time
\r
68 static DateTime last_time;
\r
69 static void ShowTime (string msg)
\r
71 DateTime now = DateTime.Now;
\r
72 TimeSpan span = now - last_time;
\r
76 "[{0:00}:{1:000}] {2}",
\r
77 span.Seconds, span.Milliseconds, msg);
\r
80 static void Usage (bool is_error)
\r
83 "MonoBASIC compiler, (C) 2001 Rafael Teixeira.\n" +
\r
84 "mcs [options] source-files\n" +
\r
85 " --about About the MonoBASIC compiler\n" +
\r
86 " --checked Set default context to checked\n" +
\r
87 " --define SYM Defines the symbol SYM\n" +
\r
88 " --fatal Makes errors fatal\n" +
\r
89 " -L PATH Adds PATH to the assembly link path\n" +
\r
90 " --nostdlib Does not load core libraries\n" +
\r
91 " --nowarn XXX Ignores warning number XXX\n" +
\r
92 " -o FNAME Specifies output file\n" +
\r
93 " --optimize Optimizes\n" +
\r
94 " --parse Only parses the source file\n" +
\r
95 " --probe X Probes for the source to generate code X on line L\n" +
\r
96 " --resource FILE Addds FILE as a resource\n" +
\r
97 " --stacktrace Shows stack trace at error location\n" +
\r
98 " --target KIND Specifies the target (KIND is one of: exe, winexe, " +
\r
99 "library, module)\n" +
\r
100 " --timestamp Displays time stamps of various compiler events\n" +
\r
101 " --unsafe Allows unsafe code\n" +
\r
102 " --werror Treat warnings as errors\n" +
\r
103 " --wlevel LEVEL Sets warning level (the highest is 4, the default)\n" +
\r
104 " -r References an assembly\n" +
\r
105 " -v Verbose parsing (for debugging the parser)\n" +
\r
106 " @file Read response file for more options");
\r
111 static void About ()
\r
113 Console.WriteLine (
\r
114 "The MonoBASIC compiler is (C) 2002 Rafael Teixeira\n\n" +
\r
115 "The compiler source code is released under the terms of the GNU GPL\n\n" +
\r
117 "For more information on Mono, visit the project Web site\n" +
\r
118 " http://www.go-mono.com\n\n" +
\r
120 "The compiler was written by Rafael Teixeira and Miguel de Icaza and Ravi Pratap");
\r
123 static void error (string msg)
\r
125 Console.WriteLine ("Error: " + msg);
\r
128 static void notice (string msg)
\r
130 Console.WriteLine (msg);
\r
133 public static int Main(string[] args)
\r
137 return (error_count + Report.Errors) != 0 ? 1 : 0;
\r
140 static public int LoadAssembly (string assembly)
\r
143 string total_log = "";
\r
146 a = Assembly.Load (assembly);
\r
147 RootContext.TypeManager.AddAssembly (a);
\r
149 } catch (FileNotFoundException){
\r
150 foreach (string dir in link_paths){
\r
151 string full_path = dir + "/" + assembly + ".dll";
\r
154 a = Assembly.LoadFrom (full_path);
\r
155 RootContext.TypeManager.AddAssembly (a);
\r
157 } catch (FileNotFoundException ff) {
\r
158 total_log += ff.FusionLog;
\r
162 } catch (BadImageFormatException f) {
\r
163 error ("// Bad file format while loading assembly");
\r
164 error ("Log: " + f.FusionLog);
\r
166 } catch (FileLoadException f){
\r
167 error ("// File Load Exception: ");
\r
168 error ("Log: " + f.FusionLog);
\r
170 } catch (ArgumentNullException){
\r
171 error ("// Argument Null exception ");
\r
175 Report.Error (6, "Can not find assembly `" + assembly + "'" );
\r
176 Console.WriteLine ("Log: \n" + total_log);
\r
182 /// Loads all assemblies referenced on the command line
\r
184 static public int LoadReferences ()
\r
188 foreach (string r in references){
\r
189 errors += LoadAssembly (r);
\r
196 static void SetupDefaultDefines ()
\r
198 defines = new ArrayList ();
\r
199 defines.Add ("__MonoBASIC__");
\r
202 static string [] LoadArgs (string file)
\r
205 ArrayList args = new ArrayList ();
\r
208 f = new StreamReader (file);
\r
213 while ((line = f.ReadLine ()) != null){
\r
214 string [] line_args = line.Split (new char [] { ' ' });
\r
216 foreach (string arg in line_args)
\r
220 string [] ret_value = new string [args.Count];
\r
221 args.CopyTo (ret_value, 0);
\r
227 // Returns the directory where the system assemblies are installed
\r
229 static string GetSystemDir ()
\r
231 Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
\r
232 foreach (Assembly a in assemblies){
\r
233 string codebase = a.CodeBase;
\r
234 if (codebase.EndsWith ("corlib.dll")){
\r
235 return codebase.Substring (0, codebase.LastIndexOf ("/"));
\r
239 Report.Error (-15, "Can not compute my system path");
\r
244 // Given a path specification, splits the path from the file/pattern
\r
246 static void SplitPathAndPattern (string spec, out string path, out string pattern)
\r
248 int p = spec.LastIndexOf ("/");
\r
251 // Windows does not like /file.cs, switch that to:
\r
256 pattern = spec.Substring (1);
\r
258 path = spec.Substring (0, p);
\r
259 pattern = spec.Substring (p + 1);
\r
264 p = spec.LastIndexOf ("\\");
\r
266 path = spec.Substring (0, p - 1);
\r
267 pattern = spec.Substring (p);
\r
275 static int ProcessFile (string f)
\r
277 if (source_files.Contains (f)){
\r
280 "Source file `" + f + "' specified multiple times");
\r
281 Environment.Exit (1);
\r
283 source_files.Add (f, f);
\r
286 GenericParser.Tokenize(f);
\r
288 return GenericParser.Parse(f);
\r
293 static void RecurseOn (string pattern)
\r
295 // FIXME: implement.
\r
299 /// Parses the arguments, and drives the compilation
\r
304 /// TODO: Mostly structured to debug the compiler
\r
305 /// now, needs to be turned into a real driver soon.
\r
307 static void MainDriver (string [] args)
\r
310 bool parsing_options = true;
\r
311 string output_file = null;
\r
313 references = new ArrayList ();
\r
314 link_paths = new ArrayList ();
\r
316 SetupDefaultDefines ();
\r
317 link_paths.Add (GetSystemDir ());
\r
319 int argc = args.Length;
\r
320 for (i = 0; i < argc; i++)
\r
322 string arg = args [i];
\r
324 if (arg.StartsWith ("@"))
\r
326 string [] new_args, extra_args;
\r
327 string response_file = arg.Substring (1);
\r
329 if (response_file_list == null)
\r
330 response_file_list = new Hashtable ();
\r
332 if (response_file_list.Contains (response_file)){
\r
334 1515, "Response file `" + response_file +
\r
335 "' specified multiple times");
\r
336 Environment.Exit (1);
\r
339 response_file_list.Add (response_file, response_file);
\r
341 extra_args = LoadArgs (response_file);
\r
342 if (extra_args == null){
\r
343 Report.Error (2011, "Unable to open response file: " +
\r
348 new_args = new string [extra_args.Length + argc];
\r
349 args.CopyTo (new_args, 0);
\r
350 extra_args.CopyTo (new_args, argc);
\r
352 argc = new_args.Length;
\r
359 // Prepare to recurse
\r
362 if (parsing_options && (arg.StartsWith ("-") || arg.StartsWith ("/")))
\r
366 parsing_options = false;
\r
370 GenericParser.yacc_verbose_flag = true;
\r
377 case "--main": case "-m":
\r
378 if ((i + 1) >= argc){
\r
382 RootContext.MainClass = args [++i];
\r
386 RootContext.Unsafe = true;
\r
390 RootContext.Optimize = true;
\r
401 if ((i + 1) >= argc)
\r
406 defines.Add (args [++i]);
\r
412 code = Int32.Parse (
\r
413 args [++i], NumberStyles.AllowLeadingSign);
\r
414 Report.SetProbe (code);
\r
416 Report.Error (-14, "Invalid number specified");
\r
421 case "--tokenize": {
\r
428 if ((i + 1) >= argc){
\r
432 output_file = args [++i];
\r
436 RootContext.Checked = true;
\r
439 case "--stacktrace":
\r
440 Report.Stacktrace = true;
\r
444 if ((i + 1) >= argc){
\r
446 Console.WriteLine("Missing argument to --resource");
\r
449 if (resources == null)
\r
450 resources = new ArrayList ();
\r
452 resources.Add (args [++i]);
\r
456 if ((i + 1) >= argc){
\r
461 string type = args [++i];
\r
464 target = Target.Library;
\r
465 target_ext = ".dll";
\r
469 target = Target.Exe;
\r
473 target = Target.WinExe;
\r
477 target = Target.Module;
\r
478 target_ext = ".dll";
\r
484 if ((i + 1) >= argc){
\r
489 references.Add (args [++i]);
\r
493 if ((i + 1) >= argc){
\r
497 link_paths.Add (args [++i]);
\r
501 RootContext.StdLib = false;
\r
505 Report.Fatal = true;
\r
509 Report.WarningsAreErrors = true;
\r
513 if ((i + 1) >= argc){
\r
520 warn = Int32.Parse (args [++i]);
\r
525 Report.SetIgnoreWarning (warn);
\r
529 if ((i + 1) >= argc){
\r
537 level = Int32.Parse (args [++i]);
\r
542 if (level < 0 || level > 4){
\r
543 Report.Error (1900, "Warning level must be 0 to 4");
\r
546 RootContext.WarningLevel = level;
\r
554 if ((i + 1) >= argc){
\r
559 RecurseOn (args [++i]);
\r
562 case "--timestamp":
\r
564 last_time = DateTime.Now;
\r
572 } catch (System.IndexOutOfRangeException){
\r
576 } catch (System.FormatException) {
\r
582 if (first_source == null)
\r
583 first_source = arg;
\r
585 string path, pattern;
\r
586 SplitPathAndPattern (arg, out path, out pattern);
\r
587 string [] files = null;
\r
589 files = Directory.GetFiles (path, pattern);
\r
590 } catch (System.IO.DirectoryNotFoundException) {
\r
591 Report.Error (2001, "Source file `" + arg + "' could not be found");
\r
594 foreach (string f in files)
\r
595 errors += ProcessFile (f);
\r
602 if (first_source == null){
\r
603 Report.Error (2008, "No files to compile were specified");
\r
607 if (Report.Errors > 0)
\r
614 // Load Core Library for default compilation
\r
616 if (RootContext.StdLib){
\r
617 references.Insert (0, "mscorlib");
\r
618 references.Insert (1, "System");
\r
622 error ("Parsing failed");
\r
627 // Load assemblies required
\r
630 ShowTime ("Loading references");
\r
631 errors += LoadReferences ();
\r
633 ShowTime (" References loaded");
\r
636 error ("Could not load one or more assemblies");
\r
640 error_count = errors;
\r
645 if (output_file == null){
\r
646 int pos = first_source.LastIndexOf (".");
\r
649 output_file = first_source.Substring (0, pos) + target_ext;
\r
651 output_file = first_source + target_ext;
\r
654 RootContext.CodeGen = new CodeGen (output_file, output_file);
\r
657 // Before emitting, we need to get the core
\r
658 // types emitted from the user defined types
\r
659 // or from the system ones.
\r
662 ShowTime ("Initializing Core Types");
\r
663 RootContext.TypeManager.InitCoreTypes ();
\r
665 ShowTime (" Core Types done");
\r
667 RootContext.TypeManager.AddModule (RootContext.CodeGen.ModuleBuilder);
\r
670 // The second pass of the compiler
\r
673 ShowTime ("Resolving tree");
\r
674 RootContext.ResolveTree ();
\r
676 ShowTime ("Populate tree");
\r
678 if (Report.Errors > 0){
\r
679 error ("Compilation failed");
\r
683 RootContext.PopulateTypes ();
\r
685 if (Report.Errors > 0){
\r
686 error ("Compilation failed");
\r
691 // The code generator
\r
694 ShowTime ("Emitting code");
\r
695 RootContext.EmitCode ();
\r
697 ShowTime (" done");
\r
699 if (Report.Errors > 0){
\r
700 error ("Compilation failed");
\r
705 ShowTime ("Closing types");
\r
707 RootContext.CloseTypes ();
\r
709 PEFileKinds k = PEFileKinds.ConsoleApplication;
\r
711 if (target == Target.Library || target == Target.Module)
\r
712 k = PEFileKinds.Dll;
\r
713 else if (target == Target.Exe)
\r
714 k = PEFileKinds.ConsoleApplication;
\r
715 else if (target == Target.WinExe)
\r
716 k = PEFileKinds.WindowApplication;
\r
718 if (target == Target.Exe || target == Target.WinExe){
\r
719 MethodInfo ep = RootContext.EntryPoint;
\r
722 Report.Error (5001, "Program " + output_file +
\r
723 " does not have an entry point defined");
\r
727 RootContext.CodeGen.AssemblyBuilder.SetEntryPoint (ep, k);
\r
731 // Add the resources
\r
733 if (resources != null)
\r
735 foreach (string file in resources)
\r
736 RootContext.CodeGen.AssemblyBuilder.AddResourceFile (file, file);
\r
739 RootContext.CodeGen.Save (output_file);
\r
741 ShowTime ("Saved output");
\r
743 if (Report.Errors > 0){
\r
744 error ("Compilation failed");
\r
746 } else if (Report.ProbeCode != 0){
\r
747 error ("Failed to report code " + Report.ProbeCode);
\r
748 Environment.Exit (124);
\r