Drop DeriveDataTypeable extension
[hadrian.git] / src / Oracles / ModuleFiles.hs
1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 module Oracles.ModuleFiles (
3 decodeModule, encodeModule, 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 -- | Given a module name extract the directory and file name, e.g.:
30 --
31 -- > decodeModule "Data.Functor.Identity" == ("Data/Functor", "Identity")
32 -- > decodeModule "Prelude" == ("", "Prelude")
33 decodeModule :: String -> (FilePath, String)
34 decodeModule modName = (intercalate "/" (init xs), last xs)
35 where
36 xs = words $ replaceEq '.' ' ' modName
37
38 -- | Given the directory and file name find the corresponding module name, e.g.:
39 --
40 -- > encodeModule "Data/Functor" "Identity.hs" == "Data.Functor.Identity"
41 -- > encodeModule "" "Prelude" == "Prelude"
42 -- > uncurry encodeModule (decodeModule name) == name
43 encodeModule :: FilePath -> String -> String
44 encodeModule dir file
45 | dir == "" = takeBaseName file
46 | otherwise = replaceEq '/' '.' dir ++ '.' : takeBaseName file
47
48 -- | Find the generator for a given 'Context' and a source file. For example:
49 -- findGenerator (Context Stage1 compiler vanilla)
50 -- "_build/stage1/compiler/build/Lexer.hs"
51 -- == Just ("compiler/parser/Lexer.x", Alex)
52 -- findGenerator (Context Stage1 base vanilla)
53 -- "_build/stage1/base/build/Prelude.hs"
54 -- == Nothing
55 findGenerator :: Context -> FilePath -> Action (Maybe (FilePath, Builder))
56 findGenerator Context {..} file = do
57 maybeSource <- askOracle $ Generator (stage, package, file)
58 return $ do
59 source <- maybeSource
60 builder <- determineBuilder source
61 return (source, builder)
62
63 -- | Find all Haskell source files for a given 'Context'.
64 haskellSources :: Context -> Action [FilePath]
65 haskellSources context = do
66 let autogen = buildPath context -/- "autogen"
67 -- Generated source files live in buildPath and have extension "hs", except
68 -- for GHC/Prim.hs that lives in autogen. TODO: fix the inconsistency?
69 let modFile ("GHC.Prim", _) = autogen -/- "GHC/Prim.hs"
70 modFile (m, Nothing ) = generatedFile context m
71 modFile (m, Just file )
72 | takeExtension file `elem` [".hs", ".lhs"] = file
73 | otherwise = generatedFile context m
74 map modFile <$> contextFiles context
75
76 generatedFile :: Context -> String -> FilePath
77 generatedFile context moduleName =
78 buildPath context -/- replaceEq '.' '/' moduleName <.> "hs"
79
80 contextFiles :: Context -> Action [(String, Maybe FilePath)]
81 contextFiles context@Context {..} = do
82 modules <- fmap sort . pkgDataList . Modules $ buildPath context
83 zip modules <$> askOracle (ModuleFilesKey (stage, package))
84
85 -- | This is an important oracle whose role is to find and cache module source
86 -- files. It takes a 'Stage' and a 'Package', looks up corresponding source
87 -- directories @dirs@ and a sorted list of module names @modules@, and for each
88 -- module, e.g. @A.B.C@, returns a 'FilePath' of the form @dir/A/B/C.extension@,
89 -- such that @dir@ belongs to @dirs@, and file @dir/A/B/C.extension@ exists, or
90 -- 'Nothing' if there is no such file. If more than one matching file is found
91 -- an error is raised. For example, for 'Stage1' and 'compiler', @dirs@ will
92 -- contain ["compiler/codeGen", "compiler/parser"], and @modules@ will contain
93 -- ["CodeGen.Platform.ARM", "Config", "Lexer"]; the oracle will produce a list
94 -- containing [Just "compiler/codeGen/CodeGen/Platform/ARM.hs", Nothing,
95 -- Just "compiler/parser/Lexer.x"]. The oracle ignores @.(l)hs-boot@ files.
96 moduleFilesOracle :: Rules ()
97 moduleFilesOracle = void $ do
98 void $ addOracle $ \(ModuleFilesKey (stage, package)) -> do
99 let path = buildPath $ vanillaContext stage package
100 srcDirs <- pkgDataList $ SrcDirs path
101 modules <- fmap sort . pkgDataList $ Modules path
102 let dirs = (path -/- "autogen") : map (pkgPath package -/-) srcDirs
103 modDirFiles = groupSort $ map decodeModule modules
104 result <- fmap concat . forM dirs $ \dir -> do
105 todo <- filterM (doesDirectoryExist . (dir -/-) . fst) modDirFiles
106 forM todo $ \(mDir, mFiles) -> do
107 let fullDir = unifyPath $ dir -/- mDir
108 files <- getDirectoryFiles fullDir ["*"]
109 let noBoot = filter (not . (isSuffixOf "-boot")) files
110 cmp fe f = compare (dropExtension fe) f
111 found = intersectOrd cmp noBoot mFiles
112 return (map (fullDir -/-) found, mDir)
113 let pairs = sort [ (encodeModule d f, f) | (fs, d) <- result, f <- fs ]
114 multi = [ (m, f1, f2) | (m, f1):(n, f2):_ <- tails pairs, m == n ]
115 unless (null multi) $ do
116 let (m, f1, f2) = head multi
117 putError $ "Module " ++ m ++ " has more than one source file: "
118 ++ f1 ++ " and " ++ f2 ++ "."
119 return $ lookupAll modules pairs
120
121 -- Optimisation: we discard .(l)hs files here, because they are never used
122 -- as generators, and hence would be discarded in 'findGenerator' anyway.
123 generators <- newCache $ \(stage, package) -> do
124 let context = vanillaContext stage package
125 files <- contextFiles context
126 return $ Map.fromList [ (generatedFile context modName, src)
127 | (modName, Just src) <- files
128 , takeExtension src `notElem` [".hs", ".lhs"] ]
129
130 addOracle $ \(Generator (stage, package, file)) ->
131 Map.lookup file <$> generators (stage, package)