[Haskell-cafe] How to add use custom preprocessor in cabal

Duncan Coutts duncan.coutts at googlemail.com
Sat Oct 10 11:17:39 EDT 2009


On Thu, 2009-10-08 at 17:28 +0200, Bernd Brassel wrote:
> Hi folks,
> 
> I am trying to integrate my own preprocessor into a cabal build  
> process. But there are several points that I get stuck with. Could  
> someone help me, please?
> 
> A simplification of my problem:
> I have files "Abc.foo" and each of them should be transformed into  
> "Abc.hs" by calling function "transform". The resulting "Abc.hs" files  
> should then be exposed as Haskell library modules.
> 
> 1. Question: How do I tell cabal that the file Abc.foo belongs to the  
> package?
> After reading the User's Guide I had the impression that I should  
> write something like
> 
> Library
>    Eposed-Modules:
>        Abc.foo
> 
> But this leads to a parse error.

Use

Library
  Exposed-Modules:
      Abc

It is a list of modules, not files.

> Adding a
> 
> Data-Files:
>    Abc.foo
> 
> to the heading, on the other hand, does not seem to do anything.

Right, those are for files that will be installed with the app and used
at runtime.

> 2. Question: How to add the preprocessor? I have tried
> 
> main =
>    defaultMainWithHooks
>           simpleUserHooks{hookedPreProcessors=[("foo",transformation)]}
> 
> transformation :: BuildInfo -> LocalBuildInfo -> PreProcessor

That looks right. Here's how to complete it (example taken from the
Cabal haddock docs for the PreProcess module):

transformation _ _ =
  PreProcessor {
    platformIndependent = True,
    runPreProcessor =
      mkSimplePreProcessor $ \inFile outFile verbosity -> do
        fail $ "transformation: " ++ inFile ++ " " ++ outFile
  }

and it works fine:

runghc Setup.hs build
Preprocessing library foo-1.0...
Setup.hs: transformation: Abc.foo dist/build/Abc.hs

> But under which circumstances will this function be called? Up to now  
> I have not succeeded in making cabal call this function.

It calls it when it goes and looks for the module Abc (ie Abc.hs
or .lhs), and since if does not find it, it'll check through the list of
preprocessors and go looking for the corresponding files, ie Abc.foo.

Duncan



More information about the Haskell-Cafe mailing list