开发者

How to configure build-dependencies in haskell+cabal?

开发者 https://www.devze.com 2023-04-05 08:53 出处:网络
I have to build some app that depends on OS. For example I\'m searching over directory, and if I see some item is symbolic link (in linux), I just skip them and go over. To check this I use function S

I have to build some app that depends on OS. For example I'm searching over directory, and if I see some item is symbolic link (in linux), I just skip them and go over. To check this I use function System.Posix.Files.isSymbolicLink that ships by "unix" package. The problem is that this package would not build on Windows, and I have to bypass this limitation somehow when app builds on Windows. I've tried this trick:

1) Created additional file: ./System/Posix/Files.hs, where exposed function isSymbolicLink which always returns False.

2) Updated .cabal file with following:

if os(windows)
    other-modules: System.Posix.Files
else
    build-depends: unix

But, despite my configuration, cabal build always builds my /System/Posix/Files.hs since I have import System.Posix.Files in my main.hs:

Preprocessing executables for test-1.1.1...

Building test-1.1.1...

[1 of 2] Compiling System.Posix.Files ( System/Posix/Files.hs, dist/build/test/test-tmp/System/Posix/Files.o )

[2 of 2] Compiling Main ( main.hs, dist/build/test/test-tmp/Main.o )

Linking dist/build/test/test ...

So even I build on linux, application gets my blank isSymbolicLink function instead those provided by "unix" package. How can I tell to cabal do not include this file, and use module provided by "unix" package when I build on linux? Or may开发者_如何学JAVAbe there is some another approach to solve such problems?


This can be solved using the CPP preprocessor.

In your .cabal file, add

if os(windows)
  Cpp-options: -DWINDOWS
else
  Build-depends: unix

Then add a module which will conditionally export isSymbolicLink from the unix package, or a dummy one for Windows.

{-# LANGUAGE CPP #-}
module Compatibility (isSymbolicLink) where

#ifndef WINDOWS
import System.Posix.Files
#else
isSymbolicLink _ = return False
#endif

Now just import this module whereever you need to use this function, and you should be good to go.


Another option is to stick with your initial approach, but move your System/Posix/Files.hs into a subdirectory which you conditionally add to Hs-Source-Dirs.

if os(windows)
  Other-modules: System.Posix.Files
  -- Assuming you moved it to windows/System/Posix/Files.hs
  Hs-source-dirs: .,windows    
else
  Build-depends: unix
  Hs-Source-Dirs: .
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号