Drop redundant 'build' component in build paths, rename contextPath to buildPath.
[hadrian.git] / src / Oracles / ModuleFiles.hs
1 {-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
2 module Oracles.ModuleFiles (
3 findGenerator, haskellSources, moduleFilesOracle
4 ) where
5
6 import qualified Data.HashMap.Strict as Map
7
8 import Base
9 import Context
10 import Expression
11 import Oracles.PackageData
12 import Settings.Paths
13
14 newtype ModuleFilesKey = ModuleFilesKey (Stage, Package)
15 deriving (Show, Typeable, Eq, Hashable, Binary, NFData)
16
17 newtype Generator = Generator (Stage, Package, FilePath)
18 deriving (Show, Typeable, Eq, Hashable, Binary, NFData)
19
20 -- The following generators and corresponding source extensions are supported:
21 determineBuilder :: FilePath -> Maybe Builder
22 determineBuilder file = case takeExtension file of
23 ".x" -> Just Alex
24 ".y" -> Just Happy
25 ".ly" -> Just Happy
26 ".hsc" -> Just Hsc2Hs
27 _ -> Nothing
28
29 -- | Find the generator for a given 'Context' and a source file. For example:
30 -- findGenerator (Context Stage1 compiler vanilla)
31 -- ".build/stage1/compiler/build/Lexer.hs"
32 -- == Just ("compiler/parser/Lexer.x", Alex)
33 -- findGenerator (Context Stage1 base vanilla)
34 -- ".build/stage1/base/build/Prelude.hs"
35 -- == Nothing
36 findGenerator :: Context -> FilePath -> Action (Maybe (FilePath, Builder))
37 findGenerator Context {..} file = do
38 maybeSource <- askOracle $ Generator (stage, package, file)
39 return $ do
40 source <- maybeSource
41 builder <- determineBuilder source
42 return (source, builder)
43
44 -- | Find all Haskell source files for a given 'Context'.
45 haskellSources :: Context -> Action [FilePath]
46 haskellSources context = do
47 let autogen = buildPath context -/- "autogen"
48 -- Generated source files live in buildPath and have extension "hs", except
49 -- for GHC/Prim.hs that lives in autogen. TODO: fix the inconsistency?
50 let modFile ("GHC.Prim", _) = autogen -/- "GHC/Prim.hs"
51 modFile (m, Nothing ) = generatedFile context m
52 modFile (m, Just file )
53 | takeExtension file `elem` [".hs", ".lhs"] = file
54 | otherwise = generatedFile context m
55 map modFile <$> contextFiles context
56
57 generatedFile :: Context -> String -> FilePath
58 generatedFile context moduleName =
59 buildPath context -/- replaceEq '.' '/' moduleName <.> "hs"
60
61 contextFiles :: Context -> Action [(String, Maybe FilePath)]
62 contextFiles context@Context {..} = do
63 modules <- fmap sort . pkgDataList . Modules $ buildPath context
64 zip modules <$> askOracle (ModuleFilesKey (stage, package))
65
66 -- | This is an important oracle whose role is to find and cache module source
67 -- files. It takes a 'Stage' and a 'Package', looks up corresponding source
68 -- directories @dirs@ and a sorted list of module names @modules@, and for each
69 -- module, e.g. @A.B.C@, returns a 'FilePath' of the form @dir/A/B/C.extension@,
70 -- such that @dir@ belongs to @dirs@, and file @dir/A/B/C.extension@ exists, or
71 -- 'Nothing' if there is no such file. If more than one matching file is found
72 -- an error is raised. For example, for 'Stage1' and 'compiler', @dirs@ will
73 -- contain ["compiler/codeGen", "compiler/parser"], and @modules@ will contain
74 -- ["CodeGen.Platform.ARM", "Config", "Lexer"]; the oracle will produce a list
75 -- containing [Just "compiler/codeGen/CodeGen/Platform/ARM.hs", Nothing,
76 -- Just "compiler/parser/Lexer.x"]. The oracle ignores @.(l)hs-boot@ files.
77 moduleFilesOracle :: Rules ()
78 moduleFilesOracle = void $ do
79 void $ addOracle $ \(ModuleFilesKey (stage, package)) -> do
80 let path = buildPath $ vanillaContext stage package
81 srcDirs <- pkgDataList $ SrcDirs path
82 modules <- fmap sort . pkgDataList $ Modules path
83 let dirs = (path -/- "autogen") : map (pkgPath package -/-) srcDirs
84 modDirFiles = groupSort $ map decodeModule modules
85 result <- fmap concat . forM dirs $ \dir -> do
86 todo <- filterM (doesDirectoryExist . (dir -/-) . fst) modDirFiles
87 forM todo $ \(mDir, mFiles) -> do
88 let fullDir = dir -/- mDir
89 files <- getDirectoryFiles fullDir ["*"]
90 let noBoot = filter (not . (isSuffixOf "-boot")) files
91 cmp fe f = compare (dropExtension fe) f
92 found = intersectOrd cmp noBoot mFiles
93 return (map (fullDir -/-) found, mDir)
94 let pairs = sort [ (encodeModule d f, f) | (fs, d) <- result, f <- fs ]
95 multi = [ (m, f1, f2) | (m, f1):(n, f2):_ <- tails pairs, m == n ]
96 unless (null multi) $ do
97 let (m, f1, f2) = head multi
98 putError $ "Module " ++ m ++ " has more than one source file: "
99 ++ f1 ++ " and " ++ f2 ++ "."
100 return $ lookupAll modules pairs
101
102 -- Optimisation: we discard .(l)hs files here, because they are never used
103 -- as generators, and hence would be discarded in 'findGenerator' anyway.
104 generators <- newCache $ \(stage, package) -> do
105 let context = vanillaContext stage package
106 files <- contextFiles context
107 return $ Map.fromList [ (generatedFile context modName, src)
108 | (modName, Just src) <- files
109 , takeExtension src `notElem` [".hs", ".lhs"] ]
110
111 addOracle $ \(Generator (stage, package, file)) ->
112 Map.lookup file <$> generators (stage, package)