From 5b05c27bf186e66edc4fbf4a54943c8bd04f5024 Mon Sep 17 00:00:00 2001
From: Matthew Pickering <matthewtpickering@gmail.com>
Date: Thu, 27 Feb 2025 12:28:15 +0000
Subject: [PATCH] Disable self recomp in release flavour

The interface files that we distribute should not contain any
information which is used by the recompilation checking logic since
source file will never be compiled again.

I am not 100% sure this won't cause unexpected issues, there many be
downstream consumers which are incorrectly using the information from
interfaces, but this commit can be reverted if we detect issues.
---
 hadrian/doc/flavours.md                  |  6 +++++-
 hadrian/src/Flavour.hs                   | 13 +++++++++++++
 hadrian/src/Settings/Flavours/Release.hs |  6 +++++-
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/hadrian/doc/flavours.md b/hadrian/doc/flavours.md
index 72095944ef62..5d1ef7c55454 100644
--- a/hadrian/doc/flavours.md
+++ b/hadrian/doc/flavours.md
@@ -107,7 +107,7 @@ when compiling the `compiler` library, and `hsGhc` when compiling/linking the GH
     <td>-O2</td>
   </tr>
   <tr>
-    <th>release (same as perf with -haddock)</td>
+    <th>release (same as perf with -haddock and +no_self_recomp)</td>
     <td></td>
     <td>-O<br>-H64m</td>
     <td>-O<br>-H64m</td>
@@ -329,6 +329,10 @@ The supported transformers are listed below:
         <td><code>dump_stg</code></td>
         <td>Dump STG of all modules compiled by a stage1 compiler to a file</td>
     </tr>
+    <tr>
+        <td><code>no_self_recomp</code></td>
+        <td>Disable including self-recompilation information in interface files via <code>-fno-write-if-self-recomp</code>. If you are building a distribution you can enable this flag to produce more deterministic interface files.</td>
+    </tr>
 </table>
 
 ### Static
diff --git a/hadrian/src/Flavour.hs b/hadrian/src/Flavour.hs
index 3195964f2aa8..f5e1530e5a7a 100644
--- a/hadrian/src/Flavour.hs
+++ b/hadrian/src/Flavour.hs
@@ -16,6 +16,7 @@ module Flavour
   , disableProfiledLibs
   , enableLinting
   , enableHaddock
+  , disableSelfRecompInfo
   , enableHiCore
   , useNativeBignum
   , enableTextWithSIMDUTF
@@ -67,6 +68,7 @@ flavourTransformers = M.fromList
     , "debug_stage1_ghc" =: debugGhc Stage1
     , "lint"             =: enableLinting
     , "haddock"          =: enableHaddock
+    , "no_self_recomp"   =: disableSelfRecompInfo
     , "hi_core"          =: enableHiCore
     , "late_ccs"         =: enableLateCCS
     , "boot_nonmoving_gc" =: enableBootNonmovingGc
@@ -208,6 +210,17 @@ enableHaddock =
       [ arg "-haddock"
       ]
 
+-- | Disable self recompilation information in interface files
+disableSelfRecompInfo :: Flavour -> Flavour
+disableSelfRecompInfo =
+    addArgs $ stage1 ? mconcat
+      [ builder (Ghc CompileHs) ? selfRecomp
+      ]
+  where
+    selfRecomp = mconcat
+      [ arg "-fno-write-if-self-recomp"
+      ]
+
 -- | Build stage2 dependencies with options to emit Core into
 -- interface files which is sufficient to restart code generation.
 enableHiCore :: Flavour -> Flavour
diff --git a/hadrian/src/Settings/Flavours/Release.hs b/hadrian/src/Settings/Flavours/Release.hs
index 2f3daadae101..7e878b84135f 100644
--- a/hadrian/src/Settings/Flavours/Release.hs
+++ b/hadrian/src/Settings/Flavours/Release.hs
@@ -4,4 +4,8 @@ import Settings.Flavours.Performance
 import Flavour
 
 releaseFlavour :: Flavour
-releaseFlavour = enableHaddock performanceFlavour { name = "release" }
+releaseFlavour =
+  -- 1. These interface files will be distributed and the source files never recompiled.
+  disableSelfRecompInfo
+  -- 2. Include documentation in the interface for tools such as haddock and HLS to use
+  $ enableHaddock performanceFlavour { name = "release" }
-- 
GitLab