From 0d7e7c30ca0269594bd0cddee5f8c5d515b33051 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Wed, 11 Oct 2023 21:40:38 +0300 Subject: [PATCH 1/4] Run Flax.Build when building main C# project --- .../VisualStudio/CSSDKProjectGenerator.cs | 17 ++++-- .../VisualStudioProjectGenerator.cs | 60 +++++++++++++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs index 387a0dcff..1ff3ea4db 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs @@ -90,6 +90,11 @@ namespace Flax.Build.Projects.VisualStudio var baseConfiguration = project.Configurations.First(); var baseOutputDir = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? baseConfiguration.TargetBuildOptions.OutputFolder, projectDirectory); var baseIntermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(baseConfiguration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory); + + bool isMainProject = Globals.Project.ProjectFolderPath == project.WorkspaceRootPath && project.Name != "BuildScripts"; + var flaxBuildTargetsFilename = isMainProject ? "Flax.Build.CSharp.targets" : "Flax.Build.CSharp.SkipBuild.targets"; + var cacheProjectsPath = Utilities.MakePathRelativeTo(Path.Combine(Globals.Root, "Cache", "Projects"), projectDirectory); + var flaxBuildTargetsPath = !string.IsNullOrEmpty(cacheProjectsPath) ? Path.Combine(cacheProjectsPath, flaxBuildTargetsFilename) : flaxBuildTargetsFilename; csProjectFileContent.AppendLine(" net7.0"); csProjectFileContent.AppendLine(" disable"); @@ -106,14 +111,14 @@ namespace Flax.Build.Projects.VisualStudio csProjectFileContent.AppendLine(" 11.0"); csProjectFileContent.AppendLine(" 512"); - // Needed for Hostfxr - csProjectFileContent.AppendLine(" true"); - csProjectFileContent.AppendLine(" true"); //csProjectFileContent.AppendLine(" false"); // TODO: use it to reduce burden of framework libs - // This needs to be set here to fix errors in VS - csProjectFileContent.AppendLine(string.Format(" {0}", baseOutputDir)); - csProjectFileContent.AppendLine(string.Format(" {0}", baseIntermediateOutputPath)); + // Custom .targets file for overriding MSBuild build tasks + csProjectFileContent.AppendLine(string.Format(" $(MSBuildThisFileDirectory){0}", flaxBuildTargetsPath)); + + // Hide annoying warnings during build + csProjectFileContent.AppendLine(" false"); + csProjectFileContent.AppendLine(" True"); csProjectFileContent.AppendLine(" "); csProjectFileContent.AppendLine(""); diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs index 1ba8e6709..dd4bf2f42 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs @@ -679,6 +679,57 @@ namespace Flax.Build.Projects.VisualStudio Utilities.WriteFileIfChanged(dotSettingsUserFilePath, dotSettingsFileContent.ToString()); } + + // Custom MSBuild .targets file to prevent building Flax C#-projects directly with MSBuild + { + var targetsFileContent = new StringBuilder(); + targetsFileContent.AppendLine(""); + targetsFileContent.AppendLine(" "); + targetsFileContent.AppendLine(" "); + targetsFileContent.AppendLine(""); + + Utilities.WriteFileIfChanged(Path.Combine(Globals.Root, "Cache", "Projects", "Flax.Build.CSharp.SkipBuild.targets"), targetsFileContent.ToString()); + } + + // Override MSBuild build tasks to run Flax.Build in C#-only projects + { + // Build command for the build tool + var buildToolPath = Path.ChangeExtension(Utilities.MakePathRelativeTo(typeof(Builder).Assembly.Location, Path.GetDirectoryName(solution.MainProject.Path)), null); + + var targetsFileContent = new StringBuilder(); + targetsFileContent.AppendLine(""); + targetsFileContent.AppendLine(" "); + targetsFileContent.AppendLine(" "); + AppendBuildToolCommands(targetsFileContent, "-build"); + targetsFileContent.AppendLine(" "); + targetsFileContent.AppendLine(" "); + AppendBuildToolCommands(targetsFileContent, "-rebuild"); + targetsFileContent.AppendLine(" "); + targetsFileContent.AppendLine(" "); + AppendBuildToolCommands(targetsFileContent, "-clean"); + targetsFileContent.AppendLine(" "); + targetsFileContent.AppendLine(""); + + Utilities.WriteFileIfChanged(Path.Combine(Globals.Root, "Cache", "Projects", "Flax.Build.CSharp.targets"), targetsFileContent.ToString()); + + void AppendBuildToolCommands(StringBuilder str, string extraArgs) + { + foreach (var configuration in solution.MainProject.Configurations) + { + var cmdLine = string.Format("{0} -log -mutex -workspace={1} -arch={2} -configuration={3} -platform={4} -buildTargets={5}", + FixPath(buildToolPath), + FixPath(solution.MainProject.WorkspaceRootPath), + configuration.Architecture, + configuration.Configuration, + configuration.Platform, + configuration.Target); + if (!string.IsNullOrEmpty(Configuration.Compiler)) + cmdLine += " -compiler=" + Configuration.Compiler; + + str.AppendLine(string.Format(" ", cmdLine, extraArgs, configuration.Name)); + } + } + } } /// @@ -716,5 +767,14 @@ namespace Flax.Build.Projects.VisualStudio projects.Add(project); } } + + private static string FixPath(string path) + { + if (path.Contains(' ')) + { + path = "\"" + path + "\""; + } + return path; + } } } From 5b3e09baec4bb8680186d4d586c71136bc8b4fd1 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Wed, 11 Oct 2023 21:42:06 +0300 Subject: [PATCH 2/4] Build C# projects in VS/Rider solution configurations Rider's solution wide analysis does not work properly when projects are not included in the active configuration for build. --- .../VisualStudio/VisualStudioProjectGenerator.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs index dd4bf2f42..48ecb6425 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/VisualStudioProjectGenerator.cs @@ -518,7 +518,15 @@ namespace Flax.Build.Projects.VisualStudio else if (firstFullMatch != -1) { projectConfiguration = configuration; - build = solution.MainProject == project || (solution.MainProject == null && project.Name == solution.Name); + + // Always build the main project + build = solution.MainProject == project; + + // Build C# projects (needed for Rider solution wide analysis) + build |= project.Type == TargetType.DotNetCore; + + // + build |= solution.MainProject == null && project.Name == solution.Name; } else if (firstPlatformMatch != -1 && !configuration.Name.StartsWith("Editor.")) { From ff7e6d82f8cff320cdbd7afb8049ad0dca89f434 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Wed, 11 Oct 2023 21:42:45 +0300 Subject: [PATCH 3/4] Hide exception when build errors occurs in referenced targets --- .../Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs | 6 ++++++ Source/Tools/Flax.Build/Globals.cs | 5 +++++ Source/Tools/Flax.Build/Program.cs | 4 +++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs index 1e8550c4e..4192b0caa 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs @@ -628,7 +628,10 @@ namespace Flax.Build } } if (failed) + { + Globals.BuildErrors = true; throw new Exception($"Failed to build target {target.Name}. See log."); + } } else { @@ -689,7 +692,10 @@ namespace Flax.Build } } if (failed) + { + Globals.BuildErrors = true; throw new Exception($"Failed to build target {target.Name}. See log."); + } } else { diff --git a/Source/Tools/Flax.Build/Globals.cs b/Source/Tools/Flax.Build/Globals.cs index b5f3088d6..994094124 100644 --- a/Source/Tools/Flax.Build/Globals.cs +++ b/Source/Tools/Flax.Build/Globals.cs @@ -22,6 +22,11 @@ namespace Flax.Build /// public static ProjectInfo Project; + /// + /// Set when any build related errors were raised. + /// + public static bool BuildErrors = false; + /// /// All platforms array. /// diff --git a/Source/Tools/Flax.Build/Program.cs b/Source/Tools/Flax.Build/Program.cs index 5e7cf926c..bac1caaa8 100644 --- a/Source/Tools/Flax.Build/Program.cs +++ b/Source/Tools/Flax.Build/Program.cs @@ -171,7 +171,9 @@ namespace Flax.Build } catch (Exception ex) { - Log.Exception(ex); + // Ignore exception logging for build errors + if (!Globals.BuildErrors) + Log.Exception(ex); failed = true; } finally From bcdd6c0551cd126a483624f66ba033f46f0b2091 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 15 Oct 2023 16:15:51 +0300 Subject: [PATCH 4/4] Fix FlaxEngine C#-project getting built twice in engine solution --- .../Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs index 1ff3ea4db..77b7abf46 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs @@ -91,7 +91,7 @@ namespace Flax.Build.Projects.VisualStudio var baseOutputDir = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? baseConfiguration.TargetBuildOptions.OutputFolder, projectDirectory); var baseIntermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(baseConfiguration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory); - bool isMainProject = Globals.Project.ProjectFolderPath == project.WorkspaceRootPath && project.Name != "BuildScripts"; + bool isMainProject = Globals.Project.ProjectFolderPath == project.WorkspaceRootPath && project.Name != "BuildScripts" && (Globals.Project.Name != "Flax" || project.Name != "FlaxEngine"); var flaxBuildTargetsFilename = isMainProject ? "Flax.Build.CSharp.targets" : "Flax.Build.CSharp.SkipBuild.targets"; var cacheProjectsPath = Utilities.MakePathRelativeTo(Path.Combine(Globals.Root, "Cache", "Projects"), projectDirectory); var flaxBuildTargetsPath = !string.IsNullOrEmpty(cacheProjectsPath) ? Path.Combine(cacheProjectsPath, flaxBuildTargetsFilename) : flaxBuildTargetsFilename;