Is it possible to compile OCaml codes using Core without corebuild?

400 Views Asked by At

I'm using Ubuntu 18.04. I have OCaml 4.05 installed (via apt-get) as well as utop and Core (via opam). And this is the content of my ~/.ocamlinit:

(* Added by OPAM. *)
let () =
  try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found -> ()
;;

#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;

Compiling OCaml codes which use Core works fine with corebuild. It doesn't work with plain ocamlc or ocamlopt though. They complain:

Error: Unbound module Core

And I found corebuild to be pretty picky. It doesn't like any existing non-OCaml codes in the same directory:

SANITIZE: a total of 12 files that should probably not be in your source tree
  has been found. A script shell file "/home/anta40/Codes/_build/sanitize.sh"
  is being created. Check this script and run it to remove unwanted files or
  use other options (such as defining hygiene exceptions or using the
  -no-hygiene option).
IMPORTANT: I cannot work with leftover compiled files.
ERROR: Leftover object files:
  File hellod.o in . has suffix .o
  File helloscheme_.o in . has suffix .o
  File nqueens.o in . has suffix .o
  File helloscheme.o in . has suffix .o
  File HelloHS.o in . has suffix .o
  File helloml.o in . has suffix .o
ERROR: Leftover OCaml compilation files:
  File nqueens.cmo in . has suffix .cmo
  File helloml.cmo in . has suffix .cmo
  File helloml.cmi in . has suffix .cmi
  File nqueens.cmi in . has suffix .cmi
  File nqueens.cmx in . has suffix .cmx
  File helloml.cmx in . has suffix .cmx
Exiting due to hygiene violations.
Compilation unsuccessful after building 0 targets (0 cached) in 00:00:00.

Using the -no-hygiene option, e.g corebuild sum.ml -no-hygiene doesn't produce any native executable. Is there any workaround? `

2

There are 2 best solutions below

2
tbrk On

The OPAM package manager can support multiple OCaml instances, where each groups an OCaml installation (ocaml, ocamlc, ocamlopt, etc.) with installed packages. Each instance is called a switch.

Type ocaml switch to see a list of instances. In terms of switches, the installation of OCaml using packages (apt-get) is called system.

It may be better to avoid mixing the system and OPAM installations. I suggest that you create a new switch, ensure that the OPAM settings are properly sourced, and install Core into it the new switch :

opam switch 4.06.1
eval $(opam config env)
opam install core

The automatic build tools assume that they control the entire build process and complain when they find other compilation results. Normally, running the suggested shell script and retrying side-steps the problem, even if only to uncover another one. Your second problem (with -no-hygeine) may be related to the installation issues.

0
octachron On

Your installation is dated.

I imagine that you followed instructions in Real World OCaml but OCaml ecosystem has moved since the first edition of RWO. To get a modern installation you should follow the dev version of Real World OCaml https://dev.realworldocaml.org/install.html .

To answer your question, corebuild is a fine wrapper around ocamlbuild. To build an executable from a sum.ml with corebuild, you should use

corebuild sum.native

for native executable or

corebuild sum.byte

for bytecode compilation

Otherwise, you can invoke ocamlopt through ocamlfind:

ocamlfind ocamlopt -package core sum.ml

Finally, if you intend to build larger project, you ought to have a look to dune https://jbuilder.readthedocs.io/en/latest/overview.html .