Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default-package-bounds (RFC #9569) implementation #9570

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cabal-syntax/Cabal-syntax.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ library
Distribution.Types.CondTree
Distribution.Types.Condition
Distribution.Types.ConfVar
Distribution.Types.DefaultBounds
Distribution.Types.DefaultBounds.Lens
Distribution.Types.Dependency
Distribution.Types.DependencyMap
Distribution.Types.ExeDependency
Expand Down
2 changes: 2 additions & 0 deletions Cabal-syntax/src/Distribution/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Distribution.Package
, module Distribution.Types.PackageName
, module Distribution.Types.PkgconfigName
, module Distribution.Types.Dependency
, module Distribution.Types.DefaultBounds
, Package (..)
, packageName
, packageVersion
Expand All @@ -44,6 +45,7 @@ import Distribution.Version

import Distribution.Types.AbiHash
import Distribution.Types.ComponentId
import Distribution.Types.DefaultBounds
import Distribution.Types.Dependency
import Distribution.Types.Module
import Distribution.Types.MungedPackageId
Expand Down
2 changes: 2 additions & 0 deletions Cabal-syntax/src/Distribution/PackageDescription.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module Distribution.PackageDescription
, module Distribution.Types.PkgconfigVersionRange

-- * Dependencies
, module Distribution.Types.DefaultBounds
, module Distribution.Types.Dependency
, module Distribution.Types.ExeDependency
, module Distribution.Types.LegacyExeDependency
Expand Down Expand Up @@ -95,6 +96,7 @@ import Distribution.Types.ComponentName
import Distribution.Types.CondTree
import Distribution.Types.Condition
import Distribution.Types.ConfVar
import Distribution.Types.DefaultBounds
import Distribution.Types.Dependency
import Distribution.Types.ExeDependency
import Distribution.Types.Executable
Expand Down
32 changes: 24 additions & 8 deletions Cabal-syntax/src/Distribution/PackageDescription/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Distribution.PackageDescription.Configuration
, transformAllBuildInfos
, transformAllBuildDepends
, transformAllBuildDependsN
, transformDefaultBuildDepends
, simplifyWithSysParams
) where

Expand All @@ -40,6 +41,7 @@ import Prelude ()

-- lens
import qualified Distribution.Types.BuildInfo.Lens as L
import qualified Distribution.Types.DefaultBounds.Lens as L
import qualified Distribution.Types.GenericPackageDescription.Lens as L
import qualified Distribution.Types.PackageDescription.Lens as L
import qualified Distribution.Types.SetupBuildInfo.Lens as L
Expand Down Expand Up @@ -476,7 +478,7 @@ finalizePD
(Platform arch os)
impl
constraints
(GenericPackageDescription pkg _ver flags mb_lib0 sub_libs0 flibs0 exes0 tests0 bms0) = do
(GenericPackageDescription pkg _ver flags defBounds mb_lib0 sub_libs0 flibs0 exes0 tests0 bms0) = do
(targetSet, flagVals) <-
resolveWithFlags flagChoices enabled os arch impl constraints condTrees check
let
Expand Down Expand Up @@ -509,14 +511,20 @@ finalizePD
, flagVals
)
where
appBounds
:: L.HasBuildInfo a
=> CondTree v [Dependency] a
-> CondTree v [Dependency] a
appBounds = applyDefaultBoundsToCondTree defBounds

