3#ifndef MIN_VERSION_Cabal
4#define MIN_VERSION_Cabal(x,y,z) 0
6#if MIN_VERSION_Cabal(1,24,0)
7#define InstalledPackageId UnitId
9module Main (main) where
11import Control.Monad ( when )
12import Data.List ( nub )
13import Distribution.Package ( InstalledPackageId )
14import Distribution.Package ( PackageId, Package (..), packageVersion )
15import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) , Library (..), BuildInfo (..))
16import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks )
17import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose )
18import Distribution.Simple.BuildPaths ( autogenModulesDir )
19import Distribution.Simple.Setup ( BuildFlags(buildDistPref, buildVerbosity), fromFlag)
20import Distribution.Simple.LocalBuildInfo ( withPackageDB, withLibLBI, withTestLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps), compiler )
21import Distribution.Simple.Compiler ( showCompilerId , PackageDB (..))
22import Distribution.Text ( display , simpleParse )
23import System.FilePath ( (</>) )
25#if MIN_VERSION_Cabal(1,25,0)
26import Distribution.Simple.BuildPaths ( autogenComponentModulesDir )
30main = defaultMainWithHooks simpleUserHooks
31 { buildHook = \pkg lbi hooks flags -> do
32 generateBuildModule flags pkg lbi
33 buildHook simpleUserHooks pkg lbi hooks flags
36generateBuildModule :: BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
37generateBuildModule flags pkg lbi = do
38 let verbosity = fromFlag (buildVerbosity flags)
39 let distPref = fromFlag (buildDistPref flags)
42 let dbStack = withPackageDB lbi ++ [ SpecificPackageDB $ distPref </> "package.conf.inplace" ]
43 let dbFlags = "-hide-all-packages" : packageDbArgs dbStack
45 withLibLBI pkg lbi $ \lib libcfg -> do
46 let libBI = libBuildInfo lib
48 -- Lib sources and includes
49 let iArgs = map ("-i"++) $ hsSourceDirs libBI
50 let includeArgs = map ("-I"++) $ includeDirs libBI
52 -- We need the directory with library's cabal_macros.h!
53#if MIN_VERSION_Cabal(1,25,0)
54 let libAutogenDir = autogenComponentModulesDir lbi libcfg
56 let libAutogenDir = autogenModulesDir lbi
59 -- CPP includes, i.e. include cabal_macros.h
60 let cppFlags = [ "-optP-include", "-optP" ++ libAutogenDir ++ "/cabal_macros.h" ]
62 -- Actually we need to check whether testName suite == "doctests"
63 -- pending https://github.com/haskell/cabal/pull/4229 getting into GHC HEAD tree
64 withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == testName suite) $ do
66 -- get and create autogen dir
67#if MIN_VERSION_Cabal(1,25,0)
68 let testAutogenDir = autogenComponentModulesDir lbi suitecfg
70 let testAutogenDir = autogenModulesDir lbi
72 createDirectoryIfMissingVerbose verbosity True testAutogenDir
74 -- write autogen'd file
75 rewriteFile (testAutogenDir </> "Build_doctests.hs") $ unlines
76 [ "module Build_doctests where"
78 -- -package-id etc. flags
80 , "pkgs = " ++ (show $ formatDeps $ testDeps libcfg suitecfg)
83 , "flags = " ++ show (iArgs ++ includeArgs ++ dbFlags ++ cppFlags)
85 , "src_dirs :: [String]"
86 , "src_dirs = " ++ show (hsSourceDirs libBI)
89 -- we do this check in Setup, as then doctests don't need to depend on Cabal
90 isOldCompiler = maybe False id $ do
91 a <- simpleParse $ showCompilerId $ compiler lbi
92 b <- simpleParse "7.5"
93 return $ packageVersion (a :: PackageId) < b
95 formatDeps = map formatOne
96 formatOne (installedPkgId, pkgId)
97 -- The problem is how different cabal executables handle package databases
98 -- when doctests depend on the library
99 | packageId pkg == pkgId = "-package=" ++ display pkgId
100 | otherwise = "-package-id=" ++ display installedPkgId
102 -- From Distribution.Simple.Program.GHC
103 packageDbArgs :: [PackageDB] -> [String]
104 packageDbArgs | isOldCompiler = packageDbArgsConf
105 | otherwise = packageDbArgsDb
107 -- GHC <7.6 uses '-package-conf' instead of '-package-db'.
108 packageDbArgsConf :: [PackageDB] -> [String]
109 packageDbArgsConf dbstack = case dbstack of
110 (GlobalPackageDB:UserPackageDB:dbs) -> concatMap specific dbs
111 (GlobalPackageDB:dbs) -> ("-no-user-package-conf")
112 : concatMap specific dbs
115 specific (SpecificPackageDB db) = [ "-package-conf=" ++ db ]
117 ierror = error $ "internal error: unexpected package db stack: "
120 -- GHC >= 7.6 uses the '-package-db' flag. See
121 -- https://ghc.haskell.org/trac/ghc/ticket/5977.
122 packageDbArgsDb :: [PackageDB] -> [String]
123 -- special cases to make arguments prettier in common scenarios
124 packageDbArgsDb dbstack = case dbstack of
125 (GlobalPackageDB:UserPackageDB:dbs)
126 | all isSpecific dbs -> concatMap single dbs
127 (GlobalPackageDB:dbs)
128 | all isSpecific dbs -> "-no-user-package-db"
129 : concatMap single dbs
130 dbs -> "-clear-package-db"
131 : concatMap single dbs
133 single (SpecificPackageDB db) = [ "-package-db=" ++ db ]
134 single GlobalPackageDB = [ "-global-package-db" ]
135 single UserPackageDB = [ "-user-package-db" ]
136 isSpecific (SpecificPackageDB _) = True
139testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]
140testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list