[Haskell-cafe] ifdef based on which OS you're on

Andrew Cowie andrew at operationaldynamics.com
Fri Feb 15 15:05:13 CET 2013


I've got a piece of code that looks like this:

        baselineContextSSL :: IO SSLContext
        baselineContextSSL = do
            ctx <- SSL.context
            SSL.contextSetDefaultCiphers ctx
        #if defined __MACOSX__
            SSL.contextSetVerificationMode ctx SSL.VerifyNone
        #elif defined __WIN32__
            SSL.contextSetVerificationMode ctx SSL.VerifyNone
        #else
            SSL.contextSetCADirectory ctx "/etc/ssl/certs"
            SSL.contextSetVerificationMode ctx $
                SSL.VerifyPeer True True Nothing
        #endif
            return ctx

all very nice (this being necessary because apparently the non-free
operating systems don't store their certs in a reliably discoverable
place; bummer).

That, however, is not the problem. After all, this sort of thing is what
#ifdefs are for. The problem is needing to get an appropriate symbol
based on what OS you're using defined.

I naively assumed there would be __LINUX__ and __MACOSX__ and __WIN32__
defined by GHC because, well, that's just the sort of wishful thinking
that powers the universe.

So my question is: what's an appropriate Haskell mechanism for building
code that is OS / arch  / distro specific? It's not like I have autoconf
running generating me a config.h I could #include, right?

This feels simple and an appropriate use of CPP; even the symbol names
look just about like what I would have expected; stackoverflow said so,
must be true. Just need to get the right symbol defined at build time.

Any suggestions?

AfC
Sydney

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130216/9098072f/attachment.pgp>


More information about the Haskell-Cafe mailing list