-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 {
- Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename)
- (path ++ '/':hs);
- Nothing ->
-
- case lookupFM home_map lhs of {
- 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
- }}}}}
-
-mkHomeModuleLocn mod_name basename source_fn = do
-
- -- figure out the .hi file name: it lives in the same dir as the
- -- source, unless there's a -ohi flag on the command line.
- ohi <- readIORef v_Output_hi
+maybeHomeModule :: ModuleName -> Bool -> IO (Maybe (Module, ModuleLocation))
+maybeHomeModule mod_name is_source = do
+ home_path <- readIORef v_Import_paths
+ hisuf <- readIORef v_Hi_suf
+ mode <- readIORef v_GhcMode
+
+ let mod_str = moduleNameUserString mod_name
+ basename = map (\c -> if c == '.' then '/' else c) mod_str
+
+ -- In compilation manager modes, we look for source files in the home
+ -- package because we can compile these automatically. In one-shot
+ -- compilation mode we look for .hi files only.
+ --
+ -- When generating dependencies, we're interested in either category.
+ --
+ source_exts =
+ [ ("hs", \ fName path -> mkHomeModuleLocn mod_name path fName)
+ , ("lhs", \ fName path -> mkHomeModuleLocn mod_name path fName)
+ ]
+ hi_exts = [ (hisuf, \ fName path -> mkHiOnlyModuleLocn mod_name fName) ]
+
+ std_exts
+ | mode == DoMkDependHS = hi_exts ++ source_exts
+ | isCompManagerMode mode = source_exts
+ | otherwise = hi_exts
+
+ -- 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 home_path basename
+ (if is_source then boot_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
+ }
+ )
+
+-- The .hi file always follows the module name, whereas the object
+-- file may follow the name of the source file in the case where the
+-- two differ (see summariseFile in compMan/CompManager.lhs).
+
+mkHomeModuleLocn mod_name
+ basename -- everything but the extension
+ source_fn -- full path to the source (required)
+ = do
+