Distributing a library via a .cma and dune

I have a library that takes a very long time to compile, and sometimes causes the compiler to stack-overflow depending on the system where it’s being built (yes, it’s autogenerated code :frowning: ).

I was wondering if it might be possible to distribute this library using the .cma file generated by dune. My ideal setup is that I would be able to build the library from scratch if I have an environment variable set, and otherwise dune would just copy an archive that I had previously promoted to the source directory over to the lib folder.

I’ve tried using various rules and installs but can’t seem to find a way to achieve this where dune is happy.

Any ideas? Thanks!

How does the stack trace look like when the compiler crashes? I’ve experienced crashes in register allocator (also generated code), and, if I remember correctly, in this case it might help to use -linscan to change the register allocation strategy.

If that doesn’t work, another approach is to use bytecode-compiled version of the native code compiler to compile your file using a custom dune rule. I’m not sure if it is available on all systems, but it is called ocamlopt.byte. You can set an OCAMLRUNPARAM parameter called l to increase the stack size, but it only affects byte-code–compiled ocamlopt.byte. However, it will produce native code.

OCAMLRUNPARAM='l=10000000' ocamlopt.byte <params…>

Not sure if of any consolation, but—correct me if I’m wrong—in OCaml 5.0 stack overflow will not be a problem because of the new heap-allocated stacks.

1 Like

Change the way you autogenerate your code it’s the only robust solution. Maybe try to look into what the compiler devs say in this discussion in case you find yourself in the same situation.

Or as @keleshev says make you library 5.0+ only :–). Though doing the right thing may also reduce compilation times (not sure).

1 Like

This did it, thank you! A band-aid solution for right now, but a solution nonetheless.

Oh, how I’ve tried. One day I’ll figure out how to clean up this mess…

Thanks @dbuenzli and @keleshev!

1 Like