Skip to content

Commit

Permalink
Move CLibName and CSubLibName to a separate type
Browse files Browse the repository at this point in the history
This gives us more type safety when dealing with libraries only
  • Loading branch information
fgaz committed Oct 16, 2018
1 parent d732f91 commit d751bfe
Show file tree
Hide file tree
Showing 35 changed files with 242 additions and 158 deletions.
1 change: 1 addition & 0 deletions Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ library
Distribution.Types.ModuleReexport
Distribution.Types.ModuleRenaming
Distribution.Types.ComponentName
Distribution.Types.LibraryName
Distribution.Types.MungedPackageName
Distribution.Types.PackageName
Distribution.Types.PkgconfigName
Expand Down
4 changes: 2 additions & 2 deletions Cabal/Distribution/Backpack/ComponentsGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ mkComponentsGraph enabled pkg_descr =
(CExeName <$> getAllInternalToolDependencies pkg_descr bi)

++ [ if pkgname == packageName pkg_descr
then CLibName
else CSubLibName toolname
then CLibName LMainLibName
else CLibName (LSubLibName toolname)
| Dependency pkgname _ <- targetBuildDepends bi
, let toolname = packageNameToUnqualComponentName pkgname
, toolname `elem` internalPkgDeps ]
Expand Down
8 changes: 4 additions & 4 deletions Cabal/Distribution/Backpack/ConfiguredComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mkConfiguredComponent pkg_descr this_cid lib_deps exe_deps component = do
bi = componentBuildInfo component
deps_map = Map.fromList [ ((packageName dep, ann_cname dep), dep)
| dep <- lib_deps ]
is_public = componentName component == CLibName
is_public = componentName component == CLibName LMainLibName

