@@ -28,7 +28,6 @@ module Distribution.PackageDescription.Configuration
2828 , mapCondTree
2929 , mapTreeData
3030 , mapTreeConds
31- , mapTreeConstrs
3231 , transformAllBuildInfos
3332 , transformAllBuildDepends
3433 , transformAllBuildDependsN
@@ -39,6 +38,7 @@ import Distribution.Compat.Prelude
3938import Prelude ()
4039
4140-- lens
41+ import qualified Distribution.Compat.Lens as L
4242import qualified Distribution.Types.BuildInfo.Lens as L
4343import qualified Distribution.Types.GenericPackageDescription.Lens as L
4444import qualified Distribution.Types.PackageDescription.Lens as L
@@ -64,6 +64,7 @@ import Distribution.Utils.Path (sameDirectory)
6464import Distribution.Version
6565
6666import Data.Tree (Tree (Node ))
67+ import Data.Tuple
6768
6869------------------------------------------------------------------------------
6970
@@ -192,7 +193,7 @@ resolveWithFlags
192193 -- ^ Compiler information
193194 -> [PackageVersionConstraint ]
194195 -- ^ Additional constraints
195- -> [CondTree ConfVar [ Dependency ] PDTagged ]
196+ -> [CondTree ConfVar PDTagged ]
196197 -> ([Dependency ] -> DepTestRslt )
197198 -- ^ Dependency test function.
198199 -> Either [MissingDependency ] (TargetSet PDTagged , FlagAssignment )
@@ -203,10 +204,10 @@ resolveWithFlags dom enabled os arch impl constrs trees checkDeps =
203204 where
204205 -- simplify trees by (partially) evaluating all conditions and converting
205206 -- dependencies to dependency maps.
206- simplifiedTrees :: [CondTree FlagName DependencyMap PDTagged ]
207+ simplifiedTrees :: [CondTree FlagName ( PDTagged , DependencyMap ) ]
207208 simplifiedTrees =
208209 map
209- ( mapTreeConstrs toDepMap -- convert to maps
210+ ( mapTreeData ( \ x -> (x, toDepMap $ L. view L. targetBuildDepends x))
210211 . addBuildableConditionPDTagged
211212 . mapTreeConds (fst . simplifyWithSysParams os arch impl)
212213 )
@@ -226,6 +227,7 @@ resolveWithFlags dom enabled os arch impl constrs trees checkDeps =
226227 flip map simplifiedTrees $
227228 -- apply additional constraints to all dependencies
228229 first (`constrainBy` constrs)
230+ . swap
229231 . simplifyCondTree (env flags)
230232 deps = overallDependencies enabled targetSet
231233 in case checkDeps (fromDepMap deps) of
@@ -262,15 +264,15 @@ resolveWithFlags dom enabled os arch impl constrs trees checkDeps =
262264-- can determine that Buildable is always True, it returns the input unchanged.
263265-- If Buildable is always False, it returns the empty 'CondTree'.
264266addBuildableCondition
265- :: (Eq v , Monoid a , Monoid c )
267+ :: (Eq v , Monoid a )
266268 => (a -> BuildInfo )
267- -> CondTree v c a
268- -> CondTree v c a
269+ -> CondTree v a
270+ -> CondTree v a
269271addBuildableCondition getInfo t =
270272 case extractCondition (buildable . getInfo) t of
271273 Lit True -> t
272- Lit False -> CondNode mempty mempty []
273- c -> CondNode mempty mempty [condIfThen c t]
274+ Lit False -> CondNode mempty []
275+ c -> CondNode mempty [condIfThen c t]
274276
275277-- | This is a special version of 'addBuildableCondition' for the 'PDTagged'
276278-- type.
@@ -282,16 +284,18 @@ addBuildableCondition getInfo t =
282284--
283285-- See <https://github.com/haskell/cabal/pull/4094> for more details.
284286addBuildableConditionPDTagged
285- :: ( Eq v , Monoid c )
286- => CondTree v c PDTagged
287- -> CondTree v c PDTagged
287+ :: Eq v
288+ => CondTree v PDTagged
289+ -> CondTree v PDTagged
288290addBuildableConditionPDTagged t =
289291 case extractCondition (buildable . getInfo) t of
290292 Lit True -> t
291- Lit False -> deleteConstraints t
292- c -> CondNode mempty mempty [condIfThenElse c t (deleteConstraints t)]
293+ Lit False -> mapTreeData deleteConstraints t
294+ c -> CondNode mempty [condIfThenElse c t (mapTreeData deleteConstraints t)]
293295 where
294- deleteConstraints = mapTreeConstrs (const mempty )
296+ deleteConstraints (Lib lib) = Lib (L. set L. targetBuildDepends mempty lib)
297+ deleteConstraints (SubComp unqualName comp) = SubComp unqualName (L. set L. targetBuildDepends mempty comp)
298+ deleteConstraints PDNull = PDNull
295299
296300 getInfo :: PDTagged -> BuildInfo
297301 getInfo (Lib l) = libBuildInfo l
@@ -326,10 +330,10 @@ extractConditions f gpkg =
326330 , extractCondition (f . benchmarkBuildInfo) . snd <$> condBenchmarks gpkg
327331 ]
328332
329- freeVars :: CondTree ConfVar c a -> [FlagName ]
333+ freeVars :: CondTree ConfVar a -> [FlagName ]
330334freeVars t = [f | PackageFlag f <- freeVars' t]
331335 where
332- freeVars' (CondNode _ _ ifs) = concatMap compfv ifs
336+ freeVars' (CondNode _ ifs) = concatMap compfv ifs
333337 compfv (CondBranch c ct mct) = condfv c ++ freeVars' ct ++ maybe [] freeVars' mct
334338 condfv c = case c of
335339 Var v -> [v]
@@ -406,6 +410,12 @@ instance Semigroup PDTagged where
406410 SubComp n x <> SubComp n' x' | n == n' = SubComp n (x <> x')
407411 _ <> _ = cabalBug " Cannot combine incompatible tags"
408412
413+ instance L. HasBuildInfo PDTagged where
414+ buildInfo f x = case x of
415+ Lib lib -> Lib <$> L. buildInfo f lib
416+ SubComp name comp -> SubComp name <$> L. buildInfo f comp
417+ PDNull -> PDNull <$ f mempty
418+
409419-- | Create a package description with all configurations resolved.
410420--
411421-- This function takes a `GenericPackageDescription` and several environment
@@ -554,36 +564,47 @@ flattenPackageDescription
554564 where
555565 mlib = f <$> mlib0
556566 where
557- f lib = (libFillInDefaults . fst . ignoreConditions $ lib){libName = LMainLibName }
567+ f :: CondTree ConfVar Library -> Library
568+ f lib = (libFillInDefaults . ignoreConditions $ lib){libName = LMainLibName }
558569 sub_libs = flattenLib <$> sub_libs0
559570 flibs = flattenFLib <$> flibs0
560571 exes = flattenExe <$> exes0
561572 tests = flattenTst <$> tests0
562573 bms = flattenBm <$> bms0
574+
575+ flattenLib :: (UnqualComponentName , CondTree ConfVar Library ) -> Library
563576 flattenLib (n, t) =
564577 libFillInDefaults $
565- (fst $ ignoreConditions t)
578+ (ignoreConditions t)
566579 { libName = LSubLibName n
567580 , libExposed = False
568581 }
582+
583+ flattenFLib :: (UnqualComponentName , CondTree ConfVar ForeignLib ) -> ForeignLib
569584 flattenFLib (n, t) =
570585 flibFillInDefaults $
571- (fst $ ignoreConditions t)
586+ (ignoreConditions t)
572587 { foreignLibName = n
573588 }
589+
590+ flattenExe :: (UnqualComponentName , CondTree ConfVar Executable ) -> Executable
574591 flattenExe (n, t) =
575592 exeFillInDefaults $
576- (fst $ ignoreConditions t)
593+ (ignoreConditions t)
577594 { exeName = n
578595 }
596+
597+ flattenTst :: (UnqualComponentName , CondTree ConfVar TestSuite ) -> TestSuite
579598 flattenTst (n, t) =
580599 testFillInDefaults $
581- (fst $ ignoreConditions t)
600+ (ignoreConditions t)
582601 { testName = n
583602 }
603+
604+ flattenBm :: (UnqualComponentName , CondTree ConfVar Benchmark ) -> Benchmark
584605 flattenBm (n, t) =
585606 benchFillInDefaults $
586- (fst $ ignoreConditions t)
607+ (ignoreConditions t)
587608 { benchmarkName = n
588609 }
589610
@@ -640,8 +661,6 @@ transformAllBuildDepends
640661transformAllBuildDepends f =
641662 over (L. traverseBuildInfos . L. targetBuildDepends . traverse ) f
642663 . over (L. packageDescription . L. setupBuildInfo . traverse . L. setupDepends . traverse ) f
643- -- cannot be point-free as normal because of higher rank
644- . over (\ f' -> L. allCondTrees $ traverseCondTreeC f') (map f)
645664
646665-- | Walk a 'GenericPackageDescription' and apply @f@ to all nested
647666-- @build-depends@ fields.
@@ -652,5 +671,3 @@ transformAllBuildDependsN
652671transformAllBuildDependsN f =
653672 over (L. traverseBuildInfos . L. targetBuildDepends) f
654673 . over (L. packageDescription . L. setupBuildInfo . traverse . L. setupDepends) f
655- -- cannot be point-free as normal because of higher rank
656- . over (\ f' -> L. allCondTrees $ traverseCondTreeC f') f
0 commit comments