[cabal] Extract simple static information from PackageDescription

Hello, I have a simple question regarding GenericPackageDescription/ PackageDescription. I don't understand it from the docs: https://hackage.haskell.org/package/Cabal-1.22.4.0/docs/Distribution-Package... Google and IRC #haskell also didn't help me. I am trying to extract a simple static information out of the cabal file: the hs-source-dirs Is there a way to get this without resolving conditionals, or with minimal effort? (I don't even understand how to resolving conditionals.) I tried it like this: https://gist.github.com/schoettl/b321baa5002ca48a46c5 But here the PackageDescription has no information (library == Nothing, executables == []). I appreciate any help so much. And I will release the program as an open-source example so that it may help other people in future. Thank you, Jakob -- Jakob Schöttl E-Mail: jschoett@gmail.com PGP-Key: 0x25055C7F http://jakob.keramik-schoettl.de

On Fri, 6 Nov 2015, Jakob Schöttl wrote:
I am trying to extract a simple static information out of the cabal file: the hs-source-dirs
Is there a way to get this without resolving conditionals, or with minimal effort? (I don't even understand how to resolving conditionals.)
If you write in a package description: If flag(foo) Hs-Source-Dirs: foo Else Hs-Source-Dirs: bar then Hs-Source-Dirs depends on conditionals. I use this technique in order to select specific modules for Unix and Windows without CPP. How do you want to extract Hs-Source-Dirs without knowing the flag values?

On 06.11.2015 12:04, Henning Thielemann wrote:
On Fri, 6 Nov 2015, Jakob Schöttl wrote:
I am trying to extract a simple static information out of the cabal file: the hs-source-dirs
Is there a way to get this without resolving conditionals, or with minimal effort? (I don't even understand how to resolving conditionals.)
If you write in a package description:
 If flag(foo)    Hs-Source-Dirs: foo  Else    Hs-Source-Dirs: bar
then Hs-Source-Dirs depends on conditionals. I use this technique in order to select specific modules for Unix and Windows without CPP. How do you want to extract Hs-Source-Dirs without knowing the flag values?
Thank you, Henning. I see, I need to handle this conditionals even if I don't have such conditionals and flags in my cabal file. How can I resolve the conditionals? Can I somehow update the GenericPackageDescription i.e. set all flag values and then fetch the complete PackageDescription in using packageDescription? https://hackage.haskell.org/package/Cabal-1.22.4.0/docs/Distribution-Package... Or are condLibrary, condExecutable, ... the only way to access this data?

On Fri, 6 Nov 2015, Jakob Schöttl wrote:
Thank you, Henning. I see, I need to handle this conditionals even if I don't have such conditionals and flags in my cabal file.
How can I resolve the conditionals?
I think, this one should help: https://www.haskell.org/cabal/release/cabal-latest/doc/API/Cabal/Distributio...

Ah, that's perfect in my case! Thank you! I managed to merge the hs-source-dirs fields "manually": https://gist.github.com/schoettl/b321baa5002ca48a46c5 But flattenPackageDescription seems to do exactly that. Thanks, Jakob On 07.11.2015 00:16, Henning Thielemann wrote:
On Fri, 6 Nov 2015, Jakob Schöttl wrote:
Thank you, Henning. I see, I need to handle this conditionals even if I don't have such conditionals and flags in my cabal file.
How can I resolve the conditionals?
I think, this one should help: https://www.haskell.org/cabal/release/cabal-latest/doc/API/Cabal/Distributio...

Hi Jakob, you might like the cabal-lenses[1] library. Getting 'hs-source-dirs' from all sections of the cabal file (library, executable, ...) and evaluating the conditionals in the cabal file (considering the default value of flags and the current platform), you should be able to write: import Control.Lens -- from the lens library import CabalLenses -- from the cabal-lenses library hsSourceDirs :: GenericPackageDescription -> [String] hsSourceDirs pkgDesc = pkgDesc ^.. allBuildInfo . traversed . hsSourceDirsL You can also specify[2] which section you like to read or how flags for the conditionals should be set. If you only need a command line programming for the reading of 'hs-source-dirs', then there's cabal-cargs[3]. The above example: cabal-cargs --only=hs_source_dirs --format=pure Greetings, Daniel [1] https://hackage.haskell.org/package/cabal-lenses-0.4.7 [2] https://hackage.haskell.org/package/cabal-lenses-0.4.5/docs/CabalLenses-Trav... [3] https://hackage.haskell.org/package/cabal-cargs
participants (3)
-
Daniel Trstenjak
-
Henning Thielemann
-
Jakob Schöttl