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