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