Cabal: N executables without building all the modules N times?

Henning Thielemann lemming at henning-thielemann.de
Sun Aug 16 04:51:56 UTC 2020


On Mon, 10 Aug 2020, James Cook wrote:

> Hi libraries@,
>
> (Let me know if there's a better place for Cabal questions. This list is 
> linked from https://www.haskell.org/cabal/ as a place to ask questions.)
>
>
> I have a project with lots of modules, and lots of executables that use 
> those modules. How should I structure my .cabal file if I want cabal to 
> only build the common modules once?
>
>
> I have found I can do it as follows, but I am wondering if there's a way 
> to do it without step 1:
>
> 1. Create a "library_src" directory with the source for all the shared 
> modules. Put the source files for the executables somewhere else.
>
> 2. Create a .cabal file with one "library" stanza with "hs-source-dirs: 
> library_source", and several executable stanzas.

I use to setup directory 'src' for the library, 'test' for test code and, 
say, 'example' for executables.

> If I leave out step 1 and put all the source files in the same place, 
> then the compiler builds them separately for each executable, and I get 
> a "missing-home-modules" warning.
>
> Is it just a fact of life that I need to separate my source into 
> separate directories if I want this to work properly?

Yes. Problem is, that Cabal calls GHC's 'make' feature and that one 
searches for appropriate modules in its search path.


Btw. you give a Library section a name you get a private sub-library of 
private modules that can be shared between the public library and e.g. the 
test suite.


More information about the Libraries mailing list