Skip to content

Commit 4dc0f30

Browse files
committed
Revert "Revert "Merge pull request haskell#4382 from Ericson2314/munge""
This reverts commit 4774fb6.
1 parent e6aadf4 commit 4dc0f30

34 files changed

+432
-194
lines changed

Cabal/Cabal.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ library
219219
Distribution.Types.PkgconfigDependency
220220
Distribution.Types.DependencyMap
221221
Distribution.Types.ComponentId
222+
Distribution.Types.MungedPackageId
222223
Distribution.Types.PackageId
223224
Distribution.Types.UnitId
224225
Distribution.Types.Executable
@@ -230,6 +231,7 @@ library
230231
Distribution.Types.ModuleReexport
231232
Distribution.Types.ModuleRenaming
232233
Distribution.Types.ComponentName
234+
Distribution.Types.MungedPackageName
233235
Distribution.Types.PackageName
234236
Distribution.Types.PkgconfigName
235237
Distribution.Types.UnqualComponentName

Cabal/Distribution/Backpack/Configure.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Distribution.Simple.Setup as Setup
3939
import Distribution.Simple.LocalBuildInfo
4040
import Distribution.Types.ComponentRequestedSpec
4141
import Distribution.Types.ComponentInclude
42+
import Distribution.Types.MungedPackageId
4243
import Distribution.Verbosity
4344
import qualified Distribution.Compat.Graph as Graph
4445
import Distribution.Compat.Graph (Graph, IsNode(..))
@@ -81,7 +82,7 @@ configureComponentLocalBuildInfos
8182
(dispComponentsGraph graph0)
8283