-- Combine lib, exes, and tests into one list of @CondTree@s with tagged data
condTrees =
maybeToList (fmap (mapTreeData Lib) mb_lib0)
++ map (\(name, tree) -> mapTreeData (SubComp name . CLib) tree) sub_libs0
++ map (\(name, tree) -> mapTreeData (SubComp name . CFLib) tree) flibs0
++ map (\(name, tree) -> mapTreeData (SubComp name . CExe) tree) exes0
++ map (\(name, tree) -> mapTreeData (SubComp name . CTest) tree) tests0
++ map (\(name, tree) -> mapTreeData (SubComp name . CBench) tree) bms0
maybeToList (fmap (mapTreeData Lib . appBounds) mb_lib0)
++ map (\(name, tree) -> mapTreeData (SubComp name . CLib) $ appBounds tree) sub_libs0
++ map (\(name, tree) -> mapTreeData (SubComp name . CFLib) $ appBounds tree) flibs0
++ map (\(name, tree) -> mapTreeData (SubComp name . CExe) $ appBounds tree) exes0
++ map (\(name, tree) -> mapTreeData (SubComp name . CTest) $ appBounds tree) tests0
++ map (\(name, tree) -> mapTreeData (SubComp name . CBench) $ appBounds tree) bms0

flagChoices = map (\(MkPackageFlag n _ d manual) -> (n, d2c manual n d)) flags
d2c manual n b = case lookupFlagAssignment n userflags of
Expand Down Expand Up @@ -556,7 +564,7 @@ resolveWithFlags [] Distribution.System.Linux Distribution.System.I386 (Distribu
-- function.
flattenPackageDescription :: GenericPackageDescription -> PackageDescription
flattenPackageDescription
(GenericPackageDescription pkg _ _ mlib0 sub_libs0 flibs0 exes0 tests0 bms0) =
(GenericPackageDescription pkg _ _ _ mlib0 sub_libs0 flibs0 exes0 tests0 bms0) =
pkg
{ library = mlib
, subLibraries = reverse sub_libs
Expand Down Expand Up @@ -668,3 +676,11 @@ transformAllBuildDependsN f =
. over (L.packageDescription . L.setupBuildInfo . traverse . L.setupDepends) f
-- cannot be point-free as normal because of higher rank
. over (\f' -> L.allCondTrees $ traverseCondTreeC f') f

-- | Apply @f@ to the default bounds in the 'PackageDescription'.
transformDefaultBuildDepends
:: (Dependency -> Dependency)
-> GenericPackageDescription
-> GenericPackageDescription
transformDefaultBuildDepends f =
over (L.genDefaultPackageBounds . L.defaultTargetBuildDepends . traverse) f
21 changes: 21 additions & 0 deletions Cabal-syntax/src/Distribution/PackageDescription/FieldGrammar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ module Distribution.PackageDescription.FieldGrammar

-- * Component build info
, buildInfoFieldGrammar

-- * default-package-bounds
, defaultPackageBoundsFieldGrammar
) where

import Distribution.Compat.Lens
Expand Down Expand Up @@ -807,6 +810,24 @@ setupBInfoFieldGrammar def =
{-# SPECIALIZE setupBInfoFieldGrammar :: Bool -> ParsecFieldGrammar' SetupBuildInfo #-}
{-# SPECIALIZE setupBInfoFieldGrammar :: Bool -> PrettyFieldGrammar' SetupBuildInfo #-}

-------------------------------------------------------------------------------
-- default-package-bounds
-------------------------------------------------------------------------------

defaultPackageBoundsFieldGrammar
:: ( FieldGrammar c g
, Applicative (g DefaultBounds)
, c (List CommaVCat (Identity Dependency) Dependency)
, c (List CommaFSep (Identity ExeDependency) ExeDependency)
)
=> g DefaultBounds DefaultBounds
defaultPackageBoundsFieldGrammar =
DefaultBounds
<$> (monoidalFieldAla "build-depends" formatDependencyList L.defaultTargetBuildDepends)
<*> (monoidalFieldAla "build-tool-depends" (alaList CommaFSep) L.defaultBuildToolDepends)
{-# SPECIALIZE defaultPackageBoundsFieldGrammar :: ParsecFieldGrammar' DefaultBounds #-}
{-# SPECIALIZE defaultPackageBoundsFieldGrammar :: PrettyFieldGrammar' DefaultBounds #-}

-------------------------------------------------------------------------------
-- Define how field values should be formatted for 'pretty'.
-------------------------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion Cabal-syntax/src/Distribution/PackageDescription/Parsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ goSections specVer = traverse_ process
process (Field (Name pos name) _) =
lift $
parseWarning pos PWTTrailingFields $
"Ignoring trailing fields after sections: " ++ show name
"Ignoring trailing fields after sections: " ++ show name ++ ". Consider moving these fields before any stanzas in your cabal file."
process (Section name args secFields) =
parseSection name args secFields

Expand Down Expand Up @@ -395,6 +395,12 @@ goSections specVer = traverse_ process
flag <- lift $ parseFields specVer fields (flagFieldGrammar name'')
-- Check default flag
stateGpd . L.genPackageFlags %= snoc flag
| name == "default-package-bounds" =
if specVer >= CabalSpecV3_14
then do
dpb <- lift $ parseFields specVer fields defaultPackageBoundsFieldGrammar
stateGpd . L.genDefaultPackageBounds .= dpb
else lift $ parseWarning pos PWTUnknownSection $ "Ignoring section: default-package-bounds. You should set cabal-version: 3.14 or larger to use default-package-bounds."
| name == "custom-setup" && null args = do
sbi <- lift $ parseFields specVer fields (setupBInfoFieldGrammar False)
stateGpd . L.packageDescription . L.setupBuildInfo ?= sbi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pdToGpd pd =
{ packageDescription = pd
, gpdScannedVersion = Nothing
, genPackageFlags = []
, genDefaultPackageBounds = emptyDefaultBounds
, condLibrary = mkCondTree <$> library pd
, condSubLibraries = mkCondTreeL <$> subLibraries pd
, condForeignLibs = mkCondTree' foreignLibName <$> foreignLibs pd
Expand Down
65 changes: 65 additions & 0 deletions Cabal-syntax/src/Distribution/Types/DefaultBounds.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}

module Distribution.Types.DefaultBounds
( DefaultBounds (..)
, applyDefaultBoundsToCondTree
, emptyDefaultBounds
) where

import Distribution.Compat.Lens
import Distribution.Compat.Prelude

import qualified Distribution.Types.BuildInfo.Lens as L
import Distribution.Types.CondTree
import Distribution.Types.Dependency
import Distribution.Types.ExeDependency
import Distribution.Types.VersionRange

data DefaultBounds = DefaultBounds
{ defaultTargetBuildDepends :: [Dependency]
, defaultBuildToolDepends :: [ExeDependency]
}
deriving (Generic, Read, Show, Eq, Data, Typeable)

instance Binary DefaultBounds
instance Structured DefaultBounds
instance NFData DefaultBounds where rnf = genericRnf

emptyDefaultBounds :: DefaultBounds
emptyDefaultBounds = DefaultBounds [] []

applyDefaultBoundsToCondTree
:: L.HasBuildInfo a
=> DefaultBounds
-> CondTree cv [Dependency] a
-> CondTree cv [Dependency] a
applyDefaultBoundsToCondTree db =
mapTreeData (applyDefaultBoundsToBuildInfo db)
. mapTreeConstrs (applyDefaultBoundsToDependencies db)

applyDefaultBoundsToBuildInfo :: L.HasBuildInfo a => DefaultBounds -> a -> a
applyDefaultBoundsToBuildInfo db bi =
bi
& L.targetBuildDepends %~ applyDefaultBoundsToDependencies db
& L.buildToolDepends %~ applyDefaultBoundsToExeDependencies db

applyDefaultBoundsToDependencies :: DefaultBounds -> [Dependency] -> [Dependency]
applyDefaultBoundsToDependencies (DefaultBounds [] _) = id
applyDefaultBoundsToDependencies (DefaultBounds bd _) =
map
( \dep@(Dependency pkg vorig l) ->
if isAnyVersion vorig
then maybe dep (\v -> Dependency pkg (depVerRange v) l) $ find ((pkg ==) . depPkgName) bd
else dep
)

applyDefaultBoundsToExeDependencies :: DefaultBounds -> [ExeDependency] -> [ExeDependency]
applyDefaultBoundsToExeDependencies (DefaultBounds _ []) = id
applyDefaultBoundsToExeDependencies (DefaultBounds _ btd) =
map
( \dep@(ExeDependency pkg comp vorig) ->
if isAnyVersion vorig
then maybe dep (ExeDependency pkg comp . exeDepVerRange) $ find (\(ExeDependency pkg' comp' _) -> pkg == pkg' && comp == comp') btd
else dep
)
26 changes: 26 additions & 0 deletions Cabal-syntax/src/Distribution/Types/DefaultBounds/Lens.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Distribution.Types.DefaultBounds.Lens
( DefaultBounds
, module Distribution.Types.DefaultBounds.Lens
) where

import Distribution.Compat.Lens
import Distribution.Compat.Prelude
import Prelude ()

import qualified Distribution.Types.DefaultBounds as T

import Distribution.Types.DefaultBounds (DefaultBounds)
import Distribution.Types.Dependency (Dependency)
import Distribution.Types.ExeDependency (ExeDependency)

-------------------------------------------------------------------------------
-- GenericPackageDescription
-------------------------------------------------------------------------------

defaultTargetBuildDepends :: Lens' DefaultBounds [Dependency]
defaultTargetBuildDepends f s = fmap (\x -> s{T.defaultTargetBuildDepends = x}) (f (T.defaultTargetBuildDepends s))
{-# INLINE defaultTargetBuildDepends #-}

defaultBuildToolDepends :: Lens' DefaultBounds [ExeDependency]
defaultBuildToolDepends f s = fmap (\x -> s{T.defaultBuildToolDepends = x}) (f (T.defaultBuildToolDepends s))
{-# INLINE defaultBuildToolDepends #-}
12 changes: 12 additions & 0 deletions Cabal-syntax/src/Distribution/Types/ExeDependency.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
module Distribution.Types.ExeDependency
( ExeDependency (..)
, qualifiedExeName
, exeDepPkgName
, exeDepUnqualComp
, exeDepVerRange
) where

import Distribution.Compat.Prelude
Expand Down Expand Up @@ -31,6 +34,15 @@ instance Binary ExeDependency
instance Structured ExeDependency
instance NFData ExeDependency where rnf = genericRnf

exeDepPkgName :: ExeDependency -> PackageName
exeDepPkgName (ExeDependency pn _ _) = pn

exeDepUnqualComp :: ExeDependency -> UnqualComponentName
exeDepUnqualComp (ExeDependency _ uc _) = uc

exeDepVerRange :: ExeDependency -> VersionRange
exeDepVerRange (ExeDependency _ _ vr) = vr

instance Pretty ExeDependency where
pretty (ExeDependency name exe ver) =
pretty name <<>> PP.colon <<>> pretty exe PP.<+> pver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data GenericPackageDescription = GenericPackageDescription
-- Perfectly, PackageIndex should have sum type, so we don't need to
-- have dummy GPDs.
, genPackageFlags :: [PackageFlag]
, genDefaultPackageBounds :: DefaultBounds
, condLibrary :: Maybe (CondTree ConfVar [Dependency] Library)
, condSubLibraries
:: [ ( UnqualComponentName
Expand Down Expand Up @@ -81,17 +82,18 @@ instance Structured GenericPackageDescription
instance NFData GenericPackageDescription where rnf = genericRnf

emptyGenericPackageDescription :: GenericPackageDescription
emptyGenericPackageDescription = GenericPackageDescription emptyPackageDescription Nothing [] Nothing [] [] [] [] []
emptyGenericPackageDescription = GenericPackageDescription emptyPackageDescription Nothing [] emptyDefaultBounds Nothing [] [] [] [] []

-- -----------------------------------------------------------------------------
-- Traversal Instances

instance L.HasBuildInfos GenericPackageDescription where
traverseBuildInfos f (GenericPackageDescription p v a1 x1 x2 x3 x4 x5 x6) =
traverseBuildInfos f (GenericPackageDescription p v a1 bs x1 x2 x3 x4 x5 x6) =
GenericPackageDescription
<$> L.traverseBuildInfos f p
<*> pure v
<*> pure a1
<*> pure bs
<*> (traverse . traverseCondTreeBuildInfo) f x1
<*> (traverse . L._2 . traverseCondTreeBuildInfo) f x2
<*> (traverse . L._2 . traverseCondTreeBuildInfo) f x3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Distribution.System (Arch, OS)
import Distribution.Types.Benchmark (Benchmark)
import Distribution.Types.CondTree (CondTree)
import Distribution.Types.ConfVar (ConfVar (..))
import Distribution.Types.DefaultBounds (DefaultBounds)
import Distribution.Types.Dependency (Dependency)
import Distribution.Types.Executable (Executable)
import Distribution.Types.Flag (FlagName, PackageFlag (MkPackageFlag))
Expand Down Expand Up @@ -49,6 +50,10 @@ genPackageFlags :: Lens' GenericPackageDescription [PackageFlag]
genPackageFlags f s = fmap (\x -> s{T.genPackageFlags = x}) (f (T.genPackageFlags s))
{-# INLINE genPackageFlags #-}

genDefaultPackageBounds :: Lens' GenericPackageDescription DefaultBounds
genDefaultPackageBounds f s = fmap (\x -> s{T.genDefaultPackageBounds = x}) (f (T.genDefaultPackageBounds s))
{-# INLINE genDefaultPackageBounds #-}

condLibrary :: Lens' GenericPackageDescription (Maybe (CondTree ConfVar [Dependency] Library))
condLibrary f s = fmap (\x -> s{T.condLibrary = x}) (f (T.condLibrary s))
{-# INLINE condLibrary #-}
Expand Down Expand Up @@ -81,11 +86,12 @@ allCondTrees
)
-> GenericPackageDescription
-> f GenericPackageDescription
allCondTrees f (GenericPackageDescription p v a1 x1 x2 x3 x4 x5 x6) =
allCondTrees f (GenericPackageDescription p v a1 bs x1 x2 x3 x4 x5 x6) =
GenericPackageDescription
<$> pure p
<*> pure v
<*> pure a1
<*> pure bs
<*> traverse f x1
<*> (traverse . _2) f x2
<*> (traverse . _2) f x3
Expand Down
2 changes: 2 additions & 0 deletions Cabal-syntax/src/Distribution/Types/Lens.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Distribution.Types.Lens
( module Distribution.Types.Benchmark.Lens
, module Distribution.Types.BuildInfo.Lens
, module Distribution.Types.DefaultBounds.Lens
, module Distribution.Types.Executable.Lens
, module Distribution.Types.ForeignLib.Lens
, module Distribution.Types.GenericPackageDescription.Lens
Expand All @@ -14,6 +15,7 @@ module Distribution.Types.Lens

import Distribution.Types.Benchmark.Lens
import Distribution.Types.BuildInfo.Lens
import Distribution.Types.DefaultBounds.Lens
import Distribution.Types.Executable.Lens
import Distribution.Types.ForeignLib.Lens
import Distribution.Types.GenericPackageDescription.Lens
Expand Down
1 change: 1 addition & 0 deletions Cabal-tests/tests/NoThunks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ instance NoThunks BuildType
instance NoThunks CabalSpecVersion
instance NoThunks CompilerFlavor
instance NoThunks ConfVar
instance NoThunks DefaultBounds
instance NoThunks Dependency
instance NoThunks Executable
instance NoThunks ExecutableScope
Expand Down
4 changes: 4 additions & 0 deletions Cabal-tests/tests/ParserTests/regressions/Octree-0.5.expr
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ GenericPackageDescription {
extraDocFiles = []},
gpdScannedVersion = Nothing,
genPackageFlags = [],
genDefaultPackageBounds =
DefaultBounds {
defaultTargetBuildDepends = [],
defaultBuildToolDepends = []},
condLibrary = Just
CondNode {
condTreeData = Library {
Expand Down
Loading
Loading