forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCompilerBase.cs
More file actions
256 lines (202 loc) · 8.32 KB
/
Copy pathScriptCompilerBase.cs
File metadata and controls
256 lines (202 loc) · 8.32 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
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
// 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.IO;
using System.Linq;
using UnityEngine;
using UnityEditor.Utils;
namespace UnityEditor.Scripting.Compilers
{
internal abstract class ScriptCompilerBase : IDisposable
{
private Program process;
private string _responseFile = null;
private bool _runAPIUpdater;
// ToDo: would be nice to move MonoIsland to MonoScriptCompilerBase
protected MonoIsland _island;
protected abstract Program StartCompiler();
protected abstract CompilerOutputParserBase CreateOutputParser();
protected ScriptCompilerBase(MonoIsland island, bool runAPIUpdater)
{
_island = island;
_runAPIUpdater = runAPIUpdater;
}
protected string[] GetErrorOutput()
{
return process.GetErrorOutput();
}
protected string[] GetStandardOutput()
{
return process.GetStandardOutput();
}
public void BeginCompiling()
{
if (process != null)
throw new InvalidOperationException("Compilation has already begun!");
process = StartCompiler();
}
public virtual void Dispose()
{
if (process != null)
{
process.Dispose();
process = null;
}
if (_responseFile != null)
{
File.Delete(_responseFile);
_responseFile = null;
}
}
public virtual bool Poll()
{
if (process == null)
return true;
return process.HasExited;
}
public void WaitForCompilationToFinish()
{
process.WaitForExit();
}
protected string GetMonoProfileLibDirectory()
{
var profile = BuildPipeline.CompatibilityProfileToClassLibFolder(_island._api_compatibility_level);
var monoInstall = (PlayerSettingsEditor.IsLatestApiCompatibility(_island._api_compatibility_level))
? MonoInstallationFinder.MonoBleedingEdgeInstallation
: MonoInstallationFinder.MonoInstallation;
return MonoInstallationFinder.GetProfileDirectory(profile, monoInstall);
}
protected bool AddCustomResponseFileIfPresent(List<string> arguments, string responseFileName)
{
var relativeCustomResponseFilePath = Path.Combine("Assets", responseFileName);
if (!File.Exists(relativeCustomResponseFilePath))
return false;
arguments.Add("@" + relativeCustomResponseFilePath);
return true;
}
protected string GenerateResponseFile(List<string> arguments)
{
_responseFile = CommandLineFormatter.GenerateResponseFile(arguments);
return _responseFile;
}
public static string[] GetResponseFileDefinesFromFile(string responseFileName)
{
var relativeCustomResponseFilePath = Path.Combine("Assets", responseFileName);
if (!File.Exists(relativeCustomResponseFilePath))
return new string[0];
var responseFileText = File.ReadAllText(relativeCustomResponseFilePath);
return GetResponseFileDefinesFromText(responseFileText);
}
public static string[] GetResponseFileDefinesFromText(string responseFileText)
{
const string defineString = "-define:";
var defineStringLength = defineString.Length;
if (!responseFileText.Contains(defineString))
return new string[0];
List<string> result = new List<string>();
var textLines = responseFileText.Split(' ', '\n');
foreach (var line in textLines)
{
var trimmedLine = line.Trim();
if (trimmedLine.StartsWith(defineString))
{
var definesSubString = trimmedLine.Substring(defineStringLength);
var defines = definesSubString.Split(',', ';');
result.AddRange(defines);
}
}
return result.ToArray();
}
protected static string PrepareFileName(string fileName)
{
return CommandLineFormatter.PrepareFileName(fileName);
}
//do not change the returntype, native unity depends on this one.
public virtual CompilerMessage[] GetCompilerMessages()
{
if (!Poll())
Debug.LogWarning("Compile process is not finished yet. This should not happen.");
DumpStreamOutputToLog();
return CreateOutputParser().Parse(GetStreamContainingCompilerMessages(), CompilationHadFailure()).ToArray();
}
protected bool CompilationHadFailure()
{
return (process.ExitCode != 0);
}
protected virtual string[] GetStreamContainingCompilerMessages()
{
List<string> errors = new List<string>();
errors.AddRange(GetErrorOutput());
errors.Add(string.Empty);
errors.AddRange(GetStandardOutput());
return errors.ToArray();
}
private void DumpStreamOutputToLog()
{
bool hadCompilationFailure = CompilationHadFailure();
string[] errorOutput = GetErrorOutput();
if (hadCompilationFailure || errorOutput.Length != 0)
{
Console.WriteLine("");
Console.WriteLine("-----Compiler Commandline Arguments:");
process.LogProcessStartInfo();
string[] stdOutput = GetStandardOutput();
Console.WriteLine("-----CompilerOutput:-stdout--exitcode: " + process.ExitCode + "--compilationhadfailure: " + hadCompilationFailure + "--outfile: " + _island._output);
foreach (string line in stdOutput)
Console.WriteLine(line);
Console.WriteLine("-----CompilerOutput:-stderr----------");
foreach (string line in errorOutput)
Console.WriteLine(line);
Console.WriteLine("-----EndCompilerOutput---------------");
}
}
protected void RunAPIUpdaterIfRequired(string responseFile)
{
if (!_runAPIUpdater)
return;
APIUpdaterHelper.UpdateScripts(responseFile, _island.GetExtensionOfSourceFiles());
}
}
/// Normalized 'status' code for a [[CompilerMessage]]
internal enum NormalizedCompilerStatusCode
{
NotNormalized = 0,
/// Maps to C# CS0117 and Boo BCE0019.
MemberNotFound = 1, // details syntax: TypeNamespaceQualifiedName:MemberName
// Maps to C# CS0246/CS0234 and Boo XXXX
UnknownTypeOrNamespace // details syntax: typename or namespace.typename
}
internal struct NormalizedCompilerStatus
{
public NormalizedCompilerStatusCode code;
/// each normalized compiler status defines the syntax of the details
public string details;
}
/// Marks the type of a [[CompilerMessage]]
internal enum CompilerMessageType
{
/// The message is an error. The compilation has failed.
Error = 0,
/// The message is an warning only. If there are no error messages, the compilation has completed successfully.
Warning = 1
}
/// This struct should be returned from GetCompilerMessages() on ScriptCompilerBase implementations
internal struct CompilerMessage
{
/// The text of the error or warning message
public string message;
/// The path name of the file the message refers to
public string file;
/// The line in the source file the message refers to
public int line;
/// The column of the line the message refers to
public int column;
/// The type of the message. Either Error or Warning
public CompilerMessageType type;
/// The normalized status. Each class deriving from ScriptCompilerBase must map errors / warning #
/// if it can be mapped to a NormalizedCompilerStatusCode.
public NormalizedCompilerStatus normalizedStatus;
}
}