This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Cscompmgd / Microsoft.CSharp / Compiler.cs
1 // Microsoft.CSharp.Compiler
2 //
3 // Author(s):
4 //  Jackson Harper (Jackson@LatitudeGeo.com)
5 //
6 // (C) 2002 Jackson Harper, All rights reserved.
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Text.RegularExpressions;
36
37 namespace Microsoft.CSharp {
38
39         public class Compiler {
40                 
41                 private Compiler()
42                 {
43                 }
44
45                 [MonoTODO("Have not implemented bugreports")]
46                 public static CompilerError[] Compile(string[] sourceTexts,
47                         string[] sourceTextNames, string target, string[] imports,
48                         IDictionary options)
49                 {
50                         VerifyArgs (sourceTexts, sourceTextNames, target);
51                         
52                         string[] temp_cs_files;
53                         CompilerError[] errors;
54                         string bugreport_path = null;   
55                         StreamWriter bug_report = null;
56                         
57                         temp_cs_files = CreateCsFiles (sourceTexts, sourceTextNames);
58                         
59                         if (options != null)
60                                 bugreport_path = (string)options["bugreport"];  
61                         
62                         if (bugreport_path != null) {
63                                 bug_report = CreateBugReport (sourceTexts, sourceTextNames, bugreport_path);
64                         }                       
65
66                         try {
67                                 errors = CompileFiles (temp_cs_files, target, imports, options, bug_report);
68                         } catch {
69                                 throw;
70                         } finally {
71                                 foreach (string temp_file in temp_cs_files) {
72                                         FileInfo file = new FileInfo (temp_file);
73                                         file.Delete ();
74                                 }
75                                 if (bug_report != null)
76                                         bug_report.Close ();
77                         }
78                         
79                         return errors;
80                 }
81                 
82                 //
83                 // Private Methods
84                 //
85
86                 private static CompilerError[] CompileFiles (string[] cs_files,
87                         string target, string[] imports, IDictionary options, StreamWriter bug_report) 
88                 {
89                         ArrayList error_list = new ArrayList ();
90                         Process mcs = new Process ();
91                         string mcs_output;
92                         string[] mcs_output_lines;
93
94                         mcs.StartInfo.FileName = "mcs";
95                         mcs.StartInfo.Arguments = BuildArgs (cs_files, 
96                                 target, imports, options);
97                         mcs.StartInfo.CreateNoWindow = true;
98                         mcs.StartInfo.UseShellExecute = false;
99                         mcs.StartInfo.RedirectStandardOutput = true;
100
101                         try {
102                                 mcs.Start ();
103                                 mcs_output = mcs.StandardOutput.ReadToEnd();
104                                 mcs.WaitForExit ();
105                         } finally {
106                                 mcs.Close ();
107                         }
108                         
109                         mcs_output_lines = mcs_output.Split (
110                                 System.Environment.NewLine.ToCharArray ());
111                         foreach (string error_line in mcs_output_lines) {
112                                 CompilerError error = CreateErrorFromString (error_line);
113                                 if (null != error)
114                                         error_list.Add (error); 
115                         }
116                         
117                         if (bug_report != null) {
118                                 bug_report.WriteLine ("### Compiler Output");
119                                 bug_report.Write (mcs_output);
120                         }
121
122                         return (CompilerError[])error_list.ToArray (typeof(CompilerError));
123                 }
124
125                 /// <summary>
126                 ///   Converts an error string into a CompilerError object
127                 ///   Return null if the line was not an error string
128                 /// </summary>
129                 private static CompilerError CreateErrorFromString(string error_string) 
130                 {
131                         CompilerError error = new CompilerError();
132                         Regex reg = new Regex (@"^((?<file>.*)\((?<line>\d*)(,(?<column>\d*))?\)\s){0,}(?<level>\w+)\sCS(?<number>\d*):\s(?<message>.*)", 
133                         RegexOptions.Compiled | RegexOptions.ExplicitCapture);
134
135                         Match match = reg.Match (error_string);
136                         
137                         if (!match.Success)
138                                 return null;
139                         
140                         if (String.Empty != match.Result ("${file}"))
141                                 error.SourceFile = match.Result ("${file}");
142                         if (String.Empty != match.Result ("${line}"))
143                                 error.SourceLine = Int32.Parse (match.Result ("${line}"));
144                         if (String.Empty != match.Result ("${column}"))
145                                 error.SourceColumn = Int32.Parse (match.Result ("${column}"));
146                         error.ErrorLevel = (ErrorLevel)Enum.Parse (typeof(ErrorLevel),
147                                 match.Result ("${level}"), true);
148                         error.ErrorNumber = Int32.Parse (match.Result ("${number}"));
149                         error.ErrorMessage = match.Result ("${message}");
150                         
151                         return error;
152                 }
153
154                 private static string[] CreateCsFiles (string[] source_text, string[] source_name) 
155                 {
156                         ArrayList temp_file_list = new ArrayList ();
157
158                         for (int i=0; i<source_text.Length; i++) {
159                                 string temp_path = Path.GetTempFileName ();
160                                 StreamWriter writer = null;
161                                 try {
162                                         writer = new StreamWriter (temp_path);
163                                         writer.WriteLine (String.Format ("#line 1 \"{0}\"", 
164                                                 source_name[i]));
165                                         writer.Write (source_text[i]);
166                                 } catch {
167                                 } finally {
168                                         if (writer != null)
169                                                 writer.Close ();
170                                 }
171                                 temp_file_list.Add (temp_path);
172                         }
173                 
174                         return (string[])temp_file_list.ToArray (typeof(string));       
175                 }
176
177                 private static string BuildArgs(string[] source_files,
178                         string target, string[] imports, IDictionary options)
179                 {
180                         StringBuilder args = new StringBuilder ();
181
182                         args.AppendFormat ("/out:{0} ", target);
183                         
184                         if (null != imports) {
185                                 foreach (string import in imports)
186                                         args.AppendFormat ("/r:{0} ", import);
187                         }
188                         
189                         if (null != options) {
190                                 foreach (object option in options.Keys) {
191                                         object value = options[option];
192                                         if (!ValidOption ((string)option))
193                                                 continue;
194                                         args.AppendFormat ("{0} ", OptionString (option,value));
195                                 }
196                         }
197                         
198                         foreach (string source in source_files)
199                                 args.AppendFormat ("{0} ", source);
200
201                         return args.ToString ();
202                 }
203
204                 private static string OptionString(object option, object value)
205                 {
206                         if (null != value)
207                                 return String.Format ("/{0}:{1}", option, value);
208                         
209                         return String.Format("/{0}", option);
210                 }
211
212                 private static void VerifyArgs (string[] sourceTexts,
213                         string[] sourceTextNames, string target)
214                 {
215                         if (null == sourceTexts)
216                                 throw new ArgumentNullException ("sourceTexts");
217                         if (null == sourceTextNames)
218                                 throw new ArgumentNullException ("sourceTextNames");
219                         if (null == target)
220                                 throw new ArgumentNullException ("target");
221
222                         if (sourceTexts.Length <= 0 || sourceTextNames.Length <= 0)
223                                 throw new IndexOutOfRangeException ();
224                 }
225
226                 private static StreamWriter CreateBugReport (string[] source_texts, 
227                         string[] source_names, string path)
228                 {
229                         StreamWriter bug_report = null;
230
231                         try {
232                                 bug_report = new StreamWriter (path);
233                                 bug_report.WriteLine ("### C# Compiler Defect Report," + 
234                                         " created {0}", DateTime.Now);
235                                 // Compiler Version
236                                 // Runtime
237                                 // Operating System
238                                 // Username
239                                 for (int i=0; i<source_texts.Length; i++) {
240                                         bug_report.WriteLine ("### Source file: '{0}'",
241                                                 source_names[i]);
242                                         bug_report.Write (source_texts[i]);
243                                 }
244                         } catch {
245                                 if (bug_report != null)
246                                         bug_report.Close ();
247                                 throw;
248                         }
249                         
250                         return bug_report;
251                 }
252
253
254                 private static bool ValidOption (string option)
255                 {
256                         switch (option) {
257                                 case "addmodule":
258                                 case "baseaddress":
259                                 case "checked":
260                                 case "d":
261                                 case "debug":
262                                 case "doc":
263                                 case "filealign":
264                                 case "incr":
265                                 case "lib":
266                                 case "linkres":
267                                 case "m":
268                                 case "nostdlib":
269                                 case "nowarn":
270                                 case "o":
271                                 case "r":
272                                 case "res":
273                                 case "target":
274                                 case "unsafe":
275                                 case "w":
276                                 case "warnaserror":
277                                 case "win32icon":
278                                 case "win32res":
279                                         return true;
280                         }
281                         return false;
282                 }
283
284         }
285
286 }
287