Merge pull request #498 from Unroll-Me/master
[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         
48         internal class CSharpCodeCompiler : CSharpCodeGenerator, ICodeCompiler
49         {
50                 static string windowsMcsPath;
51                 static string windowsMonoPath;
52
53                 Mutex mcsOutMutex;
54                 StringCollection mcsOutput;
55                 
56                 static CSharpCodeCompiler ()
57                 {
58                         if (Path.DirectorySeparatorChar == '\\') {
59                                 PropertyInfo gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static|BindingFlags.NonPublic);
60                                 MethodInfo get_gac = gac.GetGetMethod (true);
61                                 string p = Path.GetDirectoryName (
62                                         (string) get_gac.Invoke (null, null));
63                                 windowsMonoPath = Path.Combine (
64                                         Path.GetDirectoryName (
65                                                 Path.GetDirectoryName (p)),
66                                         "bin\\mono.bat");
67                                 if (!File.Exists (windowsMonoPath))
68                                         windowsMonoPath = Path.Combine (
69                                                 Path.GetDirectoryName (
70                                                         Path.GetDirectoryName (p)),
71                                                 "bin\\mono.exe");
72                                 if (!File.Exists (windowsMonoPath))
73                                         windowsMonoPath = Path.Combine (
74                                                 Path.GetDirectoryName (
75                                                         Path.GetDirectoryName (
76                                                                 Path.GetDirectoryName (p))),
77                                                 "mono\\mono\\mini\\mono.exe");
78                                 if (!File.Exists (windowsMonoPath))
79                                         throw new FileNotFoundException ("Windows mono path not found: " + windowsMonoPath);
80
81                                 windowsMcsPath = Path.Combine (p, "4.5\\mcs.exe");
82                                 if (!File.Exists (windowsMcsPath))
83                                         windowsMcsPath = Path.Combine(Path.GetDirectoryName (p), "lib\\build\\mcs.exe");
84                                 
85                                 if (!File.Exists (windowsMcsPath))
86                                         throw new FileNotFoundException ("Windows mcs path not found: " + windowsMcsPath);
87                         }
88                 }
89
90                 //
91                 // Constructors
92                 //
93                 public CSharpCodeCompiler()
94                 {
95                 }
96
97                 public CSharpCodeCompiler (IDictionary <string, string> providerOptions) :
98                         base (providerOptions)
99                 {
100                 }
101                 
102                 //
103                 // Methods
104                 //
105                 public CompilerResults CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
106                 {
107                         return CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { e });
108                 }
109
110                 public CompilerResults CompileAssemblyFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
111                 {
112                         if (options == null) {
113                                 throw new ArgumentNullException ("options");
114                         }
115
116                         try {
117                                 return CompileFromDomBatch (options, ea);
118                         } finally {
119                                 options.TempFiles.Delete ();
120                         }
121                 }
122
123                 public CompilerResults CompileAssemblyFromFile (CompilerParameters options, string fileName)
124                 {
125                         return CompileAssemblyFromFileBatch (options, new string[] { fileName });
126                 }
127
128                 public CompilerResults CompileAssemblyFromFileBatch (CompilerParameters options, string[] fileNames)
129                 {
130                         if (options == null) {
131                                 throw new ArgumentNullException ("options");
132                         }
133
134                         try {
135                                 return CompileFromFileBatch (options, fileNames);
136                         } finally {
137                                 options.TempFiles.Delete ();
138                         }
139                 }
140
141                 public CompilerResults CompileAssemblyFromSource (CompilerParameters options, string source)
142                 {
143                         return CompileAssemblyFromSourceBatch (options, new string[] { source });
144                 }
145
146                 public CompilerResults CompileAssemblyFromSourceBatch (CompilerParameters options, string[] sources)
147                 {
148                         if (options == null) {
149                                 throw new ArgumentNullException ("options");
150                         }
151
152                         try {
153                                 return CompileFromSourceBatch (options, sources);
154                         } finally {
155                                 options.TempFiles.Delete ();
156                         }
157                 }
158
159                 private CompilerResults CompileFromFileBatch (CompilerParameters options, string[] fileNames)
160                 {
161                         if (null == options)
162                                 throw new ArgumentNullException("options");
163                         if (null == fileNames)
164                                 throw new ArgumentNullException("fileNames");
165
166                         CompilerResults results=new CompilerResults(options.TempFiles);
167                         Process mcs=new Process();
168
169                         // FIXME: these lines had better be platform independent.
170                         if (Path.DirectorySeparatorChar == '\\') {
171                                 mcs.StartInfo.FileName = windowsMonoPath;
172                                 mcs.StartInfo.Arguments = "\"" + windowsMcsPath + "\" " +
173                                         BuildArgs (options, fileNames, ProviderOptions);
174                         } else {
175                                 mcs.StartInfo.FileName="mcs";
176                                 mcs.StartInfo.Arguments=BuildArgs(options, fileNames, ProviderOptions);
177                         }
178
179                         mcsOutput = new StringCollection ();
180                         mcsOutMutex = new Mutex ();
181 #if !NET_4_0
182                         /*
183                          * !:. KLUDGE WARNING .:!
184                          *
185                          * When running the 2.0 test suite some assemblies will invoke mcs via
186                          * CodeDOM and the new mcs process will find the MONO_PATH variable in its
187                          * environment pointing to the net_2_0 library which will cause the runtime
188                          * to attempt to load the 2.0 corlib into 4.0 process and thus mcs will
189                          * fail. At the same time, we must not touch MONO_PATH when running outside
190                          * the test suite, thus the kludge.
191                          *
192                          * !:. KLUDGE WARNING .:!
193                          */
194                         if (Environment.GetEnvironmentVariable ("MONO_TESTS_IN_PROGRESS") != null) {
195                                 string monoPath = Environment.GetEnvironmentVariable ("MONO_PATH");
196                                 if (!String.IsNullOrEmpty (monoPath)) {
197                                         monoPath = monoPath.Replace ("/class/lib/net_2_0", "/class/lib/net_4_0");
198                                         mcs.StartInfo.EnvironmentVariables ["MONO_PATH"] = monoPath;
199                                 }
200                         }
201 #endif
202 /*                     
203                         string monoPath = Environment.GetEnvironmentVariable ("MONO_PATH");
204                         if (monoPath != null)
205                                 monoPath = String.Empty;
206
207                         string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
208                         if (privateBinPath != null && privateBinPath.Length > 0)
209                                 monoPath = String.Format ("{0}:{1}", privateBinPath, monoPath);
210
211                         if (monoPath.Length > 0) {
212                                 StringDictionary dict = mcs.StartInfo.EnvironmentVariables;
213                                 if (dict.ContainsKey ("MONO_PATH"))
214                                         dict ["MONO_PATH"] = monoPath;
215                                 else
216                                         dict.Add ("MONO_PATH", monoPath);
217                         }
218 */
219                         /*
220                          * reset MONO_GC_PARAMS - we are invoking compiler possibly with another GC that
221                          * may not handle some of the options causing compilation failure
222                          */
223                         mcs.StartInfo.EnvironmentVariables ["MONO_GC_PARAMS"] = String.Empty;
224
225                         mcs.StartInfo.CreateNoWindow=true;
226                         mcs.StartInfo.UseShellExecute=false;
227                         mcs.StartInfo.RedirectStandardOutput=true;
228                         mcs.StartInfo.RedirectStandardError=true;
229                         mcs.ErrorDataReceived += new DataReceivedEventHandler (McsStderrDataReceived);
230                         
231                         try {
232                                 mcs.Start();
233                         } catch (Exception e) {
234                                 Win32Exception exc = e as Win32Exception;
235                                 if (exc != null) {
236                                         throw new SystemException (String.Format ("Error running {0}: {1}", mcs.StartInfo.FileName,
237                                                                         Win32Exception.W32ErrorMessage (exc.NativeErrorCode)));
238                                 }
239                                 throw;
240                         }
241
242                         try {
243                                 mcs.BeginOutputReadLine ();
244                                 mcs.BeginErrorReadLine ();
245                                 mcs.WaitForExit();
246                                 
247                                 results.NativeCompilerReturnValue = mcs.ExitCode;
248                         } finally {
249                                 mcs.CancelErrorRead ();
250                                 mcs.CancelOutputRead ();
251                                 mcs.Close();
252                         }
253
254                         StringCollection sc = mcsOutput;
255                        
256                         bool loadIt=true;
257                         foreach (string error_line in mcsOutput) {
258                                 CompilerError error = CreateErrorFromString (error_line);
259                                 if (error != null) {
260                                         results.Errors.Add (error);
261                                         if (!error.IsWarning)
262                                                 loadIt = false;
263                                 }
264                         }
265                         
266                         if (sc.Count > 0) {
267                                 sc.Insert (0, mcs.StartInfo.FileName + " " + mcs.StartInfo.Arguments + Environment.NewLine);
268                                 results.Output = sc;
269                         }
270
271                         if (loadIt) {
272                                 if (!File.Exists (options.OutputAssembly)) {
273                                         StringBuilder sb = new StringBuilder ();
274                                         foreach (string s in sc)
275                                                 sb.Append (s + Environment.NewLine);
276                                         
277                                         throw new Exception ("Compiler failed to produce the assembly. Output: '" + sb.ToString () + "'");
278                                 }
279                                 
280                                 if (options.GenerateInMemory) {
281                                         using (FileStream fs = File.OpenRead(options.OutputAssembly)) {
282                                                 byte[] buffer = new byte[fs.Length];
283                                                 fs.Read(buffer, 0, buffer.Length);
284                                                 results.CompiledAssembly = Assembly.Load(buffer, null);
285                                                 fs.Close();
286                                         }
287                                 } else {
288                                         // Avoid setting CompiledAssembly right now since the output might be a netmodule
289                                         results.PathToAssembly = options.OutputAssembly;
290                                 }
291                         } else {
292                                 results.CompiledAssembly = null;
293                         }
294                         
295                         return results;
296                 }
297
298                 void McsStderrDataReceived (object sender, DataReceivedEventArgs args)
299                 {
300                         if (args.Data != null) {
301                                 mcsOutMutex.WaitOne ();
302                                 mcsOutput.Add (args.Data);
303                                 mcsOutMutex.ReleaseMutex ();
304                         }
305                 }               
306
307                 private static string BuildArgs(CompilerParameters options,string[] fileNames, IDictionary <string, string> providerOptions)
308                 {
309                         StringBuilder args=new StringBuilder();
310                         if (options.GenerateExecutable)
311                                 args.Append("/target:exe ");
312                         else
313                                 args.Append("/target:library ");
314
315                         string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
316                         if (privateBinPath != null && privateBinPath.Length > 0)
317                                 args.AppendFormat ("/lib:\"{0}\" ", privateBinPath);
318                         
319                         if (options.Win32Resource != null)
320                                 args.AppendFormat("/win32res:\"{0}\" ",
321                                         options.Win32Resource);
322
323                         if (options.IncludeDebugInformation)
324                                 args.Append("/debug+ /optimize- ");
325                         else
326                                 args.Append("/debug- /optimize+ ");
327
328                         if (options.TreatWarningsAsErrors)
329                                 args.Append("/warnaserror ");
330
331                         if (options.WarningLevel >= 0)
332                                 args.AppendFormat ("/warn:{0} ", options.WarningLevel);
333
334                         if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) {
335                                 string extension = (options.GenerateExecutable ? "exe" : "dll");
336                                 options.OutputAssembly = GetTempFileNameWithExtension (options.TempFiles, extension,
337                                         !options.GenerateInMemory);
338                         }
339                         args.AppendFormat("/out:\"{0}\" ",options.OutputAssembly);
340
341                         foreach (string import in options.ReferencedAssemblies) {
342                                 if (import == null || import.Length == 0)
343                                         continue;
344
345                                 args.AppendFormat("/r:\"{0}\" ",import);
346                         }
347
348                         if (options.CompilerOptions != null) {
349                                 args.Append (options.CompilerOptions);
350                                 args.Append (" ");
351                         }
352
353                         foreach (string embeddedResource in options.EmbeddedResources) {
354                                 args.AppendFormat("/resource:\"{0}\" ", embeddedResource);
355                         }
356
357                         foreach (string linkedResource in options.LinkedResources) {
358                                 args.AppendFormat("/linkresource:\"{0}\" ", linkedResource);
359                         }
360                         
361                         if (providerOptions != null && providerOptions.Count > 0) {
362                                 string langver;
363
364                                 if (!providerOptions.TryGetValue ("CompilerVersion", out langver))
365 #if NET_4_0
366                                         langver = "3.5";
367 #else
368                                         langver = "2.0";
369 #endif
370
371                                 if (langver.Length >= 1 && langver [0] == 'v')
372                                         langver = langver.Substring (1);
373
374                                 switch (langver) {
375                                         case "2.0":
376                                                 args.Append ("/langversion:ISO-2 ");
377                                                 break;
378
379                                         case "3.5":
380                                                 // current default, omit the switch
381                                                 break;
382                                 }
383                         }
384
385 #if NET_4_5                     
386                         args.Append("/sdk:4.5");
387 #elif NET_4_0
388                         args.Append("/sdk:4");
389 #else
390                         args.Append("/sdk:2");
391 #endif
392
393                         args.Append (" -- ");
394                         foreach (string source in fileNames)
395                                 args.AppendFormat("\"{0}\" ",source);
396                         return args.ToString();
397                 }
398                 private static CompilerError CreateErrorFromString(string error_string)
399                 {
400                         if (error_string.StartsWith ("BETA"))
401                                 return null;
402
403                         if (error_string == null || error_string == "")
404                                 return null;
405
406                         CompilerError error=new CompilerError();
407                         Regex reg = new Regex (@"^(\s*(?<file>.*)\((?<line>\d*)(,(?<column>\d*))?\)(:)?\s+)*(?<level>\w+)\s*(?<number>.*):\s(?<message>.*)",
408                                 RegexOptions.Compiled | RegexOptions.ExplicitCapture);
409                         Match match=reg.Match(error_string);
410                         if (!match.Success) {
411                                 // We had some sort of runtime crash
412                                 error.ErrorText = error_string;
413                                 error.IsWarning = false;
414                                 error.ErrorNumber = "";
415                                 return error;
416                         }
417                         if (String.Empty != match.Result("${file}"))
418                                 error.FileName=match.Result("${file}");
419                         if (String.Empty != match.Result("${line}"))
420                                 error.Line=Int32.Parse(match.Result("${line}"));
421                         if (String.Empty != match.Result("${column}"))
422                                 error.Column=Int32.Parse(match.Result("${column}"));
423
424                         string level = match.Result ("${level}");
425                         if (level == "warning")
426                                 error.IsWarning = true;
427                         else if (level != "error")
428                                 return null; // error CS8028 will confuse the regex.
429
430                         error.ErrorNumber=match.Result("${number}");
431                         error.ErrorText=match.Result("${message}");
432                         return error;
433                 }
434
435                 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension, bool keepFile)
436                 {
437                         return temp_files.AddExtension (extension, keepFile);
438                 }
439
440                 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension)
441                 {
442                         return temp_files.AddExtension (extension);
443                 }
444
445                 private CompilerResults CompileFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
446                 {
447                         if (options == null) {
448                                 throw new ArgumentNullException ("options");
449                         }
450
451                         if (ea == null) {
452                                 throw new ArgumentNullException ("ea");
453                         }
454
455                         string[] fileNames = new string[ea.Length];
456                         StringCollection assemblies = options.ReferencedAssemblies;
457
458                         for (int i = 0; i < ea.Length; i++) {
459                                 CodeCompileUnit compileUnit = ea[i];
460                                 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
461                                 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
462                                 StreamWriter s = new StreamWriter (f, Encoding.UTF8);
463                                 if (compileUnit.ReferencedAssemblies != null) {
464                                         foreach (string str in compileUnit.ReferencedAssemblies) {
465                                                 if (!assemblies.Contains (str))
466                                                         assemblies.Add (str);
467                                         }
468                                 }
469
470                                 ((ICodeGenerator) this).GenerateCodeFromCompileUnit (compileUnit, s, new CodeGeneratorOptions ());
471                                 s.Close ();
472                                 f.Close ();
473                         }
474                         return CompileAssemblyFromFileBatch (options, fileNames);
475                 }
476
477                 private CompilerResults CompileFromSourceBatch (CompilerParameters options, string[] sources)
478                 {
479                         if (options == null) {
480                                 throw new ArgumentNullException ("options");
481                         }
482
483                         if (sources == null) {
484                                 throw new ArgumentNullException ("sources");
485                         }
486
487                         string[] fileNames = new string[sources.Length];
488
489                         for (int i = 0; i < sources.Length; i++) {
490                                 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
491                                 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
492                                 using (StreamWriter s = new StreamWriter (f, Encoding.UTF8)) {
493                                         s.Write (sources[i]);
494                                         s.Close ();
495                                 }
496                                 f.Close ();
497                         }
498                         return CompileFromFileBatch (options, fileNames);
499                 }
500         }
501 }