-maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
-maybeHomeModule mod_name = do
- home_cache <- readIORef v_HomeDirCache
-
- home_map <-
- case home_cache of
- Nothing -> do
- -- populate the home dir cache, using the import path (the import
- -- path is changed by -i flags on the command line, and defaults
- -- to ["."]).
- home_imports <- readIORef v_Import_paths
- let extendFM fm path = do
- contents <- getDirectoryContents' path
- let clean_contents = filter isUsefulFile contents
- return (addListToFM fm (zip clean_contents (repeat path)))
- home_map <- foldM extendFM emptyFM home_imports
- writeIORef v_HomeDirCache (Just home_map)
- return home_map
-
- Just home_map -> return home_map
-
- let basename = moduleNameUserString mod_name
- hs = basename ++ ".hs"
- lhs = basename ++ ".lhs"
-
- case lookupFM home_map hs of {
- -- special case to avoid getting "./foo.hs" all the time
- Just "." -> mkHomeModuleLocn mod_name basename hs;
- Just path -> mkHomeModuleLocn mod_name
- (path ++ '/':basename) (path ++ '/':hs);
- Nothing ->
-
- case lookupFM home_map lhs of {
- -- special case to avoid getting "./foo.hs" all the time
- Just "." -> mkHomeModuleLocn mod_name basename lhs;
- Just path -> mkHomeModuleLocn mod_name
- (path ++ '/':basename) (path ++ '/':lhs);
- Nothing -> do
-
- -- can't find a source file anywhere, check for a lone .hi file.
- hisuf <- readIORef v_Hi_suf
- let hi = basename ++ '.':hisuf
- case lookupFM home_map hi of {
- Just path -> mkHomeModuleLocn mod_name
- (path ++ '/':basename) (path ++ '/':hs);
- Nothing -> do
-
- -- last chance: .hi-boot-<ver> and .hi-boot
- let hi_boot = basename ++ ".hi-boot"
- let hi_boot_ver = basename ++ ".hi-boot-" ++ cHscIfaceFileVersion
- case lookupFM home_map hi_boot_ver of {
- Just path -> mkHomeModuleLocn mod_name
- (path ++ '/':basename) (path ++ '/':hs);
- Nothing -> do
- case lookupFM home_map hi_boot of {
- Just path -> mkHomeModuleLocn mod_name
- (path ++ '/':basename) (path ++ '/':hs);
- Nothing -> return Nothing
- }}}}}
-
+maybeHomeModule :: ModuleName -> Bool -> IO (Maybe (Module, ModuleLocation))
+maybeHomeModule mod_name is_source = do
+ home_path <- readIORef v_Import_paths
+ hisuf <- readIORef v_Hi_suf
+
+ let mod_str = moduleNameUserString mod_name
+ basename = map (\c -> if c == '.' then '/' else c) mod_str
+
+ std_exts =
+ [ ("hs", \ _ fName path -> mkHomeModuleLocn mod_name path fName)
+ , ("lhs", \ _ fName path -> mkHomeModuleLocn mod_name path fName)
+ , (hisuf, \ _ fName path -> mkHiOnlyModuleLocn mod_name fName)
+ ]
+ -- look for the .hi file last, because if there's a source file about
+ -- we want to find it.
+
+ -- last chance: .hi-boot-<ver> and .hi-boot
+ hi_boot_ver = "hi-boot-" ++ cHscIfaceFileVersion
+
+ boot_exts =
+ [ (hi_boot_ver, \ _ fName path -> mkHiOnlyModuleLocn mod_name fName)
+ , ("hi-boot", \ _ fName path -> mkHiOnlyModuleLocn mod_name fName)
+ ]
+
+ searchPathExts
+ (map ((,) undefined) home_path)
+ basename
+ (if is_source then (boot_exts++std_exts) else std_exts ++ boot_exts)
+ -- for SOURCE imports, check the hi-boot extensions
+ -- before the source/iface ones, to avoid
+ -- creating circ Makefile deps.
+
+mkHiOnlyModuleLocn mod_name hi_file =
+ return
+ ( mkHomeModule mod_name
+ , ModuleLocation{ ml_hspp_file = Nothing
+ , ml_hs_file = Nothing
+ , ml_hi_file = hi_file
+ , ml_obj_file = Nothing
+ }
+ )