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