Minor clean up, taking hlint suggestions
[ghc.git] / src / Oracles / ModuleFiles.hs
1 {-# LANGUAGE TypeFamilies #-}
2 module Oracles.ModuleFiles (
3 decodeModule, encodeModule, findGenerator, hsSources, hsObjects, moduleFilesOracle
4 ) where
5
6 import qualified Data.HashMap.Strict as Map
7
8 import Base
9 import Builder
10 import Context
11 import GHC
12 import Oracles.PackageData
13
14 newtype ModuleFiles = ModuleFiles (Stage, Package)
15 deriving (Binary, Eq, Hashable, NFData, Show, Typeable)
16 type instance RuleResult ModuleFiles = [Maybe FilePath]
17
18 newtype Generator = Generator (Stage, Package, FilePath)
19 deriving (Binary, Eq, Hashable, NFData, Show, Typeable)
20 type instance RuleResult Generator = Maybe FilePath
21
22 -- | We scan for the following Haskell source extensions when looking for module
23 -- files. Note, we do not list "*.(l)hs-boot" files here, as they can never
24 -- appear by themselves and always have accompanying "*.(l)hs" master files.
25 haskellExtensions :: [String]
26 haskellExtensions = [".hs", ".lhs"]
27
28 -- | Non-Haskell source extensions and corresponding builders.
29 otherExtensions :: [(String, Builder)]
30 otherExtensions = [ (".x" , Alex )
31 , (".y" , Happy )
32 , (".ly" , Happy )
33 , (".hsc", Hsc2Hs) ]
34
35 -- | We match the following file patterns when looking for module files.
36 moduleFilePatterns :: [FilePattern]
37 moduleFilePatterns = map ("*" ++) $ haskellExtensions ++ map fst otherExtensions
38
39 -- | Given a FilePath determine the corresponding builder.
40 determineBuilder :: FilePath -> Maybe Builder
41 determineBuilder file = lookup (takeExtension file) otherExtensions
42
43 -- | Given a module name extract the directory and file name, e.g.:
44 --
45 -- > decodeModule "Data.Functor.Identity" == ("Data/Functor", "Identity")
46 -- > decodeModule "Prelude" == ("", "Prelude")
47 decodeModule :: String -> (FilePath, String)
48 decodeModule modName = (intercalate "/" (init xs), last xs)
49 where
50 xs = words $ replaceEq '.' ' ' modName
51
52 -- | Given the directory and file name find the corresponding module name, e.g.:
53 --
54 -- > encodeModule "Data/Functor" "Identity.hs" == "Data.Functor.Identity"
55 -- > encodeModule "" "Prelude" == "Prelude"
56 -- > uncurry encodeModule (decodeModule name) == name
57 encodeModule :: FilePath -> String -> String
58 encodeModule dir file
59 | dir == "" = takeBaseName file
60 | otherwise = replaceEq '/' '.' dir ++ '.' : takeBaseName file
61
62 -- | Find the generator for a given 'Context' and a source file. For example:
63 -- findGenerator (Context Stage1 compiler vanilla)
64 -- "_build/stage1/compiler/build/Lexer.hs"
65 -- == Just ("compiler/parser/Lexer.x", Alex)
66 -- findGenerator (Context Stage1 base vanilla)
67 -- "_build/stage1/base/build/Prelude.hs"
68 -- == Nothing
69 findGenerator :: Context -> FilePath -> Action (Maybe (FilePath, Builder))
70 findGenerator Context {..} file = do
71 maybeSource <- askOracle $ Generator (stage, package, file)
72 return $ do
73 source <- maybeSource
74 builder <- determineBuilder source
75 return (source, builder)
76
77 -- | Find all Haskell source files for a given 'Context'.
78 hsSources :: Context -> Action [FilePath]
79 hsSources context = do
80 let modFile (m, Nothing ) = generatedFile context m
81 modFile (m, Just file )
82 | takeExtension file `elem` haskellExtensions = return file
83 | otherwise = generatedFile context m
84 mapM modFile =<< contextFiles context
85
86 -- | Find all Haskell object files for a given 'Context'. Note: this is a much
87 -- simpler function compared to 'hsSources', because all object files live in
88 -- the build directory regardless of whether they are generated or not.
89 hsObjects :: Context -> Action [FilePath]
90 hsObjects context = do
91 path <- buildPath context
92 modules <- pkgDataList (Modules path)
93 -- GHC.Prim module is only for documentation, we do not actually build it.
94 mapM (objectPath context . moduleSource) (filter (/= "GHC.Prim") modules)
95
96 -- | Generated module files live in the 'Context' specific build directory.
97 generatedFile :: Context -> String -> Action FilePath
98 generatedFile context moduleName = do
99 path <- buildPath context
100 return $ path -/- moduleSource moduleName
101
102 moduleSource :: String -> FilePath
103 moduleSource moduleName = replaceEq '.' '/' moduleName <.> "hs"
104
105 -- | Module files for a given 'Context'.
106 contextFiles :: Context -> Action [(String, Maybe FilePath)]
107 contextFiles context@Context {..} = do
108 path <- buildPath context
109 modules <- fmap sort . pkgDataList $ Modules path
110 zip modules <$> askOracle (ModuleFiles (stage, package))
111
112 -- | This is an important oracle whose role is to find and cache module source
113 -- files. It takes a 'Stage' and a 'Package', looks up corresponding source
114 -- directories @dirs@ and a sorted list of module names @modules@, and for each
115 -- module, e.g. @A.B.C@, returns a 'FilePath' of the form @dir/A/B/C.extension@,
116 -- such that @dir@ belongs to @dirs@, and file @dir/A/B/C.extension@ exists, or
117 -- 'Nothing' if there is no such file. If more than one matching file is found
118 -- an error is raised. For example, for 'Stage1' and 'compiler', @dirs@ will
119 -- contain ["compiler/codeGen", "compiler/parser"], and @modules@ will contain
120 -- ["CodeGen.Platform.ARM", "Config", "Lexer"]; the oracle will produce a list
121 -- containing [Just "compiler/codeGen/CodeGen/Platform/ARM.hs", Nothing,
122 -- Just "compiler/parser/Lexer.x"]. The oracle ignores @.(l)hs-boot@ files.
123 moduleFilesOracle :: Rules ()
124 moduleFilesOracle = void $ do
125 void . addOracle $ \(ModuleFiles (stage, package)) -> do
126 let context = vanillaContext stage package
127 path <- buildPath context
128 srcDirs <- pkgDataList $ SrcDirs path
129 modules <- fmap sort . pkgDataList $ Modules path
130 autogen <- autogenPath context
131 let dirs = autogen : map (pkgPath package -/-) srcDirs
132 modDirFiles = groupSort $ map decodeModule modules
133 result <- concatForM dirs $ \dir -> do
134 todo <- filterM (doesDirectoryExist . (dir -/-) . fst) modDirFiles
135 forM todo $ \(mDir, mFiles) -> do
136 let fullDir = unifyPath $ dir -/- mDir
137 files <- getDirectoryFiles fullDir moduleFilePatterns
138 let cmp f = compare (dropExtension f)
139 found = intersectOrd cmp files mFiles
140 return (map (fullDir -/-) found, mDir)
141 let pairs = sort [ (encodeModule d f, f) | (fs, d) <- result, f <- fs ]
142 multi = [ (m, f1, f2) | (m, f1):(n, f2):_ <- tails pairs, m == n ]
143 unless (null multi) $ do
144 let (m, f1, f2) = head multi
145 error $ "Module " ++ m ++ " has more than one source file: "
146 ++ f1 ++ " and " ++ f2 ++ "."
147 return $ lookupAll modules pairs
148
149 -- Optimisation: we discard Haskell files here, because they are never used
150 -- as generators, and hence would be discarded in 'findGenerator' anyway.
151 generators <- newCache $ \(stage, package) -> do
152 let context = vanillaContext stage package
153 files <- contextFiles context
154 list <- sequence [ (,src) <$> generatedFile context modName
155 | (modName, Just src) <- files
156 , takeExtension src `notElem` haskellExtensions ]
157 return $ Map.fromList list
158
159 addOracle $ \(Generator (stage, package, file)) ->
160 Map.lookup file <$> generators (stage, package)