[Git][ghc/ghc][wip/romes/hardwire-ghc-unit-id] Hardwire a better unit-id for ghc

Rodrigo Mesquita (@alt-romes) gitlab at gitlab.haskell.org
Mon Mar 13 16:51:48 UTC 2023



Rodrigo Mesquita pushed to branch wip/romes/hardwire-ghc-unit-id at Glasgow Haskell Compiler / GHC


Commits:
702bd403 by romes at 2023-03-13T16:51:20+00:00
Hardwire a better unit-id for ghc

Previously, the unit-id of ghc-the-library was fixed as `ghc`.
This was done primarily because the compiler must know the unit-id of
some packages (including ghc) a-priori to define wired-in names.

However, as seen in #20742, a reinstallable `ghc` whose unit-id is fixed
to `ghc` might result in subtle bugs when different ghc's interact.

A good example of this is having GHC_A load a plugin compiled by GHC_B,
where GHC_A and GHC_B are linked to ghc-libraries that are ABI
incompatible. Without a distinction between the unit-id of the ghc library
GHC_A is linked against and the ghc library the plugin it is loading was
compiled against, we can't check compatibility.

This patch gives a slightly better unit-id to ghc (ghc-version) by
(1) Not setting -this-unit-id to ghc, but rather to the new unit-id (modulo stage0)
(2) Adding a definition to `GHC.Version` whose value is the new unit-id.
This unit-id definition is imported by `GHC.Unit.Types` and used to
set the wired-in unit-id of "ghc", which was previously fixed to "ghc"

The commits following this one will improve the unit-id with a
cabal-style package hash, ensure cabal-built ghcs also correctly use
a better unit-id, and check compatibility when loading plugins.

- - - - -


6 changed files:

- compiler/GHC/Driver/Session.hs
- compiler/GHC/Unit/Types.hs
- compiler/ghc.cabal.in
- hadrian/src/Rules/Generate.hs
- hadrian/src/Settings/Builders/Cabal.hs
- hadrian/src/Settings/Builders/Ghc.hs


Changes:

=====================================
compiler/GHC/Driver/Session.hs
=====================================
@@ -4703,6 +4703,7 @@ compilerInfo dflags
        ("Project Patch Level",         cProjectPatchLevel),
        ("Project Patch Level1",        cProjectPatchLevel1),
        ("Project Patch Level2",        cProjectPatchLevel2),
+       ("Project Unit Id",             cProjectUnitId),
        ("Booter version",              cBooterVersion),
        ("Stage",                       cStage),
        ("Build platform",              cBuildPlatformString),


=====================================
compiler/GHC/Unit/Types.hs
=====================================
@@ -99,6 +99,7 @@ import GHC.Data.FastString
 import GHC.Utils.Encoding
 import GHC.Utils.Fingerprint
 import GHC.Utils.Misc
+import GHC.Version (cProjectUnitId)
 
 import Control.DeepSeq
 import Data.Data
@@ -597,7 +598,7 @@ primUnitId        = UnitId (fsLit "ghc-prim")
 bignumUnitId      = UnitId (fsLit "ghc-bignum")
 baseUnitId        = UnitId (fsLit "base")
 rtsUnitId         = UnitId (fsLit "rts")
-thisGhcUnitId     = UnitId (fsLit "ghc")
+thisGhcUnitId     = UnitId (fsLit cProjectUnitId)
 interactiveUnitId = UnitId (fsLit "interactive")
 thUnitId          = UnitId (fsLit "template-haskell")
 


=====================================
compiler/ghc.cabal.in
=====================================
@@ -57,6 +57,12 @@ Flag build-tool-depends
     Description: Use build-tool-depends
     Default: True
 
+-- While the boot compiler fixes ghc's unit-id to `ghc`, the stage0 compiler must still be compiled with `-this-unit-id ghc`
+Flag hadrian-stage0
+    Description: Enable if compiling the stage0 compiler with hadrian
+    Default: False
+    Manual: True
+
 Library
     Default-Language: Haskell2010
     Exposed: False
