19

I maintain a library with FFI bindings on Hackage. So my Haskell library depends on the corresponding C library and its header files. Now I specify the external dependency in the .cabal file like this:

PkgConfig-Depends:
      libfoo >= 1.2

And it works well for me in Linux. However, I have a user of the library who reports, that installing pkg-config on Windows is rather cumbersome, and instead he prefers

Includes:
      foo.h
Extra-libraries:
      foo

I'd like my library to be as easy to build as possible, and don't want to force build dependencies which are not strictly required. However, I see that Cabal manual suggests to use PkgConfig-Depends.

My questions:

  • Which way I should prefer for cross-platform packages?
  • Is it possible to write a .cabal file in such a way, that it can work with pkg-config and without?
  • And, by the way, is pkg-config included in the Haskell platform (I don't have a Windows machine to check right now)?
sastanin
  • 40,473
  • 13
  • 103
  • 130

2 Answers2

18

The pkg-config method is preferable because pkg-config knows where to find include and library files, which may be in nonstandard locations on some systems.

You can write the .cabal file to use both methods. Using a flag, as shown here, has the advantage that Cabal will automatically try the other flag value if the default fails. (Below example is not tested)

Flag UsePkgConfig
  Description: Use pkg-config to check for library dependences
  Default: True

Executable hax
  if flag(UsePkgConfig)
    PkgConfig-Depends: libfoo >= 1.2
  else
    Includes: foo.h
    Extra-libraries: foo
Heatsink
  • 7,721
  • 1
  • 25
  • 36
  • 1
    Thank you. I chose this approach with a flag, alas I preferred `NoPkgConfig` flag instead, because `cabal configure -fNoPkgConfig` is more readable than `cabal configure -f-UsePkgConfig`. This approach works well on Linux (either with and without `pkg-config` installed). I'll report what's the Windows user feedback is later. – sastanin Feb 16 '11 at 11:34
6

pkg-config is not included in the Haskell Platform, nor could I imagine that it ever would be.

Usually I will use includes/Extra-libraries if they're relatively simple. But for complex packages that may have a lot of included libraries, such as gtk, it's much nicer to use pkg-config when available.

It is possible to write a .cabal file that will work with and without specific fields. Try this:

if os(windows)
  Includes:
      foo.h
  Extra-libraries:
      foo
else
  PkgConfig-Depends:
      libfoo >= 1.2

Also note that .cabal can run a configure script, which can help in some situations but isn't very windows-friendly.

John L
  • 27,937
  • 4
  • 73
  • 88
  • Thank you. I considered to do something like this, but probably the solution proposed by @Heatsink is more flexible. I'll try that one first. – sastanin Feb 10 '11 at 19:18
  • 2
    @jetxee - it's true that flags can be more flexible, but they can cause problems too. Cabal seems to want to recompile already-installed packages when a new package is built with a different set of flags, which can put your package db into an inconsistent state. I may be biased because I've just had to fix my package db though. – John L Feb 10 '11 at 21:13