8384
let conf_pkg_map = Map.fromListWith Map.union
84-
[(pc_pkgname pkg, Map.singleton (pc_compname pkg) (pc_cid pkg, pc_pkgid pkg))
85+
[(pc_pkgname pkg, Map.singleton (pc_compname pkg) (pc_cid pkg, packageId pkg))
8586
| pkg <- prePkgDeps]
8687
graph1 <- toConfiguredComponents use_external_internal_deps
8788
flagAssignment
@@ -107,9 +108,9 @@ configureComponentLocalBuildInfos
107108
(vcat (map dispLinkedComponent graph2))
108109

109110
let pid_map = Map.fromList $
110-
[ (pc_uid pkg, pc_pkgid pkg)
111+
[ (pc_uid pkg, pc_munged_id pkg)
111112
| pkg <- prePkgDeps] ++
112-
[ (Installed.installedUnitId pkg, Installed.sourcePackageId pkg)
113+
[ (Installed.installedUnitId pkg, Installed.sourceMungedPackageId pkg)
113114
| (_, Module uid _) <- instantiate_with
114115
, Just pkg <- [PackageIndex.lookupUnitId
115116
installedPackageSet (unDefUnitId uid)] ]
@@ -204,12 +205,12 @@ toComponentLocalBuildInfos
204205
-- TODO: This is probably wrong for Backpack
205206
let pseudoTopPkg :: InstalledPackageInfo
206207
pseudoTopPkg = emptyInstalledPackageInfo {
207-
Installed.installedUnitId =
208-
mkLegacyUnitId (packageId pkg_descr),
209-
Installed.sourcePackageId = packageId pkg_descr,
210-
Installed.depends =
211-
map pc_uid externalPkgDeps
208+
Installed.installedUnitId = mkLegacyUnitId munged_id,
209+
Installed.sourceMungedPackageId = munged_id,
210+
Installed.depends = map pc_uid externalPkgDeps
212211
}
212+
where munged_id = computeCompatPackageId (packageId pkg_descr)
213+
CLibName
213214
case PackageIndex.dependencyInconsistencies
214215
. PackageIndex.insert pseudoTopPkg
215216
$ packageDependsIndex of

Cabal/Distribution/Backpack/ConfiguredComponent.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component = do
9999
-- Resolve each @mixins@ into the actual dependency
100100
-- from @lib_deps@.
101101
explicit_includes <- forM (mixins bi) $ \(Mixin name rns) -> do
102-
let keys = fixFakePkgName pkg_decr name
102+
let keys@(_, cname) = fixFakePkgName pkg_decr name
103103
(cid, pid) <-
104104
case Map.lookup keys deps_map of
105105
Nothing ->
@@ -110,10 +110,8 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component = do
110110
Just r -> return r
111111
return ComponentInclude {
112112
ci_id = cid,
113-
-- TODO: We set pkgName = name here to make error messages
114-
-- look better. But it would be better to properly
115-
-- record component name here.
116-
ci_pkgid = pid { pkgName = name },
113+
ci_pkgid = pid,
114+
ci_compname = cname,
117115
ci_renaming = rns,
118116
ci_implicit = False
119117
}
@@ -122,10 +120,10 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component = do
122120
-- @backpack-include@ is converted into an "implicit" include.
123121
let used_explicitly = Set.fromList (map ci_id explicit_includes)
124122
implicit_includes
125-
= map (\((pn, _), (cid, pid)) -> ComponentInclude {
123+
= map (\((_, cn), (cid, pid)) -> ComponentInclude {
126124
ci_id = cid,
127-
-- See above ci_pkgid
128-
ci_pkgid = pid { pkgName = pn },
125+
ci_pkgid = pid,
126+
ci_compname = cn,
129127
ci_renaming = defaultIncludeRenaming,
130128
ci_implicit = True
131129
})

Cabal/Distribution/Backpack/Id.hs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Distribution.Simple.LocalBuildInfo
2020
import Distribution.Types.ComponentId
2121
import Distribution.Types.PackageId
2222
import Distribution.Types.UnitId
23-
import Distribution.Types.PackageName
23+
import Distribution.Types.MungedPackageName
2424
import Distribution.Utils.Base62
2525
import Distribution.Version
2626

@@ -71,55 +71,6 @@ computeComponentId deterministic mb_ipid mb_cid pid cname mb_details =
7171
Nothing -> ""
7272
Just s -> "-" ++ unUnqualComponentName s)
7373

74-
-- | Computes the package name for a library. If this is the public
75-
-- library, it will just be the original package name; otherwise,
76-
-- it will be a munged package name recording the original package
77-
-- name as well as the name of the internal library.
78-
--
79-
-- A lot of tooling in the Haskell ecosystem assumes that if something
80-
-- is installed to the package database with the package name 'foo',
81-
-- then it actually is an entry for the (only public) library in package
82-
-- 'foo'. With internal packages, this is not necessarily true:
83-
-- a public library as well as arbitrarily many internal libraries may
84-
-- come from the same package. To prevent tools from getting confused
85-
-- in this case, the package name of these internal libraries is munged
86-
-- so that they do not conflict the public library proper. A particular
87-
-- case where this matters is ghc-pkg: if we don't munge the package
88-
-- name, the inplace registration will OVERRIDE a different internal
89-
-- library.
90-
--
91-
-- We munge into a reserved namespace, "z-", and encode both the
92-
-- component name and the package name of an internal library using the
93-
-- following format:
94-
--
95-
-- compat-pkg-name ::= "z-" package-name "-z-" library-name
96-
--
97-
-- where package-name and library-name have "-" ( "z" + ) "-"
98-
-- segments encoded by adding an extra "z".
99-
--
100-
-- When we have the public library, the compat-pkg-name is just the
101-
-- package-name, no surprises there!
102-
--
103-
computeCompatPackageName :: PackageName -> ComponentName -> PackageName
104-
-- First handle the cases where we can just use the original 'PackageName'.
105-
-- This is for the PRIMARY library, and it is non-Backpack, or the
106-
-- indefinite package for us.
107-
computeCompatPackageName pkg_name CLibName = pkg_name
108-
computeCompatPackageName pkg_name cname
109-
= mkPackageName $ "z-" ++ zdashcode (display pkg_name)
110-
++ (case componentNameString cname of
111-
Just cname_u -> "-z-" ++ zdashcode cname_str
112-
where cname_str = unUnqualComponentName cname_u
113-
Nothing -> "")
114-
115-
zdashcode :: String -> String
116-
zdashcode s = go s (Nothing :: Maybe Int) []
117-
where go [] _ r = reverse r
118-
go ('-':z) (Just n) r | n > 0 = go z (Just 0) ('-':'z':r)
119-
go ('-':z) _ r = go z (Just 0) ('-':r)
120-
go ('z':z) (Just n) r = go z (Just (n+1)) ('z':r)
121-
go (c:z) _ r = go z Nothing (c:r)
122-
12374
-- | In GHC 8.0, the string we pass to GHC to use for symbol
12475
-- names for a package can be an arbitrary, IPID-compatible string.
12576
-- However, prior to GHC 8.0 there are some restrictions on what
@@ -169,7 +120,7 @@ zdashcode s = go s (Nothing :: Maybe Int) []
169120
--
170121
computeCompatPackageKey
171122
:: Compiler
172-
-> PackageName
123+
-> MungedPackageName
173124
-> Version
174125
-> UnitId
175126
-> String

Cabal/Distribution/Backpack/LinkedComponent.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Distribution.Types.ModuleRenaming
2929
import Distribution.Types.IncludeRenaming
3030
import Distribution.Types.ComponentInclude
3131
import Distribution.Types.ComponentId
32+
import Distribution.Types.MungedPackageId
3233
import Distribution.Types.PackageId
3334
import Distribution.Package
3435
import Distribution.PackageDescription as PD hiding (Flag)
@@ -105,6 +106,10 @@ dispLinkedComponent lc =
105106
instance Package LinkedComponent where
106107
packageId = lc_pkgid
107108

109+
instance HasMungedPackageId LinkedComponent where
110+
mungedId LinkedComponent { lc_pkgid = pkgid, lc_component = component }
111+
= computeCompatPackageId pkgid (componentName component)
112+
108113
toLinkedComponent
109114
:: Verbosity
110115
-> FullDb
@@ -137,8 +142,8 @@ toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
137142
-- *unlinked* unit identity. We will use unification (relying
138143
-- on the ModuleShape) to resolve these into linked identities.
139144
unlinked_includes :: [ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming]
140-
unlinked_includes = [ ComponentInclude (lookupUid cid) pid rns i
141-
| ComponentInclude cid pid rns i <- cid_includes ]
145+
unlinked_includes = [ ComponentInclude (lookupUid cid) pid cn rns i
146+
| ComponentInclude cid pid cn rns i <- cid_includes ]
142147

143148
lookupUid :: ComponentId -> (OpenUnitId, ModuleShape)
144149
lookupUid cid = fromMaybe (error "linkComponent: lookupUid")
@@ -178,11 +183,12 @@ toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
178183
-- src_reqs_u <- mapM convertReq src_reqs
179184
-- Read out all the final results by converting back
180185
-- into a pure representation.
181-
let convertIncludeU (ComponentInclude uid_u pid rns i) = do
186+
let convertIncludeU (ComponentInclude uid_u pid cn rns i) = do
182187
uid <- convertUnitIdU uid_u
183188
return (ComponentInclude {
184189
ci_id = uid,
185190
ci_pkgid = pid,
191+
ci_compname = cn,
186192
ci_renaming = rns,
187193
ci_implicit = i
188194
})

Cabal/Distribution/Backpack/PreExistingComponent.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Distribution.Compat.Prelude
1010
import Distribution.Backpack.ModuleShape
1111
import Distribution.Backpack
1212
import Distribution.Types.ComponentId
13+
import Distribution.Types.MungedPackageId
1314
import Distribution.Types.PackageId
1415
import Distribution.Types.UnitId
1516
import Distribution.Types.ComponentName
@@ -31,7 +32,7 @@ data PreExistingComponent
3132
pc_pkgname :: PackageName,
3233
-- | The actual name of the component.
3334
pc_compname :: ComponentName,
34-
pc_pkgid :: PackageId,
35+
pc_munged_id :: MungedPackageId,
3536
pc_uid :: UnitId,
3637
pc_cid :: ComponentId,
3738
pc_open_uid :: OpenUnitId,
@@ -44,11 +45,9 @@ data PreExistingComponent
4445
ipiToPreExistingComponent :: InstalledPackageInfo -> PreExistingComponent
4546
ipiToPreExistingComponent ipi =
4647
PreExistingComponent {
47-
pc_pkgname = case Installed.sourcePackageName ipi of
48-
Just n -> n
49-
Nothing -> pkgName $ Installed.sourcePackageId ipi,
48+
pc_pkgname = Installed.sourcePackageName' ipi,
5049
pc_compname = libraryComponentName $ Installed.sourceLibName ipi,
51-
pc_pkgid = Installed.sourcePackageId ipi,
50+
pc_munged_id = Installed.sourceMungedPackageId ipi,
5251
pc_uid = Installed.installedUnitId ipi,
5352
pc_cid = Installed.installedComponentId ipi,
5453
pc_open_uid =
@@ -57,8 +56,12 @@ ipiToPreExistingComponent ipi =
5756
pc_shape = shapeInstalledPackage ipi
5857
}
5958

59+
instance HasMungedPackageId PreExistingComponent where
60+
mungedId = pc_munged_id
61+
6062
instance Package PreExistingComponent where
61-
packageId = pc_pkgid
63+
packageId pec = PackageIdentifier (pc_pkgname pec) v
64+
where MungedPackageId _ v = pc_munged_id pec
6265

6366
instance HasUnitId PreExistingComponent where
6467
installedUnitId = pc_uid

Cabal/Distribution/Backpack/ReadyComponent.hs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import Distribution.Types.PackageId
2828
import Distribution.Types.UnitId
2929
import Distribution.Compat.Graph (IsNode(..))
3030
import Distribution.Types.Module
31-
import Distribution.Types.PackageName
31+
import Distribution.Types.MungedPackageId
32+
import Distribution.Types.MungedPackageName
3233

3334
import Distribution.ModuleName
3435
import Distribution.Package
@@ -91,7 +92,7 @@ data InstantiatedComponent
9192
-- | Dependencies induced by 'instc_insts'. These are recorded
9293
-- here because there isn't a convenient way otherwise to get
9394
-- the 'PackageId' we need to fill 'componentPackageDeps' as needed.
94-
instc_insts_deps :: [(UnitId, PackageId)],
95+
instc_insts_deps :: [(UnitId, MungedPackageId)],
9596
-- | The modules exported/reexported by this library.
9697
instc_provides :: Map ModuleName Module,
9798
-- | The dependencies which need to be passed to the compiler
@@ -116,20 +117,27 @@ data IndefiniteComponent
116117

117118
-- | Compute the dependencies of a 'ReadyComponent' that should
118119
-- be recorded in the @depends@ field of 'InstalledPackageInfo'.
119-
rc_depends :: ReadyComponent -> [(UnitId, PackageId)]
120+
rc_depends :: ReadyComponent -> [(UnitId, MungedPackageId)]
120121
rc_depends rc = ordNub $
121122
case rc_i rc of
122123
Left indefc ->
123-
map (\ci -> (abstractUnitId (ci_id ci), ci_pkgid ci))
124+
map (\ci -> (abstractUnitId $ ci_id ci, toMungedPackageId ci))
124125
(indefc_includes indefc)
125126
Right instc ->
126-
map (\ci -> (unDefUnitId (ci_id ci), ci_pkgid ci))
127+
map (\ci -> (unDefUnitId $ ci_id ci, toMungedPackageId ci))
127128
(instc_includes instc)
128129
++ instc_insts_deps instc
130+
where
131+
toMungedPackageId :: ComponentInclude x y -> MungedPackageId
132+
toMungedPackageId ci = computeCompatPackageId (ci_pkgid ci) (ci_compname ci)
129133

130134
instance Package ReadyComponent where
131135
packageId = rc_pkgid
132136

137+
instance HasMungedPackageId ReadyComponent where
138+
mungedId ReadyComponent { rc_pkgid = pkgid, rc_component = component }
139+
= computeCompatPackageId pkgid (componentName component)
140+
133141
instance HasUnitId ReadyComponent where
134142
installedUnitId = rc_uid
135143

@@ -146,8 +154,8 @@ instance IsNode ReadyComponent where
146154
ordNub (map fst (rc_depends rc)) ++
147155
map fst (rc_exe_deps rc)
148156

149-
rc_compat_name :: ReadyComponent -> PackageName
150-
rc_compat_name ReadyComponent{
157+
rc_compat_name :: ReadyComponent -> MungedPackageName
158+
rc_compat_name ReadyComponent {
151159
rc_pkgid = PackageIdentifier pkg_name _,
152160
rc_component = component
153161
}
@@ -213,7 +221,7 @@ instance Monad InstM where
213221
-- instantiated components are given 'HashedUnitId'.
214222
--
215223
toReadyComponents
216-
:: Map UnitId PackageId
224+
:: Map UnitId MungedPackageId
217225
-> Map ModuleName Module -- subst for the public component
218226
-> [LinkedComponent]
219227
-> [ReadyComponent]
@@ -257,11 +265,10 @@ toReadyComponents pid_map subst0 comps
257265
= [(dep_uid,
258266
fromMaybe err_pid $
259267
Map.lookup dep_uid pid_map A.<|>
260-
fmap rc_pkgid (join (Map.lookup dep_uid s)))]
268+
fmap mungedId (join (Map.lookup dep_uid s)))]
261269
where
262-
err_pid =
263-
PackageIdentifier
264-
(mkPackageName "nonexistent-package-this-is-a-cabal-bug")
270+
err_pid = MungedPackageId
271+
(mkMungedPackageName "nonexistent-package-this-is-a-cabal-bug")
265272
(mkVersion [0])
266273
instc = InstantiatedComponent {
267274
instc_insts = Map.toList insts,

Cabal/Distribution/Backpack/UnifyM.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ convertInclude
450450
convertInclude ci@(ComponentInclude {
451451
ci_id = (uid, ModuleShape provs reqs),
452452
ci_pkgid = pid,
453+
ci_compname = compname,
453454
ci_renaming = incl@(IncludeRenaming prov_rns req_rns),
454455
ci_implicit = implicit
455456
}) = addErrContext (text "In" <+> ci_msg ci) $ do
@@ -587,6 +588,7 @@ convertInclude ci@(ComponentInclude {
587588
else Left) (ComponentInclude {
588589
ci_id = uid_u,
589590
ci_pkgid = pid,
591+
ci_compname = compname,
590592
ci_renaming = prov_rns',
591593
ci_implicit = ci_implicit ci
592594
}))

0 commit comments

Comments
 (0)