Dune and embedding OCaml byte code


#1

Hi all,

I’ve written a transpiler in OCaml and would like to embed it in an application. Pretty sure I get the gist of this from Real World OCaml however I’ve been fiddling around with dune and can’t seem to figure out how to get this working. Can anyone point me to an example of this that uses dune, preferably to -output-obj a c file linkable with the OCaml runtime?

Many thanks,
Jason


#2

It would be easier to help you if you could show us some code?


#3

Could you give us some more details of your design?

  • What code is in control? Is the app calling the OCaml code or vice versa?
  • What is the relevance of the bytecode? Is it loaded at runtime?

#4

I’m not sure about your needs. Maybe, this repository may help.

Best regards


#5

Many thanks, this is very helpful! I’ve forked your repo and extended the demo to illustrate doing the same via an object file (repo). If you would like me to make a pull request let me know.

What I’d really like is to get dune to generate a c file with the OCaml bytecode via -output-obj filename.c so that I can ship this around and only need to link to libcamlrun. I’m pretty sure however that dune doesn’t support this currently.

Thanks again,
Jason


#6

Certainly not. But it may be possible to write a (rule ) to generate the C file?


#7

In my old cludgie Makefile approach the only change needed would be to modify:

ocamlc ... -output-obj filename.o

to

ocamlc ... -output-obj filename.c

This is the first project I’ve tried using dune so take with a grain of salt but I think a mode such as “cfile” and an “%{ext_c}” extension would be needed for this to be simple. I’m sure this could be done using rules in several dune files if you know dune well enough.


#8

I’ve created a simple example of why one might be interested in using ocamlc to output C code containing OCaml bytecode (repo).


#9

Maybe, you should ask directly dune developers by opening a ticket on github?


#10

See pull request #1229.


#11

Nice. I did not spot it.