forked from SharpGenTools/SharpGenTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpGenTask.cs
More file actions
560 lines (459 loc) · 19.6 KB
/
Copy pathSharpGenTask.cs
File metadata and controls
560 lines (459 loc) · 19.6 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CodeAnalysis.CSharp;
using SharpGen;
using SharpGen.Config;
using SharpGen.CppModel;
using SharpGen.Generator;
using SharpGen.Logging;
using SharpGen.Model;
using SharpGen.Parser;
using SharpGen.Platform;
using SharpGen.Platform.Documentation;
using SharpGen.Transform;
using SharpGenTools.Sdk.Documentation;
using SharpGenTools.Sdk.Extensibility;
using SharpGenTools.Sdk.Internal;
using Logger = SharpGen.Logging.Logger;
using SdkResolver = SharpGen.Parser.SdkResolver;
namespace SharpGenTools.Sdk;
public sealed partial class SharpGenTask : Task, ICancelableTask
{
// Default encoding used by MSBuild ReadLinesFromFile task
internal static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true);
private static readonly char[] SpaceSeparator = { ' ' };
private readonly IocServiceContainer serviceContainer = new();
private readonly Ioc ioc = new();
private string? profilePath;
private volatile bool isCancellationRequested;
private Mutex? workerLock;
private bool workerLockAcquired, regenerationStarted;
// ReSharper disable MemberCanBePrivate.Global, UnusedAutoPropertyAccessor.Global
[Required] public string[]? CastXmlArguments { get; set; }
[Required] public string? CastXmlExecutable { get; set; }
[Required] public string[]? ConfigFiles { get; set; }
[Required] public string? ConsumerBindMappingConfigId { get; set; }
[Required] public bool DebugWaitForDebuggerAttach { get; set; }
[Required] public bool DocumentationFailuresAsErrors { get; set; }
[Required] public string[]? ExtensionAssemblies { get; set; }
[Required] public string[]? ExternalDocumentation { get; set; }
[Required] public ITaskItem[]? GlobalNamespaceOverrides { get; set; }
[Required] public string[]? Macros { get; set; }
[Required] public string? IntermediateOutputDirectory { get; set; }
public string? PlatformName { get; set; }
[Required] public string[]? Platforms { get; set; }
[Output]
public string ProfilePath
{
get => profilePath ?? throw new InvalidOperationException("Profile not set");
set
{
if (profilePath is not null)
throw new InvalidOperationException("Profile cannot be set twice");
profilePath = Utilities.EnsureTrailingSlash(value ?? throw new InvalidOperationException("Profile cannot be null"));
}
}
public string? RuntimeIdentifier { get; set; }
[Required] public string[]? SilenceMissingDocumentationErrorIdentifierPatterns { get; set; }
// ReSharper restore UnusedAutoPropertyAccessor.Global, MemberCanBePrivate.Global
private string GeneratedCodeFile => GetProfileChild("SharpGen.Bindings.g.cs");
private string InputsCache => GetProfileChild("InputsCache.txt");
private string PropertyCache => GetProfileChild("PropertyCache.txt");
private string DocumentationCache => GetProfileChild("DocumentationCache.json");
private string DirtyMarkerFile => GetProfileChild("dirty");
private string PackagePropsFile => GetProfileChild("Package.props");
private string ConsumerBindMappingConfig => GetProfileChild(
(ConsumerBindMappingConfigId ?? throw new InvalidOperationException(nameof(ConsumerBindMappingConfigId))) +
".BindMapping.xml"
);
private string GetProfileChild(string childName) => Path.Combine(ProfilePath, childName);
private Logger SharpGenLogger { get; set; }
[Conditional("DEBUG")]
private void WaitForDebuggerAttach()
{
if (!Debugger.IsAttached)
{
SharpGenLogger.Warning(null, $"{GetType().Name} is waiting for attach: {Process.GetCurrentProcess().Id}");
Thread.Yield();
}
while (!Debugger.IsAttached && !isCancellationRequested)
Thread.Sleep(TimeSpan.FromSeconds(1));
}
public void Cancel() => isCancellationRequested = true;
private static string[] PreprocessPathListProperty(string[] items) =>
items.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(static x => x, StringComparer.OrdinalIgnoreCase)
.ToArray();
public override bool Execute()
{
BindingRedirectResolution.Enable();
SharpGenLogger = new Logger(new MSBuildSharpGenLogger(Log));
#if DEBUG
if (DebugWaitForDebuggerAttach)
WaitForDebuggerAttach();
#endif
var success = false;
try
{
ConfigFiles = PreprocessPathListProperty(ConfigFiles);
ExtensionAssemblies = PreprocessPathListProperty(ExtensionAssemblies);
ExternalDocumentation = PreprocessPathListProperty(ExternalDocumentation);
if (GeneratePropertyCache())
{
return success = ExecuteImpl();
}
else
{
if (AbortExecution)
return success = false;
if (IsInputsCacheValid())
return success = true;
return success = ExecuteImpl();
}
}
catch (CodeGenFailedException ex)
{
SharpGenLogger.Fatal("Internal SharpGen exception", ex);
return success = false;
}
finally
{
Debug.Assert(workerLock != null, nameof(workerLock) + " != null");
try
{
GeneratePackagesProps();
}
catch (Exception e)
{
SharpGenLogger.LogRawMessage(
LogLevel.Warning, null,
"Internal SharpGen exception while generating NuGet package properties file", e
);
}
if (regenerationStarted)
{
try
{
GenerateInputsCache();
}
catch (Exception e)
{
SharpGenLogger.LogRawMessage(
LogLevel.Warning, null,
"Internal SharpGen exception while generating input file cache", e
);
}
}
if (success && !AbortExecution && File.Exists(DirtyMarkerFile))
File.Delete(DirtyMarkerFile);
if (workerLockAcquired)
workerLock.ReleaseMutex();
workerLock.Dispose();
workerLock = null;
}
}
private bool AbortExecution => SharpGenLogger.HasErrors || isCancellationRequested;
private bool ExecuteImpl()
{
File.WriteAllBytes(DirtyMarkerFile, Array.Empty<byte>());
regenerationStarted = true;
if (AbortExecution)
return false;
serviceContainer.AddService(SharpGenLogger);
serviceContainer.AddService<IDocumentationLinker, DocumentationLinker>();
serviceContainer.AddService<GlobalNamespaceProvider>();
serviceContainer.AddService(new TypeRegistry(ioc));
ioc.ConfigureServices(serviceContainer);
ExtensibilityDriver.Instance.LoadExtensions(SharpGenLogger, ExtensionAssemblies);
AddInputsCacheFiles(ExtensionAssemblies);
ConfigFile config = new()
{
Files = ConfigFiles.ToList(),
Id = "SharpGen-MSBuild"
};
LoadConfig(config);
config.GetFilesWithIncludesAndExtensionHeaders(
out var configsWithHeaders,
out var configsWithExtensionHeaders
);
AddInputsCacheFiles(config.ConfigFilesLoaded.Select(x => x.AbsoluteFilePath));
CppHeaderGenerator cppHeaderGenerator = new(ProfilePath, ioc);
var cppHeaderGenerationResult = cppHeaderGenerator.GenerateCppHeaders(config, configsWithHeaders, configsWithExtensionHeaders);
if (AbortExecution)
return false;
IncludeDirectoryResolver resolver = new(ioc);
resolver.Configure(config);
AddInputsCacheFile(CastXmlExecutable);
CastXmlRunner castXml = new(resolver, CastXmlExecutable, CastXmlArguments, ioc)
{
OutputPath = ProfilePath
};
var module = config.CreateSkeletonModule();
MacroManager macroManager = new(castXml);
macroManager.Parse(Path.Combine(ProfilePath, config.HeaderFileName), module);
AddInputsCacheFiles(macroManager.IncludedFiles);
new CppExtensionHeaderGenerator().GenerateExtensionHeaders(
config, ProfilePath, module, configsWithExtensionHeaders, cppHeaderGenerationResult.UpdatedConfigs
);
AddInputsCacheFiles(configsWithExtensionHeaders.Select(x => Path.Combine(ProfilePath, x.ExtensionFileName)));
if (AbortExecution)
return false;
// Run the parser
var parser = new CppParser(config, ioc)
{
OutputPath = ProfilePath
};
if (AbortExecution)
return false;
CppModule group;
using (var xmlReader = castXml.Process(parser.RootConfigHeaderFileName))
{
// Run the C++ parser
group = parser.Run(module, xmlReader);
}
if (AbortExecution)
return false;
config.ExpandDynamicVariables(SharpGenLogger, group);
var docLinker = ioc.DocumentationLinker;
var globalNamespace = ioc.GlobalNamespace;
NamingRulesManager namingRules = new();
foreach (var nameOverride in GlobalNamespaceOverrides)
{
var wellKnownName = nameOverride.ItemSpec;
var overridenName = nameOverride.GetMetadata("Override");
if (string.IsNullOrEmpty(overridenName))
continue;
if (Enum.TryParse(wellKnownName, out WellKnownName name))
{
globalNamespace.OverrideName(name, overridenName);
}
else
{
SharpGenLogger.Warning(
LoggingCodes.InvalidGlobalNamespaceOverride,
"Invalid override of \"{0}\": unknown class name, ignoring the override.",
wellKnownName
);
}
}
serviceContainer.AddService(
new GeneratorConfig
{
Platforms = ConfigPlatforms
}
);
// Run the main mapping process
TransformManager transformer = new(
namingRules,
new ConstantManager(namingRules, ioc),
ioc
);
var (solution, defines) = transformer.Transform(group, config);
var consumerConfig = new ConfigFile
{
Id = ConsumerBindMappingConfigId,
IncludeProlog = {cppHeaderGenerationResult.Prologue},
Extension = new List<ExtensionBaseRule>(defines)
};
var (bindings, generatedDefines) = transformer.GenerateTypeBindingsForConsumers();
consumerConfig.Bindings.AddRange(bindings);
consumerConfig.Extension.AddRange(generatedDefines);
consumerConfig.Mappings.AddRange(
docLinker.GetAllDocLinks().Select(
link => new MappingRule
{
DocItem = link.cppName,
MappingNameFinal = link.cSharpName
}
)
);
GenerateConfigForConsumers(consumerConfig);
if (AbortExecution)
return false;
var documentationCacheItemSpec = DocumentationCache;
Utilities.RequireAbsolutePath(documentationCacheItemSpec, nameof(DocumentationCache));
var cache = File.Exists(documentationCacheItemSpec)
? DocItemCache.Read(documentationCacheItemSpec)
: new DocItemCache();
DocumentationLogger docLogger = new(SharpGenLogger) {MaxLevel = LogLevel.Warning};
var docContext = new Lazy<DocumentationContext>(() => new DocumentationContext(docLogger));
ExtensibilityDriver.Instance.DocumentModule(SharpGenLogger, cache, solution, docContext).Wait();
if (docContext.IsValueCreated)
{
Regex[] silencePatterns = null;
var docLogLevelDefault = DocumentationFailuresAsErrors ? LogLevel.Error : LogLevel.Warning;
foreach (var queryFailure in docContext.Value.Failures)
{
if (silencePatterns == null)
{
silencePatterns = new Regex[SilenceMissingDocumentationErrorIdentifierPatterns.Length];
for (var i = 0; i < silencePatterns.Length; i++)
silencePatterns[i] = new Regex(
SilenceMissingDocumentationErrorIdentifierPatterns[i],
RegexOptions.CultureInvariant
);
}
if (silencePatterns.Length != 0)
{
bool SilencePredicate(Regex x) => x.Match(queryFailure.Query).Success;
if (silencePatterns.Any(SilencePredicate))
continue;
}
var providerName = queryFailure.FailedProviderName ?? "<null>";
var docLogLevel = queryFailure.TreatProviderFailuresAsErrors
? docLogLevelDefault
: docLogLevelDefault > LogLevel.Warning
? LogLevel.Warning
: docLogLevelDefault;
switch (queryFailure.Exceptions)
{
case { Count: > 1 } exceptions:
{
var exceptionsCount = exceptions.Count;
for (var index = 0; index < exceptionsCount; index++)
{
var exception = exceptions[index];
SharpGenLogger.LogRawMessage(
docLogLevel,
LoggingCodes.DocumentationProviderInternalError,
"Documentation provider [{0}] query for \"{1}\" failed ({2}/{3}).",
exception,
providerName,
queryFailure.Query,
index + 1,
exceptionsCount
);
}
break;
}
case { Count: 1 } exceptions:
SharpGenLogger.LogRawMessage(
docLogLevel,
LoggingCodes.DocumentationProviderInternalError,
"Documentation provider [{0}] query for \"{1}\" failed.",
exceptions[0],
providerName,
queryFailure.Query
);
break;
default:
SharpGenLogger.LogRawMessage(
docLogLevel,
LoggingCodes.DocumentationProviderInternalError,
"Documentation provider [{0}] query for \"{1}\" failed.",
null,
providerName,
queryFailure.Query
);
break;
}
}
}
cache.WriteIfDirty(documentationCacheItemSpec);
if (AbortExecution)
return false;
var documentationFiles = new Dictionary<string, XmlDocument>();
AddInputsCacheFiles(ExternalDocumentation);
foreach (var file in ExternalDocumentation)
{
using var stream = File.OpenRead(file);
var xml = new XmlDocument();
xml.Load(stream);
documentationFiles.Add(file, xml);
}
if (AbortExecution)
return false;
serviceContainer.AddService(new ExternalDocCommentsReader(documentationFiles));
serviceContainer.AddService<IGeneratorRegistry>(new DefaultGenerators(ioc));
RoslynGenerator generator = new();
using var codeStream = File.Open(GeneratedCodeFile, FileMode.Create, FileAccess.Write);
using var codeWriter = new StreamWriter(codeStream, DefaultEncoding);
generator.Run(solution, ioc).GetCompilationUnitRoot().WriteTo(codeWriter);
return !SharpGenLogger.HasErrors;
}
private PlatformDetectionType ConfigPlatforms
{
get
{
PlatformDetectionType platformMask = 0;
foreach (var platform in Platforms)
{
if (!Enum.TryParse<PlatformDetectionType>(platform, out var parsedPlatform))
{
SharpGenLogger.Warning(
LoggingCodes.InvalidPlatformDetectionType,
"The platform type {0} is an unknown platform to SharpGenTools. Falling back to Any platform detection.",
platform
);
platformMask = PlatformDetectionType.Any;
}
else
{
platformMask |= parsedPlatform;
}
}
return platformMask == 0 ? PlatformDetectionType.Any : platformMask;
}
}
private void GenerateConfigForConsumers(ConfigFile consumerConfig)
{
using var consumerBindMapping = File.Create(ConsumerBindMappingConfig);
consumerConfig.Write(consumerBindMapping);
}
private void LoadConfig(ConfigFile config)
{
config.Load(null, Macros, SharpGenLogger);
AddInputsCacheEnvironmentVariable("SHARPGEN_VS_OVERRIDE");
AddInputsCacheEnvironmentVariable("SHARPGEN_SDK_OVERRIDE");
SdkResolver sdkResolver = new(SharpGenLogger);
SharpGenLogger.Message("Resolving SDKs...");
foreach (var cfg in config.ConfigFilesLoaded)
{
SharpGenLogger.Message("Resolving SDK for Config {0}", cfg);
foreach (var sdk in cfg.Sdks)
{
SharpGenLogger.Message("Resolving {0}: Version {1}", sdk.Name, sdk.Version);
foreach (var directory in sdkResolver.ResolveIncludeDirsForSdk(sdk))
{
SharpGenLogger.Message("Resolved include directory {0}", directory);
cfg.IncludeDirs.Add(directory);
}
}
}
}
private void GeneratePackagesProps()
{
using CacheFile cacheFile = new(new FileInfo(PackagePropsFile));
{
using var writer = cacheFile.StreamWriter;
writer.WriteLine(@"<Project>");
writer.WriteLine(@" <ItemGroup>");
writer.WriteLine(
$@" <SharpGenConsumerMapping Include=""$([MSBuild]::NormalizePath('$(MSBuildThisFileDirectory)', '..', 'build', '{ConsumerBindMappingConfigId}.BindMapping.xml'))""/>"
);
writer.WriteLine(@" </ItemGroup>");
writer.WriteLine(@"</Project>");
}
SharpGenLogger.Message(
cacheFile.State switch
{
CacheFile.CacheState.Hit => "NuGet package properties file is already up-to-date.",
CacheFile.CacheState.Miss => "NuGet package properties file is out-of-date.",
CacheFile.CacheState.Absent => "NuGet package properties file doesn't exist.",
_ => throw new ArgumentOutOfRangeException()
}
);
if (cacheFile.IsWriteNeeded)
cacheFile.Write();
}
}