Implement buildPackageDependencies rule.
[hadrian.git] / src / Settings / GhcM.hs
1 module Settings.GhcM (ghcMArgs) where
2
3 import Way
4 import Util
5 import Stage
6 import Builder
7 import Package
8 import Switches
9 import Expression
10 import Oracles.Flag
11 import Oracles.PackageData
12 import Settings.Util
13 import Settings.Ways
14 import Settings.TargetDirectory
15 import Development.Shake
16
17 ghcMArgs :: Args
18 ghcMArgs = do
19 stage <- getStage
20 builder (GhcM stage) ? do
21 pkg <- getPackage
22 cppArgs <- getPkgDataList CppArgs
23 hsArgs <- getPkgDataList HsArgs
24 hsSrcs <- getHsSources
25 ways <- getWays
26 let buildPath = targetPath stage pkg -/- "build"
27 mconcat
28 [ arg "-M"
29 , packageGhcArgs
30 , includeGhcArgs
31 , append . map ("-optP" ++) $ cppArgs
32 , arg "-odir" , arg buildPath
33 , arg "-stubdir" , arg buildPath
34 , arg "-hidir" , arg buildPath
35 , arg "-dep-makefile", arg $ buildPath -/- "haskell.deps"
36 , append . concatMap (\way -> ["-dep-suffix", wayPrefix way]) $ ways
37 , append hsArgs
38 , append hsSrcs ]
39
40 packageGhcArgs :: Args
41 packageGhcArgs = do
42 stage <- getStage
43 supportsPackageKey <- getFlag SupportsPackageKey
44 pkgKey <- getPkgData PackageKey
45 pkgDepKeys <- getPkgDataList DepKeys
46 pkgDeps <- getPkgDataList Deps
47 mconcat
48 [ arg "-hide-all-packages"
49 , arg "-no-user-package-db"
50 , arg "-include-pkg-deps"
51 , stage0 ? arg "-package-db libraries/bootstrapping.conf"
52 , if supportsPackageKey || stage /= Stage0
53 then mconcat [ arg $ "-this-package-key " ++ pkgKey
54 , append . map ("-package-key " ++) $ pkgDepKeys ]
55 else mconcat [ arg $ "-package-name" ++ pkgKey
56 , append . map ("-package " ++) $ pkgDeps ]]
57
58 includeGhcArgs :: Args
59 includeGhcArgs = do
60 stage <- getStage
61 pkg <- getPackage
62 srcDirs <- getPkgDataList SrcDirs
63 incDirs <- getPkgDataList IncludeDirs
64 let buildPath = targetPath stage pkg -/- "build"
65 autogenPath = buildPath -/- "autogen"
66 mconcat
67 [ arg "-i"
68 , append . map (\dir -> "-i" ++ pkgPath pkg -/- dir) $ srcDirs
69 , arg $ "-i" ++ buildPath
70 , arg $ "-i" ++ autogenPath
71 , arg $ "-I" ++ buildPath
72 , arg $ "-I" ++ autogenPath
73 , append . map (\dir -> "-I" ++ pkgPath pkg -/- dir) $ incDirs
74 , arg "-optP-include" -- TODO: Shall we also add -cpp?
75 , arg $ "-optP" ++ autogenPath -/- "cabal_macros.h" ]
76
77 getHsSources :: Expr [FilePath]
78 getHsSources = do
79 stage <- getStage
80 pkg <- getPackage
81 srcDirs <- getPkgDataList SrcDirs
82 let autogen = targetPath stage pkg -/- "build/autogen"
83 paths = autogen : map (pkgPath pkg -/-) srcDirs
84 getSourceFiles paths [".hs", ".lhs"]
85
86 -- Find all source files in specified paths and with given extensions
87 getSourceFiles :: [FilePath] -> [String] -> Expr [FilePath]
88 getSourceFiles paths exts = do
89 modules <- getPkgDataList Modules
90 let modPaths = map (replaceEq '.' '/') modules
91 candidates = [ p -/- m ++ e | p <- paths, m <- modPaths, e <- exts ]
92 files <- lift $ filterM (doesDirectoryExist . takeDirectory) candidates
93 result <- lift $ getDirectoryFiles "" files
94 return $ map unifyPath result