Merge pull request #3057 from BrzVlad/fix-major-log3
[mono.git] / mcs / class / Cscompmgd / Microsoft.CSharp / Compiler.cs
index 33d25a675ba58df7fabc79c92f9c37560e304f34..5b75d9c890481c3675cad5caaae9f5637ca0e183 100644 (file)
@@ -6,6 +6,27 @@
 // (C) 2002 Jackson Harper, All rights reserved.
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.IO;
 using System.Text;
@@ -15,9 +36,10 @@ using System.Text.RegularExpressions;
 
 namespace Microsoft.CSharp {
 
+       [System.Obsolete]
        public class Compiler {
                
-               public Compiler()
+               private Compiler()
                {
                }
 
@@ -30,11 +52,20 @@ namespace Microsoft.CSharp {
                        
                        string[] temp_cs_files;
                        CompilerError[] errors;
-
+                       string bugreport_path = null;   
+                       StreamWriter bug_report = null;
+                       
                        temp_cs_files = CreateCsFiles (sourceTexts, sourceTextNames);
                        
+                       if (options != null)
+                               bugreport_path = (string)options["bugreport"];  
+                       
+                       if (bugreport_path != null) {
+                               bug_report = CreateBugReport (sourceTexts, sourceTextNames, bugreport_path);
+                       }                       
+
                        try {
-                               errors = CompileFiles (temp_cs_files, target, imports, options);
+                               errors = CompileFiles (temp_cs_files, target, imports, options, bug_report);
                        } catch {
                                throw;
                        } finally {
@@ -42,6 +73,8 @@ namespace Microsoft.CSharp {
                                        FileInfo file = new FileInfo (temp_file);
                                        file.Delete ();
                                }
+                               if (bug_report != null)
+                                       bug_report.Close ();
                        }
                        
                        return errors;
@@ -52,7 +85,7 @@ namespace Microsoft.CSharp {
                //
 
                private static CompilerError[] CompileFiles (string[] cs_files,
-                       string target, string[] imports, IDictionary options) 
+                       string target, string[] imports, IDictionary options, StreamWriter bug_report
                {
                        ArrayList error_list = new ArrayList ();
                        Process mcs = new Process ();
@@ -65,10 +98,12 @@ namespace Microsoft.CSharp {
                        mcs.StartInfo.CreateNoWindow = true;
                        mcs.StartInfo.UseShellExecute = false;
                        mcs.StartInfo.RedirectStandardOutput = true;
+                       mcs.StartInfo.RedirectStandardError = true;
 
                        try {
                                mcs.Start ();
-                               mcs_output = mcs.StandardOutput.ReadToEnd();
+                               mcs_output = mcs.StandardError.ReadToEnd ();
+                               mcs.StandardOutput.ReadToEnd ();
                                mcs.WaitForExit ();
                        } finally {
                                mcs.Close ();
@@ -82,6 +117,11 @@ namespace Microsoft.CSharp {
                                        error_list.Add (error); 
                        }
                        
+                       if (bug_report != null) {
+                               bug_report.WriteLine ("### Compiler Output");
+                               bug_report.Write (mcs_output);
+                       }
+
                        return (CompilerError[])error_list.ToArray (typeof(CompilerError));
                }
 
@@ -152,6 +192,8 @@ namespace Microsoft.CSharp {
                        if (null != options) {
                                foreach (object option in options.Keys) {
                                        object value = options[option];
+                                       if (!ValidOption ((string)option))
+                                               continue;
                                        args.AppendFormat ("{0} ", OptionString (option,value));
                                }
                        }
@@ -184,6 +226,64 @@ namespace Microsoft.CSharp {
                                throw new IndexOutOfRangeException ();
                }
 
+               private static StreamWriter CreateBugReport (string[] source_texts, 
+                       string[] source_names, string path)
+               {
+                       StreamWriter bug_report = null;
+
+                       try {
+                               bug_report = new StreamWriter (path);
+                               bug_report.WriteLine ("### C# Compiler Defect Report," + 
+                                       " created {0}", DateTime.Now);
+                               // Compiler Version
+                               // Runtime
+                               // Operating System
+                               // Username
+                               for (int i=0; i<source_texts.Length; i++) {
+                                       bug_report.WriteLine ("### Source file: '{0}'",
+                                               source_names[i]);
+                                       bug_report.Write (source_texts[i]);
+                               }
+                       } catch {
+                               if (bug_report != null)
+                                       bug_report.Close ();
+                               throw;
+                       }
+                       
+                       return bug_report;
+               }
+
+
+               private static bool ValidOption (string option)
+               {
+                       switch (option) {
+                               case "addmodule":
+                               case "baseaddress":
+                               case "checked":
+                               case "d":
+                               case "debug":
+                               case "doc":
+                               case "filealign":
+                               case "incr":
+                               case "lib":
+                               case "linkres":
+                               case "m":
+                               case "nostdlib":
+                               case "nowarn":
+                               case "o":
+                               case "r":
+                               case "res":
+                               case "target":
+                               case "unsafe":
+                               case "w":
+                               case "warnaserror":
+                               case "win32icon":
+                               case "win32res":
+                                       return true;
+                       }
+                       return false;
+               }
+
        }
 
 }