-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathProgram.cs
1411 lines (1214 loc) · 62.1 KB
/
Program.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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Help;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using ClangSharp.Interop;
using static ClangSharp.Interop.CXDiagnosticSeverity;
using static ClangSharp.Interop.CXErrorCode;
using static ClangSharp.Interop.CXTranslationUnit_Flags;
namespace ClangSharp;
public static class Program
{
private static readonly string[] s_additionalOptionAliases = ["--additional", "-a"];
private static readonly string[] s_configOptionAliases = ["--config", "-c"];
private static readonly string[] s_defineMacroOptionAliases = ["--define-macro", "-D"];
private static readonly string[] s_excludeOptionAliases = ["--exclude", "-e"];
private static readonly string[] s_fileOptionAliases = ["--file", "-f"];
private static readonly string[] s_fileDirectionOptionAliases = ["--file-directory", "-F"];
private static readonly string[] s_headerOptionAliases = ["--headerFile", "-h"];
private static readonly string[] s_includeOptionAliases = ["--include", "-i"];
private static readonly string[] s_includeDirectoryOptionAliases = ["--include-directory", "-I"];
private static readonly string[] s_languageOptionAliases = ["--language", "-x"];
private static readonly string[] s_libraryOptionAliases = ["--libraryPath", "-l"];
private static readonly string[] s_methodClassNameOptionAliases = ["--methodClassName", "-m"];
private static readonly string[] s_namespaceOptionAliases = ["--namespace", "-n"];
private static readonly string[] s_nativeTypeNamesStripOptionAliases = ["--nativeTypeNamesToStrip"];
private static readonly string[] s_outputModeOptionAliases = ["--output-mode", "-om"];
private static readonly string[] s_outputOptionAliases = ["--output", "-o"];
private static readonly string[] s_prefixStripOptionAliases = ["--prefixStrip", "-p"];
private static readonly string[] s_remapOptionAliases = ["--remap", "-r"];
private static readonly string[] s_stdOptionAliases = ["--std", "-std"];
private static readonly string[] s_testOutputOptionAliases = ["--test-output", "-to"];
private static readonly string[] s_traverseOptionAliases = ["--traverse", "-t"];
private static readonly string[] s_versionOptionAliases = ["--version", "-v"];
private static readonly string[] s_withAccessSpecifierOptionAliases = ["--with-access-specifier", "-was"];
private static readonly string[] s_withAttributeOptionAliases = ["--with-attribute", "-wa"];
private static readonly string[] s_withCallConvOptionAliases = ["--with-callconv", "-wcc"];
private static readonly string[] s_withClassOptionAliases = ["--with-class", "-wc"];
private static readonly string[] s_withGuidOptionAliases = ["--with-guid", "-wg"];
private static readonly string[] s_withLengthOptionAliases = ["--with-length", "-wl"];
private static readonly string[] s_withLibraryPathOptionAliases = ["--with-librarypath", "-wlb"];
private static readonly string[] s_withManualImportOptionAliases = ["--with-manual-import", "-wmi"];
private static readonly string[] s_withNamespaceOptionAliases = ["--with-namespace", "-wn"];
private static readonly string[] s_withPackingOptionAliases = ["--with-packing", "-wp"];
private static readonly string[] s_withSetLastErrorOptionAliases = ["--with-setlasterror", "-wsle"];
private static readonly string[] s_withSuppressGCTransitionOptionAliases = ["--with-suppressgctransition", "-wsgct"];
private static readonly string[] s_withTransparentStructOptionAliases = ["--with-transparent-struct", "-wts"];
private static readonly string[] s_withTypeOptionAliases = ["--with-type", "-wt"];
private static readonly string[] s_withUsingOptionAliases = ["--with-using", "-wu"];
private static readonly Option<string[]> s_additionalOption = GetAdditionalOption();
private static readonly Option<string[]> s_configOption = GetConfigOption();
private static readonly Option<string[]> s_defineMacros = GetDefineMacroOption();
private static readonly Option<string[]> s_excludedNames = GetExcludeOption();
private static readonly Option<string[]> s_files = GetFileOption();
private static readonly Option<string> s_fileDirectory = GetFileDirectoryOption();
private static readonly Option<string> s_headerFile = GetHeaderOption();
private static readonly Option<string[]> s_includedNames = GetIncludeOption();
private static readonly Option<string[]> s_includeDirectories = GetIncludeDirectoryOption();
private static readonly Option<string> s_language = GetLanguageOption();
private static readonly Option<string> s_libraryPath = GetLibraryOption();
private static readonly Option<string> s_methodClassName = GetMethodClassNameOption();
private static readonly Option<string> s_methodPrefixToStrip = GetPrefixStripOption();
private static readonly Option<string> s_namespaceName = GetNamespaceOption();
private static readonly Option<string[]> s_nativeTypeNamesToStrip = GetNativeTypeNamesStripOption();
private static readonly Option<string> s_outputLocation = GetOutputOption();
private static readonly Option<PInvokeGeneratorOutputMode> s_outputMode = GetOutputModeOption();
private static readonly Option<string[]> s_remappedNameValuePairs = GetRemapOption();
private static readonly Option<string> s_std = GetStdOption();
private static readonly Option<string> s_testOutputLocation = GetTestOutputOption();
private static readonly Option<string[]> s_traversalNames = GetTraverseOption();
private static readonly Option<bool> s_versionOption = GetVersionOption();
private static readonly Option<string[]> s_withAccessSpecifierNameValuePairs = GetWithAccessSpecifierOption();
private static readonly Option<string[]> s_withAttributeNameValuePairs = GetWithAttributeOption();
private static readonly Option<string[]> s_withCallConvNameValuePairs = GetWithCallConvOption();
private static readonly Option<string[]> s_withClassNameValuePairs = GetWithClassOption();
private static readonly Option<string[]> s_withGuidNameValuePairs = GetWithGuidOption();
private static readonly Option<string[]> s_withLengthNameValuePairs = GetWithLengthOption();
private static readonly Option<string[]> s_withLibraryPathNameValuePairs = GetWithLibraryPathOption();
private static readonly Option<string[]> s_withManualImports = GetWithManualImportOption();
private static readonly Option<string[]> s_withNamespaceNameValuePairs = GetWithNamespaceOption();
private static readonly Option<string[]> s_withPackingNameValuePairs = GetWithPackingOption();
private static readonly Option<string[]> s_withSetLastErrors = GetWithSetLastErrorOption();
private static readonly Option<string[]> s_withSuppressGCTransitions = GetWithSuppressGCTransitionOption();
private static readonly Option<string[]> s_withTransparentStructNameValuePairs = GetWithTransparentStructOption();
private static readonly Option<string[]> s_withTypeNameValuePairs = GetWithTypeOption();
private static readonly Option<string[]> s_withUsingNameValuePairs = GetWithUsingOption();
private static readonly RootCommand s_rootCommand = GetRootCommand();
private static readonly TwoColumnHelpRow[] s_configOptions =
[
new TwoColumnHelpRow("?, h, help", "Show help and usage information for -c, --config"),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Codegen Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("compatible-codegen", "Bindings should be generated with .NET Standard 2.0 compatibility. Setting this disables preview code generation."),
new TwoColumnHelpRow("default-codegen", "Bindings should be generated for the current LTS version of .NET/C#. This is currently .NET 6/C# 10."),
new TwoColumnHelpRow("latest-codegen", "Bindings should be generated for the current STS version of .NET/C#. This is currently .NET 7/C# 11."),
new TwoColumnHelpRow("preview-codegen", "Bindings should be generated for the preview version of .NET/C#. This is currently .NET 8/C# 12."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# File Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("single-file", "Bindings should be generated to a single output file. This is the default."),
new TwoColumnHelpRow("multi-file", "Bindings should be generated so there is approximately one type per file."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Type Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("unix-types", "Bindings should be generated assuming Unix defaults. This is the default on Unix platforms."),
new TwoColumnHelpRow("windows-types", "Bindings should be generated assuming Windows defaults. This is the default on Windows platforms."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Exclusion Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("exclude-anonymous-field-helpers", "The helper ref properties generated for fields in nested anonymous structs and unions should not be generated."),
new TwoColumnHelpRow("exclude-com-proxies", "Types recognized as COM proxies should not have bindings generated. These are currently function declarations ending with _UserFree, _UserMarshal, _UserSize, _UserUnmarshal, _Proxy, or _Stub."),
new TwoColumnHelpRow("exclude-default-remappings", "Default remappings for well known types should not be added. This currently includes intptr_t, ptrdiff_t, size_t, and uintptr_t"),
new TwoColumnHelpRow("exclude-empty-records", "Bindings for records that contain no members should not be generated. These are commonly encountered for opaque handle like types such as HWND."),
new TwoColumnHelpRow("exclude-enum-operators", "Bindings for operators over enum types should not be generated. These are largely unnecessary in C# as the operators are available by default."),
new TwoColumnHelpRow("exclude-fnptr-codegen", "Generated bindings for latest or preview codegen should not use function pointers."),
new TwoColumnHelpRow("exclude-funcs-with-body", "Bindings for functions with bodies should not be generated."),
new TwoColumnHelpRow("exclude-using-statics-for-enums", "Enum usages should be fully qualified and should not include a corresponding 'using static EnumName;'"),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Vtbl Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("explicit-vtbls", "VTBLs should have an explicit type generated with named fields per entry."),
new TwoColumnHelpRow("implicit-vtbls", "VTBLs should be implicit to reduce metadata bloat. This is the current default"),
new TwoColumnHelpRow("trimmable-vtbls", "VTBLs should be defined but not used in helper methods to reduce metadata bloat when trimming."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Test Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("generate-tests-nunit", "Basic tests validating size, blittability, and associated metadata should be generated for NUnit."),
new TwoColumnHelpRow("generate-tests-xunit", "Basic tests validating size, blittability, and associated metadata should be generated for XUnit."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Generation Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("generate-aggressive-inlining", "[MethodImpl(MethodImplOptions.AggressiveInlining)] should be added to generated helper functions."),
new TwoColumnHelpRow("generate-callconv-member-function", "Instance function pointers should use [CallConvMemberFunction] where applicable."),
new TwoColumnHelpRow("generate-cpp-attributes", "[CppAttributeList(\"\")] should be generated to document the encountered C++ attributes."),
new TwoColumnHelpRow("generate-disable-runtime-marshalling", "[assembly: DisableRuntimeMarshalling] should be generated."),
new TwoColumnHelpRow("generate-doc-includes", "<include> xml documentation tags should be generated for declarations."),
new TwoColumnHelpRow("generate-file-scoped-namespaces", "Namespaces should be scoped to the file to reduce nesting."),
new TwoColumnHelpRow("generate-guid-member", "Types with an associated GUID should have a corresponding member generated."),
new TwoColumnHelpRow("generate-helper-types", "Code files should be generated for various helper attributes and declared transparent structs."),
new TwoColumnHelpRow("generate-macro-bindings", "Bindings for macro-definitions should be generated. This currently only works with value like macros and not function-like ones."),
new TwoColumnHelpRow("generate-marker-interfaces", "Bindings for marker interfaces representing native inheritance hierarchies should be generated."),
new TwoColumnHelpRow("generate-native-bitfield-attribute", "[NativeBitfield(\"\", offset: #, length: #)] attribute should be generated to document the encountered bitfield layout."),
new TwoColumnHelpRow("generate-native-inheritance-attribute", "[NativeInheritance(\"\")] attribute should be generated to document the encountered C++ base type."),
new TwoColumnHelpRow("generate-generic-pointer-wrapper", "Pointer<T> should be used for limited generic type support."),
new TwoColumnHelpRow("generate-setslastsystemerror-attribute", "[SetsLastSystemError] attribute should be generated rather than using SetLastError = true."),
new TwoColumnHelpRow("generate-template-bindings", "Bindings for template-definitions should be generated. This is currently experimental."),
new TwoColumnHelpRow("generate-unmanaged-constants", "Unmanaged constants should be generated using static ref readonly properties. This is currently experimental."),
new TwoColumnHelpRow("generate-vtbl-index-attribute", "[VtblIndex(#)] attribute should be generated to document the underlying VTBL index for a helper method."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Stripping Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("strip-enum-member-type-name", "Strips the enum type name from the beginning of its member names."),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("# Logging Options", ""),
new TwoColumnHelpRow("", ""),
new TwoColumnHelpRow("log-exclusions", "A list of excluded declaration types should be generated. This will also log if the exclusion was due to an exact or partial match."),
new TwoColumnHelpRow("log-potential-typedef-remappings", "A list of potential typedef remappings should be generated. This can help identify missing remappings."),
new TwoColumnHelpRow("log-visited-files", "A list of the visited files should be generated. This can help identify traversal issues."),
];
public static IEnumerable<HelpSectionDelegate> GetExtendedHelp(HelpContext context)
{
foreach (var sectionDelegate in HelpBuilder.Default.GetLayout())
{
yield return sectionDelegate;
}
yield return _ => {
Console.WriteLine("Wildcards:");
Console.WriteLine("You can use * as catch-all rule for remapping procedures. For example if you want make all of your generated code internal you can use --with-access-specifier *=Internal.");
};
}
public static async Task<int> Main(params string[] args)
{
var parser = new CommandLineBuilder(s_rootCommand)
.UseHelp(context => context.HelpBuilder.CustomizeLayout(GetExtendedHelp))
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.UseTypoCorrections()
.UseParseErrorReporting()
.UseExceptionHandler()
.CancelOnProcessTermination()
.Build();
return await parser.InvokeAsync(args).ConfigureAwait(false);
}
public static void Run(InvocationContext context)
{
ArgumentNullException.ThrowIfNull(context);
var additionalArgs = context.ParseResult.GetValueForOption(s_additionalOption) ?? [];
var configSwitches = context.ParseResult.GetValueForOption(s_configOption) ?? [];
var defineMacros = context.ParseResult.GetValueForOption(s_defineMacros) ?? [];
var excludedNames = context.ParseResult.GetValueForOption(s_excludedNames) ?? [];
var files = context.ParseResult.GetValueForOption(s_files) ?? [];
var fileDirectory = context.ParseResult.GetValueForOption(s_fileDirectory) ?? "";
var headerFile = context.ParseResult.GetValueForOption(s_headerFile) ?? "";
var includedNames = context.ParseResult.GetValueForOption(s_includedNames) ?? [];
var includeDirectories = context.ParseResult.GetValueForOption(s_includeDirectories) ?? [];
var language = context.ParseResult.GetValueForOption(s_language) ?? "";
var libraryPath = context.ParseResult.GetValueForOption(s_libraryPath) ?? "";
var methodClassName = context.ParseResult.GetValueForOption(s_methodClassName) ?? "";
var methodPrefixToStrip = context.ParseResult.GetValueForOption(s_methodPrefixToStrip) ?? "";
var nativeTypeNamesToStrip = context.ParseResult.GetValueForOption(s_nativeTypeNamesToStrip) ?? [];
var namespaceName = context.ParseResult.GetValueForOption(s_namespaceName) ?? "";
var outputLocation = context.ParseResult.GetValueForOption(s_outputLocation) ?? "";
var outputMode = context.ParseResult.GetValueForOption(s_outputMode);
var remappedNameValuePairs = context.ParseResult.GetValueForOption(s_remappedNameValuePairs) ?? [];
var std = context.ParseResult.GetValueForOption(s_std) ?? "";
var testOutputLocation = context.ParseResult.GetValueForOption(s_testOutputLocation) ?? "";
var traversalNames = context.ParseResult.GetValueForOption(s_traversalNames) ?? [];
var withAccessSpecifierNameValuePairs = context.ParseResult.GetValueForOption(s_withAccessSpecifierNameValuePairs) ?? [];
var withAttributeNameValuePairs = context.ParseResult.GetValueForOption(s_withAttributeNameValuePairs) ?? [];
var withCallConvNameValuePairs = context.ParseResult.GetValueForOption(s_withCallConvNameValuePairs) ?? [];
var withClassNameValuePairs = context.ParseResult.GetValueForOption(s_withClassNameValuePairs) ?? [];
var withGuidNameValuePairs = context.ParseResult.GetValueForOption(s_withGuidNameValuePairs) ?? [];
var withLengthNameValuePairs = context.ParseResult.GetValueForOption(s_withLengthNameValuePairs) ?? [];
var withLibraryPathNameValuePairs = context.ParseResult.GetValueForOption(s_withLibraryPathNameValuePairs) ?? [];
var withManualImports = context.ParseResult.GetValueForOption(s_withManualImports) ?? [];
var withNamespaceNameValuePairs = context.ParseResult.GetValueForOption(s_withNamespaceNameValuePairs) ?? [];
var withSetLastErrors = context.ParseResult.GetValueForOption(s_withSetLastErrors) ?? [];
var withSuppressGCTransitions = context.ParseResult.GetValueForOption(s_withSuppressGCTransitions) ?? [];
var withTransparentStructNameValuePairs = context.ParseResult.GetValueForOption(s_withTransparentStructNameValuePairs) ?? [];
var withTypeNameValuePairs = context.ParseResult.GetValueForOption(s_withTypeNameValuePairs) ?? [];
var withUsingNameValuePairs = context.ParseResult.GetValueForOption(s_withUsingNameValuePairs) ?? [];
var withPackingNameValuePairs = context.ParseResult.GetValueForOption(s_withPackingNameValuePairs) ?? [];
var versionResult = context.ParseResult.FindResultFor(s_versionOption);
if (versionResult is not null)
{
context.Console.WriteLine($"{s_rootCommand.Description} version 18.1.3");
context.Console.WriteLine($" {clang.getClangVersion()}");
context.Console.WriteLine($" {clangsharp.getVersion()}");
context.ExitCode = -1;
return;
}
var errorList = new List<string>();
if (files.Length == 0)
{
errorList.Add("Error: No input C/C++ files provided. Use --file or -f");
}
if (string.IsNullOrWhiteSpace(namespaceName))
{
errorList.Add("Error: No namespace provided. Use --namespace or -n");
}
if (string.IsNullOrWhiteSpace(outputLocation))
{
errorList.Add("Error: No output file location provided. Use --output or -o");
}
ParseKeyValuePairs(remappedNameValuePairs, errorList, out Dictionary<string, string> remappedNames);
ParseKeyValuePairs(withAccessSpecifierNameValuePairs, errorList, out Dictionary<string, AccessSpecifier> withAccessSpecifiers);
ParseKeyValuePairs(withAttributeNameValuePairs, errorList, out Dictionary<string, IReadOnlyList<string>> withAttributes);
ParseKeyValuePairs(withCallConvNameValuePairs, errorList, out Dictionary<string, string> withCallConvs);
ParseKeyValuePairs(withClassNameValuePairs, errorList, out Dictionary<string, string> withClasses);
ParseKeyValuePairs(withGuidNameValuePairs, errorList, out Dictionary<string, Guid> withGuids);
ParseKeyValuePairs(withLengthNameValuePairs, errorList, out Dictionary<string, string> withLengths);
ParseKeyValuePairs(withLibraryPathNameValuePairs, errorList, out Dictionary<string, string> withLibraryPaths);
ParseKeyValuePairs(withNamespaceNameValuePairs, errorList, out Dictionary<string, string> withNamespaces);
ParseKeyValuePairs(withTransparentStructNameValuePairs, errorList, out Dictionary<string, (string, PInvokeGeneratorTransparentStructKind)> withTransparentStructs);
ParseKeyValuePairs(withTypeNameValuePairs, errorList, out Dictionary<string, string> withTypes);
ParseKeyValuePairs(withUsingNameValuePairs, errorList, out Dictionary<string, IReadOnlyList<string>> withUsings);
ParseKeyValuePairs(withPackingNameValuePairs, errorList, out Dictionary<string, string> withPackings);
foreach (var key in withTransparentStructs.Keys)
{
remappedNames.Add(key, key);
}
var configOptions = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeGeneratorConfigurationOptions.None : PInvokeGeneratorConfigurationOptions.GenerateUnixTypes;
var printConfigHelp = false;
foreach (var configSwitch in configSwitches)
{
switch (configSwitch)
{
case "?":
case "h":
case "help":
{
printConfigHelp = true;
break;
}
// Codegen Options
case "compatible-codegen":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateLatestCode;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GeneratePreviewCode;
break;
}
case "default-codegen":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateLatestCode;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GeneratePreviewCode;
break;
}
case "latest-codegen":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode;
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateLatestCode;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GeneratePreviewCode;
break;
}
case "preview-codegen":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateLatestCode;
configOptions |= PInvokeGeneratorConfigurationOptions.GeneratePreviewCode;
break;
}
// File Options
case "single-file":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateMultipleFiles;
break;
}
case "multi-file":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateMultipleFiles;
break;
}
// Type Options
case "unix-types":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateUnixTypes;
break;
}
case "windows-types":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateUnixTypes;
break;
}
// Exclusion Options
case "exclude-anonymous-field-helpers":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeAnonymousFieldHelpers;
break;
}
case "exclude-com-proxies":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeComProxies;
break;
}
case "exclude-default-remappings":
case "no-default-remappings":
{
configOptions |= PInvokeGeneratorConfigurationOptions.NoDefaultRemappings;
break;
}
case "exclude-empty-records":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeEmptyRecords;
break;
}
case "exclude-enum-operators":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeEnumOperators;
break;
}
case "exclude-fnptr-codegen":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeFnptrCodegen;
break;
}
case "exclude-funcs-with-body":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeFunctionsWithBody;
break;
}
case "exclude-nint-codegen":
{
configOptions |= PInvokeGeneratorConfigurationOptions.ExcludeNIntCodegen;
break;
}
case "exclude-using-statics-for-enums":
case "dont-use-using-statics-for-enums":
{
configOptions |= PInvokeGeneratorConfigurationOptions.DontUseUsingStaticsForEnums;
break;
}
// VTBL Options
case "explicit-vtbls":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateExplicitVtbls;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateTrimmableVtbls;
break;
}
case "implicit-vtbls":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateExplicitVtbls;
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateTrimmableVtbls;
break;
}
case "trimmable-vtbls":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.GenerateExplicitVtbls;
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateTrimmableVtbls;
break;
}
// Test Options
case "generate-tests-nunit":
{
if (string.IsNullOrWhiteSpace(testOutputLocation))
{
errorList.Add("Error: No test output file location provided. Use --test-output or -to");
}
if (configOptions.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsXUnit))
{
errorList.Add("Cannot generate both NUnit and XUnit tests.");
}
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateTestsNUnit;
break;
}
case "generate-tests-xunit":
{
if (string.IsNullOrWhiteSpace(testOutputLocation))
{
errorList.Add("Error: No test output file location provided. Use --test-output or -to");
}
if (configOptions.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsNUnit))
{
errorList.Add("Cannot generate both NUnit and XUnit tests.");
}
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateTestsXUnit;
break;
}
// Generation Options
case "generate-aggressive-inlining":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateAggressiveInlining;
break;
}
case "generate-callconv-member-function":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateCallConvMemberFunction;
break;
}
case "generate-cpp-attributes":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateCppAttributes;
break;
}
case "generate-disable-runtime-marshalling":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateDisableRuntimeMarshalling;
break;
}
case "generate-doc-includes":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateDocIncludes;
break;
}
case "generate-file-scoped-namespaces":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateFileScopedNamespaces;
break;
}
case "generate-guid-member":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateGuidMember;
break;
}
case "generate-helper-types":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateHelperTypes;
break;
}
case "generate-macro-bindings":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateMacroBindings;
break;
}
case "generate-marker-interfaces":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateMarkerInterfaces;
break;
}
case "generate-native-bitfield-attribute":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateNativeBitfieldAttribute;
break;
}
case "generate-native-inheritance-attribute":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateNativeInheritanceAttribute;
break;
}
case "generate-generic-pointer-wrapper":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateGenericPointerWrapper;
break;
}
case "generate-setslastsystemerror-attribute":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateSetsLastSystemErrorAttribute;
break;
}
case "generate-template-bindings":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateTemplateBindings;
break;
}
case "generate-unmanaged-constants":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateUnmanagedConstants;
break;
}
case "generate-vtbl-index-attribute":
{
configOptions |= PInvokeGeneratorConfigurationOptions.GenerateVtblIndexAttribute;
break;
}
// Logging Options
case "log-exclusions":
{
configOptions |= PInvokeGeneratorConfigurationOptions.LogExclusions;
break;
}
case "log-potential-typedef-remappings":
{
configOptions |= PInvokeGeneratorConfigurationOptions.LogPotentialTypedefRemappings;
break;
}
case "log-visited-files":
{
configOptions |= PInvokeGeneratorConfigurationOptions.LogVisitedFiles;
break;
}
// Strip Options
case "strip-enum-member-type-name":
{
configOptions |= PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName;
break;
}
// Legacy Options
case "default-remappings":
{
configOptions &= ~PInvokeGeneratorConfigurationOptions.NoDefaultRemappings;
break;
}
default:
{
errorList.Add($"Error: Unrecognized config switch: {configSwitch}.");
break;
}
}
}
if (!string.IsNullOrWhiteSpace(testOutputLocation) && !configOptions.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsNUnit) && !configOptions.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsXUnit))
{
errorList.Add("Error: No test format provided. Use --config generate-tests-nunit or --config generate-tests-xunit");
}
if (printConfigHelp)
{
var helpBuilder = new CustomHelpBuilder(context.Console, context.LocalizationResources);
helpBuilder.Write(s_configOption);
helpBuilder.WriteLine();
helpBuilder.Write(s_configOptions);
context.ExitCode = -1;
return;
}
if (errorList.Count != 0)
{
context.Console.Error.Write($"Error in args for '{files.FirstOrDefault()}'");
context.Console.Error.Write(Environment.NewLine);
foreach (var error in errorList)
{
context.Console.Error.Write(error);
context.Console.Error.Write(Environment.NewLine);
}
context.Console.Error.Write(Environment.NewLine);
using var textWriter = context.Console.Out.CreateTextWriter();
var customHelpBuilder = new CustomHelpBuilder(context.Console, context.LocalizationResources);
customHelpBuilder.Write(s_rootCommand, textWriter);
context.ExitCode = -1;
return;
}
var clangCommandLineArgs = string.IsNullOrWhiteSpace(std)
? new string[] {
$"--language={language}", // Treat subsequent input files as having type <language>
"-Wno-pragma-once-outside-header" // We are processing files which may be header files
} : [
$"--language={language}", // Treat subsequent input files as having type <language>
$"--std={std}", // Language standard to compile for
"-Wno-pragma-once-outside-header" // We are processing files which may be header files
];
clangCommandLineArgs = [.. clangCommandLineArgs, .. includeDirectories.Select(x => $"--include-directory={x}")];
clangCommandLineArgs = [.. clangCommandLineArgs, .. defineMacros.Select(x => $"--define-macro={x}")];
clangCommandLineArgs = [.. clangCommandLineArgs, .. additionalArgs];
var translationFlags = CXTranslationUnit_None;
translationFlags |= CXTranslationUnit_IncludeAttributedTypes; // Include attributed types in CXType
translationFlags |= CXTranslationUnit_VisitImplicitAttributes; // Implicit attributes should be visited
var config = new PInvokeGeneratorConfiguration(language, std, namespaceName, outputLocation, headerFile, outputMode, configOptions) {
DefaultClass = methodClassName,
ExcludedNames = excludedNames,
IncludedNames = includedNames,
LibraryPath = libraryPath,
MethodPrefixToStrip = methodPrefixToStrip,
NativeTypeNamesToStrip = nativeTypeNamesToStrip,
RemappedNames = remappedNames,
TraversalNames = traversalNames,
TestOutputLocation = testOutputLocation,
WithAccessSpecifiers = withAccessSpecifiers,
WithAttributes = withAttributes,
WithCallConvs = withCallConvs,
WithClasses = withClasses,
WithGuids = withGuids,
WithLengths = withLengths,
WithLibraryPaths = withLibraryPaths,
WithManualImports = withManualImports,
WithNamespaces = withNamespaces,
WithSetLastErrors = withSetLastErrors,
WithSuppressGCTransitions = withSuppressGCTransitions,
WithTransparentStructs = withTransparentStructs,
WithTypes = withTypes,
WithUsings = withUsings,
WithPackings = withPackings,
};
if (config.GenerateMacroBindings)
{
translationFlags |= CXTranslationUnit_DetailedPreprocessingRecord;
}
var exitCode = 0;
using (var pinvokeGenerator = new PInvokeGenerator(config))
{
foreach (var file in files)
{
var filePath = Path.Combine(fileDirectory, file);
var translationUnitError = CXTranslationUnit.TryParse(pinvokeGenerator.IndexHandle, filePath, clangCommandLineArgs, [], translationFlags, out var handle);
var skipProcessing = false;
if (translationUnitError != CXError_Success)
{
context.Console.WriteLine($"Error: Parsing failed for '{filePath}' due to '{translationUnitError}'.");
skipProcessing = true;
}
else if (handle.NumDiagnostics != 0)
{
context.Console.WriteLine($"Diagnostics for '{filePath}':");
for (uint i = 0; i < handle.NumDiagnostics; ++i)
{
using var diagnostic = handle.GetDiagnostic(i);
context.Console.Write(" ");
context.Console.WriteLine(diagnostic.Format(CXDiagnostic.DefaultDisplayOptions).ToString());
skipProcessing |= diagnostic.Severity == CXDiagnostic_Error;
skipProcessing |= diagnostic.Severity == CXDiagnostic_Fatal;
}
}
if (skipProcessing)
{
context.Console.WriteLine($"Skipping '{filePath}' due to one or more errors listed above.");
context.Console.WriteLine("");
exitCode = -1;
continue;
}
#pragma warning disable CA1031 // Do not catch general exception types
try
{
using var translationUnit = TranslationUnit.GetOrCreate(handle);
Debug.Assert(translationUnit is not null);
context.Console.WriteLine($"Processing '{filePath}'");
pinvokeGenerator.GenerateBindings(translationUnit, filePath, clangCommandLineArgs, translationFlags);
}
catch (Exception e)
{
context.Console.WriteLine(e.ToString());
}
#pragma warning restore CA1031 // Do not catch general exception types
}
if (pinvokeGenerator.Diagnostics.Count != 0)
{
context.Console.WriteLine($"Diagnostics for binding generation of {pinvokeGenerator.FilePath}:");
foreach (var diagnostic in pinvokeGenerator.Diagnostics)
{
context.Console.Write(" ");
context.Console.WriteLine(diagnostic.ToString());
if (diagnostic.Level == DiagnosticLevel.Warning)
{
if (exitCode >= 0)
{
exitCode++;
}
}
else if (diagnostic.Level == DiagnosticLevel.Error)
{
if (exitCode >= 0)
{
exitCode = -1;
}
else
{
exitCode--;
}
}
}
}
}
context.ExitCode = exitCode;
}
private static void ParseKeyValuePairs(IEnumerable<string> keyValuePairs, List<string> errorList, out Dictionary<string, string> result)
{
result = [];
foreach (var keyValuePair in keyValuePairs)
{
var parts = keyValuePair.Split('=', 2);
if (parts.Length < 2)
{
errorList.Add($"Error: Invalid key/value pair argument: {keyValuePair}. Expected 'name=value'");
continue;
}
var key = parts[0].TrimEnd();
if (result.TryGetValue(key, out var value))
{
errorList.Add($"Error: A key with the given name already exists: {key}. Existing: {value}");
continue;
}
result.Add(key, parts[1].TrimStart());
}
}
private static void ParseKeyValuePairs(IEnumerable<string> keyValuePairs, List<string> errorList, out Dictionary<string, AccessSpecifier> result)
{
result = [];
foreach (var keyValuePair in keyValuePairs)
{
var parts = keyValuePair.Split('=');
if (parts.Length != 2)
{
errorList.Add($"Error: Invalid key/value pair argument: {keyValuePair}. Expected 'name=value'");
continue;
}
var key = parts[0].TrimEnd();
if (result.TryGetValue(key, out var value))
{
errorList.Add($"Error: A key with the given name already exists: {key}. Existing: {value}");
continue;
}
result.Add(key, PInvokeGeneratorConfiguration.ConvertStringToAccessSpecifier(parts[1].TrimStart()));
}
}
private static void ParseKeyValuePairs(IEnumerable<string> keyValuePairs, List<string> errorList, out Dictionary<string, Guid> result)
{
result = [];
foreach (var keyValuePair in keyValuePairs)
{
var parts = keyValuePair.Split('=');
if (parts.Length != 2)
{
errorList.Add($"Error: Invalid key/value pair argument: {keyValuePair}. Expected 'name=value'");
continue;
}
var key = parts[0].TrimEnd();
if (result.TryGetValue(key, out var value))
{
errorList.Add($"Error: A key with the given name already exists: {key}. Existing: {value}");
continue;
}
if (!Guid.TryParse(parts[1].TrimStart(), out var guid))
{
errorList.Add($"Error: Failed to parse guid: {parts[1]}");
continue;
}
result.Add(key, guid);
}
}
private static void ParseKeyValuePairs(IEnumerable<string> keyValuePairs, List<string> errorList, out Dictionary<string, (string, PInvokeGeneratorTransparentStructKind)> result)
{
result = [];
foreach (var keyValuePair in keyValuePairs)
{
var parts = keyValuePair.Split('=');
if (parts.Length != 2)
{
errorList.Add($"Error: Invalid key/value pair argument: {keyValuePair}. Expected 'name=value' or 'name=value;kind'");
continue;
}
var key = parts[0].TrimEnd();
if (result.TryGetValue(key, out var value))
{
errorList.Add($"Error: A key with the given name already exists: {key}. Existing: {value}");
continue;
}
parts = parts[1].Split(';');
if (parts.Length == 1)
{
result.Add(key, (parts[0], PInvokeGeneratorTransparentStructKind.Unknown));
}
else if ((parts.Length == 2) && Enum.TryParse<PInvokeGeneratorTransparentStructKind>(parts[1], out var transparentStructKind))
{
result.Add(key, (parts[0], transparentStructKind));
}
else
{
errorList.Add($"Error: Invalid key/value pair argument: {keyValuePair}. Expected 'name=value' or 'name=value;kind'");
continue;
}
}
}
private static void ParseKeyValuePairs(IEnumerable<string> keyValuePairs, List<string> errorList, out Dictionary<string, IReadOnlyList<string>> result)
{
result = [];
foreach (var keyValuePair in keyValuePairs)
{
var parts = keyValuePair.Split('=');
if (parts.Length != 2)
{
errorList.Add($"Error: Invalid key/value pair argument: {keyValuePair}. Expected 'name=value'");
continue;
}
var key = parts[0].TrimEnd();
if (!result.TryGetValue(key, out var value))
{
value = new List<string>();
result.Add(key, value);
}
var list = (List<string>)value;
list.Add(parts[1].TrimStart());
}
}
private static Option<string[]> GetAdditionalOption()
{
return new Option<string[]>(
aliases: s_additionalOptionAliases,
description: "An argument to pass to Clang when parsing the input files.",
getDefaultValue: Array.Empty<string>
) {
AllowMultipleArgumentsPerToken = true
};
}
private static Option<string[]> GetConfigOption()
{
return new Option<string[]>(
aliases: s_configOptionAliases,
description: "A configuration option that controls how the bindings are generated. Specify 'help' to see the available options.",
getDefaultValue: Array.Empty<string>
) {
AllowMultipleArgumentsPerToken = true