Skip to content

Commit

Permalink
[New][Bff] Record the unity files in the auto cleanup db, so we can a…
Browse files Browse the repository at this point in the history
…uto remove stale ones
  • Loading branch information
belkiss committed Feb 9, 2022
1 parent 76637b8 commit a0c127e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Sharpmake.Generators/FastBuild/Bff.Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class Unity
public string UseRelativePaths = FileGeneratorUtilities.RemoveLineTag; // (optional) Use relative paths for generated Unity files

public const string DefaultUnityInputPatternExtension = ".cpp";
public const string DefaultUnityOutputPatternExtension = "Unity*.cpp";

internal string UnityFullOutputPath = string.Empty; // Path to output generated Unity files

public override int GetHashCode()
{
Expand Down
32 changes: 29 additions & 3 deletions Sharpmake.Generators/FastBuild/Bff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,10 +1366,35 @@ List<string> skipFiles
}

// Write all unity sections together at the beginning of the .bff just after the header.
foreach (var unityFile in _unities.Keys.OrderBy(u => u.UnityName))
if (_unities.Any())
{
using (bffWholeFileGenerator.Declare("unityFile", unityFile))
bffWholeFileGenerator.Write(Template.ConfigurationFile.UnitySection);
foreach (var unityFile in _unities.Keys.OrderBy(u => u.UnityName))
{
using (bffWholeFileGenerator.Declare("unityFile", unityFile))
bffWholeFileGenerator.Write(Template.ConfigurationFile.UnitySection);

// Record the unities in the autocleanupdb to allow auto removal when they become stale.
// Note that can't record them as 'generated', since they are created by FastBuild and not by us.
int nbUnities = 1;
if (unityFile.UnityNumFiles != FileGeneratorUtilities.RemoveLineTag)
{
if (!int.TryParse(unityFile.UnityNumFiles, out nbUnities))
throw new Error("'{0}' cannot be converted to int!", unityFile.UnityNumFiles);
}

string outputPattern = unityFile.UnityOutputPattern == FileGeneratorUtilities.RemoveLineTag ? Sharpmake.Generators.FastBuild.Bff.Unity.DefaultUnityOutputPatternExtension : unityFile.UnityOutputPattern;
int wildcardIndex = outputPattern.IndexOf('*');
if (wildcardIndex == -1)
throw new Error("UnityOutputPattern must include a '*', but none was found in '{0}'!", unityFile.UnityNumFiles);

string firstStringChunk = outputPattern.Substring(0, wildcardIndex);
string lastStringChunk = outputPattern.Substring(wildcardIndex + 1);
for (int i = 1; i <= nbUnities; ++i)
{
string fullPath = Path.Combine(unityFile.UnityFullOutputPath, $"{firstStringChunk}{i}{lastStringChunk}");
Util.RecordInAutoCleanupDatabase(fullPath);
}
}
}

// Now combine all the streams.
Expand Down Expand Up @@ -1878,6 +1903,7 @@ private void ConfigureUnities(IGenerationContext context, Dictionary<Project.Con
{
// Note that the UnityName and UnityOutputPattern are intentionally left empty: they will be set in the Resolve
UnityOutputPath = CurrentBffPathKeyCombine(Util.PathGetRelative(context.ProjectDirectoryCapitalized, conf.FastBuildUnityPath, true)),
UnityFullOutputPath = Path.Combine(context.ProjectDirectoryCapitalized, conf.FastBuildUnityPath),
UnityInputIsolateWritableFiles = conf.FastBuildUnityInputIsolateWritableFiles.ToString().ToLower(),
UnityInputIsolateWritableFilesLimit = conf.FastBuildUnityInputIsolateWritableFiles ? conf.FastBuildUnityInputIsolateWritableFilesLimit.ToString() : FileGeneratorUtilities.RemoveLineTag,
UnityPCH = conf.PrecompHeader ?? FileGeneratorUtilities.RemoveLineTag,
Expand Down
7 changes: 6 additions & 1 deletion Sharpmake/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,15 @@ public static bool FileWriteIfDifferent(FileInfo file, MemoryStream stream)
return Builder.Instance.Context.WriteGeneratedFile(null, file, stream);
}

internal static bool RecordInAutoCleanupDatabase(string fullPath)
{
return s_writtenFiles.TryAdd(fullPath, DateTime.Now);
}

internal static bool FileWriteIfDifferentInternal(FileInfo file, MemoryStream stream, bool bypassAutoCleanupDatabase = false)
{
if (!bypassAutoCleanupDatabase)
s_writtenFiles.TryAdd(file.FullName, DateTime.Now);
RecordInAutoCleanupDatabase(file.FullName);

if (file.Exists)
{
Expand Down

0 comments on commit a0c127e

Please sign in to comment.