{-# LANGUAGE TypeFamilies #-}
module Distribution.Client.ProjectPlanning.SetupPolicy
( mkDefaultSetupDeps
, packageSetupScriptStyle
, packageSetupScriptSpecVersion
, NonSetupLibDepSolverPlanPackage (..)
)
where
import Distribution.Client.Compat.Prelude
import Prelude ()
import Distribution.Client.ProjectPlanning.Types (SetupScriptStyle (..))
import Distribution.Client.SolverInstallPlan (SolverPlanPackage)
import Distribution.Solver.Types.ComponentDeps (ComponentDeps)
import qualified Distribution.Solver.Types.ComponentDeps as CD
import Distribution.Solver.Types.ResolverPackage (resolverPackageLibDeps)
import Distribution.Solver.Types.SolverId (SolverId)
import Distribution.CabalSpecVersion
import Distribution.Package
import Distribution.PackageDescription
import Distribution.Simple.Compiler
import Distribution.System
import Distribution.Simple.Utils
import Distribution.Version
import Distribution.Compat.Graph (IsNode (..))
import qualified Distribution.Compat.Graph as Graph
packageSetupScriptStyle :: PackageDescription -> SetupScriptStyle
packageSetupScriptStyle :: PackageDescription -> SetupScriptStyle
packageSetupScriptStyle PackageDescription
pkg
| PackageDescription -> BuildType
buildType PackageDescription
pkg BuildType -> BuildType -> Bool
forall a. Eq a => a -> a -> Bool
== BuildType
Custom
, Just SetupBuildInfo
setupbi <- PackageDescription -> Maybe SetupBuildInfo
setupBuildInfo PackageDescription
pkg
, Bool -> Bool
not (SetupBuildInfo -> Bool
defaultSetupDepends SetupBuildInfo
setupbi)
=
SetupScriptStyle
SetupCustomExplicitDeps
| PackageDescription -> BuildType
buildType PackageDescription
pkg BuildType -> BuildType -> Bool
forall a. Eq a => a -> a -> Bool
== BuildType
Custom
, Just SetupBuildInfo
setupbi <- PackageDescription -> Maybe SetupBuildInfo
setupBuildInfo PackageDescription
pkg
, SetupBuildInfo -> Bool
defaultSetupDepends SetupBuildInfo
setupbi
=
SetupScriptStyle
SetupCustomImplicitDeps
| PackageDescription -> BuildType
buildType PackageDescription
pkg BuildType -> BuildType -> Bool
forall a. Eq a => a -> a -> Bool
== BuildType
Custom
, Maybe SetupBuildInfo
Nothing <- PackageDescription -> Maybe SetupBuildInfo
setupBuildInfo PackageDescription
pkg
=
SetupScriptStyle
SetupCustomImplicitDeps
| PackageDescription -> CabalSpecVersion
specVersion PackageDescription
pkg CabalSpecVersion -> CabalSpecVersion -> Bool
forall a. Ord a => a -> a -> Bool
> CabalSpecVersion
cabalSpecLatest =
SetupScriptStyle
SetupNonCustomExternalLib
| Bool
otherwise =
SetupScriptStyle
SetupNonCustomInternalLib
mkDefaultSetupDeps
:: Compiler
-> Platform
-> PackageDescription
-> Maybe [Dependency]
mkDefaultSetupDeps :: Compiler -> Platform -> PackageDescription -> Maybe [Dependency]
mkDefaultSetupDeps Compiler
compiler Platform
platform PackageDescription
pkg =
case PackageDescription -> SetupScriptStyle
packageSetupScriptStyle PackageDescription
pkg of
SetupScriptStyle
SetupCustomImplicitDeps ->
[Dependency] -> Maybe [Dependency]
forall a. a -> Maybe a
Just ([Dependency] -> Maybe [Dependency])
-> [Dependency] -> Maybe [Dependency]
forall a b. (a -> b) -> a -> b
$
[ PackageName
-> VersionRange -> NonEmptySet LibraryName -> Dependency
Dependency PackageName
depPkgname VersionRange
anyVersion NonEmptySet LibraryName
mainLibSet
| PackageName
depPkgname <- Compiler -> Platform -> [PackageName]
legacyCustomSetupPkgs Compiler
compiler Platform
platform
]
[Dependency] -> [Dependency] -> [Dependency]
forall a. [a] -> [a] -> [a]
++ [ PackageName
-> VersionRange -> NonEmptySet LibraryName -> Dependency
Dependency PackageName
cabalPkgname VersionRange
cabalConstraint NonEmptySet LibraryName
mainLibSet
| PackageDescription -> PackageName
forall pkg. Package pkg => pkg -> PackageName
packageName PackageDescription
pkg PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
/= PackageName
cabalPkgname
]
where
cabalConstraint :: VersionRange
cabalConstraint =
Version -> VersionRange
orLaterVersion (CabalSpecVersion -> Version
csvToVersion (PackageDescription -> CabalSpecVersion
specVersion PackageDescription
pkg))
VersionRange -> VersionRange -> VersionRange
`intersectVersionRanges` Version -> VersionRange
earlierVersion Version
cabalCompatMaxVer
cabalCompatMaxVer :: Version
cabalCompatMaxVer = [Int] -> Version
mkVersion [Int
1, Int
25]
SetupScriptStyle
SetupNonCustomExternalLib ->
[Dependency] -> Maybe [Dependency]
forall a. a -> Maybe a
Just
[ PackageName
-> VersionRange -> NonEmptySet LibraryName -> Dependency
Dependency PackageName
cabalPkgname VersionRange
cabalConstraint NonEmptySet LibraryName
mainLibSet
, PackageName
-> VersionRange -> NonEmptySet LibraryName -> Dependency
Dependency PackageName
basePkgname VersionRange
anyVersion NonEmptySet LibraryName
mainLibSet
]
where
cabalConstraint :: VersionRange
cabalConstraint = Version -> VersionRange
orLaterVersion (CabalSpecVersion -> Version
csvToVersion (PackageDescription -> CabalSpecVersion
specVersion PackageDescription
pkg))
SetupScriptStyle
SetupNonCustomInternalLib -> [Dependency] -> Maybe [Dependency]
forall a. a -> Maybe a
Just []
SetupScriptStyle
SetupCustomExplicitDeps ->
[Char] -> Maybe [Dependency]
forall a. HasCallStack => [Char] -> a
error ([Char] -> Maybe [Dependency]) -> [Char] -> Maybe [Dependency]
forall a b. (a -> b) -> a -> b
$
[Char]
"mkDefaultSetupDeps: called for a package with explicit "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"setup deps: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ PackageIdentifier -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow (PackageDescription -> PackageIdentifier
forall pkg. Package pkg => pkg -> PackageIdentifier
packageId PackageDescription
pkg)
where
csvToVersion :: CabalSpecVersion -> Version
csvToVersion :: CabalSpecVersion -> Version
csvToVersion = [Int] -> Version
mkVersion ([Int] -> Version)
-> (CabalSpecVersion -> [Int]) -> CabalSpecVersion -> Version
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CabalSpecVersion -> [Int]
cabalSpecMinimumLibraryVersion
newtype NonSetupLibDepSolverPlanPackage = NonSetupLibDepSolverPlanPackage
{NonSetupLibDepSolverPlanPackage -> SolverPlanPackage
unNonSetupLibDepSolverPlanPackage :: SolverPlanPackage}
instance Package NonSetupLibDepSolverPlanPackage where
packageId :: NonSetupLibDepSolverPlanPackage -> PackageIdentifier
packageId (NonSetupLibDepSolverPlanPackage SolverPlanPackage
spkg) =
SolverPlanPackage -> PackageIdentifier
forall pkg. Package pkg => pkg -> PackageIdentifier
packageId SolverPlanPackage
spkg
instance IsNode NonSetupLibDepSolverPlanPackage where
type Key NonSetupLibDepSolverPlanPackage = SolverId
nodeKey :: NonSetupLibDepSolverPlanPackage
-> Key NonSetupLibDepSolverPlanPackage
nodeKey (NonSetupLibDepSolverPlanPackage SolverPlanPackage
spkg) =
SolverPlanPackage -> Key SolverPlanPackage
forall a. IsNode a => a -> Key a
nodeKey SolverPlanPackage
spkg
nodeNeighbors :: NonSetupLibDepSolverPlanPackage
-> [Key NonSetupLibDepSolverPlanPackage]
nodeNeighbors (NonSetupLibDepSolverPlanPackage SolverPlanPackage
spkg) =
[Key NonSetupLibDepSolverPlanPackage]
-> [Key NonSetupLibDepSolverPlanPackage]
forall a. Ord a => [a] -> [a]
ordNub ([Key NonSetupLibDepSolverPlanPackage]
-> [Key NonSetupLibDepSolverPlanPackage])
-> [Key NonSetupLibDepSolverPlanPackage]
-> [Key NonSetupLibDepSolverPlanPackage]
forall a b. (a -> b) -> a -> b
$ ComponentDeps [SolverId] -> [SolverId]
forall a. Monoid a => ComponentDeps a -> a
CD.nonSetupDeps (SolverPlanPackage -> ComponentDeps [SolverId]
forall loc. ResolverPackage loc -> ComponentDeps [SolverId]
resolverPackageLibDeps SolverPlanPackage
spkg)
packageSetupScriptSpecVersion
:: SetupScriptStyle
-> PackageDescription
-> Graph.Graph NonSetupLibDepSolverPlanPackage
-> ComponentDeps [SolverId]
-> Version
packageSetupScriptSpecVersion :: SetupScriptStyle
-> PackageDescription
-> Graph NonSetupLibDepSolverPlanPackage
-> ComponentDeps [SolverId]
-> Version
packageSetupScriptSpecVersion SetupScriptStyle
SetupNonCustomInternalLib PackageDescription
_ Graph NonSetupLibDepSolverPlanPackage
_ ComponentDeps [SolverId]
_ =
Version
cabalVersion
packageSetupScriptSpecVersion SetupScriptStyle
SetupCustomImplicitDeps PackageDescription
pkg Graph NonSetupLibDepSolverPlanPackage
_ ComponentDeps [SolverId]
_
| PackageDescription -> PackageName
forall pkg. Package pkg => pkg -> PackageName
packageName PackageDescription
pkg PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageName
cabalPkgname =
PackageDescription -> Version
forall pkg. Package pkg => pkg -> Version
packageVersion PackageDescription
pkg
packageSetupScriptSpecVersion SetupScriptStyle
_ PackageDescription
pkg Graph NonSetupLibDepSolverPlanPackage
libDepGraph ComponentDeps [SolverId]
deps =
case (PackageIdentifier -> Bool)
-> [PackageIdentifier] -> Maybe PackageIdentifier
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((PackageName
cabalPkgname PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
==) (PackageName -> Bool)
-> (PackageIdentifier -> PackageName) -> PackageIdentifier -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageIdentifier -> PackageName
forall pkg. Package pkg => pkg -> PackageName
packageName) [PackageIdentifier]
setupLibDeps of
Just PackageIdentifier
dep -> PackageIdentifier -> Version
forall pkg. Package pkg => pkg -> Version
packageVersion PackageIdentifier
dep
Maybe PackageIdentifier
Nothing -> [Int] -> Version
mkVersion (CabalSpecVersion -> [Int]
cabalSpecMinimumLibraryVersion (PackageDescription -> CabalSpecVersion
specVersion PackageDescription
pkg))
where
setupLibDeps :: [PackageIdentifier]
setupLibDeps =
(NonSetupLibDepSolverPlanPackage -> PackageIdentifier)
-> [NonSetupLibDepSolverPlanPackage] -> [PackageIdentifier]
forall a b. (a -> b) -> [a] -> [b]
map NonSetupLibDepSolverPlanPackage -> PackageIdentifier
forall pkg. Package pkg => pkg -> PackageIdentifier
packageId ([NonSetupLibDepSolverPlanPackage] -> [PackageIdentifier])
-> [NonSetupLibDepSolverPlanPackage] -> [PackageIdentifier]
forall a b. (a -> b) -> a -> b
$
[NonSetupLibDepSolverPlanPackage]
-> Maybe [NonSetupLibDepSolverPlanPackage]
-> [NonSetupLibDepSolverPlanPackage]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [NonSetupLibDepSolverPlanPackage]
-> [NonSetupLibDepSolverPlanPackage])
-> Maybe [NonSetupLibDepSolverPlanPackage]
-> [NonSetupLibDepSolverPlanPackage]
forall a b. (a -> b) -> a -> b
$
Graph NonSetupLibDepSolverPlanPackage
-> [Key NonSetupLibDepSolverPlanPackage]
-> Maybe [NonSetupLibDepSolverPlanPackage]
forall a. Graph a -> [Key a] -> Maybe [a]
Graph.closure Graph NonSetupLibDepSolverPlanPackage
libDepGraph (ComponentDeps [SolverId] -> [SolverId]
forall a. Monoid a => ComponentDeps a -> a
CD.setupDeps ComponentDeps [SolverId]
deps)
cabalPkgname, basePkgname :: PackageName
cabalPkgname :: PackageName
cabalPkgname = [Char] -> PackageName
mkPackageName [Char]
"Cabal"
basePkgname :: PackageName
basePkgname = [Char] -> PackageName
mkPackageName [Char]
"base"
legacyCustomSetupPkgs :: Compiler -> Platform -> [PackageName]
legacyCustomSetupPkgs :: Compiler -> Platform -> [PackageName]
legacyCustomSetupPkgs Compiler
compiler (Platform Arch
_ OS
os) =
([Char] -> PackageName) -> [[Char]] -> [PackageName]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> PackageName
mkPackageName ([[Char]] -> [PackageName]) -> [[Char]] -> [PackageName]
forall a b. (a -> b) -> a -> b
$
[ [Char]
"array"
, [Char]
"base"
, [Char]
"binary"
, [Char]
"bytestring"
, [Char]
"containers"
, [Char]
"deepseq"
, [Char]
"directory"
, [Char]
"filepath"
, [Char]
"pretty"
, [Char]
"process"
, [Char]
"time"
, [Char]
"transformers"
]
[[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
"Win32" | OS
os OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
Windows]
[[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
"unix" | OS
os OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
/= OS
Windows]
[[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
"ghc-prim" | Bool
isGHC]
[[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
"template-haskell" | Bool
isGHC]
[[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
"old-time" | Bool
notGHC710]
where
isGHC :: Bool
isGHC = CompilerFlavor -> Compiler -> Bool
compilerCompatFlavor CompilerFlavor
GHC Compiler
compiler
notGHC710 :: Bool
notGHC710 = case CompilerFlavor -> Compiler -> Maybe Version
compilerCompatVersion CompilerFlavor
GHC Compiler
compiler of
Maybe Version
Nothing -> Bool
False
Just Version
v -> Version
v Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
<= [Int] -> Version
mkVersion [Int
7, Int
9]