1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Linker.Tests.Extensions;
namespace Mono.Linker.Tests.TestCasesRunner {
public class TestCaseCompiler {
protected readonly TestCaseMetadaProvider _metadataProvider;
protected readonly TestCaseSandbox _sandbox;
protected readonly ILCompiler _ilCompiler;
public TestCaseCompiler (TestCaseSandbox sandbox, TestCaseMetadaProvider metadataProvider)
: this(sandbox, metadataProvider, new ILCompiler ())
{
}
public TestCaseCompiler (TestCaseSandbox sandbox, TestCaseMetadaProvider metadataProvider, ILCompiler ilCompiler)
{
_ilCompiler = ilCompiler;
_sandbox = sandbox;
_metadataProvider = metadataProvider;
}
public NPath CompileTestIn (NPath outputDirectory, string outputName, IEnumerable<string> sourceFiles, IEnumerable<string> references, IEnumerable<string> defines)
{
var originalReferences = references.Select (r => r.ToNPath ()).ToArray ();
var originalDefines = defines?.ToArray () ?? new string [0];
Prepare (outputDirectory);
var compiledReferences = CompileBeforeTestCaseAssemblies (outputDirectory, originalReferences, originalDefines).ToArray ();
var allTestCaseReferences = originalReferences.Concat (compiledReferences).ToArray ();
var options = CreateOptionsForTestCase (
outputDirectory.Combine (outputName),
sourceFiles.Select (s => s.ToNPath ()).ToArray (),
allTestCaseReferences,
originalDefines);
var testAssembly = CompileAssembly (options);
// The compile after step is used by tests to mess around with the input to the linker. Generally speaking, it doesn't seem like we would ever want to mess with the
// expectations assemblies because this would undermine our ability to inspect them for expected results during ResultChecking. The UnityLinker UnresolvedHandling tests depend on this
// behavior of skipping the after test compile
if (outputDirectory != _sandbox.ExpectationsDirectory)
CompileAfterTestCaseAssemblies (outputDirectory, originalReferences, originalDefines);
return testAssembly;
}
protected virtual void Prepare (NPath outputDirectory)
{
}
protected virtual CompilerOptions CreateOptionsForTestCase (NPath outputPath, NPath[] sourceFiles, NPath[] references, string[] defines)
{
return new CompilerOptions
{
OutputPath = outputPath,
SourceFiles = sourceFiles,
References = references,
Defines = defines.Concat (_metadataProvider.GetDefines ()).ToArray ()
};
}
protected virtual CompilerOptions CreateOptionsForSupportingAssembly (SetupCompileInfo setupCompileInfo, NPath outputDirectory, NPath[] sourceFiles, NPath[] references, string[] defines)
{
var allDefines = defines.Concat (setupCompileInfo.Defines ?? new string [0]).ToArray ();
var allReferences = references.Concat (setupCompileInfo.References?.Select (p => MakeSupportingAssemblyReferencePathAbsolute (outputDirectory, p)) ?? new NPath [0]).ToArray ();
return new CompilerOptions
{
OutputPath = outputDirectory.Combine (setupCompileInfo.OutputName),
SourceFiles = sourceFiles,
References = allReferences,
Defines = allDefines
};
}
private IEnumerable<NPath> CompileBeforeTestCaseAssemblies (NPath outputDirectory, NPath[] references, string[] defines)
{
foreach (var setupCompileInfo in _metadataProvider.GetSetupCompileAssembliesBefore ())
{
var options = CreateOptionsForSupportingAssembly (setupCompileInfo, outputDirectory, CollectSetupBeforeSourcesFiles (setupCompileInfo), references, defines);
var output = CompileAssembly (options);
if (setupCompileInfo.AddAsReference)
yield return output;
}
}
private void CompileAfterTestCaseAssemblies (NPath outputDirectory, NPath[] references, string[] defines)
{
foreach (var setupCompileInfo in _metadataProvider.GetSetupCompileAssembliesAfter ())
{
var options = CreateOptionsForSupportingAssembly (setupCompileInfo, outputDirectory, CollectSetupAfterSourcesFiles (setupCompileInfo), references, defines);
CompileAssembly (options);
}
}
private NPath[] CollectSetupBeforeSourcesFiles (SetupCompileInfo info)
{
return CollectSourceFilesFrom (_sandbox.BeforeReferenceSourceDirectoryFor (info.OutputName));
}
private NPath[] CollectSetupAfterSourcesFiles (SetupCompileInfo info)
{
return CollectSourceFilesFrom (_sandbox.AfterReferenceSourceDirectoryFor (info.OutputName));
}
private static NPath[] CollectSourceFilesFrom (NPath directory)
{
var sourceFiles = directory.Files ("*.cs").ToArray ();
if (sourceFiles.Length > 0)
return sourceFiles;
sourceFiles = directory.Files ("*.il").ToArray ();
if (sourceFiles.Length > 0)
return sourceFiles;
throw new FileNotFoundException ($"Didn't find any sources files in {directory}");
}
protected static NPath MakeSupportingAssemblyReferencePathAbsolute (NPath outputDirectory, string referenceFileName)
{
// Not a good idea to use a full path in a test, but maybe someone is trying to quickly test something locally
if (Path.IsPathRooted (referenceFileName))
return referenceFileName.ToNPath ();
var possiblePath = outputDirectory.Combine (referenceFileName);
if (possiblePath.FileExists ())
return possiblePath;
return referenceFileName.ToNPath();
}
protected NPath CompileAssembly (CompilerOptions options)
{
if (options.SourceFiles.Any (path => path.ExtensionWithDot == ".cs"))
return CompileCSharpAssembly (options);
if (options.SourceFiles.Any (path => path.ExtensionWithDot == ".il"))
return CompileIlAssembly (options);
throw new NotSupportedException ($"Unable to compile sources files with extension `{options.SourceFiles.First ().ExtensionWithDot}`");
}
protected virtual NPath CompileCSharpAssembly (CompilerOptions options)
{
var compilerOptions = CreateCodeDomCompilerOptions (options);
var provider = CodeDomProvider.CreateProvider ("C#");
var result = provider.CompileAssemblyFromFile (compilerOptions, options.SourceFiles.Select (p => p.ToString ()).ToArray ());
if (!result.Errors.HasErrors)
return compilerOptions.OutputAssembly.ToNPath ();
var errors = new StringBuilder ();
foreach (var error in result.Errors)
errors.AppendLine (error.ToString ());
throw new Exception ("Compilation errors: " + errors);
}
protected NPath CompileIlAssembly (CompilerOptions options)
{
return _ilCompiler.Compile (options);
}
private CompilerParameters CreateCodeDomCompilerOptions (CompilerOptions options)
{
var compilerParameters = new CompilerParameters
{
OutputAssembly = options.OutputPath.ToString (),
GenerateExecutable = options.OutputPath.FileName.EndsWith (".exe")
};
compilerParameters.CompilerOptions = options.Defines?.Aggregate (string.Empty, (buff, arg) => $"{buff} /define:{arg}");
compilerParameters.ReferencedAssemblies.AddRange (options.References.Select (r => r.ToString ()).ToArray ());
return compilerParameters;
}
}
}
|