2 // Mono.CSharp CSharpCodeCompiler Class implementation
5 // Sean Kasun (seank@users.sf.net)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (c) Novell, Inc. (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
36 using System.CodeDom.Compiler;
37 using System.ComponentModel;
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;
48 internal class CSharpCodeCompiler : CSharpCodeGenerator, ICodeCompiler
50 static string windowsMcsPath;
51 static string windowsMonoPath;
54 StringCollection mcsOutput;
56 static CSharpCodeCompiler ()
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)),
67 if (!File.Exists (windowsMonoPath))
68 windowsMonoPath = Path.Combine (
69 Path.GetDirectoryName (
70 Path.GetDirectoryName (p)),
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);
81 windowsMcsPath = Path.Combine (p, "4.0\\mcs.exe");
82 if (!File.Exists (windowsMcsPath))
83 windowsMcsPath = Path.Combine(Path.GetDirectoryName (p), "lib\\build\\mcs.exe");
85 if (!File.Exists (windowsMcsPath))
86 throw new FileNotFoundException ("Windows mcs path not found: " + windowsMcsPath);
93 public CSharpCodeCompiler()
97 public CSharpCodeCompiler (IDictionary <string, string> providerOptions) :
98 base (providerOptions)
105 public CompilerResults CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
107 return CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { e });
110 public CompilerResults CompileAssemblyFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
112 if (options == null) {
113 throw new ArgumentNullException ("options");
117 return CompileFromDomBatch (options, ea);
119 options.TempFiles.Delete ();
123 public CompilerResults CompileAssemblyFromFile (CompilerParameters options, string fileName)
125 return CompileAssemblyFromFileBatch (options, new string[] { fileName });
128 public CompilerResults CompileAssemblyFromFileBatch (CompilerParameters options, string[] fileNames)
130 if (options == null) {
131 throw new ArgumentNullException ("options");
135 return CompileFromFileBatch (options, fileNames);
137 options.TempFiles.Delete ();
141 public CompilerResults CompileAssemblyFromSource (CompilerParameters options, string source)
143 return CompileAssemblyFromSourceBatch (options, new string[] { source });
146 public CompilerResults CompileAssemblyFromSourceBatch (CompilerParameters options, string[] sources)
148 if (options == null) {
149 throw new ArgumentNullException ("options");
153 return CompileFromSourceBatch (options, sources);
155 options.TempFiles.Delete ();
159 private CompilerResults CompileFromFileBatch (CompilerParameters options, string[] fileNames)
162 throw new ArgumentNullException("options");
163 if (null == fileNames)
164 throw new ArgumentNullException("fileNames");
166 CompilerResults results=new CompilerResults(options.TempFiles);
167 Process mcs=new Process();
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);
175 mcs.StartInfo.FileName="mcs";
176 mcs.StartInfo.Arguments=BuildArgs(options, fileNames, ProviderOptions);
179 mcsOutput = new StringCollection ();
180 mcsOutMutex = new Mutex ();
183 * !:. KLUDGE WARNING .:!
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.
192 * !:. KLUDGE WARNING .:!
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;
203 string monoPath = Environment.GetEnvironmentVariable ("MONO_PATH");
204 if (monoPath != null)
205 monoPath = String.Empty;
207 string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
208 if (privateBinPath != null && privateBinPath.Length > 0)
209 monoPath = String.Format ("{0}:{1}", privateBinPath, monoPath);
211 if (monoPath.Length > 0) {
212 StringDictionary dict = mcs.StartInfo.EnvironmentVariables;
213 if (dict.ContainsKey ("MONO_PATH"))
214 dict ["MONO_PATH"] = monoPath;
216 dict.Add ("MONO_PATH", monoPath);
219 mcs.StartInfo.CreateNoWindow=true;
220 mcs.StartInfo.UseShellExecute=false;
221 mcs.StartInfo.RedirectStandardOutput=true;
222 mcs.StartInfo.RedirectStandardError=true;
223 mcs.ErrorDataReceived += new DataReceivedEventHandler (McsStderrDataReceived);
227 } catch (Exception e) {
228 Win32Exception exc = e as Win32Exception;
230 throw new SystemException (String.Format ("Error running {0}: {1}", mcs.StartInfo.FileName,
231 Win32Exception.W32ErrorMessage (exc.NativeErrorCode)));
237 mcs.BeginOutputReadLine ();
238 mcs.BeginErrorReadLine ();
241 results.NativeCompilerReturnValue = mcs.ExitCode;
243 mcs.CancelErrorRead ();
244 mcs.CancelOutputRead ();
248 StringCollection sc = mcsOutput;
251 foreach (string error_line in mcsOutput) {
252 CompilerError error = CreateErrorFromString (error_line);
254 results.Errors.Add (error);
255 if (!error.IsWarning)
261 sc.Insert (0, mcs.StartInfo.FileName + " " + mcs.StartInfo.Arguments + Environment.NewLine);
266 if (!File.Exists (options.OutputAssembly)) {
267 StringBuilder sb = new StringBuilder ();
268 foreach (string s in sc)
269 sb.Append (s + Environment.NewLine);
271 throw new Exception ("Compiler failed to produce the assembly. Output: '" + sb.ToString () + "'");
274 if (options.GenerateInMemory) {
275 using (FileStream fs = File.OpenRead(options.OutputAssembly)) {
276 byte[] buffer = new byte[fs.Length];
277 fs.Read(buffer, 0, buffer.Length);
278 results.CompiledAssembly = Assembly.Load(buffer, null, options.Evidence);
282 // Avoid setting CompiledAssembly right now since the output might be a netmodule
283 results.PathToAssembly = options.OutputAssembly;
286 results.CompiledAssembly = null;
292 void McsStderrDataReceived (object sender, DataReceivedEventArgs args)
294 if (args.Data != null) {
295 mcsOutMutex.WaitOne ();
296 mcsOutput.Add (args.Data);
297 mcsOutMutex.ReleaseMutex ();
301 private static string BuildArgs(CompilerParameters options,string[] fileNames, IDictionary <string, string> providerOptions)
303 StringBuilder args=new StringBuilder();
304 if (options.GenerateExecutable)
305 args.Append("/target:exe ");
307 args.Append("/target:library ");
309 string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
310 if (privateBinPath != null && privateBinPath.Length > 0)
311 args.AppendFormat ("/lib:\"{0}\" ", privateBinPath);
313 if (options.Win32Resource != null)
314 args.AppendFormat("/win32res:\"{0}\" ",
315 options.Win32Resource);
317 if (options.IncludeDebugInformation)
318 args.Append("/debug+ /optimize- ");
320 args.Append("/debug- /optimize+ ");
322 if (options.TreatWarningsAsErrors)
323 args.Append("/warnaserror ");
325 if (options.WarningLevel >= 0)
326 args.AppendFormat ("/warn:{0} ", options.WarningLevel);
328 if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) {
329 string extension = (options.GenerateExecutable ? "exe" : "dll");
330 options.OutputAssembly = GetTempFileNameWithExtension (options.TempFiles, extension,
331 !options.GenerateInMemory);
333 args.AppendFormat("/out:\"{0}\" ",options.OutputAssembly);
335 foreach (string import in options.ReferencedAssemblies) {
336 if (import == null || import.Length == 0)
339 args.AppendFormat("/r:\"{0}\" ",import);
342 if (options.CompilerOptions != null) {
343 args.Append (options.CompilerOptions);
347 foreach (string embeddedResource in options.EmbeddedResources) {
348 args.AppendFormat("/resource:\"{0}\" ", embeddedResource);
351 foreach (string linkedResource in options.LinkedResources) {
352 args.AppendFormat("/linkresource:\"{0}\" ", linkedResource);
355 if (providerOptions != null && providerOptions.Count > 0) {
358 if (!providerOptions.TryGetValue ("CompilerVersion", out langver))
365 if (langver.Length >= 1 && langver [0] == 'v')
366 langver = langver.Substring (1);
370 args.Append ("/langversion:ISO-2 ");
374 // current default, omit the switch
380 args.Append("/sdk:4");
382 args.Append("/sdk:2");
385 args.Append (" -- ");
386 foreach (string source in fileNames)
387 args.AppendFormat("\"{0}\" ",source);
388 return args.ToString();
390 private static CompilerError CreateErrorFromString(string error_string)
392 if (error_string.StartsWith ("BETA"))
395 if (error_string == null || error_string == "")
398 CompilerError error=new CompilerError();
399 Regex reg = new Regex (@"^(\s*(?<file>.*)\((?<line>\d*)(,(?<column>\d*))?\)(:)?\s+)*(?<level>\w+)\s*(?<number>.*):\s(?<message>.*)",
400 RegexOptions.Compiled | RegexOptions.ExplicitCapture);
401 Match match=reg.Match(error_string);
402 if (!match.Success) {
403 // We had some sort of runtime crash
404 error.ErrorText = error_string;
405 error.IsWarning = false;
406 error.ErrorNumber = "";
409 if (String.Empty != match.Result("${file}"))
410 error.FileName=match.Result("${file}");
411 if (String.Empty != match.Result("${line}"))
412 error.Line=Int32.Parse(match.Result("${line}"));
413 if (String.Empty != match.Result("${column}"))
414 error.Column=Int32.Parse(match.Result("${column}"));
416 string level = match.Result ("${level}");
417 if (level == "warning")
418 error.IsWarning = true;
419 else if (level != "error")
420 return null; // error CS8028 will confuse the regex.
422 error.ErrorNumber=match.Result("${number}");
423 error.ErrorText=match.Result("${message}");
427 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension, bool keepFile)
429 return temp_files.AddExtension (extension, keepFile);
432 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension)
434 return temp_files.AddExtension (extension);
437 private CompilerResults CompileFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
439 if (options == null) {
440 throw new ArgumentNullException ("options");
444 throw new ArgumentNullException ("ea");
447 string[] fileNames = new string[ea.Length];
448 StringCollection assemblies = options.ReferencedAssemblies;
450 for (int i = 0; i < ea.Length; i++) {
451 CodeCompileUnit compileUnit = ea[i];
452 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
453 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
454 StreamWriter s = new StreamWriter (f, Encoding.UTF8);
455 if (compileUnit.ReferencedAssemblies != null) {
456 foreach (string str in compileUnit.ReferencedAssemblies) {
457 if (!assemblies.Contains (str))
458 assemblies.Add (str);
462 ((ICodeGenerator) this).GenerateCodeFromCompileUnit (compileUnit, s, new CodeGeneratorOptions ());
466 return CompileAssemblyFromFileBatch (options, fileNames);
469 private CompilerResults CompileFromSourceBatch (CompilerParameters options, string[] sources)
471 if (options == null) {
472 throw new ArgumentNullException ("options");
475 if (sources == null) {
476 throw new ArgumentNullException ("sources");
479 string[] fileNames = new string[sources.Length];
481 for (int i = 0; i < sources.Length; i++) {
482 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
483 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
484 using (StreamWriter s = new StreamWriter (f, Encoding.UTF8)) {
485 s.Write (sources[i]);
490 return CompileFromFileBatch (options, fileNames);