I'm putting together an intro OCaml project for a CS class and part of it involves implementing list operations. I want them to be able to use Pervasives, but not List or any other standard library modules. Is there a way to set up ocamlbuild so it only links against Pervasives in the standard library?
I see two opportunities: either remove module directly from the OCaml standard library or hide them by overloading with a module with different (possibly empty) signature.
The first variant requires editing OCaml distribution Makefiles. With opam and is not that scary, actually, as you can patch OCaml quite easily and distribute each patched OCaml as a separate compiler. To remove module from the stdlib archive you will need to edit stdlib/Makefile.shared
, stdlib/StdlibModules
, and stdlib.mllib
. After you've removed the unnecessary modules, you can do:
./configure
make world.opt
make install
Another option is to (ab)use the -open
command line argument of ocamlc
. When this option is specified with a name of a module, this module will be automatically opened in the compiled program. For example, you can write your own overlay over a standard library, that has the following interface (minimal.mli
):
module List = sig end (* or whatever you want to expose *)
and then you can compile either with ocamlc -open minimal ...
, or, with ocamlbuild
: ocamlbuild -cflags -open,minimal ...
(you can also use _tags
file to pass the open flag, or write an ocamlbuild plugin).