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