John Wickerson
John Wickerson

Reputation: 1232

Require specific OCaml version in makefile

My OCaml program uses some functions in the OCaml standard library that were only introduced in version 4.01.0. How can I arrange that when my user compiles my code, the compiler emits an error if the OCaml compiler's version is not 4.01.0 or higher? I feel that this error would be more helpful than just a generic "unbound variable" error.

I see that ocaml -vnum emits "4.01.0" so I guess I could try to check that in my Makefile, but perhaps there is a proper way to do this already? I'm using OCamlBuild, if that helps.

My current Makefile looks like this, by the way:

all:
    @ echo "Attention: requires OCaml version >= 4.01.0."
    ocamlbuild -cflag -annot -lib str -lib unix name_of_my_project.native
    mv name_of_my_project.native name_of_my_project

clean:
    ocamlbuild -clean
    rm -f name_of_my_project

Upvotes: 1

Views: 202

Answers (2)

ivg
ivg

Reputation: 35280

There is always more than one proper way :) My proper way would be to use oasis. Others would probably use Autotool AC_PROG_OCAML with the specified version. There are other build systems, that probably have their own methods. When you will release your package to opam (it is also very easy), you will add the constraints to the opam file, so that the package manager will not even try to build it.

So, to summarize my personal advice is to use oasis. It is very simple, but still quite versatile. In your case the _oasis control file should looks something like this:

OASISFormat: 0.4
Name:        name
Version:     0.0.1
OCamlVersion: >= 4.01.0
Synopsis:    Cool package
Authors:     Myself
Maintainers: Myself
License:     MIT
Copyrights:  (C) Myself
Plugins:     META (0.4), DevFiles (0.4)
BuildTools: ocamlbuild


Executable "project"
  Path:           src
  MainIs:         project.ml
  CompiledObject: best
  BuildDepends:   str, unix

Once the file is created, you can create a configure script with

oasis setup

And after it is ready (it will also create the setup.ml file), you can use the common:

 ./configure
  make
  make install

Upvotes: 1

alavrik
alavrik

Reputation: 2161

If you already have a Makefile, inserting a check there would probably be the most pragmatic way to achieve this. Can be done using either Makefile expressions or shell. Here's a shell version of a similar check: https://github.com/alavrik/piqi/blob/master/configure#L144

Somewhat related, I was looking for conditional compilation for OCaml and ended up using optcomp. Here's an example.

Upvotes: 1

Related Questions