I started playing with ocamlc
today, which I have mostly ignored until now, either preferring to use ocamlopt
(via dune, mostly), or executing small programs directly with ocaml
.
The first thing I tried to do was disassemble the bytecode, which I did not succeed in doing. I was directed to use ocamldumpobj
, which unfortunately is not in my path. (I have OCaml installed from opam, not from OS packages). It was mentioned that ocamldumpobj
is not always installed, and I may need to find dumpobj
in the OCaml tools directory. I searched my opam installation and found a dumpobj.ml
, but no executables.
So the first question is, what is the path forward for disassembling OCaml bytecode?
Next up, portability.
Like many Linux users, I keep user config files in git, along with the contents of my ~/bin directory for short useful scripts I like to carry around with me.
Also like many Linux users, I have a fancy shell prompt that gives various bits of useful information in fancy colors. In my case, this prompt is generated by an OCaml program (with a Python fallback in the ~/bin
directory if I haven’t compiled the OCaml prompt on the given system yet).
Thinking about these portable bytecode binaries, I thought perhaps I could dispense with my Python fallback prompt and instead put compiled OCaml bytecode executables into my ~/bin directory.
I discovered two possible issues, one of which appears to be solvable and the other which may not be. The first is that the path to the runocaml
executable is hard coded in the shebang. The first line of my compiled output:
#!/home/ninjaaron/.opam/5.3.0+flambda/bin/ocamlrun
Obviously this is not very portable. It contains my username and the path to a specific switch which may or may not exist on other machines.
I looked for a way to change this at compile time to something like #!/usr/bin/env ocamlrun
, which I didn’t find, but if absolutely necessary, it would be trivial to automate changing this with sed
or something.
It then occurred to me that the bytecode may not be stable between OCaml releases—and I soon discovered it’s actually worse than that. I have switches for 5.3.0 installed with and without flambda, and bytecode is not even compatible between these installations because the magic number is different.
So I guess my second question is, is there any way to have portable OCaml executables that are either a) not dependent on the architecture and libc (via ocamlopt
) or b) not dependent on the details of the local OCaml environment (via ocamlc
)?
Is it better to stick with my Python fallback? (My OCaml prompt script depends on a non-trival library which deals with launching and communicating with processes, so executing it directly with ocaml
is probably not a great option.)