Skip to content

Commit

Permalink
Refasm a dll after compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
safesparrow committed Oct 31, 2023
1 parent fe5041b commit 653a543
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions MSBuild.CompilerCache/LocatorAndPopulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,33 @@ public UseOrPopulateResult PopulateCache(TaskLoggingHelper log, Action<string>?
//logTime?.Invoke("Start");
var postCompilationTimeUtc = DateTime.UtcNow;

// TODO Refactor, deduplicate
void RefasmCompiledDlls()
{
bool RefasmableOutputItem(OutputItem o) => Path.GetExtension(o.LocalPath) is ".dll";
var dllsToRefasm = _decomposed.OutputsToCache.Where(RefasmableOutputItem).ToArray();
foreach (var dll in dllsToRefasm)
{
var bytes = File.ReadAllBytes(dll.LocalPath);
var trimmer = new RefTrimmer();
var toBeCached = trimmer.GenerateRefData(ImmutableArray.Create(bytes));
var fileInfo = new FileInfo(dll.LocalPath);
var fileCacheKey = FileHashCacheKey.FromFileInfo(fileInfo);
var hashString = _fileHashCache.Get(fileCacheKey);
var extract = new LocalFileExtract(fileCacheKey, hashString);
var name = Path.GetFileNameWithoutExtension(fileInfo.Name);
var cacheKey = BuildRefCacheKey(name, hashString);
var cached = new RefDataWithOriginalExtract(Ref: toBeCached, Original: extract);
_refCache.Set(cacheKey, cached);
}
}

var refasmCompiledDllsTask = System.Threading.Tasks.Task.Factory.StartNew(RefasmCompiledDlls);

var meta = GetCompilationMetadata(postCompilationTimeUtc);
var stuff = new AllCompilationMetadata(Metadata: meta, LocalInputs: _localInputs);
//logTime?.Invoke("Got stuff");

using var tmpDir = new DisposableDir();
var outputZip = BuildOutputsZip(tmpDir, _decomposed.OutputsToCache, stuff, Utils.DefaultHasher, log);

Expand All @@ -431,6 +455,8 @@ public UseOrPopulateResult PopulateCache(TaskLoggingHelper log, Action<string>?
$"CompilationCache miss and inputs changed during compilation. The cache will not be populated as we are not certain what inputs the compiler used.");
}

refasmCompiledDllsTask.GetAwaiter().GetResult();

return new UseOrPopulateResult();
}

Expand All @@ -440,14 +466,17 @@ public static FileInfo BuildOutputsZip(DirectoryInfo baseTmpDir, OutputItem[] it
{
var outputsDir = baseTmpDir.CreateSubdirectory("outputs_zip_building");

System.Threading.Tasks.Task.Run(() =>
async System.Threading.Tasks.Task SaveInputs()
{
var metaPath = outputsDir.CombineAsFile("__inputs.json").FullName;
{
using var fs = File.OpenWrite(metaPath);
JsonSerializer.Serialize(fs, metadata, AllCompilationMetadataJsonContext.Default.AllCompilationMetadata);
}
});
var metaPath = outputsDir!.CombineAsFile("__inputs.json").FullName;
// ReSharper disable once UseAwaitUsing
using var fs = File.OpenWrite(metaPath);
await JsonSerializer.SerializeAsync(fs, metadata,
AllCompilationMetadataJsonContext.Default.AllCompilationMetadata);
}

// Write inputs json in parallel to the rest
var saveInputsTask = SaveInputs();

var outputExtracts =
items.Select(item =>
Expand All @@ -467,20 +496,21 @@ public static FileInfo BuildOutputsZip(DirectoryInfo baseTmpDir, OutputItem[] it
JsonSerializer.Serialize(fs, outputExtracts, OutputExtractsJsonContext.Default.FileExtractArray);
}

// Make sure inputs json written before zipping the whole directory
saveInputsTask.GetAwaiter().GetResult();

var tempZipPath = baseTmpDir.CombineAsFile($"{hashForFileName}.zip");
ZipFile.CreateFromDirectory(outputsDir.FullName, tempZipPath.FullName,
CompressionLevel.NoCompression, includeBaseDirectory: false);
return tempZipPath;
}
}


[JsonSerializable(typeof(FileExtract[]))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class OutputExtractsJsonContext : JsonSerializerContext
{ }


[JsonSerializable(typeof(AllCompilationMetadata))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class AllCompilationMetadataJsonContext : JsonSerializerContext
Expand Down

0 comments on commit 653a543

Please sign in to comment.