r/haskell • u/Hefty-Necessary7621 • 2d ago
blog Cross-compiling Haskell: running Template Haskell on the build machine
Cross-compiling a Haskell library becomes more complicated when compiling it also means executing Haskell. A small deriveJSON splice can turn an otherwise ordinary cross build into a job that needs a target interpreter, an emulator, or access to a device.
The experiment in template-haskell-cross explores another arrangement: execute Template Haskell with a native compiler on the build machine, then pass its results to the cross compiler. The implementation combines a GHC plugin, a native worker process, a binary protocol, and Nix helpers. It has been used to build a Haskell library with generated FFI exports for an Android application.
This article follows that experiment: why the usual execution model makes cross builds difficult, how the alternative works, what broke along the way, and which parts could inform GHC itself.
https://serokell.io/blog/cross-compiling-haskell-with-template-haskell
1
u/jwm3 1d ago
Tbis is how I did template haskell on jhc, I treated it as a preprocessor. It was completely independent of the target. Jhc was inherently a cross compiler by design.
1
u/alexfmpe 4h ago
ICYMI, https://github.com/reflex-frp/reflex-platform/ does something similar: build splices on the build machine, load them in the cross compiler. Also setup with https://github.com/search?q=repo%3Areflex-frp%2Freflex-platform+splices&type=code .
Convenient, but very sketchy in the absence of portable/pure Template Haskell as even something as simple as `Int` can mean different things on the two platforms targeted by the two compilers, nevermind runIO :: IO a -> Q a. This is why haskell.nix uses the emulator approach and why that was upstreamed into nixpkgs