-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathCompilationEngine.cs
325 lines (286 loc) · 14.5 KB
/
CompilationEngine.cs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2015-2024 The Neo Project.
//
// The Neo.Compiler.CSharp is free software distributed under the MIT
// software license, see the accompanying file LICENSE in the main directory
// of the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
extern alias scfx;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using BigInteger = System.Numerics.BigInteger;
namespace Neo.Compiler
{
public class CompilationEngine
{
internal Compilation? Compilation;
internal CompilationOptions Options { get; private set; }
private static readonly MetadataReference[] CommonReferences;
private static readonly Dictionary<string, MetadataReference> MetaReferences = new();
internal readonly Dictionary<INamedTypeSymbol, CompilationContext> Contexts = new(SymbolEqualityComparer.Default);
static CompilationEngine()
{
string coreDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;
CommonReferences = new MetadataReference[]
{
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.dll")),
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.InteropServices.dll")),
MetadataReference.CreateFromFile(typeof(string).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DisplayNameAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(BigInteger).Assembly.Location)
};
}
public CompilationEngine(CompilationOptions options)
{
Options = options;
}
public List<CompilationContext> Compile(IEnumerable<string> sourceFiles, IEnumerable<MetadataReference> references)
{
IEnumerable<SyntaxTree> syntaxTrees = sourceFiles.OrderBy(p => p).Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p), options: Options.GetParseOptions(), path: p));
CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary, deterministic: true, nullableContextOptions: Options.Nullable);
Compilation = CSharpCompilation.Create(null, syntaxTrees, references, compilationOptions);
return CompileProjectContracts(Compilation);
}
public List<CompilationContext> CompileSources(params string[] sourceFiles)
{
return CompileSources(new CompilationSourceReferences()
{
Packages = new (string, string)[] { new("Neo.SmartContract.Framework", "3.6.2-CI00520") }
},
sourceFiles);
}
public List<CompilationContext> CompileSources(CompilationSourceReferences references, params string[] sourceFiles)
{
// Generate a dummy csproj
var packageGroup = references.Packages is null ? "" : $@"
<ItemGroup>
{string.Join(Environment.NewLine, references!.Packages!.Select(u => $" <PackageReference Include =\"{u.packageName}\" Version=\"{u.packageVersion}\" />"))}
</ItemGroup>";
var projectsGroup = references.Projects is null ? "" : $@"
<ItemGroup>
{string.Join(Environment.NewLine, references!.Projects!.Select(u => $" <ProjectReference Include =\"{u}\"/>"))}
</ItemGroup>";
var csproj = $@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>{AppContext.TargetFrameworkName!}</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<!-- Remove all Compile items from compilation -->
<ItemGroup>
<Compile Remove=""*.cs"" />
</ItemGroup>
<!-- Add specific files for compilation -->
<ItemGroup>
{string.Join(Environment.NewLine, sourceFiles.Select(u => $"<Compile Include=\"{Path.GetFullPath(u)}\" />"))}
</ItemGroup>
{packageGroup}
{projectsGroup}
</Project>";
// Write and compile
var path = Path.GetTempFileName();
File.WriteAllText(path, csproj);
try
{
return CompileProject(path);
}
catch { throw; }
finally { File.Delete(path); }
}
public List<CompilationContext> CompileProject(string csproj)
{
Compilation = GetCompilation(csproj);
return CompileProjectContracts(Compilation);
}
private List<CompilationContext> CompileProjectContracts(Compilation compilation)
{
var classDependencies = new Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>>(SymbolEqualityComparer.Default);
var allSmartContracts = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
foreach (var tree in compilation.SyntaxTrees)
{
var semanticModel = compilation.GetSemanticModel(tree);
var classNodes = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>();
foreach (var classNode in classNodes)
{
var classSymbol = semanticModel.GetDeclaredSymbol(classNode);
if (classSymbol is { IsAbstract: false, DeclaredAccessibility: Accessibility.Public } && IsDerivedFromSmartContract(classSymbol, "Neo.SmartContract.Framework.SmartContract", semanticModel))
{
allSmartContracts.Add(classSymbol);
classDependencies[classSymbol] = new List<INamedTypeSymbol>();
foreach (var member in classSymbol.GetMembers())
{
var memberTypeSymbol = (member as IFieldSymbol)?.Type ?? (member as IPropertySymbol)?.Type;
if (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol && allSmartContracts.Contains(namedTypeSymbol) && !namedTypeSymbol.IsAbstract)
{
classDependencies[classSymbol].Add(namedTypeSymbol);
}
}
}
}
}
// Verify if there is any valid smart contract class
if (classDependencies.Count == 0) throw new FormatException("No valid neo SmartContract found. Please make sure your contract is subclass of SmartContract and is not abstract.");
// Check contract dependencies, make sure there is no cycle in the dependency graph
var sortedClasses = TopologicalSort(classDependencies);
sortedClasses.ForEach(c =>
{
var context = new CompilationContext(this, c);
context.Compile();
// Process the target contract add this compilation context
Contexts.Add(c, context);
});
return Contexts.Select(p => p.Value).ToList();
}
/// <summary>
/// Sort the classes based on their topological dependencies
/// </summary>
/// <param name="dependencies">Contract dependencies map</param>
/// <returns>List of sorted contracts</returns>
/// <exception cref="InvalidOperationException"></exception>
private static List<INamedTypeSymbol> TopologicalSort(Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>> dependencies)
{
var sorted = new List<INamedTypeSymbol>();
var visited = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
var visiting = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default); // for detecting cycles
void Visit(INamedTypeSymbol classSymbol)
{
if (visited.Contains(classSymbol))
{
return;
}
if (!visiting.Add(classSymbol))
{
throw new InvalidOperationException("Cyclic dependency detected");
}
if (dependencies.TryGetValue(classSymbol, out var dependency))
{
foreach (var dep in dependency)
{
Visit(dep);
}
}
visiting.Remove(classSymbol);
visited.Add(classSymbol);
sorted.Add(classSymbol);
}
foreach (var classSymbol in dependencies.Keys)
{
Visit(classSymbol);
}
return sorted;
}
static bool IsDerivedFromSmartContract(INamedTypeSymbol classSymbol, string smartContractFullyQualifiedName, SemanticModel semanticModel)
{
var baseType = classSymbol.BaseType;
while (baseType != null)
{
if (baseType.ToDisplayString() == smartContractFullyQualifiedName)
{
return true;
}
baseType = baseType.BaseType;
}
return false;
}
public Compilation GetCompilation(string csproj)
{
// Restore project
string folder = Path.GetDirectoryName(csproj)!;
Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"restore \"{csproj}\" --source \"https://www.myget.org/F/neo/api/v3/index.json\"",
WorkingDirectory = folder
})!.WaitForExit();
// Parse csproj
XDocument document = XDocument.Load(csproj);
var remove = document.Root!.Elements("ItemGroup").Elements("Compile").Attributes("Remove")
.Select(p => p.Value.Contains("*") ? p.Value : Path.GetFullPath(p.Value)).ToArray();
var sourceFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (!remove.Contains("*.cs"))
{
var obj = Path.Combine(folder, "obj");
var binSc = Path.Combine(folder, "bin");
foreach (var entry in Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories)
.Where(p => !p.StartsWith(obj) && !p.StartsWith(binSc))
.Select(u => u))
//.GroupBy(Path.GetFileName)
//.Select(g => g.First()))
{
if (!remove.Contains(entry)) sourceFiles.Add(entry);
}
}
sourceFiles.UnionWith(document.Root!.Elements("ItemGroup").Elements("Compile").Attributes("Include").Select(p => Path.GetFullPath(p.Value, folder)));
var assetsPath = Path.Combine(folder, "obj", "project.assets.json");
var assets = (JObject)JToken.Parse(File.ReadAllBytes(assetsPath))!;
List<MetadataReference> references = new(CommonReferences);
CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary, deterministic: true, nullableContextOptions: Options.Nullable);
foreach (var (name, package) in ((JObject)assets["targets"]![0]!).Properties)
{
MetadataReference? reference = GetReference(name, (JObject)package!, assets, folder, compilationOptions);
if (reference is not null) references.Add(reference);
}
IEnumerable<SyntaxTree> syntaxTrees = sourceFiles.OrderBy(p => p).Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p), options: Options.GetParseOptions(), path: p));
return CSharpCompilation.Create(assets["project"]!["restore"]!["projectName"]!.GetString(), syntaxTrees, references, compilationOptions);
}
private MetadataReference? GetReference(string name, JObject package, JObject assets, string folder, CSharpCompilationOptions compilationOptions)
{
if (!MetaReferences.TryGetValue(name, out var reference))
{
switch (assets["libraries"]![name]!["type"]!.GetString())
{
case "package":
string packagesPath = assets["project"]!["restore"]!["packagesPath"]!.GetString();
string namePath = assets["libraries"]![name]!["path"]!.GetString();
string[] files = ((JArray)assets["libraries"]![name]!["files"]!)
.Select(p => p!.GetString())
.Where(p => p.StartsWith("src/"))
.ToArray();
if (files.Length == 0)
{
JObject? dllFiles = (JObject?)(package["compile"] ?? package["runtime"]);
if (dllFiles is null) return null;
foreach (var (file, _) in dllFiles.Properties)
{
if (file.EndsWith("_._")) continue;
string path = Path.Combine(packagesPath, namePath, file);
if (!File.Exists(path)) continue;
reference = MetadataReference.CreateFromFile(path);
break;
}
if (reference is null) return null;
}
else
{
string assemblyName = Path.GetDirectoryName(name)!;
IEnumerable<SyntaxTree> st = files.OrderBy(p => p).Select(p => Path.Combine(packagesPath, namePath, p)).Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p), path: p));
CSharpCompilation cr = CSharpCompilation.Create(assemblyName, st, CommonReferences, compilationOptions);
reference = cr.ToMetadataReference();
}
break;
case "project":
string msbuildProject = assets["libraries"]![name]!["msbuildProject"]!.GetString();
msbuildProject = Path.GetFullPath(msbuildProject, folder);
reference = GetCompilation(msbuildProject).ToMetadataReference();
break;
default:
throw new NotSupportedException();
}
MetaReferences.Add(name, reference);
}
return reference;
}
}
}