2
0

CodeCompiler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.IO;
  32. using System.Text;
  33. using System.Reflection;
  34. using System.Collections;
  35. using System.Collections.Specialized;
  36. using System.Diagnostics;
  37. using System.Security.Permissions;
  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. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  107. private CompilerResults Compile (CompilerParameters options, string[] fileNames, bool keepFiles)
  108. {
  109. if (null == options)
  110. throw new ArgumentNullException ("options");
  111. if (null == fileNames)
  112. throw new ArgumentNullException ("fileNames");
  113. options.TempFiles = new TempFileCollection (Path.GetTempPath ());
  114. foreach (string file in fileNames) {
  115. options.TempFiles.AddFile (file, keepFiles);
  116. }
  117. options.TempFiles.KeepFiles = keepFiles;
  118. string std_output = String.Empty;
  119. string err_output = String.Empty;
  120. string cmd = String.Concat (CompilerName, " ", CmdArgsFromParameters (options));
  121. CompilerResults results = new CompilerResults (new TempFileCollection ());
  122. results.NativeCompilerReturnValue = Executor.ExecWaitWithCapture (cmd,
  123. options.TempFiles, ref std_output, ref err_output);
  124. string[] compiler_output_lines = std_output.Split (Environment.NewLine.ToCharArray ());
  125. foreach (string error_line in compiler_output_lines)
  126. ProcessCompilerOutputLine (results, error_line);
  127. if (results.Errors.Count == 0)
  128. results.PathToAssembly = options.OutputAssembly;
  129. return results;
  130. }
  131. [MonoTODO]
  132. protected virtual string GetResponseFileCmdArgs (CompilerParameters options, string cmdArgs)
  133. {
  134. // FIXME I'm not sure what this function should do...
  135. throw new NotImplementedException ();
  136. }
  137. CompilerResults ICodeCompiler.CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
  138. {
  139. return FromDom (options, e);
  140. }
  141. CompilerResults ICodeCompiler.CompileAssemblyFromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
  142. {
  143. return FromDomBatch (options, ea);
  144. }
  145. CompilerResults ICodeCompiler.CompileAssemblyFromFile (CompilerParameters options, string fileName)
  146. {
  147. return FromFile (options, fileName);
  148. }
  149. CompilerResults ICodeCompiler.CompileAssemblyFromFileBatch (CompilerParameters options, string[] fileNames)
  150. {
  151. return FromFileBatch (options, fileNames);
  152. }
  153. CompilerResults ICodeCompiler.CompileAssemblyFromSource (CompilerParameters options, string source)
  154. {
  155. return FromSource (options, source);
  156. }
  157. CompilerResults ICodeCompiler.CompileAssemblyFromSourceBatch (CompilerParameters options, string[] sources)
  158. {
  159. return FromSourceBatch (options, sources);
  160. }
  161. protected static string JoinStringArray (string[] sa, string separator)
  162. {
  163. StringBuilder sb = new StringBuilder ();
  164. int length = sa.Length;
  165. if (length > 1) {
  166. for (int i=0; i < length - 1; i++) {
  167. sb.Append ("\"");
  168. sb.Append (sa [i]);
  169. sb.Append ("\"");
  170. sb.Append (separator);
  171. }
  172. }
  173. if (length > 0) {
  174. sb.Append ("\"");
  175. sb.Append (sa [length - 1]);
  176. sb.Append ("\"");
  177. }
  178. return sb.ToString ();
  179. }
  180. protected abstract void ProcessCompilerOutputLine (CompilerResults results, string line);
  181. }
  182. }