From 7e42fc1d7f5d7d41e70b1b993439d859c2e74124 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 30 Nov 2024 21:39:38 +0100 Subject: [PATCH] Avoid rebuilding libs or clr on unrelated changes --- Runner/Helpers/GitHelper.cs | 15 ++++++++++ Runner/Jobs/JitDiffJob.cs | 57 +++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/Runner/Helpers/GitHelper.cs b/Runner/Helpers/GitHelper.cs index 94b0843..3eff0f8 100644 --- a/Runner/Helpers/GitHelper.cs +++ b/Runner/Helpers/GitHelper.cs @@ -30,4 +30,19 @@ private static bool ShouldSkipLine(string line) span.StartsWith("@@", StringComparison.Ordinal) || span.StartsWith("\\ No newline at end of file", StringComparison.Ordinal); } + + public static async Task> GetChangedFilesAsync(JobBase job, string baselineRef, string workDir) + { + List lines = []; + + await job.RunProcessAsync("git", + $"diff --name-only {baselineRef}", + lines, + workDir: workDir, + checkExitCode: false, + suppressOutputLogs: true, + suppressStartingLog: true); + + return lines; + } } diff --git a/Runner/Jobs/JitDiffJob.cs b/Runner/Jobs/JitDiffJob.cs index dc151c0..f434207 100644 --- a/Runner/Jobs/JitDiffJob.cs +++ b/Runner/Jobs/JitDiffJob.cs @@ -80,11 +80,24 @@ public static async Task BuildAndCopyRuntimeBranchBitsAsync(JobBase job, string { string arch = IsArm ? "arm64" : "x64"; - await job.RunProcessAsync("bash", $"build.sh clr+libs -c Release {RuntimeHelpers.LibrariesExtraBuildArgs}", logPrefix: $"{branch} release", workDir: "runtime"); + (bool rebuildClr, bool rebuildLibs) = await ShouldRebuildAsync(); + + string targets = (rebuildClr, rebuildLibs) switch + { + (true, true) => "clr+libs", + (true, false) => "clr", + _ => "libs" + }; + + await job.RunProcessAsync("bash", $"build.sh {targets} -c Release {RuntimeHelpers.LibrariesExtraBuildArgs}", logPrefix: $"{branch} release", workDir: "runtime"); Task copyReleaseBitsTask = RuntimeHelpers.CopyReleaseArtifactsAsync(job, branch, $"artifacts-{branch}"); - await job.RunProcessAsync("bash", "build.sh clr.jit -c Checked", logPrefix: $"{branch} checked", workDir: "runtime"); + if (rebuildClr) + { + await job.RunProcessAsync("bash", "build.sh clr.jit -c Checked", logPrefix: $"{branch} checked", workDir: "runtime"); + } + await job.RunProcessAsync("cp", $"-r runtime/artifacts/bin/coreclr/linux.{arch}.Checked/. clr-checked-{branch}", logPrefix: $"{branch} checked"); if (uploadArtifacts) @@ -94,6 +107,46 @@ public static async Task BuildAndCopyRuntimeBranchBitsAsync(JobBase job, string } await copyReleaseBitsTask; + + async Task<(bool Clr, bool Libs)> ShouldRebuildAsync() + { + if (branch == "pr" && !job.TryGetFlag("forceRebuildAll")) + { + bool clr = false; + bool libs = false; + + foreach (string file in await GitHelper.GetChangedFilesAsync(job, "main", "runtime")) + { + if (file.StartsWith("src/coreclr/", StringComparison.OrdinalIgnoreCase)) + { + clr = true; + + if (file.Contains("/System.Private.CoreLib/", StringComparison.OrdinalIgnoreCase)) + { + libs = true; + } + } + else if (file.StartsWith("src/libraries/", StringComparison.OrdinalIgnoreCase)) + { + libs = true; + } + else if (!file.StartsWith("src/tests/", StringComparison.OrdinalIgnoreCase)) + { + clr = true; + libs = true; + } + } + + if (!clr && !libs) + { + await job.LogAsync($"WARNING: Don't need to rebuild anything? What is this PR?"); + } + + return (clr, libs); + } + + return (true, true); + } } private async Task CollectFrameworksDiffsAsync()