Teo Camarasu pushed to branch wip/T25282 at Glasgow Haskell Compiler / GHC
Commits:
-
cdf55d41
by Teo Camarasu at 2025-06-20T14:31:58+01:00
3 changed files:
Changes:
... | ... | @@ -3459,6 +3459,7 @@ compilerInfo dflags |
3459 | 3459 | ("Project Patch Level1", cProjectPatchLevel1),
|
3460 | 3460 | ("Project Patch Level2", cProjectPatchLevel2),
|
3461 | 3461 | ("Project Unit Id", cProjectUnitId),
|
3462 | + ("ghc-internal Unit Id", cGhcInternalUnitId), -- See Note [Special unit-ids]
|
|
3462 | 3463 | ("Booter version", cBooterVersion),
|
3463 | 3464 | ("Stage", cStage),
|
3464 | 3465 | ("Build platform", cBuildPlatformString),
|
... | ... | @@ -3512,6 +3513,23 @@ compilerInfo dflags |
3512 | 3513 | expandDirectories :: FilePath -> Maybe FilePath -> String -> String
|
3513 | 3514 | expandDirectories topd mtoold = expandToolDir useInplaceMinGW mtoold . expandTopDir topd
|
3514 | 3515 | |
3516 | +-- Note [Special unit-ids]
|
|
3517 | +-- ~~~~~~~~~~~~~~~~~~~~~~~
|
|
3518 | +-- Certain units are special to the compiler:
|
|
3519 | +-- - Wired-in identifiers reference a specific unit-id of `ghc-internal`.
|
|
3520 | +-- - GHC plugins must be linked against a specific unit-id of `ghc`,
|
|
3521 | +-- namely the same one as the compiler.
|
|
3522 | +-- - When using Template Haskell, splices refer to the Template Haskell
|
|
3523 | +-- interface defined in `ghc-internal`, and must be linked against the same
|
|
3524 | +-- unit-id as the compiler.
|
|
3525 | +--
|
|
3526 | +-- We therefore expose the unit-id of `ghc-internal` ("ghc-internal Unit Id") and
|
|
3527 | +-- ghc ("Project Unit Id") through `ghc --info`.
|
|
3528 | +--
|
|
3529 | +-- This allows build tools to act accordingly, eg, if a user wishes to build a
|
|
3530 | +-- GHC plugin, `cabal-install` might force them to use the exact `ghc` unit
|
|
3531 | +-- that the compiler was linked against.
|
|
3532 | + |
|
3515 | 3533 | {- -----------------------------------------------------------------------------
|
3516 | 3534 | Note [DynFlags consistency]
|
3517 | 3535 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
... | ... | @@ -22,6 +22,7 @@ import qualified Data.Map as Map |
22 | 22 | import GHC.ResponseFile
|
23 | 23 | import System.Environment
|
24 | 24 | |
25 | + |
|
25 | 26 | main :: IO ()
|
26 | 27 | main = defaultMainWithHooks ghcHooks
|
27 | 28 | where
|
... | ... | @@ -56,7 +57,7 @@ primopIncls = |
56 | 57 | ]
|
57 | 58 | |
58 | 59 | ghcAutogen :: Verbosity -> LocalBuildInfo -> IO ()
|
59 | -ghcAutogen verbosity lbi@LocalBuildInfo{pkgDescrFile,withPrograms,componentNameMap}
|
|
60 | +ghcAutogen verbosity lbi@LocalBuildInfo{pkgDescrFile,withPrograms,componentNameMap,installedPackageIndex}
|
|
60 | 61 | = do
|
61 | 62 | -- Get compiler/ root directory from the cabal file
|
62 | 63 | let Just compilerRoot = takeDirectory <$> pkgDescrFile
|
... | ... | @@ -96,9 +97,16 @@ ghcAutogen verbosity lbi@LocalBuildInfo{pkgDescrFile,withPrograms,componentNameM |
96 | 97 | Just [LibComponentLocalBuildInfo{componentUnitId}] -> unUnitId componentUnitId
|
97 | 98 | _ -> error "Couldn't find unique cabal library when building ghc"
|
98 | 99 | |
100 | + let cGhcInternalUnitId = case Map.lookup (mkPackageName "ghc-internal", LMainLibName) $ packageIdIndex installedPackageIndex of
|
|
101 | + Just versions
|
|
102 | + -- We assume there is exactly one copy of `ghc-internal` in our dependency closure
|
|
103 | + | [[packageInfo]] <- Map.values versions
|
|
104 | + -> unUnitId $ installedUnitId packageInfo
|
|
105 | + _ -> error "Couldn't find unique ghc-internal library when building ghc"
|
|
106 | + |
|
99 | 107 | -- Write GHC.Settings.Config
|
100 | 108 | configHsPath = autogenPackageModulesDir lbi </> "GHC/Settings/Config.hs"
|
101 | - configHs = generateConfigHs cProjectUnitId settings
|
|
109 | + configHs = generateConfigHs cProjectUnitId cGhcInternalUnitId settings
|
|
102 | 110 | createDirectoryIfMissingVerbose verbosity True (takeDirectory configHsPath)
|
103 | 111 | rewriteFileEx verbosity configHsPath configHs
|
104 | 112 | |
... | ... | @@ -110,8 +118,9 @@ getSetting settings kh kr = go settings kr |
110 | 118 | Just v -> Right v
|
111 | 119 | |
112 | 120 | generateConfigHs :: String -- ^ ghc's cabal-generated unit-id, which matches its package-id/key
|
121 | + -> String -- ^ ghc-internal's cabal-generated unit-id, which matches its package-id/key
|
|
113 | 122 | -> [(String,String)] -> String
|
114 | -generateConfigHs cProjectUnitId settings = either error id $ do
|
|
123 | +generateConfigHs cProjectUnitId cGhcInternalUnitId settings = either error id $ do
|
|
115 | 124 | let getSetting' = getSetting $ (("cStage","2"):) settings
|
116 | 125 | buildPlatform <- getSetting' "cBuildPlatformString" "Host platform"
|
117 | 126 | hostPlatform <- getSetting' "cHostPlatformString" "Target platform"
|
... | ... | @@ -127,6 +136,7 @@ generateConfigHs cProjectUnitId settings = either error id $ do |
127 | 136 | , " , cBooterVersion"
|
128 | 137 | , " , cStage"
|
129 | 138 | , " , cProjectUnitId"
|
139 | + , " , cGhcInternalUnitId"
|
|
130 | 140 | , " ) where"
|
131 | 141 | , ""
|
132 | 142 | , "import GHC.Prelude.Basic"
|
... | ... | @@ -150,4 +160,7 @@ generateConfigHs cProjectUnitId settings = either error id $ do |
150 | 160 | , ""
|
151 | 161 | , "cProjectUnitId :: String"
|
152 | 162 | , "cProjectUnitId = " ++ show cProjectUnitId
|
163 | + , ""
|
|
164 | + , "cGhcInternalUnitId :: String"
|
|
165 | + , "cGhcInternalUnitId = " ++ show cGhcInternalUnitId
|
|
153 | 166 | ] |
... | ... | @@ -601,6 +601,8 @@ generateConfigHs = do |
601 | 601 | -- 'pkgUnitId' on 'compiler' (the ghc-library package) to create the
|
602 | 602 | -- unit-id in both situations.
|
603 | 603 | cProjectUnitId <- expr . (`pkgUnitId` compiler) =<< getStage
|
604 | + |
|
605 | + cGhcInternalUnitId <- expr . (`pkgUnitId` ghcInternal) =<< getStage
|
|
604 | 606 | return $ unlines
|
605 | 607 | [ "module GHC.Settings.Config"
|
606 | 608 | , " ( module GHC.Version"
|
... | ... | @@ -610,6 +612,7 @@ generateConfigHs = do |
610 | 612 | , " , cBooterVersion"
|
611 | 613 | , " , cStage"
|
612 | 614 | , " , cProjectUnitId"
|
615 | + , " , cGhcInternalUnitId"
|
|
613 | 616 | , " ) where"
|
614 | 617 | , ""
|
615 | 618 | , "import GHC.Prelude.Basic"
|
... | ... | @@ -633,6 +636,9 @@ generateConfigHs = do |
633 | 636 | , ""
|
634 | 637 | , "cProjectUnitId :: String"
|
635 | 638 | , "cProjectUnitId = " ++ show cProjectUnitId
|
639 | + , ""
|
|
640 | + , "cGhcInternalUnitId :: String"
|
|
641 | + , "cGhcInternalUnitId = " ++ show cGhcInternalUnitId
|
|
636 | 642 | ]
|
637 | 643 | where
|
638 | 644 | stageString (Stage0 InTreeLibs) = "1"
|