+ basename = dots_to_slashes (moduleString mod)
+
+ to_search :: [(FilePath, IO FinderCacheEntry)]
+ to_search = [ (file, fn path basename)
+ | path <- paths,
+ (ext,fn) <- exts,
+ let base | path == "." = basename
+ | otherwise = path `joinFileName` basename
+ file = base `joinFileExt` ext
+ ]
+
+ search [] = return (CantFindAmongst (map fst to_search))
+ search ((file, mk_result) : rest) = do
+ b <- doesFileExist file
+ if b
+ then do { res <- mk_result; return (Ok res) }
+ else search rest
+
+mkHomeModLocationSearched :: DynFlags -> Module -> FileExt
+ -> FilePath -> BaseName -> IO FinderCacheEntry
+mkHomeModLocationSearched dflags mod suff path basename = do
+ loc <- mkHomeModLocation2 dflags mod (path `joinFileName` basename) suff
+ return (loc, Nothing)
+
+mkHiOnlyModLocation :: DynFlags -> FileExt -> FilePath -> BaseName
+ -> IO FinderCacheEntry
+mkHiOnlyModLocation dflags hisuf path basename = do
+ loc <- hiOnlyModLocation dflags path basename hisuf
+ return (loc, Nothing)
+
+mkPackageModLocation :: DynFlags -> (PackageConfig, Bool) -> FileExt
+ -> FilePath -> BaseName -> IO FinderCacheEntry
+mkPackageModLocation dflags pkg_info hisuf path basename = do
+ loc <- hiOnlyModLocation dflags path basename hisuf
+ return (loc, Just pkg_info)
+
+-- -----------------------------------------------------------------------------
+-- 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 :: DynFlags -> Module -> FilePath -> IO ModLocation
+mkHomeModLocation dflags mod src_filename = do
+ let (basename,extension) = splitFilename src_filename
+ mkHomeModLocation2 dflags mod basename extension
+
+mkHomeModLocation2 :: DynFlags
+ -> Module
+ -> FilePath -- Of source module, without suffix
+ -> String -- Suffix
+ -> IO ModLocation
+mkHomeModLocation2 dflags mod src_basename ext = do
+ let mod_basename = dots_to_slashes (moduleString mod)
+
+ obj_fn <- mkObjPath dflags src_basename mod_basename
+ hi_fn <- mkHiPath dflags src_basename mod_basename
+
+ return (ModLocation{ ml_hs_file = Just (src_basename `joinFileExt` ext),
+ ml_hi_file = hi_fn,
+ ml_obj_file = obj_fn })
+
+hiOnlyModLocation :: DynFlags -> FilePath -> String -> Suffix -> IO ModLocation
+hiOnlyModLocation dflags path basename hisuf
+ = do let full_basename = path `joinFileName` basename
+ obj_fn <- mkObjPath dflags full_basename basename
+ return ModLocation{ ml_hs_file = Nothing,
+ ml_hi_file = full_basename `joinFileExt` 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
+ }
+
+-- | Constructs the filename of a .o file for a given source file.
+-- Does /not/ check whether the .o file exists
+mkObjPath
+ :: DynFlags
+ -> FilePath -- the filename of the source file, minus the extension
+ -> String -- the module name with dots replaced by slashes
+ -> IO FilePath
+mkObjPath dflags basename mod_basename
+ = do let
+ odir = objectDir dflags
+ osuf = objectSuf dflags
+
+ obj_basename | Just dir <- odir = dir `joinFileName` mod_basename
+ | otherwise = basename
+
+ return (obj_basename `joinFileExt` osuf)
+
+-- | Constructs the filename of a .hi file for a given source file.
+-- Does /not/ check whether the .hi file exists
+mkHiPath
+ :: DynFlags
+ -> FilePath -- the filename of the source file, minus the extension
+ -> String -- the module name with dots replaced by slashes
+ -> IO FilePath
+mkHiPath dflags basename mod_basename
+ = do let
+ hidir = hiDir dflags
+ hisuf = hiSuf dflags
+
+ hi_basename | Just dir <- hidir = dir `joinFileName` mod_basename
+ | otherwise = basename
+
+ return (hi_basename `joinFileExt` hisuf)
+
+
+-- -----------------------------------------------------------------------------
+-- Filenames of the stub files
+
+-- We don't have to store these in ModLocations, because they can be derived
+-- from other available information, and they're only rarely needed.
+
+mkStubPaths
+ :: DynFlags
+ -> Module
+ -> ModLocation
+ -> (FilePath,FilePath)
+
+mkStubPaths dflags mod location
+ = let
+ stubdir = stubDir dflags
+
+ mod_basename = dots_to_slashes (moduleString mod)
+ src_basename = basenameOf (expectJust "mkStubPaths"
+ (ml_hs_file location))
+
+ stub_basename0
+ | Just dir <- stubdir = dir `joinFileName` mod_basename
+ | otherwise = src_basename
+
+ stub_basename = stub_basename0 ++ "_stub"
+ in
+ (stub_basename `joinFileExt` "c",
+ stub_basename `joinFileExt` "h")
+ -- the _stub.o filename is derived from the ml_obj_file.
+
+-- -----------------------------------------------------------------------------
+-- findLinkable isn't related to the other stuff in here,
+-- but there's no other obvious place for it
+
+findObjectLinkableMaybe :: Module -> ModLocation -> IO (Maybe Linkable)
+findObjectLinkableMaybe mod locn
+ = do let obj_fn = ml_obj_file locn
+ maybe_obj_time <- modificationTimeIfExists obj_fn
+ case maybe_obj_time of
+ Nothing -> return Nothing
+ Just obj_time -> liftM Just (findObjectLinkable mod obj_fn obj_time)
+
+-- Make an object linkable when we know the object file exists, and we know
+-- its modification time.
+findObjectLinkable :: Module -> FilePath -> ClockTime -> IO Linkable
+findObjectLinkable mod obj_fn obj_time = do
+ let stub_fn = case splitFilename3 obj_fn of
+ (dir, base, ext) -> dir ++ "/" ++ base ++ "_stub.o"
+ stub_exist <- doesFileExist stub_fn
+ if stub_exist
+ then return (LM obj_time mod [DotO obj_fn, DotO stub_fn])
+ else return (LM obj_time mod [DotO obj_fn])
+
+-- -----------------------------------------------------------------------------
+-- Utils
+
+dots_to_slashes = map (\c -> if c == '.' then '/' else c)
+
+
+-- -----------------------------------------------------------------------------
+-- Error messages
+
+cantFindError :: DynFlags -> Module -> FindResult -> SDoc
+cantFindError dflags mod_name (FoundMultiple pkgs)
+ = hang (ptext SLIT("Cannot import") <+> quotes (ppr mod_name) <> colon) 2 (
+ sep [ptext SLIT("it was found in multiple packages:"),
+ hsep (map (text.packageIdString) pkgs)]
+ )
+cantFindError dflags mod_name find_result
+ = hang (ptext SLIT("Could not find module") <+> quotes (ppr mod_name) <> colon)
+ 2 more_info
+ where
+ more_info
+ = case find_result of
+ PackageHidden pkg
+ -> ptext SLIT("it is a member of package") <+> ppr pkg <> comma
+ <+> ptext SLIT("which is hidden")
+
+ ModuleHidden pkg
+ -> ptext SLIT("it is hidden") <+> parens (ptext SLIT("in package")
+ <+> ppr pkg)
+
+ NotFound files
+ | null files
+ -> ptext SLIT("it is not a module in the current program, or in any known package.")
+ | verbosity dflags < 3
+ -> ptext SLIT("use -v to see a list of the files searched for")
+ | otherwise
+ -> hang (ptext SLIT("locations searched:"))
+ 2 (vcat (map text files))
+
+ _ -> panic "cantFindErr"