[HttpConnection] Bug fix: HttpListener's "IgnoreWriteExceptions" property value is...
[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                         references_loader.CompiledAssembly = assembly;
332                         tr.Stop (TimeReporter.TimerType.CreateTypeTotal);
333
334                         references_loader.LoadReferences (module);
335
336                         tr.Start (TimeReporter.TimerType.PredefinedTypesInit);
337                         if (!ctx.BuiltinTypes.CheckDefinitions (module))
338                                 return false;
339
340                         tr.Stop (TimeReporter.TimerType.PredefinedTypesInit);
341
342                         references_loader.LoadModules (assembly, module.GlobalRootNamespace);
343 #else
344                         var assembly = new AssemblyDefinitionDynamic (module, output_file_name, output_file);
345                         module.SetDeclaringAssembly (assembly);
346
347                         var importer = new ReflectionImporter (module, ctx.BuiltinTypes);
348                         assembly.Importer = importer;
349
350                         var loader = new DynamicLoader (importer, ctx);
351                         loader.LoadReferences (module);
352
353                         if (!ctx.BuiltinTypes.CheckDefinitions (module))
354                                 return false;
355
356                         if (!assembly.Create (AppDomain.CurrentDomain, AssemblyBuilderAccess.Save))
357                                 return false;
358
359                         module.CreateContainer ();
360
361                         loader.LoadModules (assembly, module.GlobalRootNamespace);
362 #endif
363                         module.InitializePredefinedTypes ();
364
365                         if (settings.GetResourceStrings != null)
366                                 module.LoadGetResourceStrings (settings.GetResourceStrings);
367
368                         tr.Start (TimeReporter.TimerType.ModuleDefinitionTotal);
369                         module.Define ();
370                         tr.Stop (TimeReporter.TimerType.ModuleDefinitionTotal);
371
372                         if (Report.Errors > 0)
373                                 return false;
374
375                         if (settings.DocumentationFile != null) {
376                                 var doc = new DocumentationBuilder (module);
377                                 doc.OutputDocComment (output_file, settings.DocumentationFile);
378                         }
379
380                         assembly.Resolve ();
381                         
382                         if (Report.Errors > 0)
383                                 return false;
384
385
386                         tr.Start (TimeReporter.TimerType.EmitTotal);
387                         assembly.Emit ();
388                         tr.Stop (TimeReporter.TimerType.EmitTotal);
389
390                         if (Report.Errors > 0){
391                                 return false;
392                         }
393
394                         tr.Start (TimeReporter.TimerType.CloseTypes);
395                         module.CloseContainer ();
396                         tr.Stop (TimeReporter.TimerType.CloseTypes);
397
398                         tr.Start (TimeReporter.TimerType.Resouces);
399                         if (!settings.WriteMetadataOnly)
400                                 assembly.EmbedResources ();
401                         tr.Stop (TimeReporter.TimerType.Resouces);
402
403                         if (Report.Errors > 0)
404                                 return false;
405
406                         assembly.Save ();
407
408 #if STATIC
409                         references_loader.Dispose ();
410 #endif
411                         tr.StopTotal ();
412                         tr.ShowStats ();
413
414                         return Report.Errors == 0;
415                 }
416         }
417
418         //
419         // This is the only public entry point
420         //
421         public class CompilerCallableEntryPoint : MarshalByRefObject {
422                 public static bool InvokeCompiler (string [] args, TextWriter error)
423                 {
424                         try {
425                                 CommandLineParser cmd = new CommandLineParser (error);
426                                 var setting = cmd.ParseArguments (args);
427                                 if (setting == null)
428                                         return false;
429
430                                 var d = new Driver (new CompilerContext (setting, new StreamReportPrinter (error)));
431                                 return d.Compile ();
432                         } finally {
433                                 Reset ();
434                         }
435                 }
436
437                 public static int[] AllWarningNumbers {
438                         get {
439                                 return Report.AllWarnings;
440                         }
441                 }
442
443                 public static void Reset ()
444                 {
445                         Reset (true);
446                 }
447
448                 public static void PartialReset ()
449                 {
450                         Reset (false);
451                 }
452                 
453                 public static void Reset (bool full_flag)
454                 {
455                         Location.Reset ();
456                         
457                         if (!full_flag)
458                                 return;
459
460                         Linq.QueryBlock.TransparentParameter.Reset ();
461                         TypeInfo.Reset ();
462                 }
463         }
464 }