Skip to content

Commit e6c6176

Browse files
committed
Use cache for incremental builds
1 parent 1fffa3b commit e6c6176

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/mono/sample/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
OutputDir="$(PublishDir)"
2727
IntermediateOutputPath="$(IntermediateOutputPath)"
2828
UseAotDataFile="$(UseAotDataFile)"
29+
CacheFilePath="$(IntermediateOutputPath)aot_compiler_cache.json"
2930
NetTracePath="$(NetTracePath)"
3031
PgoBinaryPath="$(PgoBinaryPath)"
3132
MibcProfilePath="$(MibcProfilePath)">

src/tasks/AotCompilerTask/MonoAOTCompiler.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,20 @@ private bool ProcessNettrace(string netTraceFile)
425425
{
426426
var outputMibcPath = Path.Combine(OutputDir, Path.ChangeExtension(Path.GetFileName(netTraceFile), ".mibc"));
427427

428+
if (_cache!.Enabled)
429+
{
430+
string hash = Utils.ComputeHash(netTraceFile);
431+
if (!_cache!.UpdateAndCheckHasFileChanged($"-mibc-source-file-{Path.GetFileName(netTraceFile)}", hash))
432+
{
433+
Log.LogMessage(MessageImportance.Low, $"Skipping generating {outputMibcPath} from {netTraceFile} because source file hasn't changed");
434+
return true;
435+
}
436+
else
437+
{
438+
Log.LogMessage(MessageImportance.Low, $"Generating {outputMibcPath} from {netTraceFile} because the source file's hash has changed.");
439+
}
440+
}
441+
428442
(int exitCode, string output) = Utils.TryRunProcess(Log,
429443
PgoBinaryPath!,
430444
$"create-mibc --trace {netTraceFile} --output {outputMibcPath}");
@@ -876,12 +890,13 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
876890
private bool PrecompileLibrary(PrecompileArguments args)
877891
{
878892
string assembly = args.AOTAssembly.GetMetadata("FullPath");
893+
string output;
879894
try
880895
{
881896
string msgPrefix = $"[{Path.GetFileName(assembly)}] ";
882897

883898
// run the AOT compiler
884-
(int exitCode, string output) = Utils.TryRunProcess(Log,
899+
(int exitCode, output) = Utils.TryRunProcess(Log,
885900
CompilerBinaryPath,
886901
$"--response=\"{args.ResponseFilePath}\"",
887902
args.EnvironmentVariables,
@@ -921,6 +936,12 @@ private bool PrecompileLibrary(PrecompileArguments args)
921936
bool copied = false;
922937
foreach (var proxyFile in args.ProxyFiles)
923938
{
939+
if (!File.Exists(proxyFile.TempFile))
940+
{
941+
Log.LogError($"Precompile command succeeded, but can't find the expected temporary output file - {proxyFile.TempFile} for {assembly}.{Environment.NewLine}{output}");
942+
return false;
943+
}
944+
924945
copied |= proxyFile.CopyOutputFileIfChanged();
925946
_fileWrites.Add(proxyFile.TargetFile);
926947
}
@@ -1135,8 +1156,20 @@ public FileCache(string? cacheFilePath, TaskLoggingHelper log)
11351156
_newCache = new(_oldCache.FileHashes);
11361157
}
11371158

1159+
public bool UpdateAndCheckHasFileChanged(string filePath, string newHash)
1160+
{
1161+
if (!Enabled)
1162+
throw new InvalidOperationException("Cache is not enabled. Make sure the cache file path is set");
1163+
1164+
_newCache!.FileHashes[filePath] = newHash;
1165+
return !_oldCache!.FileHashes.TryGetValue(filePath, out string? oldHash) || oldHash != newHash;
1166+
}
1167+
11381168
public bool ShouldCopy(ProxyFile proxyFile, [NotNullWhen(true)] out string? cause)
11391169
{
1170+
if (!Enabled)
1171+
throw new InvalidOperationException("Cache is not enabled. Make sure the cache file path is set");
1172+
11401173
cause = null;
11411174

11421175
string newHash = Utils.ComputeHash(proxyFile.TempFile);
@@ -1194,6 +1227,9 @@ public bool CopyOutputFileIfChanged()
11941227

11951228
try
11961229
{
1230+
if (!File.Exists(TempFile))
1231+
throw new LogAsErrorException($"Could not find the temporary file {TempFile} for target file {TargetFile}. Look for any errors/warnings generated earlier in the build.");
1232+
11971233
if (!_cache.ShouldCopy(this, out string? cause))
11981234
{
11991235
_cache.Log.LogMessage(MessageImportance.Low, $"Skipping copying over {TargetFile} as the contents are unchanged");

0 commit comments

Comments
 (0)