2

I want to conditionally compile a function based on the target platform. I have several versions:

linux :: Q Dec
linux = [d|f = f in f|]

windows :: Q Dec
windows = [d|f = f in f|]

-- and so on

I want to splice only the correct version into the top-level of this file. What's the best way to check from the Q monad which platform GHC is targeting? Is stuff like the Cabal project file available to TH in a non-hacky way?

lachrimae
  • 79
  • 7
  • 4
    The way this is generally done, independently of TH, is via Cabal-defined CPP macros. Which is a bit clunky though, there might well be a better way to do it with TH. – leftaroundabout Sep 01 '22 at 22:01
  • That makes sense, I think I've seen that pattern before! I would like to avoid the CPP if possible though, because of how disruptive it can be to tooling. – lachrimae Sep 01 '22 at 22:14
  • 3
    You can always use `System.Info.os` inside your TH code. – Carl Sep 01 '22 at 23:00
  • Ahh thanks @Carl, not sure why I didn't think of that! – lachrimae Sep 02 '22 at 03:02
  • @Carl that would break for any cross-compilation though, wouldn't it? – leftaroundabout Sep 02 '22 at 09:12
  • I wonder why TH's `Q` monad does not allow access to the underlying GHC session, so that we can query the compiler directly. Maybe allowing that would be a bad idea (beyond being unportable to other compilers, I mean). – chi Sep 02 '22 at 11:12
  • good points. I might check out the `base` project to see if this has ever come up in their issues tracker. @chi's idea might be a reasonable feature request. – lachrimae Sep 02 '22 at 16:41
  • @leftaroundabout I have no idea. System.Info.os isn't really compatible with cross-compilation, just at a type level. It promises those values are constants, and they really aren't when cross-compiling. So I have no idea what it'll do. If you need to interact with cross-compilation, you probably are going to have to do it manually for now - falling back to CPP and manually set flags. – Carl Sep 02 '22 at 17:08

0 Answers0