@@ -136,9 +142,10 @@ Library
 
     Include-Dirs: .
 
-    -- We need to set the unit id to ghc (without a version number)
-    -- as it's magic.
-    GHC-Options: -this-unit-id ghc
+    if flag(hadrian-stage0)
+        -- We need to set the unit id to ghc (without a version number)
+        -- as it's magic.
+        GHC-Options: -this-unit-id ghc
 
     c-sources:
         cbits/cutils.c


=====================================
hadrian/src/Rules/Generate.hs
=====================================
@@ -533,6 +533,13 @@ generateVersionHs = do
     cProjectPatchLevel  <- getSetting ProjectPatchLevel
     cProjectPatchLevel1 <- getSetting ProjectPatchLevel1
     cProjectPatchLevel2 <- getSetting ProjectPatchLevel2
+    -- ROMES:TODO: First we attempt a fixed unit-id with version but without hash.
+    -- We now use a more informative
+    -- unit-id for ghc. This same logic must be done
+    -- when passing -this-unit-id when building ghc (at
+    -- stage0 one must pass -this-unit-id ghc).
+    let cProjectUnitId = "ghc-" ++ cProjectVersion
+
     return $ unlines
         [ "module GHC.Version where"
         , ""
@@ -555,6 +562,9 @@ generateVersionHs = do
         , ""
         , "cProjectPatchLevel2   :: String"
         , "cProjectPatchLevel2   = " ++ show cProjectPatchLevel2
+        , ""
+        , "cProjectUnitId :: String"
+        , "cProjectUnitId = " ++ show cProjectUnitId
         ]
 
 -- | Generate @Platform/Host.hs@ files.


=====================================
hadrian/src/Settings/Builders/Cabal.hs
=====================================
@@ -115,6 +115,12 @@ commonCabalArgs stage = do
             , arg "--htmldir"
             , arg $ "${pkgroot}/../../doc/html/libraries/" ++ package_id
 
+            -- ROMES: While the boot compiler is not updated wrt -this-unit-id
+            -- not being fixed to `ghc`, when building stage0, we must set
+            -- -this-unit-id to `ghc` because the boot compiler expects that.
+            -- We do it through a cabal flag in ghc.cabal
+            , stage0 ? arg "-f+hadrian-stage0"
+
             -- These trigger a need on each dependency, so every important to need
             -- them in parallel or  it linearises the build of Ghc and GhcPkg
             , withStageds [Ghc CompileHs, GhcPkg Update, Cc CompileC, Ar Pack]


=====================================
hadrian/src/Settings/Builders/Ghc.hs
=====================================
@@ -245,8 +245,19 @@ wayGhcArgs = do
 
 packageGhcArgs :: Args
 packageGhcArgs = do
+    stage   <- getStage
     package <- getPackage
-    ghc_ver <- readVersion <$> (expr . ghcVersionStage =<< getStage)
+    ghc_ver <- readVersion <$> expr (ghcVersionStage stage)
+    -- ROMES: Until the boot compiler no longer needs ghc's
+    -- unit-id to be "ghc", the stage0 compiler must be built
+    -- with `-this-unit-id ghc`, while the wired-in unit-id of
+    -- ghc is correctly set to the unit-id we'll generate for
+    -- stage1 (set in generateVersionHs in Rules.Generate).
+    --
+    -- However, we don't need to set the unit-id of "ghc" to "ghc" when
+    -- building stage0 because we have a flag in compiler/ghc.cabal.in that is
+    -- sets `-this-unit-id ghc` when hadrian is building stage0, which will
+    -- overwrite this one.
     pkgId   <- expr $ pkgIdentifier package
     mconcat [ arg "-hide-all-packages"
             , arg "-no-user-package-db"



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/702bd4035e70005e010177988f23e4eea65d7631

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/702bd4035e70005e010177988f23e4eea65d7631
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230313/d7e512a8/attachment-0001.html>


More information about the ghc-commits mailing list