Using GHC API to compile Haskell file

Neil Mitchell ndmitchell at gmail.com
Mon Sep 14 21:07:14 UTC 2015


>> 1) Is there any way to do the two compilations sharing some cached
>> state, e.g. loaded packages/.hi files, so each compilation goes
>> faster.
>
> You can, using withTempSession in the GhcMonad.  The external package
> state will be preserved across calls here, but things put in the HPT
> will get thrown out.

So as far as I can tell, you are suggesting I basically do getSession
in one session, grab the cache bits of the HscEnv, and inject them
into the start of the next session with setSession? (withTempSession
and all the other session functions just seem to be some variation on
that pattern). I tried that, but even storing just hsc_EPS between
sessions (which seemed like it should both be something that never
changes), causes weird compile failures. Is moving things like hsc_EPS
between sessions supported? Or were you suggesting I do something
else?

>> 2) Is there any way to do the link alone through the GHC API.
>
> I am confused by your code. There are two ways you can do linking:
>
>     1. Explicitly specify all of the objects to link together.  This
>     works even if the source files aren't available.
>
>     2. Run ghc --make.  This does dependency analysis to figure out what
>     objects to link together, but since everything is already compiled,
>     it just links.
>
> Your code seems to be trying to do (1) and (2) simultaneously (you set
> the mode to OneShot, but then you call load which calls into GhcMake).
>
> If you want to use (1), stop calling load and call 'oneShot' instead.
> If you want to use (2), just reuse your working --make code.
>
> (BTW, how did I figure this all out? By looking at ghc/Main.hs).

Thanks. I have now started down that rabbit hole, and am currently
peering around. I haven't yet succeeded, but given what I can see, it
just seems like a matter of time.

Thanks, Neil


More information about the ghc-devs mailing list