module Distribution.Backpack.LinkedComponent (
LinkedComponent(..),
lc_insts,
lc_uid,
toLinkedComponent,
toLinkedComponents,
dispLinkedComponent,
LinkedComponentMap,
extendLinkedComponentMap,
) where
import Prelude ()
import Distribution.Compat.Prelude hiding ((<>))
import Distribution.Backpack
import Distribution.Backpack.FullUnitId
import Distribution.Backpack.ConfiguredComponent
import Distribution.Backpack.ModuleShape
import Distribution.Backpack.ModuleScope
import Distribution.Backpack.UnifyM
import Distribution.Backpack.MixLink
import Distribution.Utils.MapAccum
import Distribution.Types.ComponentName
import Distribution.Types.ModuleRenaming
import Distribution.Types.IncludeRenaming
import Distribution.Types.ComponentInclude
import Distribution.Package
import Distribution.PackageDescription as PD hiding (Flag)
import Distribution.ModuleName
import Distribution.Simple.LocalBuildInfo
import Distribution.Verbosity
import Distribution.Utils.LogProgress
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Traversable
( mapM )
import Distribution.Text
( Text(disp) )
import Text.PrettyPrint
import Data.Either
data LinkedComponent
= LinkedComponent {
lc_cid :: ComponentId,
lc_pkgid :: PackageId,
lc_component :: Component,
lc_internal_build_tools :: [OpenUnitId],
lc_public :: Bool,
lc_includes :: [ComponentInclude OpenUnitId ModuleRenaming],
lc_sig_includes :: [ComponentInclude OpenUnitId ModuleRenaming],
lc_shape :: ModuleShape
}
lc_uid :: LinkedComponent -> OpenUnitId
lc_uid lc = IndefFullUnitId (lc_cid lc) . Map.fromList $ lc_insts lc
lc_insts :: LinkedComponent -> [(ModuleName, OpenModule)]
lc_insts lc = [ (req, OpenModuleVar req)
| req <- Set.toList (modShapeRequires (lc_shape lc)) ]
dispLinkedComponent :: LinkedComponent -> Doc
dispLinkedComponent lc =
hang (text "unit" <+> disp (lc_uid lc)) 4 $
vcat [ text "include" <+> disp (ci_id incl) <+> disp (ci_renaming incl)
| incl <- lc_includes lc ]
$+$
vcat [ text "signature include" <+> disp (ci_id incl)
| incl <- lc_sig_includes lc ]
$+$ dispOpenModuleSubst (modShapeProvides (lc_shape lc))
instance Package LinkedComponent where
packageId = lc_pkgid
toLinkedComponent
:: Verbosity
-> FullDb
-> PackageId
-> LinkedComponentMap
-> ConfiguredComponent
-> LogProgress LinkedComponent
toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
cc_cid = this_cid,
cc_pkgid = pkgid,
cc_component = component,
cc_internal_build_tools = btools,
cc_public = is_public,
cc_includes = cid_includes
} = do
let
(src_reqs :: [ModuleName],
src_provs :: [ModuleName],
src_reexports :: [ModuleReexport]) =
case component of
CLib lib -> (signatures lib,
exposedModules lib,
reexportedModules lib)
_ -> ([], [], [])
unlinked_includes :: [ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming]
unlinked_includes = [ ComponentInclude (lookupUid cid) pid rns i
| ComponentInclude cid pid rns i <- cid_includes ]
lookupUid :: ComponentId -> (OpenUnitId, ModuleShape)
lookupUid cid = fromMaybe (error "linkComponent: lookupUid")
(Map.lookup cid pkg_map)
let orErr (Right x) = return x
orErr (Left [err]) = dieProgress err
orErr (Left errs) = do
dieProgress (vcat (intersperse (text "")
[ hang (text "-") 2 err | err <- errs]))
(linked_shape0 :: ModuleScope,
linked_includes :: [ComponentInclude OpenUnitId ModuleRenaming],
linked_sig_includes :: [ComponentInclude OpenUnitId ModuleRenaming])
<- orErr $ runUnifyM verbosity db $ do
(shapes_u, all_includes_u) <- fmap unzip (mapM convertInclude unlinked_includes)
failIfErrs
shape_u <- mixLink shapes_u
let convertIncludeU (ComponentInclude uid_u pid rns i) = do
uid <- convertUnitIdU uid_u
return (ComponentInclude {
ci_id = uid,
ci_pkgid = pid,
ci_renaming = rns,
ci_implicit = i
})
shape <- convertModuleScopeU shape_u
let (includes_u, sig_includes_u) = partitionEithers all_includes_u
incls <- mapM convertIncludeU includes_u
sig_incls <- mapM convertIncludeU sig_includes_u
return (shape, incls, sig_incls)
let reqs = Map.keysSet (modScopeRequires linked_shape0)
`Set.union` Set.fromList src_reqs
insts = [ (req, OpenModuleVar req)
| req <- Set.toList reqs ]
this_uid = IndefFullUnitId this_cid . Map.fromList $ insts
local_source m = [ModuleSource (packageName this_pid) defaultIncludeRenaming m True]
local_exports = Map.fromListWith (++) $
[ (mod_name, local_source (OpenModule this_uid mod_name)) | mod_name <- src_provs ]
local_reqs = Map.fromListWith (++) $
[ (mod_name, local_source (OpenModuleVar mod_name)) | mod_name <- src_reqs ]
linked_shape = linked_shape0 {
modScopeProvides =
Map.unionWith (++)
local_exports
(modScopeProvides linked_shape0),
modScopeRequires =
Map.unionWith (++)
local_reqs
(modScopeRequires linked_shape0)
}
let isNotLib (CLib _) = False
isNotLib _ = True
when (not (Set.null reqs) && isNotLib component) $
dieProgress $
hang (text "Non-library component has unfilled requirements:")
4 (vcat [disp req | req <- Set.toList reqs])
let hdl :: [Either Doc a] -> LogProgress [a]
hdl es =
case partitionEithers es of
([], rs) -> return rs
(ls, _) ->
dieProgress $
hang (text "Problem with module re-exports:") 2
(vcat [hang (text "-") 2 l | l <- ls])
reexports_list <- hdl . (flip map) src_reexports $ \reex@(ModuleReexport mb_pn from to) -> do
case Map.lookup from (modScopeProvides linked_shape) of
Just cands@(x0:xs0) -> do
(x, xs) <-
case mb_pn of
Just pn ->
case filter ((pn==) . msrc_pkgname) cands of
(x1:xs1) -> return (x1, xs1)
_ -> Left (brokenReexportMsg reex)
Nothing -> return (x0, xs0)
case filter (\x' -> msrc_module x /= msrc_module x') xs of
[] -> return ()
_ -> Left $ ambiguousReexportMsg reex x xs
return (to, msrc_module x)
_ ->
Left (brokenReexportMsg reex)
let build_reexports m (k, v)
| Map.member k m =
dieProgress $ hsep
[ text "Module name ", disp k, text " is exported multiple times." ]
| otherwise = return (Map.insert k v m)
provs <- foldM build_reexports Map.empty $
[ (mod_name, OpenModule this_uid mod_name) | mod_name <- src_provs ] ++
reexports_list
let final_linked_shape = ModuleShape provs (Map.keysSet (modScopeRequires linked_shape))
return $ LinkedComponent {
lc_cid = this_cid,
lc_pkgid = pkgid,
lc_component = component,
lc_public = is_public,
lc_internal_build_tools = map (\cid -> IndefFullUnitId cid Map.empty) btools,
lc_shape = final_linked_shape,
lc_includes = linked_includes,
lc_sig_includes = linked_sig_includes
}
toLinkedComponents
:: Verbosity
-> FullDb
-> PackageId
-> LinkedComponentMap
-> [ConfiguredComponent]
-> LogProgress [LinkedComponent]
toLinkedComponents verbosity db this_pid lc_map0 comps
= fmap snd (mapAccumM go lc_map0 comps)
where
go :: Map ComponentId (OpenUnitId, ModuleShape)
-> ConfiguredComponent
-> LogProgress (Map ComponentId (OpenUnitId, ModuleShape), LinkedComponent)
go lc_map cc = do
lc <- addProgressCtx (text "In the stanza" <+> text (componentNameStanza (cc_name cc))) $
toLinkedComponent verbosity db this_pid lc_map cc
return (extendLinkedComponentMap lc lc_map, lc)
type LinkedComponentMap = Map ComponentId (OpenUnitId, ModuleShape)
extendLinkedComponentMap :: LinkedComponent
-> LinkedComponentMap
-> LinkedComponentMap
extendLinkedComponentMap lc m =
Map.insert (lc_cid lc) (lc_uid lc, lc_shape lc) m
brokenReexportMsg :: ModuleReexport -> Doc
brokenReexportMsg (ModuleReexport (Just pn) from _to) =
vcat [ text "The package" <+> quotes (disp pn)
, text "does not export a module" <+> quotes (disp from) ]
brokenReexportMsg (ModuleReexport Nothing from _to) =
vcat [ text "The module" <+> quotes (disp from)
, text "is not exported by any suitable package."
, text "It occurs in neither the 'exposed-modules' of this package,"
, text "nor any of its 'build-depends' dependencies." ]
ambiguousReexportMsg :: ModuleReexport -> ModuleSource -> [ModuleSource] -> Doc
ambiguousReexportMsg (ModuleReexport mb_pn from _to) y1 ys =
vcat [ text "Ambiguous reexport" <+> quotes (disp from)
, hang (text "It could refer to either:") 2
(vcat (msg : msgs))
, help_msg mb_pn ]
where
msg = text " " <+> displaySource y1
msgs = [text "or" <+> displaySource y | y <- ys]
help_msg Nothing =
vcat [ text "The ambiguity can be resolved by qualifying the"
, text "re-export with a package name."
, text "The syntax is 'packagename:ModuleName [as NewName]'." ]
help_msg (Just _) =
vcat [ text "The ambiguity can be resolved by using the"
, text "mixins field to rename one of the module"
, text "names differently." ]
displaySource y
= vcat [ quotes (disp (msrc_module y))
, text "brought into scope by" <+>
if not (isDefaultIncludeRenaming (msrc_renaming y))
then text "the mixin" <+>
disp (msrc_pkgname y) <+>
parens (disp (includeProvidesRn (msrc_renaming y)))
else text "the build dependency on" <+> disp (msrc_pkgname y)
]