forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicrosoftResponseFileParser.cs
More file actions
288 lines (245 loc) · 9.92 KB
/
Copy pathMicrosoftResponseFileParser.cs
File metadata and controls
288 lines (245 loc) · 9.92 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
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
// 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 UnityEditor.Compilation;
using UnityEditor.Utils;
using UnityEngine;
namespace UnityEditor.Scripting.Compilers
{
internal static class MicrosoftResponseFileParser
{
static readonly char[] CompilerOptionArgumentSeperators = { ';', ',' };
public static ResponseFileData ParseResponseFileFromFile(
string responseFilePath,
string projectDirectory,
string[] systemReferenceDirectories)
{
responseFilePath = Paths.ConvertSeparatorsToUnity(responseFilePath);
projectDirectory = Paths.ConvertSeparatorsToUnity(projectDirectory);
var relativeResponseFilePath = GetRelativePath(responseFilePath, projectDirectory);
var responseFile = AssetDatabase.LoadAssetAtPath<TextAsset>(relativeResponseFilePath);
if (!responseFile && File.Exists(responseFilePath))
{
var responseFileText = File.ReadAllText(responseFilePath);
return ParseResponseFileText(
responseFileText,
responseFilePath,
projectDirectory,
systemReferenceDirectories);
}
if (!responseFile)
{
var empty = new ResponseFileData
{
Defines = new string[0],
FullPathReferences = new string[0],
Unsafe = false,
Errors = new string[0],
OtherArguments = new string[0],
};
return empty;
}
return ParseResponseFileText(
responseFile.text,
responseFile.name,
projectDirectory,
systemReferenceDirectories);
}
static string GetRelativePath(string responseFilePath, string projectDirectory)
{
if (Path.IsPathRooted(responseFilePath) && responseFilePath.Contains(projectDirectory))
{
responseFilePath = responseFilePath.Substring(projectDirectory.Length + 1);
}
return responseFilePath;
}
static ResponseFileData ParseResponseFileText(
string fileContent,
string fileName,
string projectDirectory,
string[] systemReferenceDirectories)
{
var compilerOptions = new List<ScriptCompilerBase.CompilerOption>();
var responseFileStrings = ResponseFileTextToStrings(fileContent);
foreach (var line in responseFileStrings)
{
int idx = line.IndexOf(':');
string arg, value;
if (idx == -1)
{
arg = line;
value = "";
}
else
{
arg = line.Substring(0, idx);
value = line.Substring(idx + 1);
}
if (!string.IsNullOrEmpty(arg) && arg[0] == '-')
arg = '/' + arg.Substring(1);
compilerOptions.Add(new ScriptCompilerBase.CompilerOption { Arg = arg, Value = value });
}
var responseArguments = new List<string>();
var defines = new List<string>();
var references = new List<string>();
bool unsafeDefined = false;
var errors = new List<string>();
foreach (var option in compilerOptions)
{
var arg = option.Arg;
var value = option.Value;
switch (arg)
{
case "/d":
case "/define":
{
if (value.Length == 0)
{
errors.Add("No value set for define");
break;
}
var defs = value.Split(CompilerOptionArgumentSeperators);
foreach (string define in defs)
defines.Add(define.Trim());
}
break;
case "/r":
case "/reference":
{
if (value.Length == 0)
{
errors.Add("No value set for reference");
break;
}
string[] refs = value.Split(CompilerOptionArgumentSeperators);
if (refs.Length != 1)
{
errors.Add("Cannot specify multiple aliases using single /reference option");
break;
}
var reference = refs[0];
if (reference.Length == 0)
{
continue;
}
int index = reference.IndexOf('=');
var responseReference = index > -1 ? reference.Substring(index + 1) : reference;
var fullPathReference = responseReference;
bool isRooted = Path.IsPathRooted(responseReference);
if (!isRooted)
{
foreach (var directory in systemReferenceDirectories)
{
var systemReferencePath = Paths.Combine(directory, responseReference);
if (File.Exists(systemReferencePath))
{
fullPathReference = systemReferencePath;
isRooted = true;
break;
}
}
var userPath = Paths.Combine(projectDirectory, responseReference);
if (File.Exists(userPath))
{
fullPathReference = userPath;
isRooted = true;
}
}
if (!isRooted)
{
errors.Add($"{fileName}: not parsed correctly: {responseReference} could not be found as a system library.\n" +
"If this was meant as a user reference please provide the relative path from project root (parent of the Assets folder) in the response file.");
continue;
}
responseReference = fullPathReference.Replace('\\', '/');
references.Add(responseReference);
}
break;
case "/unsafe":
case "/unsafe+":
{
unsafeDefined = true;
}
break;
case "/unsafe-":
{
unsafeDefined = false;
}
break;
default:
var valueWithColon = value.Length == 0 ? "" : ":" + value;
responseArguments.Add(arg + valueWithColon);
break;
}
}
var responseFileData = new ResponseFileData
{
Defines = defines.ToArray(),
FullPathReferences = references.ToArray(),
Unsafe = unsafeDefined,
Errors = errors.ToArray(),
OtherArguments = responseArguments.ToArray(),
};
return responseFileData;
}
// From:
// https://github.com/mono/mono/blob/c106cdc775792ceedda6da58de7471f9f5c0b86c/mcs/mcs/settings.cs
//
// settings.cs: All compiler settings
//
// Author: Miguel de Icaza (miguel@ximian.com)
// Ravi Pratap (ravi@ximian.com)
// Marek Safar (marek.safar@gmail.com)
//
//
// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2001 Ximian, Inc (http://www.ximian.com)
// Copyright 2004-2008 Novell, Inc
// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
static string[] ResponseFileTextToStrings(string responseFileText)
{
var args = new List<string>();
var sb = new System.Text.StringBuilder();
var textLines = responseFileText.Split('\n', '\r');
foreach (var line in textLines)
{
int t = line.Length;
for (int i = 0; i < t; i++)
{
char c = line[i];
if (c == '"' || c == '\'')
{
char end = c;
for (i++; i < t; i++)
{
c = line[i];
if (c == end)
break;
sb.Append(c);
}
}
else if (c == ' ')
{
if (sb.Length > 0)
{
args.Add(sb.ToString());
sb.Length = 0;
}
}
else
sb.Append(c);
}
if (sb.Length > 0)
{
args.Add(sb.ToString());
sb.Length = 0;
}
}
return args.ToArray();
}
}
}