type ConfiguredComponentMap =
Map PackageName (Map ComponentName (AnnotatedId ComponentId))
Expand Down Expand Up @@ -192,7 +192,7 @@ toConfiguredComponent pkg_descr this_cid lib_dep_map exe_dep_map component = do
| (pn, comp_map) <- Map.toList lib_dep_map
, pn /= packageName pkg_descr
, (cn, e) <- Map.toList comp_map
, cn == CLibName ]
, cn == CLibName LMainLibName ]
-- We have to nub here, because 'getAllToolDependencies' may return
-- duplicates (see #4986). (NB: This is not needed for lib_deps,
-- since those elaborate into includes, for which there explicitly
Expand Down Expand Up @@ -296,8 +296,8 @@ newPackageDepsBehaviour pkg =
fixFakePkgName :: PackageDescription -> PackageName -> (PackageName, ComponentName)
fixFakePkgName pkg_descr pn =
if subLibName `elem` internalLibraries
then (packageName pkg_descr, CSubLibName subLibName)
else (pn, CLibName)
then (packageName pkg_descr, CLibName (LSubLibName subLibName))
else (pn, CLibName LMainLibName )
where
subLibName = packageNameToUnqualComponentName pn
internalLibraries = mapMaybe libName (allLibraries pkg_descr)
5 changes: 3 additions & 2 deletions Cabal/Distribution/Backpack/ModuleScope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Distribution.ModuleName
import Distribution.Types.IncludeRenaming
import Distribution.Types.PackageName
import Distribution.Types.ComponentName
import Distribution.Types.LibraryName

import Distribution.Backpack
import Distribution.Backpack.ModSubst
Expand Down Expand Up @@ -113,8 +114,8 @@ dispComponent pn cn =
-- should be clear enough. To do source syntax, we'd
-- need to know what the package we're linking is.
case cn of
CLibName -> disp pn
CSubLibName ucn -> disp pn <<>> colon <<>> disp ucn
CLibName LMainLibName -> disp pn
CLibName (LSubLibName ucn) -> disp pn <<>> colon <<>> disp ucn
-- Case below shouldn't happen
_ -> disp pn <+> parens (disp cn)

Expand Down
5 changes: 3 additions & 2 deletions Cabal/Distribution/Backpack/ReadyComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Distribution.Types.Component
import Distribution.Types.ComponentInclude
import Distribution.Types.ComponentId
import Distribution.Types.ComponentName
import Distribution.Types.LibraryName
import Distribution.Types.PackageId
import Distribution.Types.UnitId
import Distribution.Compat.Graph (IsNode(..))
Expand Down Expand Up @@ -139,8 +140,8 @@ rc_depends rc = ordNub $
computeCompatPackageId
(ci_pkgid ci)
(case ci_cname ci of
CLibName -> Nothing
CSubLibName uqn -> Just uqn
CLibName LMainLibName -> Nothing
CLibName (LSubLibName uqn) -> Just uqn
_ -> error $ display (rc_cid rc) ++
" depends on non-library " ++ display (ci_id ci))

Expand Down
4 changes: 2 additions & 2 deletions Cabal/Distribution/Backpack/UnifyM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ ci_msg ci
pn = pkgName (ci_pkgid ci)
pp_pn =
case ci_cname ci of
CLibName -> disp pn
CSubLibName cn -> disp pn <<>> colon <<>> disp cn
CLibName LMainLibName -> disp pn
CLibName (LSubLibName cn) -> disp pn <<>> colon <<>> disp cn
-- Shouldn't happen
cn -> disp pn <+> parens (disp cn)

Expand Down
5 changes: 3 additions & 2 deletions Cabal/Distribution/InstalledPackageInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import Distribution.ModuleName
import Distribution.Package hiding (installedPackageId, installedUnitId)
import Distribution.ParseUtils
import Distribution.Types.ComponentName
import Distribution.Types.LibraryName
import Distribution.Utils.Generic (toUTF8BS)

import qualified Data.Map as Map
Expand Down Expand Up @@ -104,8 +105,8 @@ installedPackageId = installedUnitId
sourceComponentName :: InstalledPackageInfo -> ComponentName
sourceComponentName ipi =
case sourceLibName ipi of
Nothing -> CLibName
Just qn -> CSubLibName qn
Nothing -> CLibName LMainLibName
Just qn -> CLibName $ LSubLibName qn

-- -----------------------------------------------------------------------------
-- Parsing
Expand Down
2 changes: 2 additions & 0 deletions Cabal/Distribution/PackageDescription.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module Distribution.PackageDescription (
allBuildDepends,
enabledBuildDepends,
ComponentName(..),
LibraryName(..),
defaultLibName,
HookedBuildInfo,
emptyHookedBuildInfo,
Expand Down Expand Up @@ -135,5 +136,6 @@ import Distribution.Types.CondTree
import Distribution.Types.Condition
import Distribution.Types.PackageDescription
import Distribution.Types.ComponentName
import Distribution.Types.LibraryName
import Distribution.Types.HookedBuildInfo
import Distribution.Types.SourceRepo
6 changes: 4 additions & 2 deletions Cabal/Distribution/PackageDescription/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,14 @@ overallDependencies enabled (TargetSet targets) = mconcat depss
-- UGH. The embedded componentName in the 'Component's here is
-- BLANK. I don't know whose fault this is but I'll use the tag
-- instead. -- ezyang
removeDisabledSections (Lib _) = componentNameRequested enabled CLibName
removeDisabledSections (Lib _) = componentNameRequested
enabled
(CLibName LMainLibName)
removeDisabledSections (SubComp t c)
-- Do NOT use componentName
= componentNameRequested enabled
$ case c of
CLib _ -> CSubLibName t
CLib _ -> CLibName (LSubLibName t)
CFLib _ -> CFLibName t
CExe _ -> CExeName t
CTest _ -> CTestName t
Expand Down
2 changes: 1 addition & 1 deletion Cabal/Distribution/Simple/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ testSuiteLibV09AsLibAndExe pkg_descr
, componentInternalDeps = componentInternalDeps clbi
, componentIsIndefinite_ = False
, componentExeDeps = componentExeDeps clbi
, componentLocalName = CSubLibName (testName test)
, componentLocalName = CLibName $ LSubLibName $ testName test
, componentIsPublic = False
, componentIncludes = componentIncludes clbi
, componentUnitId = componentUnitId clbi
Expand Down
7 changes: 3 additions & 4 deletions Cabal/Distribution/Simple/BuildTarget.hs
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ pkgComponentInfo pkg =
, let bi = componentBuildInfo c ]

componentStringName :: Package pkg => pkg -> ComponentName -> ComponentStringName
componentStringName pkg CLibName = prettyShow (packageName pkg)
componentStringName _ (CSubLibName name) = unUnqualComponentName name
componentStringName pkg (CLibName LMainLibName ) = prettyShow (packageName pkg)
componentStringName _ (CLibName (LSubLibName name)) = unUnqualComponentName name
componentStringName _ (CFLibName name) = unUnqualComponentName name
componentStringName _ (CExeName name) = unUnqualComponentName name
componentStringName _ (CTestName name) = unUnqualComponentName name
Expand Down Expand Up @@ -555,8 +555,7 @@ data ComponentKind = LibKind | FLibKind | ExeKind | TestKind | BenchKind
deriving (Eq, Ord, Show)

componentKind :: ComponentName -> ComponentKind
componentKind CLibName = LibKind
componentKind (CSubLibName _) = LibKind
componentKind (CLibName _) = LibKind
componentKind (CFLibName _) = FLibKind
componentKind (CExeName _) = ExeKind
componentKind (CTestName _) = TestKind
Expand Down
12 changes: 6 additions & 6 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ dependencySatisfiable
-- Except for internal deps, when we're NOT per-component mode;
-- those are just True.
then True
else (depName, CLibName) `Map.member` requiredDepsMap
else (depName, CLibName LMainLibName) `Map.member` requiredDepsMap

| isInternalDep
= if use_external_internal_deps
Expand Down Expand Up @@ -1227,7 +1227,7 @@ selectDependency pkgid internalIndex installedIndex requiredDepsMap

-- We have to look it up externally
do_external is_internal = do
ipi <- case Map.lookup (dep_pkgname, CLibName) requiredDepsMap of
ipi <- case Map.lookup (dep_pkgname, CLibName LMainLibName) requiredDepsMap of
-- If we know the exact pkg to use, then use it.
Just pkginstance -> Right pkginstance
-- Otherwise we just pick an arbitrary instance of the latest version.
Expand Down Expand Up @@ -1410,8 +1410,8 @@ combinedConstraints constraints dependencies installedPackages = do
dependenciesPkgInfo :: [(PackageName, ComponentName, ComponentId,
Maybe InstalledPackageInfo)]
dependenciesPkgInfo =
[ (pkgname, cname, cid, mpkg)
| GivenComponent pkgname cname cid <- dependencies
[ (pkgname, CLibName lname, cid, mpkg)
| GivenComponent pkgname lname cid <- dependencies
, let mpkg = PackageIndex.lookupComponentId
installedPackages cid
]
Expand All @@ -1427,8 +1427,8 @@ combinedConstraints constraints dependencies installedPackages = do
hsep [ text "--dependency="
<<>> quotes
(pretty pkgname
<<>> case cname of CLibName -> ""
CSubLibName n -> ":" <<>> pretty n
<<>> case cname of CLibName LMainLibName -> ""
CLibName (LSubLibName n) -> ":" <<>> pretty n
_ -> ":" <<>> pretty cname
<<>> char '='
<<>> pretty cid)
Expand Down
5 changes: 3 additions & 2 deletions Cabal/Distribution/Simple/LocalBuildInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Distribution.Simple.LocalBuildInfo (
-- * Buildable package components
Component(..),
ComponentName(..),
LibraryName(..),
defaultLibName,
showComponentName,
componentNameString,
Expand Down Expand Up @@ -108,11 +109,11 @@ componentBuildDir :: LocalBuildInfo -> ComponentLocalBuildInfo -> FilePath
componentBuildDir lbi clbi
= buildDir lbi </>
case componentLocalName clbi of
CLibName ->
CLibName LMainLibName ->
if prettyShow (componentUnitId clbi) == prettyShow (componentComponentId clbi)
then ""
else prettyShow (componentUnitId clbi)
CSubLibName s ->
CLibName (LSubLibName s) ->
if prettyShow (componentUnitId clbi) == prettyShow (componentComponentId clbi)
then unUnqualComponentName s
else prettyShow (componentUnitId clbi)
Expand Down
13 changes: 6 additions & 7 deletions Cabal/Distribution/Simple/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,8 @@ configureOptions showOrParseArgs =
(parsecToReadE (const "dependency expected") ((\x -> [x]) `fmap` parsecGivenComponent))
(map (\(GivenComponent pn cn cid) ->
prettyShow pn
++ case cn of CLibName -> ""
CSubLibName n -> ":" ++ prettyShow n
_ -> ":" ++ prettyShow cn
++ case cn of LMainLibName -> ""
LSubLibName n -> ":" ++ prettyShow n
++ "=" ++ prettyShow cid)))

,option "" ["instantiate-with"]
Expand Down Expand Up @@ -740,15 +739,15 @@ showProfDetailLevelFlag (Flag dl) = [showProfDetailLevel dl]
parsecGivenComponent :: ParsecParser GivenComponent
parsecGivenComponent = do
pn <- parsec
cn <- P.option CLibName $ do
ln <- P.option LMainLibName $ do
_ <- P.char ':'
ucn <- parsec
return $ if unUnqualComponentName ucn == unPackageName pn
then CLibName
else CSubLibName ucn
then LMainLibName
else LSubLibName ucn
_ <- P.char '='
cid <- parsec
return $ GivenComponent pn cn cid
return $ GivenComponent pn ln cid

installDirsOptions :: [OptionField (InstallDirs (Flag PathTemplate))]
installDirsOptions =
Expand Down
31 changes: 11 additions & 20 deletions Cabal/Distribution/Types/ComponentName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module Distribution.Types.ComponentName (
ComponentName(..),
defaultLibName,
libraryComponentName,
showComponentName,
componentNameStanza,
Expand All @@ -16,14 +15,14 @@ import Distribution.Compat.Prelude
import qualified Distribution.Compat.ReadP as Parse
import Distribution.Compat.ReadP ((<++))
import Distribution.Types.UnqualComponentName
import Distribution.Types.LibraryName
import Distribution.Pretty
import Distribution.Text

import Text.PrettyPrint as Disp

-- Libraries live in a separate namespace, so must distinguish
data ComponentName = CLibName
| CSubLibName UnqualComponentName
data ComponentName = CLibName LibraryName
| CFLibName UnqualComponentName
| CExeName UnqualComponentName
| CTestName UnqualComponentName
Expand All @@ -34,39 +33,32 @@ instance Binary ComponentName

-- Build-target-ish syntax
instance Pretty ComponentName where
pretty CLibName = Disp.text "lib"
pretty (CSubLibName str) = Disp.text "lib:" <<>> pretty str
pretty (CLibName lib) = pretty lib
pretty (CFLibName str) = Disp.text "flib:" <<>> pretty str
pretty (CExeName str) = Disp.text "exe:" <<>> pretty str
pretty (CTestName str) = Disp.text "test:" <<>> pretty str
pretty (CBenchName str) = Disp.text "bench:" <<>> pretty str

instance Text ComponentName where
parse = parseComposite <++ parseSingle
parse = parseComposite <++ parseLib
where
parseSingle = Parse.string "lib" >> return CLibName
parseLib = CLibName <$> parse
parseComposite = do
ctor <- Parse.choice [ Parse.string "lib:" >> return CSubLibName
, Parse.string "flib:" >> return CFLibName
ctor <- Parse.choice [ Parse.string "flib:" >> return CFLibName
, Parse.string "exe:" >> return CExeName
, Parse.string "bench:" >> return CBenchName
, Parse.string "test:" >> return CTestName ]
ctor <$> parse

defaultLibName :: ComponentName
defaultLibName = CLibName

showComponentName :: ComponentName -> String
showComponentName CLibName = "library"
showComponentName (CSubLibName name) = "library '" ++ display name ++ "'"
showComponentName (CLibName lib) = showLibraryName lib
showComponentName (CFLibName name) = "foreign library '" ++ display name ++ "'"
showComponentName (CExeName name) = "executable '" ++ display name ++ "'"
showComponentName (CTestName name) = "test suite '" ++ display name ++ "'"
showComponentName (CBenchName name) = "benchmark '" ++ display name ++ "'"

componentNameStanza :: ComponentName -> String
componentNameStanza CLibName = "library"
componentNameStanza (CSubLibName name) = "library " ++ display name
componentNameStanza (CLibName lib) = libraryNameStanza lib
componentNameStanza (CFLibName name) = "foreign-library " ++ display name
componentNameStanza (CExeName name) = "executable " ++ display name
componentNameStanza (CTestName name) = "test-suite " ++ display name
Expand All @@ -77,8 +69,7 @@ componentNameStanza (CBenchName name) = "benchmark " ++ display name
-- @Nothing@ if the 'ComponentName' was for the public
-- library.
componentNameString :: ComponentName -> Maybe UnqualComponentName
componentNameString CLibName = Nothing
componentNameString (CSubLibName n) = Just n
componentNameString (CLibName lib) = libraryNameString lib
componentNameString (CFLibName n) = Just n
componentNameString (CExeName n) = Just n
componentNameString (CTestName n) = Just n
Expand All @@ -87,5 +78,5 @@ componentNameString (CBenchName n) = Just n
-- | Convert the 'UnqualComponentName' of a library into a
-- 'ComponentName'.
libraryComponentName :: Maybe UnqualComponentName -> ComponentName
libraryComponentName Nothing = CLibName
libraryComponentName (Just n) = CSubLibName n
libraryComponentName Nothing = CLibName LMainLibName
libraryComponentName (Just n) = CLibName $ LSubLibName n
9 changes: 5 additions & 4 deletions Cabal/Distribution/Types/GivenComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ module Distribution.Types.GivenComponent (
import Distribution.Compat.Prelude

import Distribution.Types.ComponentId
import Distribution.Types.ComponentName
import Distribution.Types.LibraryName
import Distribution.Types.PackageName

-- | A 'GivenComponent' represents a component depended on and explicitly
-- | A 'GivenComponent' represents a library depended on and explicitly
-- specified by the user/client with @--dependency@
--
-- It enables Cabal to know which 'ComponentId' to associate with a component
-- It enables Cabal to know which 'ComponentId' to associate with a library
--
-- @since 2.3.0.0
data GivenComponent =
GivenComponent
{ givenComponentPackage :: PackageName
, givenComponentName :: ComponentName
, givenComponentName :: LibraryName -- --dependency is for libraries
-- only, not for any component
, givenComponentId :: ComponentId }
deriving (Generic, Read, Show, Eq, Typeable)

Expand Down
Loading

0 comments on commit d751bfe

Please sign in to comment.