Using Template Haskell to add libraries with which to link

Viewed 187

I'm currently hacking my way through trying to make quasiquotes for writing Rust code inline in Haskell. I think I have the code generation work done (including things like marshaling Haskell types to and from generated Rust ones). I now have the problem of figuring out how to do all the compilation and linking from within Template Haskell. The pipeline is as follows:

  1. The quasiquote gets parsed
  2. Source code is generated for
    • a corresponding Rust function
    • Haskell FFI imports
    • the Haskell call to the imported function
  3. The Rust code gets compiled into a static library (like rustc --crate-type=staticlib qq_function.rs -o qq_function.a)
  4. The Haskell code gets compiled and linked against qq_function.a (and a handful of other libraries like m, c, etc.)

My issue is getting steps 3 and 4 to happen entirely within TemplateHaskell. Here is as far as I've gotten:

  • runIO can write out the Rust source files that I've generated
  • addDependentFile informs GHC that the generated Rust file is a dependency
  • addForeignFile regrettably does not work for automatically managing the compilation since Rust is not a supported language (this is the approach inline-c takes since C is a supported language)
  • runIO could be used to generate the static Rust library (and delete the Rust source file afterwards) by calling out to rustc.

What is still very much not clear to me is

  1. how I can use Template Haskell to add libraries against which to link and
  2. how I can use Template Haskell to clean up these generated libraries afterwards?

EDIT

I've filed a GHC feature request related to this.

0 Answers
Related