Skip to content

Commit 46d04b4

Browse files
committed
Work around changed behaviour of Cabal 2 w.r.t. autogen dirs
Cf flycheck#82, commercialhaskell/stack#3791
1 parent 849f1d5 commit 46d04b4

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

get-cabal-configuration.hs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import Data.Version (Version)
7474
#endif
7575

7676
#if Cabal2
77+
import Control.Monad (filterM)
7778
import Distribution.Package (unPackageName, depPkgName, PackageName)
7879
import Distribution.PackageDescription.Configuration (finalizePD)
7980
import Distribution.Types.ComponentRequestedSpec (ComponentRequestedSpec(..))
@@ -82,6 +83,7 @@ import Distribution.Types.UnqualComponentName (unUnqualComponentName)
8283
import qualified Distribution.Version as CabalVersion
8384
import Distribution.Types.Benchmark (Benchmark(benchmarkName))
8485
import Distribution.Types.TestSuite (TestSuite(testName))
86+
import System.Directory (doesDirectoryExist)
8587
#else
8688
import Control.Arrow (second)
8789
import Data.Version (showVersion)
@@ -157,30 +159,48 @@ getBuildDirectories
157159
:: TargetTool
158160
-> PackageDescription
159161
-> FilePath
160-
-> IO ([FilePath], FilePath)
162+
-> IO ([FilePath], [FilePath])
161163
getBuildDirectories tool pkgDesc cabalDir = do
162164
distDir' <- distDir tool
163165
let buildDir :: FilePath
164166
buildDir = cabalDir </> distDir' </> "build"
165-
-- 'dist/bulid/autogen' OR '.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/autogen/'
166-
autogenDir :: FilePath
167-
autogenDir = buildDir </> "autogen"
168167

169-
componentBuildDir :: (a -> String) -> a -> FilePath
170-
componentBuildDir componentName component =
171-
buildDir </> componentName component </> (componentName component ++ "-tmp")
168+
componentNames :: [String]
169+
componentNames =
170+
map getExeName (executables pkgDesc) ++
171+
map getTestName (testSuites pkgDesc) ++
172+
map getBenchName (benchmarks pkgDesc)
173+
174+
175+
autogenDirs <- getAutogenDirs buildDir componentNames
176+
177+
let componentBuildDir :: String -> FilePath
178+
componentBuildDir componentName =
179+
buildDir </> componentName </> (componentName ++ "-tmp")
172180

173181
buildDirs :: [FilePath]
174182
buildDirs =
175-
autogenDir :
176-
map (componentBuildDir getExeName) (executables pkgDesc) ++
177-
map (componentBuildDir getTestName) (testSuites pkgDesc) ++
178-
map (componentBuildDir getBenchName) (benchmarks pkgDesc)
183+
autogenDirs ++
184+
map componentBuildDir componentNames
179185

180186
buildDirs' = case library pkgDesc of
181187
Just _ -> buildDir : buildDirs
182188
Nothing -> buildDirs
183-
return (buildDirs', autogenDir)
189+
return (buildDirs', autogenDirs)
190+
191+
getAutogenDirs :: FilePath -> [String] -> IO [FilePath]
192+
getAutogenDirs buildDir componentNames =
193+
fmap (autogenDir :) $
194+
#if Cabal2
195+
filterM doesDirectoryExist $
196+
map (\path -> buildDir </> path </> "autogen") componentNames
197+
#else
198+
const (return []) componentNames
199+
#endif
200+
where
201+
-- 'dist/bulid/autogen' OR '.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/autogen'
202+
autogenDir :: FilePath
203+
autogenDir = buildDir </> "autogen"
184204

185205
getSourceDirectories :: [BuildInfo] -> FilePath -> [String]
186206
getSourceDirectories buildInfo cabalDir =
@@ -217,10 +237,10 @@ isAllowedOption opt =
217237

218238
dumpPackageDescription :: PackageDescription -> FilePath -> IO Sexp
219239
dumpPackageDescription pkgDesc cabalFile = do
220-
(cabalDirs, autogenDir) <- getBuildDirectories Cabal pkgDesc cabalDir
221-
(stackDirs, autogenDir') <- getBuildDirectories Stack pkgDesc cabalDir
240+
(cabalDirs, cabalAutogen) <- getBuildDirectories Cabal pkgDesc cabalDir
241+
(stackDirs, stackAutogen) <- getBuildDirectories Stack pkgDesc cabalDir
222242
let buildDirs = cabalDirs ++ stackDirs
223-
autogenDirs = [autogenDir, autogenDir']
243+
autogenDirs = cabalAutogen ++ stackAutogen
224244
return $
225245
SList
226246
[ cons (sym "build-directories") (ordNub (map normalise buildDirs))

0 commit comments

Comments
 (0)