From 13e11091fc1504ae63d922e9685e230c5d28481f Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 30 Jul 2023 21:43:28 +0300 Subject: [PATCH 1/2] Support user defined .NET analyzers/source generators in Flax.Build --- .../Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs | 6 ++++-- .../Flax.Build/Build/NativeCpp/BuildOptions.cs | 13 ++++++++++--- .../Projects/VisualStudio/CSSDKProjectGenerator.cs | 8 ++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 13433f92c..2cdc57686 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -272,8 +272,10 @@ namespace Flax.Build foreach (var reference in fileReferences) args.Add(string.Format("/reference:\"{0}\"", reference)); #if USE_NETCORE - foreach (var analyzer in buildOptions.ScriptingAPI.SystemAnalyzers) - args.Add(string.Format("/analyzer:\"{0}{1}.dll\"", referenceAnalyzers, analyzer)); + foreach (var systemAnalyzer in buildOptions.ScriptingAPI.SystemAnalyzers) + args.Add(string.Format("/analyzer:\"{0}{1}.dll\"", referenceAnalyzers, systemAnalyzer)); + foreach (var analyzer in buildOptions.ScriptingAPI.Analyzers) + args.Add(string.Format("/analyzer:\"{0}\"", analyzer)); #endif foreach (var sourceFile in sourceFiles) args.Add("\"" + sourceFile + "\""); diff --git a/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs b/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs index ef365d3b7..e612c0a95 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs @@ -200,14 +200,19 @@ namespace Flax.Build.NativeCpp public HashSet SystemReferences; /// - /// The .Net libraries references (dll or exe files paths). + /// The system analyzers/source generators. + /// + public HashSet SystemAnalyzers; + + /// + /// The .NET libraries references (dll or exe files paths). /// public HashSet FileReferences; /// - /// The .Net libraries references (dll or exe files paths). + /// The .NET analyzers (dll or exe files paths). /// - public HashSet SystemAnalyzers; + public HashSet Analyzers; /// /// True if ignore compilation warnings due to missing code documentation comments. @@ -232,6 +237,7 @@ namespace Flax.Build.NativeCpp Defines.AddRange(other.Defines); SystemReferences.AddRange(other.SystemReferences); FileReferences.AddRange(other.FileReferences); + Analyzers.AddRange(other.Analyzers); IgnoreMissingDocumentationWarnings |= other.IgnoreMissingDocumentationWarnings; } } @@ -305,6 +311,7 @@ namespace Flax.Build.NativeCpp "Microsoft.Interop.SourceGeneration", }, FileReferences = new HashSet(), + Analyzers = new HashSet(), }; /// diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs index efce04776..5fba0dc43 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs @@ -247,6 +247,14 @@ namespace Flax.Build.Projects.VisualStudio csProjectFileContent.AppendLine(" "); } } + foreach (var analyzer in configuration.TargetBuildOptions.ScriptingAPI.Analyzers) + { + csProjectFileContent.AppendLine(string.Format(" ", configuration.Name)); + csProjectFileContent.AppendLine(string.Format(" ", Path.GetFileNameWithoutExtension(analyzer))); + csProjectFileContent.AppendLine(string.Format(" {0}", Utilities.MakePathRelativeTo(analyzer, projectDirectory).Replace('/', '\\'))); + csProjectFileContent.AppendLine(" "); + csProjectFileContent.AppendLine(" "); + } csProjectFileContent.AppendLine(""); } From 735b2e30f00c29aec9ad3906a4bf4fc17dcbc41b Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 30 Jul 2023 21:55:23 +0300 Subject: [PATCH 2/2] Output generated .NET source generator files to Intermediate folder Mostly useful for debugging source generators, VS doesn't seem to utilize these files in any way. --- Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 2cdc57686..d6772379f 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -159,6 +159,7 @@ namespace Flax.Build var outputPath = Path.GetDirectoryName(buildData.Target.GetOutputFilePath(buildOptions)); var outputFile = Path.Combine(outputPath, name + ".dll"); var outputDocFile = Path.Combine(outputPath, name + ".xml"); + var outputGeneratedFiles = Path.Combine(buildOptions.IntermediateFolder); string cscPath, referenceAssemblies; #if USE_NETCORE var dotnetSdk = DotNetSdk.Instance; @@ -263,6 +264,9 @@ namespace Flax.Build #endif args.Add(string.Format("/out:\"{0}\"", outputFile)); args.Add(string.Format("/doc:\"{0}\"", outputDocFile)); +#if USE_NETCORE + args.Add(string.Format("/generatedfilesout:\"{0}\"", outputGeneratedFiles)); +#endif if (buildOptions.ScriptingAPI.Defines.Count != 0) args.Add("/define:" + string.Join(";", buildOptions.ScriptingAPI.Defines)); if (buildData.Configuration == TargetConfiguration.Debug)