-
- let basename = moduleNameUserString mod_name
- hi = basename ++ '.':package_hisuf
-
- case lookupFM pkg_cache hi of
- Nothing -> return Nothing
- Just (pkg_name,path) ->
- return (Just (mkModule mod_name pkg_name,
- ModuleLocation{
- ml_hspp_file = Nothing,
- ml_hs_file = Nothing,
- ml_hi_file = Just (path ++ '/':hi),
- ml_obj_file = Nothing
- }
- ))
-
-isUsefulFile fn
- = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
- in suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]
-
-getDirectoryContents' d
- = IO.catch (getDirectoryContents d)
- (\_ -> do hPutStr stderr
- ("WARNING: error while reading directory " ++ d)
- return []
- )
-
+ let imp_dirs = concatMap (\ pkg -> map ((,) pkg) (import_dirs pkg)) pkgs
+ mod_str = moduleNameUserString mod_name
+ basename = map (\c -> if c == '.' then '/' else c) mod_str
+
+ mkPackageModule mod_name pkg mbFName path =
+ return ( mkModule mod_name (mkFastString (name pkg))
+ , ModuleLocation{ ml_hspp_file = Nothing
+ , ml_hs_file = mbFName
+ , ml_hi_file = path ++ '.':package_hisuf
+ , ml_obj_file = Nothing
+ })
+
+ searchPathExts
+ imp_dirs basename
+ ((package_hisuf,\ pkg fName path -> mkPackageModule mod_name pkg Nothing path) :
+ -- can packages contain hi-boots?
+ (if hiOnly then [] else
+ [ ("hs", \ pkg fName path -> mkPackageModule mod_name pkg (Just fName) path)
+ , ("lhs", \ pkg fName path -> mkPackageModule mod_name pkg (Just fName) path)
+ ]))
+ where
+
+findPackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
+findPackageModule mod_name = findPackageMod mod_name True
+
+searchPathExts :: [(a, FilePath)]
+ -> String
+ -> [(String, a -> FilePath -> String -> IO (Module, ModuleLocation))]
+ -> IO (Maybe (Module, ModuleLocation))
+searchPathExts path basename exts = search exts
+ where
+ search [] = return Nothing
+ search ((x,f):xs) = do
+ let fName = (basename ++ '.':x)
+ found <- findOnPath path fName
+ case found of
+ -- special case to avoid getting "./foo.<ext>" all the time
+ Just (v,".") -> fmap Just (f v fName basename)
+ Just (v,path) -> fmap Just (f v (path ++ '/':fName)
+ (path ++ '/':basename))
+ Nothing -> search xs
+
+findOnPath :: [(a,String)] -> String -> IO (Maybe (a, FilePath))
+findOnPath path s = loop path
+ where
+ loop [] = return Nothing
+ loop ((a,d):ds) = do
+ let file = d ++ '/':s
+ b <- doesFileExist file
+ if b then return (Just (a,d)) else loop ds