+ let
+ hi_exts =
+ [ (package_hisuf,
+ mkPackageModLocation pkg_info package_hisuf mod) ]
+
+ source_exts =
+ [ ("hs", mkPackageModLocation pkg_info package_hisuf mod)
+ , ("lhs", mkPackageModLocation pkg_info package_hisuf mod)
+ ]
+
+ -- mkdependHS needs to look for source files in packages too, so
+ -- that we can make dependencies between package before they have
+ -- been built.
+ exts
+ | mode == DoMkDependHS = hi_exts ++ source_exts
+ | otherwise = hi_exts
+
+ -- we never look for a .hi-boot file in an external package;
+ -- .hi-boot files only make sense for the home package.
+ searchPathExts imp_dirs mod exts
+
+-- -----------------------------------------------------------------------------
+-- General path searching
+
+searchPathExts
+ :: [FilePath] -- paths to search
+ -> Module -- module name
+ -> [ (
+ String, -- suffix
+ String -> String -> String -> IO FindResult -- action
+ )
+ ]
+ -> IO FindResult
+
+searchPathExts path mod exts = search to_search
+ where
+ basename = dots_to_slashes (moduleUserString mod)
+
+ to_search :: [(FilePath, IO FindResult)]
+ to_search = [ (file, fn p basename ext)
+ | p <- path,
+ (ext,fn) <- exts,
+ let base | p == "." = basename
+ | otherwise = p ++ '/':basename
+ file = base ++ '.':ext
+ ]
+
+ search [] = return (NotFound (map fst to_search))
+ search ((file, result) : rest) = do
+ b <- doesFileExist file
+ if b
+ then result
+ else search rest
+
+-- -----------------------------------------------------------------------------
+-- Building ModLocations
+
+mkHiOnlyModLocation hisuf mod path basename _ext = do
+ -- basename == dots_to_slashes (moduleNameUserString mod)
+ loc <- hiOnlyModLocation path basename hisuf
+ addToFinderCache mod (loc, Nothing)
+ return (Found loc ThisPackage)
+
+mkPackageModLocation pkg_info hisuf mod path basename _ext = do
+ -- basename == dots_to_slashes (moduleNameUserString mod)
+ loc <- hiOnlyModLocation path basename hisuf
+ addToFinderCache mod (loc, pkg_info)
+ return (Found loc (pkgInfoToId pkg_info))
+
+hiOnlyModLocation path basename hisuf
+ = do let full_basename = path++'/':basename
+ obj_fn <- mkObjPath full_basename basename
+ return ModLocation{ ml_hspp_file = Nothing,
+ ml_hs_file = Nothing,
+ ml_hi_file = full_basename ++ '.':hisuf,
+ -- Remove the .hi-boot suffix from
+ -- hi_file, if it had one. We always
+ -- want the name of the real .hi file
+ -- in the ml_hi_file field.
+ ml_obj_file = obj_fn
+ }
+
+-- -----------------------------------------------------------------------------
+-- Constructing a home module location
+
+-- This is where we construct the ModLocation for a module in the home
+-- package, for which we have a source file. It is called from three
+-- places:
+--
+-- (a) Here in the finder, when we are searching for a module to import,
+-- using the search path (-i option).
+--
+-- (b) The compilation manager, when constructing the ModLocation for
+-- a "root" module (a source file named explicitly on the command line
+-- or in a :load command in GHCi).
+--
+-- (c) The driver in one-shot mode, when we need to construct a
+-- ModLocation for a source file named on the command-line.
+--
+-- Parameters are:
+--
+-- mod
+-- The name of the module
+--
+-- path
+-- (a): The search path component where the source file was found.
+-- (b) and (c): "."
+--
+-- src_basename
+-- (a): dots_to_slashes (moduleNameUserString mod)
+-- (b) and (c): The filename of the source file, minus its extension
+--
+-- ext
+-- The filename extension of the source file (usually "hs" or "lhs").
+
+mkHomeModLocation mod src_filename = do
+ let (basename,extension) = splitFilename src_filename
+ mkHomeModLocation' mod basename extension
+
+mkHomeModLocationSearched mod path basename ext = do
+ loc <- mkHomeModLocation' mod (path ++ '/':basename) ext
+ return (Found loc ThisPackage)
+
+mkHomeModLocation' mod src_basename ext = do
+ let mod_basename = dots_to_slashes (moduleUserString mod)
+
+ obj_fn <- mkObjPath src_basename mod_basename
+ hi_fn <- mkHiPath src_basename mod_basename
+
+ let loc = ModLocation{ ml_hspp_file = Nothing,
+ ml_hs_file = Just (src_basename ++ '.':ext),
+ ml_hi_file = hi_fn,
+ ml_obj_file = obj_fn }
+
+ addToFinderCache mod (loc, Nothing)
+ return loc
+
+-- | Constructs the filename of a .o file for a given source file.
+-- Does /not/ check whether the .o file exists
+mkObjPath
+ :: FilePath -- the filename of the source file, minus the extension
+ -> String -- the module name with dots replaced by slashes
+ -> IO FilePath
+mkObjPath basename mod_basename
+ = do odir <- readIORef v_Output_dir
+ osuf <- readIORef v_Object_suf
+
+ let obj_basename | Just dir <- odir = dir ++ '/':mod_basename
+ | otherwise = basename
+
+ return (obj_basename ++ '.':osuf)
+
+-- | Constructs the filename of a .hi file for a given source file.
+-- Does /not/ check whether the .hi file exists
+mkHiPath
+ :: FilePath -- the filename of the source file, minus the extension
+ -> String -- the module name with dots replaced by slashes
+ -> IO FilePath
+mkHiPath basename mod_basename
+ = do hidir <- readIORef v_Hi_dir
+ hisuf <- readIORef v_Hi_suf
+
+ let hi_basename | Just dir <- hidir = dir ++ '/':mod_basename
+ | otherwise = basename
+
+ return (hi_basename ++ '.':hisuf)
+
+-- -----------------------------------------------------------------------------
+-- findLinkable isn't related to the other stuff in here,
+-- but there's no other obvious place for it
+
+findLinkable :: Module -> ModLocation -> IO (Maybe Linkable)
+findLinkable mod locn
+ = do let obj_fn = ml_obj_file locn
+ obj_exist <- doesFileExist obj_fn
+ if not obj_exist
+ then return Nothing
+ else
+ do let stub_fn = case splitFilename3 obj_fn of
+ (dir, base, ext) -> dir ++ "/" ++ base ++ "_stub.o"
+ stub_exist <- doesFileExist stub_fn
+ obj_time <- getModificationTime obj_fn
+ if stub_exist
+ then return (Just (LM obj_time mod [DotO obj_fn, DotO stub_fn]))
+ else return (Just (LM obj_time mod [DotO obj_fn]))
+
+-- -----------------------------------------------------------------------------
+-- Utils
+
+dots_to_slashes = map (\c -> if c == '.' then '/' else c)
+