-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCreate.cs
285 lines (244 loc) · 10.8 KB
/
Create.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
using Microsoft.Build.Logging.StructuredLogger;
using System.CommandLine;
using System.CommandLine.Invocation;
using Task = Microsoft.Build.Logging.StructuredLogger.Task;
namespace illinkrepro
{
internal class Create
{
public static Command Command
{
get
{
var command = new Command("create")
{
new Argument<FileInfo>("binlog"),
new Option<DirectoryInfo>(new[] { "--out", "-o" }),
new Option<bool>(new [] { "--force", "-f" }),
new Option<string>(new[] { "--target" }),
new Option<string>(new[] { "--project" })
};
command.Handler = CommandHandler.Create(Run);
return command;
}
}
private static int Run(FileInfo binlog, DirectoryInfo? @out, bool force, string? target, string? project)
{
if (!binlog.Exists)
throw new ArgumentException($"Binlog {binlog} doesn't exist.", nameof(binlog));
if (@out == null)
{
@out = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "repro"));
}
if (@out.Exists)
{
if (!force)
{
throw new ApplicationException($"Output path {@out} already exists. Use --force to overwrite.");
}
@out.Delete(true);
}
@out.Create();
var build = BinaryLog.ReadBuild(binlog.FullName);
List<Task> ilLinkTasks = new();
build.VisitAllChildren<Task>(task => { if (task.Name == "ILLink") ilLinkTasks.Add(task); });
if (ilLinkTasks.Count == 0)
{
Console.Error.WriteLine($"No ILLink task found in the log, make sure that trimming runs (and is not skipped by incremental build).");
return -1;
}
if (!string.IsNullOrEmpty (project))
{
var candidateTasks = ilLinkTasks.Where(t =>
{
var n = (t.Parent as Target)?.Project.Name;
if (n == project)
{
return true;
}
else if (Path.GetFileNameWithoutExtension(n) == project)
{
return true;
}
return false;
});
if (!candidateTasks.Any())
{
Console.Error.WriteLine($"No ILLink task in '{project}' project found.");
return -1;
}
ilLinkTasks = candidateTasks.ToList();
}
if (!string.IsNullOrEmpty(target))
{
var candidateTasks = ilLinkTasks.Where(t => ((Target)t.Parent).Name == target);
if (!candidateTasks.Any())
{
Console.Error.WriteLine($"No ILLink task in '{target}' target found.");
return -1;
}
ilLinkTasks = candidateTasks.ToList();
}
Task? ilLinkTask = null;
if (ilLinkTasks.Count > 1)
{
// Find the first failing one
var ilLinkTasksWhichFailed = ilLinkTasks.Where(t => t.FindChild<NamedNode>("Errors") != null);
ilLinkTask = ilLinkTasksWhichFailed.FirstOrDefault();
if (ilLinkTask != null)
{
if (ilLinkTasksWhichFailed.Count() > 1)
{
Console.WriteLine($"Found more that one failing ILLink task. Picking the first failing one.");
}
else
{
Console.WriteLine($"Found failing ILLink task, picking the failing one over any other.");
}
}
else
{
ilLinkTask = ilLinkTasks[0];
Console.WriteLine($"Found more than one ILLink task and no failing one. Pickign the first one.");
}
}
else
{
ilLinkTask = ilLinkTasks[0];
}
var projectNode = (ilLinkTask.Parent as Target)?.Project!;
string projectName = projectNode.Name;
Console.WriteLine($"Creating repro for ILLink task from project {projectName}");
var ilLink = new ILLink(projectNode.ProjectDirectory, ilLinkTask.CommandLineArguments);
DirectoryInfo input = new(Path.Combine(@out.FullName, "input"));
input.Create();
Dictionary<string, string> inputFiles = new Dictionary<string, string>();
List<ILLink.Argument> reproArgs = new();
foreach (var arg in ilLink.Arguments)
{
switch (arg)
{
case ILLink.Reference reference:
reproArgs.Add(reference with { AssemblyPath = CopyFileToInput(reference.AssemblyPath) });
break;
case ILLink.Root root:
{
string assemblyPath = root.AssemblyPath;
if (File.Exists(assemblyPath))
{
reproArgs.Add(root with { AssemblyPath = CopyFileToInput(assemblyPath) });
break;
}
if (!assemblyPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
assemblyPath = assemblyPath + ".dll";
if (File.Exists(assemblyPath))
{
reproArgs.Add(root with { AssemblyPath = CopyFileToInput(assemblyPath) });
break;
}
}
reproArgs.Add(root with { AssemblyPath = Path.GetFileName(root.AssemblyPath) });
}
break;
case ILLink.Out outArg:
reproArgs.Add(new ILLink.Out("out"));
break;
case ILLink.Descriptor descriptor:
reproArgs.Add(descriptor with { Path = CopyFileToInput(descriptor.Path) });
break;
case ILLink.LinkAttributes linkAttributes:
reproArgs.Add(linkAttributes with { Path = CopyFileToInput(linkAttributes.Path) });
break;
case ILLink.SearchDirectory searchDirectory:
reproArgs.Add(searchDirectory with { Path = CopyDirectoryToInput(searchDirectory.Path) });
break;
default:
reproArgs.Add(arg);
break;
}
}
File.WriteAllLines(
Path.Combine(@out.FullName, "linker.rsp"),
reproArgs.Select(a => a.ToString()));
Console.WriteLine(@out.FullName);
return 0;
string CopyFileToInput(string path)
{
path = Path.GetFullPath(path);
string fileName = Path.GetFileName(path);
if (inputFiles.TryGetValue(path, out var inputRelativePath))
{
return inputRelativePath;
}
else
{
string inputPath = Path.Combine(input.FullName, fileName);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
string fileExtension = Path.GetExtension(fileName);
int index = 1;
while (File.Exists(inputPath))
{
fileName = fileNameWithoutExtension + $".{index++}" + fileExtension;
inputPath = Path.Combine(input.FullName, fileName);
}
File.Copy(path, inputPath);
inputRelativePath = Path.Combine(input.Name, fileName);
inputFiles.Add(path, inputRelativePath);
return inputRelativePath;
}
}
string CopyDirectoryToInput(string path)
{
path = Path.GetFullPath(path);
string directoryName = Path.GetFileName(Path.TrimEndingDirectorySeparator(path));
if (inputFiles.TryGetValue(path, out var inputRelativePath))
{
return inputRelativePath;
}
else
{
string inputPath = Path.Combine(input.FullName, directoryName);
string directoryBaseName = directoryName;
int index = 1;
while (Directory.Exists(inputPath))
{
directoryName = directoryBaseName + $".{index++}";
inputPath = Path.Combine(input.FullName, directoryName);
}
CopyDirectory(path, inputPath, true);
inputRelativePath = Path.Combine(input.Name, directoryName);
inputFiles.Add(path, inputRelativePath);
return inputRelativePath;
}
}
}
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
}
}