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;
47 using System.Globalization;
49 internal class CSharpCodeCompiler : CSharpCodeGenerator, ICodeCompiler
52 StringCollection mcsOutput;
57 public CSharpCodeCompiler()
61 public CSharpCodeCompiler (IDictionary <string, string> providerOptions) :
62 base (providerOptions)
69 public CompilerResults CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
71 return CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { e });
74 public CompilerResults CompileAssemblyFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
76 if (options == null) {
77 throw new ArgumentNullException ("options");
81 return CompileFromDomBatch (options, ea);
83 options.TempFiles.Delete ();
87 public CompilerResults CompileAssemblyFromFile (CompilerParameters options, string fileName)
89 return CompileAssemblyFromFileBatch (options, new string[] { fileName });
92 public CompilerResults CompileAssemblyFromFileBatch (CompilerParameters options, string[] fileNames)
94 if (options == null) {
95 throw new ArgumentNullException ("options");
99 return CompileFromFileBatch (options, fileNames);
101 options.TempFiles.Delete ();
105 public CompilerResults CompileAssemblyFromSource (CompilerParameters options, string source)
107 return CompileAssemblyFromSourceBatch (options, new string[] { source });
110 public CompilerResults CompileAssemblyFromSourceBatch (CompilerParameters options, string[] sources)
112 if (options == null) {
113 throw new ArgumentNullException ("options");
117 return CompileFromSourceBatch (options, sources);
119 options.TempFiles.Delete ();
123 private CompilerResults CompileFromFileBatch (CompilerParameters options, string[] fileNames)
126 throw new ArgumentNullException("options");
127 if (null == fileNames)
128 throw new ArgumentNullException("fileNames");
130 CompilerResults results=new CompilerResults(options.TempFiles);
131 Process mcs=new Process();
133 // FIXME: these lines had better be platform independent.
134 if (Path.DirectorySeparatorChar == '\\') {
135 mcs.StartInfo.FileName = MonoToolsLocator.Mono;
136 mcs.StartInfo.Arguments = "\"" + MonoToolsLocator.CSharpCompiler + "\" ";
138 mcs.StartInfo.FileName = MonoToolsLocator.CSharpCompiler;
141 mcs.StartInfo.Arguments += BuildArgs (options, fileNames, ProviderOptions);
143 mcsOutput = new StringCollection ();
144 mcsOutMutex = new Mutex ();
146 string monoPath = Environment.GetEnvironmentVariable ("MONO_PATH");
147 if (monoPath != null)
148 monoPath = String.Empty;
150 string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
151 if (privateBinPath != null && privateBinPath.Length > 0)
152 monoPath = String.Format ("{0}:{1}", privateBinPath, monoPath);
154 if (monoPath.Length > 0) {
155 StringDictionary dict = mcs.StartInfo.EnvironmentVariables;
156 if (dict.ContainsKey ("MONO_PATH"))
157 dict ["MONO_PATH"] = monoPath;
159 dict.Add ("MONO_PATH", monoPath);
163 * reset MONO_GC_PARAMS - we are invoking compiler possibly with another GC that
164 * may not handle some of the options causing compilation failure
166 mcs.StartInfo.EnvironmentVariables ["MONO_GC_PARAMS"] = String.Empty;
168 mcs.StartInfo.CreateNoWindow=true;
169 mcs.StartInfo.UseShellExecute=false;
170 mcs.StartInfo.RedirectStandardOutput=true;
171 mcs.StartInfo.RedirectStandardError=true;
172 mcs.OutputDataReceived += new DataReceivedEventHandler (McsStderrDataReceived);
174 // Use same text decoder as mcs and not user set values in Console
175 mcs.StartInfo.StandardOutputEncoding =
176 mcs.StartInfo.StandardErrorEncoding = Encoding.UTF8;
180 } catch (Exception e) {
181 Win32Exception exc = e as Win32Exception;
183 throw new SystemException (String.Format ("Error running {0}: {1}", mcs.StartInfo.FileName,
184 Win32Exception.W32ErrorMessage (exc.NativeErrorCode)));
190 mcs.BeginOutputReadLine ();
191 mcs.BeginErrorReadLine ();
194 results.NativeCompilerReturnValue = mcs.ExitCode;
196 mcs.CancelErrorRead ();
197 mcs.CancelOutputRead ();
201 StringCollection sc = mcsOutput;
204 foreach (string error_line in mcsOutput) {
205 CompilerError error = CreateErrorFromString (error_line);
207 results.Errors.Add (error);
208 if (!error.IsWarning)
214 sc.Insert (0, mcs.StartInfo.FileName + " " + mcs.StartInfo.Arguments + Environment.NewLine);
219 if (!File.Exists (options.OutputAssembly)) {
220 StringBuilder sb = new StringBuilder ();
221 foreach (string s in sc)
222 sb.Append (s + Environment.NewLine);
224 throw new Exception ("Compiler failed to produce the assembly. Output: '" + sb.ToString () + "'");
227 if (options.GenerateInMemory) {
228 using (FileStream fs = File.OpenRead(options.OutputAssembly)) {
229 byte[] buffer = new byte[fs.Length];
230 fs.Read(buffer, 0, buffer.Length);
231 results.CompiledAssembly = Assembly.Load(buffer, null);
235 // Avoid setting CompiledAssembly right now since the output might be a netmodule
236 results.PathToAssembly = options.OutputAssembly;
239 results.CompiledAssembly = null;
245 void McsStderrDataReceived (object sender, DataReceivedEventArgs args)
247 if (args.Data != null) {
248 mcsOutMutex.WaitOne ();
249 mcsOutput.Add (args.Data);
250 mcsOutMutex.ReleaseMutex ();
254 private static string BuildArgs(CompilerParameters options,string[] fileNames, IDictionary <string, string> providerOptions)
256 StringBuilder args=new StringBuilder();
257 if (options.GenerateExecutable)
258 args.Append("/target:exe ");
260 args.Append("/target:library ");
262 string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
263 if (privateBinPath != null && privateBinPath.Length > 0)
264 args.AppendFormat ("/lib:\"{0}\" ", privateBinPath);
266 if (options.Win32Resource != null)
267 args.AppendFormat("/win32res:\"{0}\" ",
268 options.Win32Resource);
270 if (options.IncludeDebugInformation)
271 args.Append("/debug:portable /optimize- ");
273 args.Append("/debug- /optimize+ ");
275 if (options.TreatWarningsAsErrors)
276 args.Append("/warnaserror ");
278 if (options.WarningLevel >= 0)
279 args.AppendFormat ("/warn:{0} ", options.WarningLevel);
281 if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) {
282 string extension = (options.GenerateExecutable ? "exe" : "dll");
283 options.OutputAssembly = GetTempFileNameWithExtension (options.TempFiles, extension,
284 !options.GenerateInMemory);
286 args.AppendFormat("/out:\"{0}\" ",options.OutputAssembly);
288 foreach (string import in options.ReferencedAssemblies) {
289 if (import == null || import.Length == 0)
292 args.AppendFormat("/r:\"{0}\" ",import);
295 if (options.CompilerOptions != null) {
296 args.Append (options.CompilerOptions);
300 foreach (string embeddedResource in options.EmbeddedResources) {
301 args.AppendFormat("/resource:\"{0}\" ", embeddedResource);
304 foreach (string linkedResource in options.LinkedResources) {
305 args.AppendFormat("/linkresource:\"{0}\" ", linkedResource);
308 if (providerOptions != null && providerOptions.Count > 0) {
311 if (!providerOptions.TryGetValue ("CompilerVersion", out langver))
314 if (langver.Length >= 1 && langver [0] == 'v')
315 langver = langver.Substring (1);
319 args.Append ("/langversion:ISO-2 ");
323 // current default, omit the switch
328 args.Append ("/noconfig ");
330 args.Append ("/nologo ");
332 // args.Append (" -- ");
333 foreach (string source in fileNames)
334 args.AppendFormat("\"{0}\" ",source);
335 return args.ToString();
338 // Keep in sync with mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ToolTask.cs
339 const string ErrorRegexPattern = @"
341 (\s*(?<file>[^\(]+) # filename (optional)
342 (\((?<line>\d*)(,(?<column>\d*[\+]*))?\))? # line+column (optional)
344 (?<level>\w+) # error|warning
346 (?<number>[^:]*\d) # CS1234
351 static readonly Regex RelatedSymbolsRegex = new Regex(
353 \(Location\ of\ the\ symbol\ related\ to\ previous\ (warning|error)\)
355 RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
357 private static CompilerError CreateErrorFromString(string error_string)
359 if (error_string.StartsWith ("BETA"))
362 if (error_string == null || error_string == "")
365 CompilerError error=new CompilerError();
366 Regex reg = new Regex (ErrorRegexPattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
367 Match match=reg.Match(error_string);
368 if (!match.Success) {
369 match = RelatedSymbolsRegex.Match (error_string);
370 if (!match.Success) {
371 // We had some sort of runtime crash
372 error.ErrorText = error_string;
373 error.IsWarning = false;
374 error.ErrorNumber = "";
377 // This line is a continuation of previous warning of error
381 if (String.Empty != match.Result("${file}"))
382 error.FileName=match.Result("${file}");
383 if (String.Empty != match.Result("${line}"))
384 error.Line=Int32.Parse(match.Result("${line}"));
385 if (String.Empty != match.Result("${column}"))
386 error.Column=Int32.Parse(match.Result("${column}").Trim('+'));
388 string level = match.Result ("${level}");
389 if (level == "warning")
390 error.IsWarning = true;
391 else if (level != "error")
392 return null; // error CS8028 will confuse the regex.
394 error.ErrorNumber=match.Result("${number}");
395 error.ErrorText=match.Result("${message}");
399 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension, bool keepFile)
401 return temp_files.AddExtension (extension, keepFile);
404 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension)
406 return temp_files.AddExtension (extension);
409 private CompilerResults CompileFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
411 if (options == null) {
412 throw new ArgumentNullException ("options");
416 throw new ArgumentNullException ("ea");
419 string[] fileNames = new string[ea.Length];
420 StringCollection assemblies = options.ReferencedAssemblies;
422 for (int i = 0; i < ea.Length; i++) {
423 CodeCompileUnit compileUnit = ea[i];
424 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
425 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
426 StreamWriter s = new StreamWriter (f, Encoding.UTF8);
427 if (compileUnit.ReferencedAssemblies != null) {
428 foreach (string str in compileUnit.ReferencedAssemblies) {
429 if (!assemblies.Contains (str))
430 assemblies.Add (str);
434 ((ICodeGenerator) this).GenerateCodeFromCompileUnit (compileUnit, s, new CodeGeneratorOptions ());
438 return CompileAssemblyFromFileBatch (options, fileNames);
441 private CompilerResults CompileFromSourceBatch (CompilerParameters options, string[] sources)
443 if (options == null) {
444 throw new ArgumentNullException ("options");
447 if (sources == null) {
448 throw new ArgumentNullException ("sources");
451 string[] fileNames = new string[sources.Length];
453 for (int i = 0; i < sources.Length; i++) {
454 fileNames[i] = GetTempFileNameWithExtension (options.TempFiles, i + ".cs");
455 FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
456 using (StreamWriter s = new StreamWriter (f, Encoding.UTF8)) {
457 s.Write (sources[i]);
462 return CompileFromFileBatch (options, fileNames);