Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / mcs / driver.cs
1 //
2 // driver.cs: The compiler command line driver.
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@gnu.org)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 //
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004, 2005, 2006, 2007, 2008 Novell, Inc
12 // Copyright 2011 Xamarin Inc
13 //
14
15 using System;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Text;
21 using System.Globalization;
22 using System.Diagnostics;
23 using System.Threading;
24
25 namespace Mono.CSharp
26 {
27         /// <summary>
28         ///    The compiler driver.
29         /// </summary>
30         class Driver
31         {
32                 readonly CompilerContext ctx;
33
34                 public Driver (CompilerContext ctx)
35                 {
36                         this.ctx = ctx;
37                 }
38
39                 Report Report {
40                         get {
41                                 return ctx.Report;
42                         }
43                 }
44
45                 void tokenize_file (SourceFile sourceFile, ModuleContainer module, ParserSession session)
46                 {
47                         Stream input;
48
49                         try {
50                                 input = File.OpenRead (sourceFile.Name);
51                         } catch {
52                                 Report.Error (2001, "Source file `" + sourceFile.Name + "' could not be found");
53                                 return;
54                         }
55
56                         using (input){
57                                 SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding);
58                                 var file = new CompilationSourceFile (module, sourceFile);
59
60                                 Tokenizer lexer = new Tokenizer (reader, file, session, ctx.Report);
61                                 int token, tokens = 0, errors = 0;
62
63                                 while ((token = lexer.token ()) != Token.EOF){
64                                         tokens++;
65                                         if (token == Token.ERROR)
66                                                 errors++;
67                                 }
68                                 Console.WriteLine ("Tokenized: " + tokens + " found " + errors + " errors");
69                         }
70                         
71                         return;
72                 }
73
74                 void Parse (ModuleContainer module)
75                 {
76                         bool tokenize_only = module.Compiler.Settings.TokenizeOnly;
77                         var sources = module.Compiler.SourceFiles;
78
79                         Location.Initialize (sources);
80
81                         var session = new ParserSession {
82                                 UseJayGlobalArrays = true,
83                                 LocatedTokens = new LocatedToken[15000]
84                         };
85
86                         for (int i = 0; i < sources.Count; ++i) {
87                                 if (tokenize_only) {
88                                         tokenize_file (sources[i], module, session);
89                                 } else {
90                                         Parse (sources[i], module, session, Report);
91                                 }
92                         }
93                 }
94
95 #if false
96                 void ParseParallel (ModuleContainer module)
97                 {
98                         var sources = module.Compiler.SourceFiles;
99
100                         Location.Initialize (sources);
101
102                         var pcount = Environment.ProcessorCount;
103                         var threads = new Thread[System.Math.Max (2, pcount - 1)];
104
105                         for (int i = 0; i < threads.Length; ++i) {
106                                 var t = new Thread (l => {
107                                         var session = new ParserSession () {
108                                                 //UseJayGlobalArrays = true,
109                                         };
110
111                                         var report = new Report (ctx, Report.Printer); // TODO: Implement flush at once printer
112
113                                         for (int ii = (int) l; ii < sources.Count; ii += threads.Length) {
114                                                 Parse (sources[ii], module, session, report);
115                                         }
116
117                                         // TODO: Merge warning regions
118                                 });
119
120                                 t.Start (i);
121                                 threads[i] = t;
122                         }
123
124                         for (int t = 0; t < threads.Length; ++t) {
125                                 threads[t].Join ();
126                         }
127                 }
128 #endif
129
130                 public void Parse (SourceFile file, ModuleContainer module, ParserSession session, Report report)
131                 {
132                         Stream input;
133
134                         try {
135                                 input = File.OpenRead (file.Name);
136                         } catch {
137                                 report.Error (2001, "Source file `{0}' could not be found", file.Name);
138                                 return;
139                         }
140
141                         // Check 'MZ' header
142                         if (input.ReadByte () == 77 && input.ReadByte () == 90) {
143
144                                 report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name);
145                                 input.Close ();
146                                 return;
147                         }
148
149                         input.Position = 0;
150                         SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding, session.StreamReaderBuffer);
151
152                         Parse (reader, file, module, session, report);
153
154                         if (ctx.Settings.GenerateDebugInfo && report.Errors == 0 && !file.HasChecksum) {
155                                 input.Position = 0;
156                                 var checksum = session.GetChecksumAlgorithm ();
157                                 file.SetChecksum (checksum.ComputeHash (input));
158                         }
159
160                         reader.Dispose ();
161                         input.Close ();
162                 }
163
164                 public static void Parse (SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module, ParserSession session, Report report)
165                 {
166                         var file = new CompilationSourceFile (module, sourceFile);
167                         module.AddTypeContainer (file);
168
169                         CSharpParser parser = new CSharpParser (reader, file, report, session);
170                         parser.parse ();
171                 }
172                 
173                 public static int Main (string[] args)
174                 {
175                         Location.InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
176
177                         CommandLineParser cmd = new CommandLineParser (Console.Out);
178                         var settings = cmd.ParseArguments (args);
179                         if (settings == null)
180                                 return 1;
181
182                         if (cmd.HasBeenStopped)
183                                 return 0;
184
185                         Driver d = new Driver (new CompilerContext (settings, new ConsoleReportPrinter ()));
186
187                         if (d.Compile () && d.Report.Errors == 0) {
188                                 if (d.Report.Warnings > 0) {
189                                         Console.WriteLine ("Compilation succeeded - {0} warning(s)", d.Report.Warnings);
190                                 }
191                                 Environment.Exit (0);
192                                 return 0;
193                         }
194                         
195                         
196                         Console.WriteLine("Compilation failed: {0} error(s), {1} warnings",
197                                 d.Report.Errors, d.Report.Warnings);
198                         Environment.Exit (1);
199                         return 1;
200                 }
201
202                 public static string GetPackageFlags (string packages, Report report)
203                 {
204                         ProcessStartInfo pi = new ProcessStartInfo ();
205                         pi.FileName = "pkg-config";
206                         pi.RedirectStandardOutput = true;
207                         pi.UseShellExecute = false;
208                         pi.Arguments = "--libs " + packages;
209                         Process p = null;
210                         try {
211                                 p = Process.Start (pi);
212                         } catch (Exception e) {
213                                 if (report == null)
214                                         throw;
215
216                                 report.Error (-27, "Couldn't run pkg-config: " + e.Message);
217                                 return null;
218                         }
219                         
220                         if (p.StandardOutput == null) {
221                                 if (report == null)
222                                         throw new ApplicationException ("Specified package did not return any information");
223
224                                 report.Warning (-27, 1, "Specified package did not return any information");
225                                 p.Close ();
226                                 return null;
227                         }
228
229                         string pkgout = p.StandardOutput.ReadToEnd ();
230                         p.WaitForExit ();
231                         if (p.ExitCode != 0) {
232                                 if (report == null)
233                                         throw new ApplicationException (pkgout);
234
235                                 report.Error (-27, "Error running pkg-config. Check the above output.");
236                                 p.Close ();
237                                 return null;
238                         }
239
240                         p.Close ();
241                         return pkgout;
242                 }
243
244                 //
245                 // Main compilation method
246                 //
247                 public bool Compile ()
248                 {
249                         var settings = ctx.Settings;
250
251                         //
252                         // If we are an exe, require a source file for the entry point or
253                         // if there is nothing to put in the assembly, and we are not a library
254                         //
255                         if (settings.FirstSourceFile == null &&
256                                 ((settings.Target == Target.Exe || settings.Target == Target.WinExe || settings.Target == Target.Module) ||
257                                 settings.Resources == null)) {
258                                 Report.Error (2008, "No files to compile were specified");
259                                 return false;
260                         }
261
262                         if (settings.Platform == Platform.AnyCPU32Preferred && (settings.Target == Target.Library || settings.Target == Target.Module)) {
263                                 Report.Error (4023, "Platform option `anycpu32bitpreferred' is valid only for executables");
264                                 return false;
265                         }
266
267                         TimeReporter tr = new TimeReporter (settings.Timestamps);
268                         ctx.TimeReporter = tr;
269                         tr.StartTotal ();
270
271                         var module = new ModuleContainer (ctx);
272                         RootContext.ToplevelTypes = module;
273
274                         tr.Start (TimeReporter.TimerType.ParseTotal);
275                         Parse (module);
276                         tr.Stop (TimeReporter.TimerType.ParseTotal);
277
278                         if (Report.Errors > 0)
279                                 return false;
280
281                         if (settings.TokenizeOnly || settings.ParseOnly) {
282                                 tr.StopTotal ();
283                                 tr.ShowStats ();
284                                 return true;
285                         }
286
287                         var output_file = settings.OutputFile;
288                         string output_file_name;
289                         if (output_file == null) {
290                                 var source_file = settings.FirstSourceFile;
291
292                                 if (source_file == null) {
293                                         Report.Error (1562, "If no source files are specified you must specify the output file with -out:");
294                                         return false;
295                                 }
296
297                                 output_file_name = source_file.Name;
298                                 int pos = output_file_name.LastIndexOf ('.');
299
300                                 if (pos > 0)
301                                         output_file_name = output_file_name.Substring (0, pos);
302                                 
303                                 output_file_name += settings.TargetExt;
304                                 output_file = output_file_name;
305                         } else {
306                                 output_file_name = Path.GetFileName (output_file);
307
308                                 if (string.IsNullOrEmpty (Path.GetFileNameWithoutExtension (output_file_name)) ||
309                                         output_file_name.IndexOfAny (Path.GetInvalidFileNameChars ()) >= 0) {
310                                         Report.Error (2021, "Output file name is not valid");
311                                         return false;
312                                 }
313                         }
314
315 #if STATIC
316                         var importer = new StaticImporter (module);
317                         var references_loader = new StaticLoader (importer, ctx);
318
319                         tr.Start (TimeReporter.TimerType.AssemblyBuilderSetup);
320                         var assembly = new AssemblyDefinitionStatic (module, references_loader, output_file_name, output_file);
321                         assembly.Create (references_loader.Domain);
322                         tr.Stop (TimeReporter.TimerType.AssemblyBuilderSetup);
323
324                         // Create compiler types first even before any referenced
325                         // assembly is loaded to allow forward referenced types from
326                         // loaded assembly into compiled builder to be resolved
327                         // correctly
328                         tr.Start (TimeReporter.TimerType.CreateTypeTotal);
329                         module.CreateContainer ();
330                         importer.AddCompiledAssembly (assembly);
331                         tr.Stop (TimeReporter.TimerType.CreateTypeTotal);
332
333                         references_loader.LoadReferences (module);
334
335                         tr.Start (TimeReporter.TimerType.PredefinedTypesInit);
336                         if (!ctx.BuiltinTypes.CheckDefinitions (module))
337                                 return false;
338
339                         tr.Stop (TimeReporter.TimerType.PredefinedTypesInit);
340
341                         references_loader.LoadModules (assembly, module.GlobalRootNamespace);
342 #else
343                         var assembly = new AssemblyDefinitionDynamic (module, output_file_name, output_file);
344                         module.SetDeclaringAssembly (assembly);
345
346                         var importer = new ReflectionImporter (module, ctx.BuiltinTypes);
347                         assembly.Importer = importer;
348
349                         var loader = new DynamicLoader (importer, ctx);
350                         loader.LoadReferences (module);
351
352                         if (!ctx.BuiltinTypes.CheckDefinitions (module))
353                                 return false;
354
355                         if (!assembly.Create (AppDomain.CurrentDomain, AssemblyBuilderAccess.Save))
356                                 return false;
357
358                         module.CreateContainer ();
359
360                         loader.LoadModules (assembly, module.GlobalRootNamespace);
361 #endif
362                         module.InitializePredefinedTypes ();
363
364                         tr.Start (TimeReporter.TimerType.ModuleDefinitionTotal);
365                         module.Define ();
366                         tr.Stop (TimeReporter.TimerType.ModuleDefinitionTotal);
367
368                         if (Report.Errors > 0)
369                                 return false;
370
371                         if (settings.DocumentationFile != null) {
372                                 var doc = new DocumentationBuilder (module);
373                                 doc.OutputDocComment (output_file, settings.DocumentationFile);
374                         }
375
376                         assembly.Resolve ();
377                         
378                         if (Report.Errors > 0)
379                                 return false;
380
381
382                         tr.Start (TimeReporter.TimerType.EmitTotal);
383                         assembly.Emit ();
384                         tr.Stop (TimeReporter.TimerType.EmitTotal);
385
386                         if (Report.Errors > 0){
387                                 return false;
388                         }
389
390                         tr.Start (TimeReporter.TimerType.CloseTypes);
391                         module.CloseContainer ();
392                         tr.Stop (TimeReporter.TimerType.CloseTypes);
393
394                         tr.Start (TimeReporter.TimerType.Resouces);
395                         if (!settings.WriteMetadataOnly)
396                                 assembly.EmbedResources ();
397                         tr.Stop (TimeReporter.TimerType.Resouces);
398
399                         if (Report.Errors > 0)
400                                 return false;
401
402                         assembly.Save ();
403
404 #if STATIC
405                         references_loader.Dispose ();
406 #endif
407                         tr.StopTotal ();
408                         tr.ShowStats ();
409
410                         return Report.Errors == 0;
411                 }
412         }
413
414         //
415         // This is the only public entry point
416         //
417         public class CompilerCallableEntryPoint : MarshalByRefObject {
418                 public static bool InvokeCompiler (string [] args, TextWriter error)
419                 {
420                         try {
421                                 CommandLineParser cmd = new CommandLineParser (error);
422                                 var setting = cmd.ParseArguments (args);
423                                 if (setting == null)
424                                         return false;
425
426                                 var d = new Driver (new CompilerContext (setting, new StreamReportPrinter (error)));
427                                 return d.Compile ();
428                         } finally {
429                                 Reset ();
430                         }
431                 }
432
433                 public static int[] AllWarningNumbers {
434                         get {
435                                 return Report.AllWarnings;
436                         }
437                 }
438
439                 public static void Reset ()
440                 {
441                         Reset (true);
442                 }
443
444                 public static void PartialReset ()
445                 {
446                         Reset (false);
447                 }
448                 
449                 public static void Reset (bool full_flag)
450                 {
451                         Location.Reset ();
452                         
453                         if (!full_flag)
454                                 return;
455
456                         Linq.QueryBlock.TransparentParameter.Reset ();
457                         TypeInfo.Reset ();
458                 }
459         }
460 }