Using GHC API with multiple targets
Eternal Recursion
eternal.recursion at proton.me
Mon Feb 6 12:42:41 UTC 2023
If this is the wrong forum for this question (which as I think about it, I suppose it is) then redirection to a more appropriate mailing list or forum (or any advice, really) would be appreciated. I just figured this would be the forum with the best understanding of how the GHC API works (and has changed over time), and my longer term goal is indeed to contribute to it after I get past my learning curve.
Sincerely,
Bob
Sent with [Proton Mail](https://proton.me/) secure email.
------- Original Message -------
On Saturday, February 4th, 2023 at 4:04 PM, Eternal Recursion via ghc-devs <ghc-devs at haskell.org> wrote:
> Hi Everyone!
>
> I'm new here, trying to learn the GHC API. using 944 with cabal 3.8.1.0.
>
> How do I correctly set a GHC Session's DynFlags (and/or other properties) to ensure local libraries imported by the main target are resolved properly at compile time?
>
> What flags need to be set so that GHC is able to load/analyze/compile all relevant Libraries in a package?
>
> This is my current code:
>
> withPath :: FilePath -> IO ()
> withPath target = do
> let targetDir = takeDirectory target
> let targetFile = takeFileName target
> listing <- listDirectory targetDir
> let imports = filter (\f -> takeExtension f == ".hs") listing
> print imports
> let moduleName = mkModuleName targetFile
> g <- defaultErrorHandler defaultFatalMessager defaultFlushOut
> $ runGhc (Just libdir) $ do
> initGhcMonad (Just libdir)
> dynflags <- getSessionDynFlags
> setSessionDynFlags $ dynflags { ghcLink = LinkInMemory
> , ghcMode = CompManager
> , backend = Interpreter
> , mainModuleNameIs = moduleName
> , workingDirectory = Just targetDir
> , importPaths = [targetDir] ++ importPaths dynflags
> }
>
> targets <- mapM (\t -> guessTarget t Nothing Nothing) imports
> setTargets targets
> setContext [ IIDecl $ simpleImportDecl (mkModuleName "Prelude") ]
> load LoadAllTargets
> liftIO . print . ppr =<< getTargets
> getModuleGraph
> putStrLn "Here we go!"
> print $ ppr $ mgModSummaries g putStrLn "☝️ "
>
> However, when I run it (passing to example/app/Main.hs, in which directory are Lib.hs and Lib2.hs, the latter being imported into Main), I get:
>
> $ cabal run cli -- example/app/Main.hs
> Up to date
> ["Main.hs","Lib.hs","Lib2.hs"]
> [main:Main.hs, main:Lib.hs, main:Lib2.hs]
> Here we go!
> [ModSummary {
> ms_hs_hash = 23f9c4415bad851a1e36db9d813f34be
> ms_mod = Lib,
> unit = main
> ms_textual_imps = [(, Prelude)]
> ms_srcimps = []
> },
> ModSummary {
> ms_hs_hash = e1eccc23af49f3498a5a9566e63abefd
> ms_mod = Lib2,
> unit = main
> ms_textual_imps = [(, Prelude)]
> ms_srcimps = []
> },
> ModSummary {
> ms_hs_hash = 5f6751d7f0d5547a1bdf39af84f8c07f
> ms_mod = Main,
> unit = main
> ms_textual_imps = [(, Prelude), (, Lib2)]
> ms_srcimps = []
> }]
> ☝
>
> example/app/Main.hs:4:1: error:
> Could not find module ‘Lib2’
> Use -v (or `:set -v` in ghci) to see a list of the files searched for.
> |
> 4 |import qualified Lib2 as L2
> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> cli: example/app/Main.hs:4:1: error:
> Could not find module `Lib2'
> Use -v (or `:set -v` in ghci) to see a list of the files searched for.
>
> What do I need to do differently to make this work?
>
> I have a local Cabal file I could use, but to know what I need out of it, I need to understand the minimum required info to get this to work. TIA!
>
> Sincerely,
>
> Bob
>
> Sent with [Proton Mail](https://proton.me/) secure email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20230206/9d14afa9/attachment.html>
More information about the ghc-devs
mailing list