Merge pull request #4040 from ntherning/disable-symbolicate-tests-on-windows
[mono.git] / mcs / class / System / Microsoft.CSharp / CSharpCodeCompiler.cs
1 //
2 // Mono.CSharp CSharpCodeCompiler Class implementation
3 //
4 // Authors:
5 //      Sean Kasun (seank@users.sf.net)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // Copyright (c) Novell, Inc. (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 namespace Mono.CSharp
33 {
34         using System;
35         using System.CodeDom;
36         using System.CodeDom.Compiler;
37         using System.ComponentModel;
38         using System.IO;
39         using System.Text;
40         using System.Reflection;
41         using System.Collections;
42         using System.Collections.Specialized;
43         using System.Diagnostics;
44         using System.Text.RegularExpressions;
45         using System.Threading;
46         using System.Collections.Generic;
47         using System.Globalization;
48         
49         internal class CSharpCodeCompiler : CSharpCodeGenerator, ICodeCompiler
50         {
51                 Mutex mcsOutMutex;
52                 StringCollection mcsOutput;
53                 
54                 //
55                 // Constructors
56                 //
57                 public CSharpCodeCompiler()
58                 {
59                 }
60
61                 public CSharpCodeCompiler (IDictionary <string, string> providerOptions) :
62                         base (providerOptions)
63                 {
64                 }
65                 
66                 //
67                 // Methods
68                 //
69                 public CompilerResults CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
70                 {
71                         return CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { e });
72                 }
73
74                 public CompilerResults CompileAssemblyFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
75                 {
76                         if (options == null) {
77                                 throw new ArgumentNullException ("options");
78                         }
79
80                         try {
81                                 return CompileFromDomBatch (options, ea);
82                         } finally {
83                                 options.TempFiles.Delete ();
84                         }
85                 }
86
87                 public CompilerResults CompileAssemblyFromFile (CompilerParameters options, string fileName)
88                 {
89                         return CompileAssemblyFromFileBatch (options, new string[] { fileName });
90                 }
91
92                 public CompilerResults CompileAssemblyFromFileBatch (CompilerParameters options, string[] fileNames)
93                 {
94                         if (options == null) {
95                                 throw new ArgumentNullException ("options");
96                         }
97
98                         try {
99                                 return CompileFromFileBatch (options, fileNames);
100                         } finally {
101                                 options.TempFiles.Delete ();
102                         }
103                 }
104
105                 public CompilerResults CompileAssemblyFromSource (CompilerParameters options, string source)
106                 {
107                         return CompileAssemblyFromSourceBatch (options, new string[] { source });
108                 }
109
110                 public CompilerResults CompileAssemblyFromSourceBatch (CompilerParameters options, string[] sources)
111                 {
112                         if (options == null) {
113                                 throw new ArgumentNullException ("options");
114                         }
115
116                         try {
117                                 return CompileFromSourceBatch (options, sources);
118                         } finally {
119                                 options.TempFiles.Delete ();
120                         }
121                 }
122
123                 private CompilerResults CompileFromFileBatch (CompilerParameters options, string[] fileNames)
124                 {
125                         if (null == options)
126                                 throw new ArgumentNullException("options");
127                         if (null == fileNames)
128                                 throw new ArgumentNullException("fileNames");
129
130                         CompilerResults results=new CompilerResults(options.TempFiles);
131                         Process mcs=new Process();
132
133                         // FIXME: these lines had better be platform independent.
134                         if (Path.DirectorySeparatorChar == '\\') {
135                                 mcs.StartInfo.FileName = MonoToolsLocator.Mono;
136                                 mcs.StartInfo.Arguments = "\"" + MonoToolsLocator.CSharpCompiler + "\" ";
137                         } else {
138                                 mcs.StartInfo.FileName = MonoToolsLocator.CSharpCompiler;
139                         }
140
141                         mcs.StartInfo.Arguments += BuildArgs (options, fileNames, ProviderOptions);
142
143                         mcsOutput = new StringCollection ();
144                         mcsOutMutex = new Mutex ();
145 /*                     
146                         string monoPath = Environment.GetEnvironmentVariable ("MONO_PATH");
147                         if (monoPath != null)
148                                 monoPath = String.Empty;
149
150                         string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
151                         if (privateBinPath != null && privateBinPath.Length > 0)
152                                 monoPath = String.Format ("{0}:{1}", privateBinPath, monoPath);
153
154                         if (monoPath.Length > 0) {
155                                 StringDictionary dict = mcs.StartInfo.EnvironmentVariables;
156                                 if (dict.ContainsKey ("MONO_PATH"))
157                                         dict ["MONO_PATH"] = monoPath;
158                                 else
159                                         dict.Add ("MONO_PATH", monoPath);
160                         }
161 */
162                         /*
163                          * reset MONO_GC_PARAMS - we are invoking compiler possibly with another GC that
164                          * may not handle some of the options causing compilation failure
165                          */
166                         mcs.StartInfo.EnvironmentVariables ["MONO_GC_PARAMS"] = String.Empty;
167
168                         mcs.StartInfo.CreateNoWindow=true;
169                         mcs.StartInfo.UseShellExecute=false;
170                         mcs.StartInfo.RedirectStandardOutput=true;
171                         mcs.StartInfo.RedirectStandardError=true;
172                         mcs.OutputDataReceived += new DataReceivedEventHandler (McsStderrDataReceived);
173
174                         // Use same text decoder as mcs and not user set values in Console
175                         mcs.StartInfo.StandardOutputEncoding =
176                         mcs.StartInfo.StandardErrorEncoding = Encoding.UTF8;
177                         
178                         try {
179                                 mcs.Start();
180                         } catch (Exception e) {
181                                 Win32Exception exc = e as Win32Exception;
182                                 if (exc != null) {
183                                         throw new SystemException (String.Format ("Error running {0}: {1}", mcs.StartInfo.FileName,
184                                                                         Win32Exception.W32ErrorMessage (exc.NativeErrorCode)));
185                                 }
186                                 throw;
187                         }
188
189                         try {
190                                 mcs.BeginOutputReadLine ();
191                                 mcs.BeginErrorReadLine ();
192                                 mcs.WaitForExit();
193                                 
194                                 results.NativeCompilerReturnValue = mcs.ExitCode;
195                         } finally {
196                                 mcs.CancelErrorRead ();
197                                 mcs.CancelOutputRead ();
198                                 mcs.Close();
199                         }
200
201                         StringCollection sc = mcsOutput;
202                        
203                         bool loadIt=true;
204                         foreach (string error_line in mcsOutput) {
205                                 CompilerError error = CreateErrorFromString (error_line);
206                                 if (error != null) {
207                                         results.Errors.Add (error);
208                                         if (!error.IsWarning)
209                                                 loadIt = false;
210                                 }
211                         }
212                         
213                         if (sc.Count > 0) {
214                                 sc.Insert (0, mcs.StartInfo.FileName + " " + mcs.StartInfo.Arguments + Environment.NewLine);
215                                 results.Output = sc;
216                         }
217
218                         if (loadIt) {
219                                 if (!File.Exists (options.OutputAssembly)) {
220                                         StringBuilder sb = new StringBuilder ();
221                                         foreach (string s in sc)
222                                                 sb.Append (s + Environment.NewLine);
223                                         
224                                         throw new Exception ("Compiler failed to produce the assembly. Output: '" + sb.ToString () + "'");
225                                 }
226                                 
227                                 if (options.GenerateInMemory) {
228                                         using (FileStream fs = File.OpenRead(options.OutputAssembly)) {
229                                                 byte[] buffer = new byte[fs.Length];
230                                                 fs.Read(buffer, 0, buffer.Length);
231                                                 results.CompiledAssembly = Assembly.Load(buffer, null);
232                                                 fs.Close();
233                                         }
234                                 } else {
235                                         // Avoid setting CompiledAssembly right now since the output might be a netmodule
236                                         results.PathToAssembly = options.OutputAssembly;
237                                 }
238                         } else {
239                                 results.CompiledAssembly = null;
240                         }
241                         
242                         return results;
243                 }
244
245                 void McsStderrDataReceived (object sender, DataReceivedEventArgs args)
246                 {
247                         if (args.Data != null) {
248                                 mcsOutMutex.WaitOne ();
249                                 mcsOutput.Add (args.Data);
250                                 mcsOutMutex.ReleaseMutex ();
251                         }
252                 }               
253
254                 private static string BuildArgs(CompilerParameters options,string[] fileNames, IDictionary <string, string> providerOptions)
255                 {
256                         StringBuilder args=new StringBuilder();
257                         if (options.GenerateExecutable)
258                                 args.Append("/target:exe ");
259                         else
260                                 args.Append("/target:library ");
261
262                         string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
263                         if (privateBinPath != null && privateBinPath.Length > 0)
264                                 args.AppendFormat ("/lib:\"{0}\" ", privateBinPath);
265                         
266                         if (options.Win32Resource != null)
267                                 args.AppendFormat("/win32res:\"{0}\" ",
268                                         options.Win32Resource);
269
270                         if (options.IncludeDebugInformation)
271                                 args.Append("/debug:portable /optimize- ");
272                         else
273                                 args.Append("/debug- /optimize+ ");
274
275                         if (options.TreatWarningsAsErrors)
276                                 args.Append("/warnaserror ");
277
278                         if (options.WarningLevel >= 0)
279                                 args.AppendFormat ("/warn:{0} ", options.WarningLevel);
280
281                         if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) {
282                                 string extension = (options.GenerateExecutable ? "exe" : "dll");
283                                 options.OutputAssembly = GetTempFileNameWithExtension (options.TempFiles, extension,
284                                         !options.GenerateInMemory);
285                         }
286                         args.AppendFormat("/out:\"{0}\" ",options.OutputAssembly);
287
288                         foreach (string import in options.ReferencedAssemblies) {
289                                 if (import == null || import.Length == 0)
290                                         continue;
291
292                                 args.AppendFormat("/r:\"{0}\" ",import);
293                         }
294
295                         if (options.CompilerOptions != null) {
296                                 args.Append (options.CompilerOptions);
297                                 args.Append (" ");
298                         }
299
300                         foreach (string embeddedResource in options.EmbeddedResources) {
301                                 args.AppendFormat("/resource:\"{0}\" ", embeddedResource);
302                         }
303
304                         foreach (string linkedResource in options.LinkedResources) {
305                                 args.AppendFormat("/linkresource:\"{0}\" ", linkedResource);
306                         }
307                         
308                         if (providerOptions != null && providerOptions.Count > 0) {
309                                 string langver;
310
311                                 if (!providerOptions.TryGetValue ("CompilerVersion", out langver))
312                                         langver = "3.5";
313
314                                 if (langver.Length >= 1 && langver [0] == 'v')
315                                         langver = langver.Substring (1);
316
317                                 switch (langver) {
318                                         case "2.0":
319                                                 args.Append ("/langversion:ISO-2 ");
320                                                 break;
321
322                                         case "3.5":
323                                                 // current default, omit the switch
324                                                 break;
325                                 }
326                         }
327
328                         args.Append ("/noconfig ");
329
330                         args.Append ("/nologo ");
331
332                         // args.Append (" -- ");
333                         foreach (string source in fileNames)
334                                 args.AppendFormat("\"{0}\" ",source);
335                         return args.ToString();
336                 }
337
338                 // Keep in sync with mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ToolTask.cs
339                 const string ErrorRegexPattern = @"
340                         ^
341                         (\s*(?<file>[^\(]+)                         # filename (optional)
342                          (\((?<line>\d*)(,(?<column>\d*[\+]*))?\))? # line+column (optional)
343                          :\s+)?
344                         (?<level>\w+)                               # error|warning
345                         \s+
346                         (?<number>[^:]*\d)                          # CS1234
347                         :
348                         \s*
349                         (?<message>.*)$";
350
351                 static readonly Regex RelatedSymbolsRegex = new Regex(
352                         @"
353             \(Location\ of\ the\ symbol\ related\ to\ previous\ (warning|error)\)
354                         ",
355                         RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
356
357                 private static CompilerError CreateErrorFromString(string error_string)
358                 {
359                         if (error_string.StartsWith ("BETA"))
360                                 return null;
361
362                         if (error_string == null || error_string == "")
363                                 return null;
364
365                         CompilerError error=new CompilerError();
366                         Regex reg = new Regex (ErrorRegexPattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
367                         Match match=reg.Match(error_string);
368                         if (!match.Success) {
369                                 match = RelatedSymbolsRegex.Match (error_string);
370                                 if (!match.Success) {
371                                         // We had some sort of runtime crash
372                                         error.ErrorText = error_string;
373                                         error.IsWarning = false;
374                                         error.ErrorNumber = "";
375                                         return error;
376                                 } else {
377                                         // This line is a continuation of previous warning of error
378                                         return null;
379                                 }
380                         }
381                         if (String.Empty != match.Result("${file}"))
382                                 error.FileName=match.Result("${file}");
383                         if (String.Empty != match.Result("${line}"))
384                                 error.Line=Int32.Parse(match.Result("${line}"));
385                         if (String.Empty != match.Result("${column}"))
386                                 error.Column=Int32.Parse(match.Result("${column}").Trim('+'));
387
388                         string level = match.Result ("${level}");
389                         if (level == "warning")
390                                 error.IsWarning = true;
391                         else if (level != "error")
392                                 return null; // error CS8028 will confuse the regex.
393
394                         error.ErrorNumber=match.Result("${number}");
395                         error.ErrorText=match.Result("${message}");
396                         return error;
397                 }
398
399                 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension, bool keepFile)
400                 {
401                         return temp_files.AddExtension (extension, keepFile);
402                 }
403
404                 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension)
405                 {
406                         return temp_files.AddExtension (extension);
407                 }
408
409                 private CompilerResults CompileFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
410                 {
411                         if (options == null) {
412                                 throw new ArgumentNullException ("options");
413                         }
414
415                         if (ea == null) {
416                                 throw new ArgumentNullException ("ea");
417                         }
418
419                         string[] fileNames = new string[ea.Length];
420                         StringCollection assemblies = options.ReferencedAssemblies;
421
422                         for (int i = 0; i < ea.Length; i++) {
423                                 CodeCompileUnit compileUnit = ea[i];
424                                 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
425                                 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
426                                 StreamWriter s = new StreamWriter (f, Encoding.UTF8);
427                                 if (compileUnit.ReferencedAssemblies != null) {
428                                         foreach (string str in compileUnit.ReferencedAssemblies) {
429                                                 if (!assemblies.Contains (str))
430                                                         assemblies.Add (str);
431                                         }
432                                 }
433
434                                 ((ICodeGenerator) this).GenerateCodeFromCompileUnit (compileUnit, s, new CodeGeneratorOptions ());
435                                 s.Close ();
436                                 f.Close ();
437                         }
438                         return CompileAssemblyFromFileBatch (options, fileNames);
439                 }
440
441                 private CompilerResults CompileFromSourceBatch (CompilerParameters options, string[] sources)
442                 {
443                         if (options == null) {
444                                 throw new ArgumentNullException ("options");
445                         }
446
447                         if (sources == null) {
448                                 throw new ArgumentNullException ("sources");
449                         }
450
451                         string[] fileNames = new string[sources.Length];
452
453                         for (int i = 0; i < sources.Length; i++) {
454                                 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
455                                 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
456                                 using (StreamWriter s = new StreamWriter (f, Encoding.UTF8)) {
457                                         s.Write (sources[i]);
458                                         s.Close ();
459                                 }
460                                 f.Close ();
461                         }
462                         return CompileFromFileBatch (options, fileNames);
463                 }
464         }
465 }