Remove an unnecessary hiding after disabling name shadow warning
[hadrian.git] / src / Rules / Library.hs
1 module Rules.Library (buildPackageLibrary, cSources, hSources) where
2
3 import Data.Char
4
5 import Base hiding (splitPath)
6 import Expression
7 import GHC
8 import Oracles
9 import Rules.Actions
10 import Rules.IntegerGmp
11 import Rules.Resources
12 import Settings
13 import qualified System.Directory as IO
14
15 buildPackageLibrary :: Resources -> PartialTarget -> Rules ()
16 buildPackageLibrary _ target @ (PartialTarget stage pkg) = do
17 let buildPath = targetPath stage pkg -/- "build"
18
19 -- TODO: handle dynamic libraries
20 matchBuildResult buildPath "a" ?> \a -> do
21
22 removeFileIfExists a
23 cSrcs <- cSources target
24 hSrcs <- hSources target
25
26 -- TODO: simplify handling of AutoApply.cmm
27 let way = detectWay a -- TODO: eliminate differences below
28 cObjs = [ buildPath -/- src -<.> osuf way | src <- cSrcs
29 , not ("//AutoApply.cmm" ?== src) ]
30 ++ [ src -<.> osuf way | src <- cSrcs, "//AutoApply.cmm" ?== src ]
31 hObjs = [ buildPath -/- src <.> osuf way | src <- hSrcs ]
32
33 -- This will create split objects if required (we don't track them
34 -- explicitly as this would needlessly bloat the Shake database).
35 need $ cObjs ++ hObjs
36
37 split <- interpretPartial target splitObjects
38 splitObjs <- if not split then return hObjs else -- TODO: make clearer!
39 fmap concat $ forM hSrcs $ \src -> do
40 let splitPath = buildPath -/- src ++ "_" ++ osuf way ++ "_split"
41 contents <- liftIO $ IO.getDirectoryContents splitPath
42 return . map (splitPath -/-)
43 . filter (not . all (== '.')) $ contents
44
45 eObjs <- extraObjects target
46 let objs = cObjs ++ splitObjs ++ eObjs
47
48 asuf <- libsuf way
49 let isLib0 = ("//*-0" <.> asuf) ?== a
50 if isLib0
51 then build $ fullTarget target Ar [] [a] -- TODO: scan for dlls
52 else build $ fullTarget target Ar objs [a]
53
54 synopsis <- interpretPartial target $ getPkgData Synopsis
55 unless isLib0 . putSuccess $ renderBox
56 [ "Successfully built library '"
57 ++ pkgNameString pkg
58 ++ "' (" ++ show stage ++ ", way "++ show way ++ ")."
59 , "Library synopsis: " ++ dropWhileEnd isPunctuation synopsis ++ "." ]
60
61 -- TODO: simplify handling of AutoApply.cmm
62 -- TODO: this looks fragile as haskell objects can match this rule if their
63 -- names start with "HS" and they are on top of the module hierarchy.
64 -- This happens with hsc2hs, which has top-level file HSCParser.hs.
65 when (pkg /= hsc2hs) $ priority 2 $ (buildPath -/- "HS*.o") %> \obj -> do
66 cSrcs <- cSources target
67 hSrcs <- hSources target
68 let cObjs = [ buildPath -/- src -<.> "o" | src <- cSrcs
69 , not ("//AutoApply.cmm" ?== src) ]
70 ++ [ src -<.> "o" | src <- cSrcs, "//AutoApply.cmm" ?== src ]
71 hObjs = [ buildPath -/- src <.> "o" | src <- hSrcs ]
72 need $ cObjs ++ hObjs
73 build $ fullTarget target Ld (cObjs ++ hObjs) [obj]
74
75 cSources :: PartialTarget -> Action [FilePath]
76 cSources target = interpretPartial target $ getPkgDataList CSrcs
77
78 hSources :: PartialTarget -> Action [FilePath]
79 hSources target = do
80 modules <- interpretPartial target $ getPkgDataList Modules
81 -- GHC.Prim is special: we do not build it
82 return . map (replaceEq '.' '/') . filter (/= "GHC.Prim") $ modules
83
84 extraObjects :: PartialTarget -> Action [FilePath]
85 extraObjects (PartialTarget _ pkg)
86 | pkg == integerGmp = do
87 need [integerGmpLibraryH]
88 getDirectoryFiles "" [integerGmpObjects -/- "*.o"]
89 | otherwise = return []