Home > Net >  How can I vary imports in haskell based on os (and can i?)?
How can I vary imports in haskell based on os (and can i?)?

Time:09-27

Essentially I want to bind different ffi stuff in Haskell based on which os the interpreter executes the script. Is there a way to do it?

CodePudding user response:

cabal has conditional blocks, and one of the supported conditions is os(name):

os(name)

Tests if the current operating system is name. The argument is tested against System.Info.os on the target system. There is unfortunately some disagreement between Haskell implementations about the standard values of System.Info.os. Cabal canonicalises it so that in particular os(windows) works on all implementations. If the canonicalised os names match, this test evaluates to true, otherwise false. The match is case-insensitive.

--- https://cabal.readthedocs.io/en/stable/cabal-package.html#conditions

This is a pretty commonly used flag.


I've never seen this other option in use, but with the CPP extension, GHC defines the <os>_HOST_OS macro, where <os> identifies your OS.

os_HOST_OS=1

This define allows conditional compilation based on the Operating System, where⟨os⟩ is the name of the current Operating System (eg. linux, mingw32 for Windows, solaris, etc.).

--- https://downloads.haskell.org/ghc/latest/docs/users_guide/phases.html?highlight=cpp#standard-cpp-macros

  • Related