Generating C code from OCaml ppx

My assumption is that you can’t use PPXes to write a separate C file. Well … as a side-effect of running the PPX you can write to a file but that sounds brittle.

Although I didn’t document my use cases in my recent DkCoder scripting article ([ANN] DkCoder 0.1.0), I have one use case where I need to compile OCaml into larger C executables while simultaneously not requiring a C compiler. Eventually I’ll be adding a feature to do linking (irrelevant to you) and do some light symbol modifications (perhaps relevant) inside DkCoder.

I have been looking into LIEF to perform those symbol modifications on existing ELF/Mach-O/PE libraries: 04 - ELF Hooking — LIEF Documentation. Kinda like patchelf but cross-platform and not as buggy. (The linked example is Python but it has a C++ API and a limited C API). So …

  1. Perhaps a small make_nbkit_ocaml_plugin executable can take your users’ OCaml-compiled libraries and add/rename/duplicate the symbols you need to make it a full-fledged nbdkit plugin. LIEF can help do that.
  2. If your plugin needs are light enough that bytecode compiled plugins are sufficient, ping me offline.