Skip to content

Commit 856a7c6

Browse files
committed
Preserve per-component build info until pretty-printing
1 parent 2ceff8f commit 856a7c6

5 files changed

Lines changed: 88 additions & 68 deletions

File tree

cabal2nix/src/Cabal2nix.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() ->
175175

176176
hpackOverrides :: Derivation -> Derivation
177177
hpackOverrides = over phaseOverrides (++ "prePatch = \"hpack\";")
178-
. set (libraryDepends . tool . contains (PP.pkg "hpack")) True
178+
. set (focusBuildInfo libraryDepends . tool . contains (PP.pkg "hpack")) True
179179

180180
cabal2nix' :: Options -> IO (Either Doc Derivation)
181181
cabal2nix' opts@Options{..} = do

cabal2nix/src/Distribution/Nixpkgs/Haskell/Derivation.hs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE RankNTypes #-}
24
{-# LANGUAGE RecordWildCards #-}
35
{-# LANGUAGE TemplateHaskell #-}
46
{-# LANGUAGE TupleSections #-}
@@ -10,11 +12,15 @@ module Distribution.Nixpkgs.Haskell.Derivation
1012
, cabalFlags, runHaddock, jailbreak, doCheck, doBenchmark, testFlags, testTargets, hyperlinkSource
1113
, enableLibraryProfiling, enableExecutableProfiling, phaseOverrides, editedCabalFile, metaSection
1214
, dependencies, setupDepends, benchmarkDepends, enableSeparateDataOutput, extraAttributes
15+
, focusBuildInfo
1316
)
1417
where
1518

1619
import Prelude hiding ((<>))
1720

21+
#if !MIN_VERSION_base(4,18,0)
22+
import Control.Applicative (liftA2)
23+
#endif
1824
import Control.DeepSeq
1925
import Control.Lens
2026
import Data.List ( isPrefixOf )
@@ -47,10 +53,10 @@ data Derivation = MkDerivation
4753
, _extraFunctionArgs :: Set Binding
4854
, _extraAttributes :: Map String String
4955
, _setupDepends :: BuildInfo
50-
, _libraryDepends :: BuildInfo
51-
, _executableDepends :: BuildInfo
52-
, _testDepends :: BuildInfo
53-
, _benchmarkDepends :: BuildInfo
56+
, _libraryDepends :: [(BuildInfo, Bool)]
57+
, _executableDepends :: [(BuildInfo, Bool)]
58+
, _testDepends :: [(BuildInfo, Bool)]
59+
, _benchmarkDepends :: [(BuildInfo, Bool)]
5460
, _configureFlags :: Set String
5561
, _cabalFlags :: FlagAssignment
5662
, _runHaddock :: Bool
@@ -103,7 +109,14 @@ nullDerivation = MkDerivation
103109

104110
makeLenses ''Derivation
105111

106-
makeLensesFor (fmap (,"dependencies") ["_setupDepends", "_libraryDepends", "_executableDepends", "_testDepends", "_benchmarkDepends"]) ''Derivation
112+
makeLensesFor (fmap (,"nonSetupDependencies") ["_libraryDepends", "_executableDepends", "_testDepends", "_benchmarkDepends"]) ''Derivation
113+
114+
dependencies :: Traversal' Derivation BuildInfo
115+
dependencies = traversal $ \focus drv ->
116+
liftA2 (set setupDepends) (focus $ view setupDepends drv) ((nonSetupDependencies . traverse . _1) focus drv)
117+
118+
focusBuildInfo :: Lens' Derivation [(BuildInfo, Bool)] -> Traversal' Derivation BuildInfo
119+
focusBuildInfo l = l . traverse . _1
107120

108121
instance Package Derivation where
109122
packageId = view pkgid
@@ -125,10 +138,10 @@ instance Pretty Derivation where
125138
, boolattr "isExecutable" (not _isLibrary || _isExecutable) _isExecutable
126139
, boolattr "enableSeparateDataOutput" _enableSeparateDataOutput _enableSeparateDataOutput
127140
, pPrintBuildInfo "setup" _setupDepends
128-
, pPrintBuildInfo "library" _libraryDepends
129-
, pPrintBuildInfo "executable" _executableDepends
130-
, pPrintBuildInfo "test" _testDepends
131-
, pPrintBuildInfo "benchmark" _benchmarkDepends
141+
, pPrintBuildInfo' "library" _libraryDepends
142+
, pPrintBuildInfo' "executable" _executableDepends
143+
, pPrintBuildInfo' "test" _testDepends
144+
, pPrintBuildInfo' "benchmark" _benchmarkDepends
132145
, boolattr "enableLibraryProfiling" _enableLibraryProfiling _enableLibraryProfiling
133146
, boolattr "enableExecutableProfiling" _enableExecutableProfiling _enableExecutableProfiling
134147
, boolattr "doHaddock" (not _runHaddock) _runHaddock
@@ -145,6 +158,9 @@ instance Pretty Derivation where
145158
, rbrace
146159
]
147160
where
161+
pPrintBuildInfo' :: String -> [(BuildInfo, Bool)] -> Doc
162+
pPrintBuildInfo' name = pPrintBuildInfo name . foldMap fst . filter snd
163+
148164
inputs :: Set String
149165
inputs = Set.unions [ Set.map (view (localName . ident)) _extraFunctionArgs
150166
, setOf (dependencies . each . folded . localName . ident) drv

cabal2nix/src/Distribution/Nixpkgs/Haskell/FromCabal.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE TupleSections #-}
45

56
module Distribution.Nixpkgs.Haskell.FromCabal
67
( HaskellResolver, NixpkgsResolver
@@ -163,8 +164,8 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
163164
& Nix.broken .~ not (null missingDeps)
164165
)
165166
where
166-
deps :: (a -> Cabal.BuildInfo) -> [a] -> Nix.BuildInfo
167-
deps getBuildInfo = foldMap (convertBuildInfo . getBuildInfo)
167+
deps :: (a -> Cabal.BuildInfo) -> [a] -> [(Nix.BuildInfo, Bool)]
168+
deps getBuildInfo = map (convertBuildInfo . getBuildInfo)
168169

169170
xrev = maybe 0 read (lookup "x-revision" customFieldsPD)
170171

@@ -211,9 +212,8 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
211212
| Just l <- library = not (null (exposedModules l))
212213
| otherwise = True
213214

214-
convertBuildInfo :: Cabal.BuildInfo -> Nix.BuildInfo
215-
convertBuildInfo Cabal.BuildInfo {..} | not buildable = mempty
216-
convertBuildInfo Cabal.BuildInfo {..} = mempty
215+
convertBuildInfo :: Cabal.BuildInfo -> (Nix.BuildInfo, Bool)
216+
convertBuildInfo Cabal.BuildInfo {..} = (, buildable) $ mempty
217217
& haskell .~ Set.fromList [ resolveInHackage (toNixName x) | (Dependency x _ _) <- targetBuildDepends, x `notElem` internalLibNames ]
218218
& system .~ Set.fromList [ resolveInNixpkgs y | x <- extraLibs, y <- libNixName x ]
219219
& pkgconfig .~ Set.fromList [ resolveInNixpkgs y | PkgconfigDependency x _ <- pkgconfigDepends, y <- libNixName (unPkgconfigName x) ]

cabal2nix/src/Distribution/Nixpkgs/Haskell/FromCabal/Normalize.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE RankNTypes #-}
24

35
module Distribution.Nixpkgs.Haskell.FromCabal.Normalize ( normalize ) where
46

@@ -19,7 +21,8 @@ normalize drv = drv
1921
& over metaSection normalizeMeta
2022
& jailbreak %~ (&& (packageName drv /= "jailbreak-cabal"))
2123
where
22-
deps f = over f $ normalizeBuildInfo (packageName drv)
24+
deps :: Lens' Derivation [(BuildInfo, Bool)] -> Derivation -> Derivation
25+
deps f = over (focusBuildInfo f) $ normalizeBuildInfo (packageName drv)
2326

2427
normalizeBuildInfo :: PackageName -> BuildInfo -> BuildInfo
2528
normalizeBuildInfo pname bi = bi

0 commit comments

Comments
 (0)