CodeCompiler.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.CodeDom.Compiler.CodeCompiler.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2002 Jackson Harper, All rights reserved
  9. // (C) 2003 Andreas Nahr
  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. using System;
  32. using System.IO;
  33. using System.Text;
  34. using System.Reflection;
  35. using System.Collections;
  36. using System.Collections.Specialized;
  37. using System.Diagnostics;
  38. namespace System.CodeDom.Compiler {
  39. public abstract class CodeCompiler : CodeGenerator, ICodeCompiler
  40. {
  41. protected CodeCompiler ()
  42. {
  43. }
  44. protected abstract string CompilerName {
  45. get;
  46. }
  47. protected abstract string FileExtension {
  48. get;
  49. }
  50. protected abstract string CmdArgsFromParameters (CompilerParameters options);
  51. protected virtual CompilerResults FromDom (CompilerParameters options, CodeCompileUnit e)
  52. {
  53. return FromDomBatch (options, new CodeCompileUnit[]{e});
  54. }
  55. protected virtual CompilerResults FromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
  56. {
  57. string[] fileNames = new string[ea.Length];
  58. int i = 0;
  59. if (options == null)
  60. options = new CompilerParameters ();
  61. StringCollection assemblies = options.ReferencedAssemblies;
  62. foreach (CodeCompileUnit e in ea) {
  63. fileNames[i] = Path.ChangeExtension (Path.GetTempFileName(), FileExtension);
  64. FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
  65. StreamWriter s = new StreamWriter (f);
  66. if (e.ReferencedAssemblies != null) {
  67. foreach (string str in e.ReferencedAssemblies) {
  68. if (!assemblies.Contains (str))
  69. assemblies.Add (str);
  70. }
  71. }
  72. ((ICodeGenerator)this).GenerateCodeFromCompileUnit (e, s, new CodeGeneratorOptions());
  73. s.Close();
  74. f.Close();
  75. i++;
  76. }
  77. return Compile (options, fileNames, false);
  78. }
  79. protected virtual CompilerResults FromFile (CompilerParameters options, string fileName)
  80. {
  81. return FromFileBatch (options, new string[] {fileName});
  82. }
  83. protected virtual CompilerResults FromFileBatch (CompilerParameters options, string[] fileNames)
  84. {
  85. return Compile (options, fileNames, true);
  86. }
  87. protected virtual CompilerResults FromSource (CompilerParameters options, string source)
  88. {
  89. return FromSourceBatch(options, new string[]{source});
  90. }
  91. protected virtual CompilerResults FromSourceBatch (CompilerParameters options, string[] sources)
  92. {
  93. string[] fileNames = new string[sources.Length];
  94. int i = 0;
  95. foreach (string source in sources) {
  96. fileNames[i] = Path.ChangeExtension (Path.GetTempFileName(), FileExtension);
  97. FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
  98. StreamWriter s = new StreamWriter (f);
  99. s.Write (source);
  100. s.Close ();
  101. f.Close ();
  102. i++;
  103. }
  104. return Compile (options, fileNames, false);
  105. }
  106. private CompilerResults Compile (CompilerParameters options, string[] fileNames, bool keepFiles)
  107. {
  108. if (null == options)
  109. throw new ArgumentNullException ("options");
  110. if (null == fileNames)
  111. throw new ArgumentNullException ("fileNames");
  112. options.TempFiles = new TempFileCollection ();
  113. foreach (string file in fileNames)
  114. {
  115. options.TempFiles.AddFile (file, keepFiles);
  116. }
  117. options.TempFiles.KeepFiles = keepFiles;
  118. CompilerResults results = new CompilerResults (new TempFileCollection());
  119. // FIXME this should probably be done by the System.CodeDom.Compiler.Executor class
  120. Process compiler = new Process();
  121. string compiler_output;
  122. string[] compiler_output_lines;
  123. compiler.StartInfo.FileName = CompilerName;
  124. compiler.StartInfo.Arguments = CmdArgsFromParameters (options);
  125. compiler.StartInfo.CreateNoWindow = true;
  126. compiler.StartInfo.UseShellExecute = false;
  127. compiler.StartInfo.RedirectStandardOutput = true;
  128. try {
  129. compiler.Start();
  130. compiler_output = compiler.StandardOutput.ReadToEnd();
  131. compiler.WaitForExit();
  132. }
  133. finally {
  134. results.NativeCompilerReturnValue = compiler.ExitCode;
  135. compiler.Close();
  136. }
  137. // END FIXME
  138. compiler_output_lines = compiler_output.Split(
  139. System.Environment.NewLine.ToCharArray());
  140. foreach (string error_line in compiler_output_lines)
  141. ProcessCompilerOutputLine (results, error_line);
  142. if (results.Errors.Count == 0)
  143. results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
  144. else
  145. results.CompiledAssembly = null;
  146. return results;
  147. }
  148. [MonoTODO]
  149. protected virtual string GetResponseFileCmdArgs (CompilerParameters options, string cmdArgs)
  150. {
  151. // FIXME I'm not sure what this function should do...
  152. throw new NotImplementedException ();
  153. }
  154. CompilerResults ICodeCompiler.CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
  155. {
  156. return FromDom (options, e);
  157. }
  158. CompilerResults ICodeCompiler.CompileAssemblyFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
  159. {
  160. return FromDomBatch (options, ea);
  161. }
  162. CompilerResults ICodeCompiler.CompileAssemblyFromFile (CompilerParameters options, string fileName)
  163. {
  164. return FromFile (options, fileName);
  165. }
  166. CompilerResults ICodeCompiler.CompileAssemblyFromFileBatch (CompilerParameters options, string[] fileNames)
  167. {
  168. return FromFileBatch (options, fileNames);
  169. }
  170. CompilerResults ICodeCompiler.CompileAssemblyFromSource (CompilerParameters options, string source)
  171. {
  172. return FromSource (options, source);
  173. }
  174. CompilerResults ICodeCompiler.CompileAssemblyFromSourceBatch (CompilerParameters options, string[] sources)
  175. {
  176. return FromSourceBatch (options, sources);
  177. }
  178. protected static string JoinStringArray (string[] sa, string separator)
  179. {
  180. StringBuilder sb = new StringBuilder ();
  181. foreach (string s in sa)
  182. sb.Append (s + separator);
  183. return sb.ToString ();
  184. }
  185. protected abstract void ProcessCompilerOutputLine (CompilerResults results, string line);
  186. }
  187. }