forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalCSharpCompiler.cs
More file actions
118 lines (102 loc) · 4.33 KB
/
Copy pathExternalCSharpCompiler.cs
File metadata and controls
118 lines (102 loc) · 4.33 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Unity.CompilationPipeline.Common;
using UnityEditor.Scripting.ScriptCompilation;
using UnityEngine;
namespace UnityEditor.Scripting.Compilers
{
internal class ExternalCSharpCompiler : ScriptCompilerBase
{
private readonly ScriptAssembly m_Assembly;
private readonly string m_TempOutputDirectory;
private CompilerBase m_ExternalCompiler;
public ExternalCSharpCompiler(ScriptAssembly assembly, string tempOutputDirectory)
: base(assembly, tempOutputDirectory)
{
m_Assembly = assembly;
m_TempOutputDirectory = tempOutputDirectory;
m_ExternalCompiler = CreateExternalCompilerBase();
}
public static bool HasExternalCompiler()
{
return GetExternalCompilerType().Any();
}
public static string ExternalCompilerName()
{
var typesDerivedFrom = GetExternalCompilerType()[0];
var creationTime = File.GetCreationTime(typesDerivedFrom.Assembly.Location);
return typesDerivedFrom.Assembly.FullName + creationTime.Ticks;
}
private static CompilerBase CreateExternalCompilerBase()
{
var typesDerivedFrom = GetExternalCompilerType();
if (typesDerivedFrom.Count > 1)
{
var compilerBaseImplementations = typesDerivedFrom.Select(x => x.FullName).ToArray();
throw new Exception($"More than 1 external C# compiler found: {string.Join(",", compilerBaseImplementations)}");
}
var externalCompilerType = typesDerivedFrom[0];
Console.WriteLine($"External compiler found: {externalCompilerType.FullName}");
return (CompilerBase)Activator.CreateInstance(externalCompilerType);
}
private static IList<Type> GetExternalCompilerType()
{
var typesDerivedFrom = TypeCache.GetTypesDerivedFrom<CompilerBase>();
return typesDerivedFrom;
}
private static AssemblyInfo ScriptAssemblyToAssemblyInfo(ScriptAssembly scriptAssembly, string outputDir)
{
return new AssemblyInfo
{
Name = scriptAssembly.Filename,
Files = scriptAssembly.Files.Select(Path.GetFullPath).ToArray(),
Defines = scriptAssembly.Defines,
References = scriptAssembly.GetAllReferences().Select(Path.GetFullPath).ToArray(),
OutputDirectory = Path.GetFullPath(outputDir),
AllowUnsafeCode = scriptAssembly.CompilerOptions.AllowUnsafeCode,
};
}
public override void Dispose()
{
m_ExternalCompiler.Dispose();
}
public override void WaitForCompilationToFinish()
{
m_ExternalCompiler.WaitForCompilationToFinish();
}
public override void BeginCompiling()
{
var assemblyInfo = ScriptAssemblyToAssemblyInfo(m_Assembly, m_TempOutputDirectory);
var systemReferenceDirectories = MonoLibraryHelpers.GetSystemReferenceDirectories(m_Assembly.CompilerOptions.ApiCompatibilityLevel);
m_ExternalCompiler.BeginCompiling(assemblyInfo, m_Assembly.CompilerOptions.ResponseFiles, SystemInfo.operatingSystemFamily, systemReferenceDirectories);
}
public override bool Poll()
{
return m_ExternalCompiler.Poll();
}
public override CompilerMessage[] GetCompilerMessages()
{
return m_ExternalCompiler.GetCompilerMessages()
.Select(x => new CompilerMessage()
{
column = x.column,
file = x.file,
line = x.line,
message = x.message,
type = x.type == Compilation.CompilerMessageType.Error ? CompilerMessageType.Error : CompilerMessageType.Warning,
assemblyName = m_Assembly.Filename,
})
.ToArray();
}
public override ProcessStartInfo GetProcessStartInfo()
{
return null;
}
}
}