From 2783eb07b282eb5f15e9aad001a25fbae1a9d268 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 9 Jul 2021 22:22:49 +0200 Subject: [PATCH] Add intermediate data folder removing on build tool clean command --- Source/Tools/Flax.Build/Build/Builder.cs | 124 +++++++++++++++++- .../Tools/Flax.Build/Build/Graph/TaskGraph.cs | 3 + 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/Source/Tools/Flax.Build/Build/Builder.cs b/Source/Tools/Flax.Build/Build/Builder.cs index fd784fc3d..23f166c4f 100644 --- a/Source/Tools/Flax.Build/Build/Builder.cs +++ b/Source/Tools/Flax.Build/Build/Builder.cs @@ -13,6 +13,42 @@ namespace Flax.Build /// public static partial class Builder { + private static void CleanDirectory(DirectoryInfo dir) + { + var subdirs = dir.GetDirectories(); + foreach (var subdir in subdirs) + { + CleanDirectory(subdir); + } + + var files = dir.GetFiles(); + foreach (var file in files) + { + try + { + file.Delete(); + } + catch (UnauthorizedAccessException) + { + File.SetAttributes(file.FullName, FileAttributes.Normal); + file.Delete(); + } + catch + { + // Skip errors + } + } + + try + { + dir.Delete(); + } + catch + { + // Skip errors + } + } + /// /// Cleans the build system cache and intermediate results. /// @@ -20,8 +56,94 @@ namespace Flax.Build { using (new ProfileEventScope("Clean")) { - var graph = new TaskGraph(Globals.Root); + // Clear task graph cache + var project = Globals.Project; + TaskGraph graph; + if (project == null) + { + graph = new TaskGraph(Globals.Root); + graph.CleanCache(); + return; + } + graph = new TaskGraph(project.ProjectFolderPath); graph.CleanCache(); + + // Pick targets to clean + var customBuildTargets = Configuration.BuildTargets; + var projectTargets = GetProjectTargets(project); + var targets = customBuildTargets == null ? projectTargets : projectTargets.Where(target => customBuildTargets.Contains(target.Name)).ToArray(); + foreach (var target in targets) + { + // Pick configurations to clean + TargetConfiguration[] configurations = Configuration.BuildConfigurations; + if (configurations != null) + { + foreach (var configuration in configurations) + { + if (!target.Configurations.Contains(configuration)) + throw new Exception(string.Format("Target {0} does not support {1} configuration.", target.Name, configuration)); + } + } + else + { + configurations = target.Configurations; + } + + foreach (var configuration in configurations) + { + // Pick platforms to clean + TargetPlatform[] platforms = Configuration.BuildPlatforms; + if (platforms != null) + { + foreach (var platform in platforms) + { + if (!target.Platforms.Contains(platform)) + throw new Exception(string.Format("Target {0} does not support {1} platform.", target.Name, platform)); + } + } + else + { + platforms = target.Platforms; + } + + foreach (var targetPlatform in platforms) + { + // Pick architectures to build + TargetArchitecture[] architectures = Configuration.BuildArchitectures; + if (architectures != null) + { + foreach (var e in architectures) + { + if (!target.Architectures.Contains(e)) + throw new Exception(string.Format("Target {0} does not support {1} architecture.", target.Name, e)); + } + } + else + { + architectures = target.GetArchitectures(targetPlatform); + } + + foreach (var architecture in architectures) + { + if (!Platform.IsPlatformSupported(targetPlatform, architecture)) + continue; + //throw new Exception(string.Format("Platform {0} {1} is not supported.", targetPlatform, architecture)); + + var platform = Platform.GetPlatform(targetPlatform); + var toolchain = platform.GetToolchain(architecture); + var targetBuildOptions = GetBuildOptions(target, toolchain.Platform, toolchain, toolchain.Architecture, configuration, project.ProjectFolderPath, string.Empty); + + // Delete all intermediate files + var intermediateFolder = new DirectoryInfo(targetBuildOptions.IntermediateFolder); + if (intermediateFolder.Exists) + { + Log.Info("Removing: " + targetBuildOptions.IntermediateFolder); + CleanDirectory(intermediateFolder); + } + } + } + } + } } } diff --git a/Source/Tools/Flax.Build/Build/Graph/TaskGraph.cs b/Source/Tools/Flax.Build/Build/Graph/TaskGraph.cs index b74eedf46..b328956f2 100644 --- a/Source/Tools/Flax.Build/Build/Graph/TaskGraph.cs +++ b/Source/Tools/Flax.Build/Build/Graph/TaskGraph.cs @@ -421,7 +421,10 @@ namespace Flax.Build.Graph { var path = CachePath; if (File.Exists(path)) + { + Log.Info("Removing: " + path); File.Delete(path); + } } ///