Debian Codegraph @

external/cecil-legacy/Test/Mono.Cecil.Tests/CompilationService.cs

raw symbols SCIP
1 using System;
2 using System.CodeDom.Compiler;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Text;
7
8 using NUnit.Framework;
9
10 namespace Mono.Cecil.Tests {
11
12 struct CompilationResult {
13 internal DateTime source_write_time;
14 internal string result_file;
15
16 public CompilationResult (DateTime write_time, string result_file)
17 {
18 this.source_write_time = write_time;
19 this.result_file = result_file;
20 }
21 }
22
23 abstract class CompilationService {
24
25 Dictionary<string, CompilationResult> files = new Dictionary<string, CompilationResult> ();
26
27 bool TryGetResult (string name, out string file_result)
28 {
29 file_result = null;
30 CompilationResult result;
31 if (!files.TryGetValue (name, out result))
32 return false;
33
34 if (result.source_write_time != File.GetLastWriteTime (name))
35 return false;
36
37 file_result = result.result_file;
38 return true;
39 }
40
41 public string Compile (string name)
42 {
43 string result_file;
44 if (TryGetResult (name, out result_file))
45 return result_file;
46
47 result_file = CompileFile (name);
48 RegisterFile (name, result_file);
49 return result_file;
50 }
51
52 void RegisterFile (string name, string result_file)
53 {
54 files [name] = new CompilationResult (File.GetLastWriteTime (name), result_file);
55 }
56
57 protected abstract string CompileFile (string name);
58
59 public static string CompileResource (string name)
60 {
61 var extension = Path.GetExtension (name);
62 if (extension == ".il")
63 return IlasmCompilationService.Instance.Compile (name);
64
65 if (extension == ".cs" || extension == ".vb")
66 return CodeDomCompilationService.Instance.Compile (name);
67
68 throw new NotSupportedException (extension);
69 }
70
71 protected static string GetCompiledFilePath (string file_name)
72 {
73 var tmp_cecil = Path.Combine (Path.GetTempPath (), "cecil");
74 if (!Directory.Exists (tmp_cecil))
75 Directory.CreateDirectory (tmp_cecil);
76
77 return Path.Combine (tmp_cecil, Path.GetFileName (file_name) + ".dll");
78 }
79
80 static bool OnMono { get { return typeof (object).Assembly.GetType ("Mono.Runtime") != null; } }
81
82 public static void Verify (string name)
83 {
84 var output = OnMono ? ShellService.PEDump (name) : ShellService.PEVerify (name);
85 if (output.ExitCode != 0)
86 Assert.Fail (output.ToString ());
87 }
88 }
89
90 class IlasmCompilationService : CompilationService {
91
92 public static readonly IlasmCompilationService Instance = new IlasmCompilationService ();
93
94 protected override string CompileFile (string name)
95 {
96 string file = GetCompiledFilePath (name);
97
98 var output = ShellService.ILAsm (name, file);
99
100 AssertAssemblerResult (output);
101
102 return file;
103 }
104
105 static void AssertAssemblerResult (ShellService.ProcessOutput output)
106 {
107 if (output.ExitCode != 0)
108 Assert.Fail (output.ToString ());
109 }
110 }
111
112 class CodeDomCompilationService : CompilationService {
113
114 public static readonly CodeDomCompilationService Instance = new CodeDomCompilationService ();
115
116 protected override string CompileFile (string name)
117 {
118 string file = GetCompiledFilePath (name);
119
120 using (var provider = GetProvider (name)) {
121 var parameters = GetDefaultParameters (name);
122 parameters.IncludeDebugInformation = false;
123 parameters.GenerateExecutable = false;
124 parameters.OutputAssembly = file;
125
126 var results = provider.CompileAssemblyFromFile (parameters, name);
127 AssertCompilerResults (results);
128 }
129
130 return file;
131 }
132
133 static void AssertCompilerResults (CompilerResults results)
134 {
135 Assert.IsFalse (results.Errors.HasErrors, GetErrorMessage (results));
136 }
137
138 static string GetErrorMessage (CompilerResults results)
139 {
140 if (!results.Errors.HasErrors)
141 return string.Empty;
142
143 var builder = new StringBuilder ();
144 foreach (CompilerError error in results.Errors)
145 builder.AppendLine (error.ToString ());
146 return builder.ToString ();
147 }
148
149 static CompilerParameters GetDefaultParameters (string name)
150 {
151 return GetCompilerInfo (name).CreateDefaultCompilerParameters ();
152 }
153
154 static CodeDomProvider GetProvider (string name)
155 {
156 return GetCompilerInfo (name).CreateProvider ();
157 }
158
159 static CompilerInfo GetCompilerInfo (string name)
160 {
161 return CodeDomProvider.GetCompilerInfo (
162 CodeDomProvider.GetLanguageFromExtension (Path.GetExtension (name)));
163 }
164 }
165
166 class ShellService {
167
168 public class ProcessOutput {
169
170 public int ExitCode;
171 public string StdOut;
172 public string StdErr;
173
174 public ProcessOutput (int exitCode, string stdout, string stderr)
175 {
176 ExitCode = exitCode;
177 StdOut = stdout;
178 StdErr = stderr;
179 }
180
181 public override string ToString ()
182 {
183 return StdOut + StdErr;
184 }
185 }
186
187 static ProcessOutput RunProcess (string target, params string [] arguments)
188 {
189 var stdout = new StringWriter ();
190 var stderr = new StringWriter ();
191
192 var process = new Process {
193 StartInfo = new ProcessStartInfo {
194 FileName = target,
195 Arguments = string.Join (" ", arguments),
196 CreateNoWindow = true,
197 UseShellExecute = false,
198 RedirectStandardError = true,
199 RedirectStandardInput = true,
200 RedirectStandardOutput = true,
201 },
202 };
203
204 process.Start ();
205
206 process.OutputDataReceived += (_, args) => stdout.Write (args.Data);
207 process.ErrorDataReceived += (_, args) => stderr.Write (args.Data);
208
209 process.BeginOutputReadLine ();
210 process.BeginErrorReadLine ();
211
212 process.WaitForExit ();
213
214 return new ProcessOutput (process.ExitCode, stdout.ToString (), stderr.ToString ());
215 }
216
217 public static ProcessOutput ILAsm (string source, string output)
218 {
219 return RunProcess ("ilasm", "/nologo", "/dll", "/out:" + Quote (output), Quote (source));
220 }
221
222 static string Quote (string file)
223 {
224 return "\"" + file + "\"";
225 }
226
227 public static ProcessOutput PEVerify (string source)
228 {
229 return RunProcess ("peverify", "/nologo", Quote (source));
230 }
231
232 public static ProcessOutput PEDump (string source)
233 {
234 return RunProcess ("pedump", "--verify code,metadata", Quote (source));
235 }
236 }
237 }