Using autoconf for OCaml projects

Today Zack and I released the first version of ocaml-autoconf, which is a collection of OCaml-related autoconf macros. That package won’t tell you how to use autoconf though, and that is something that people wrongly regard as being incomprehensible or too hard to learn. In this tutorial you’ll see that you only need to create two files to start even a complex autoconf / OCaml project.

The download which goes along with this tutorial is: ocaml-autoconf-example-1.0.tar.gz

configure.ac

The first file controls autoconf. It is called configure.ac and looks like this. Notice the use of the OCaml autoconf macros which are documented in the ocaml.m4 manual page.

AC_INIT(ocaml-autoconf-example,1.0)
AM_INIT_AUTOMAKE

AC_PROG_CC

AC_PROG_OCAML
if test "$OCAMLC" = "no"; then
  AC_MSG_ERROR([You must install the OCaml compiler])
fi

AC_PROG_FINDLIB
if test "$OCAMLFIND" = "no"; then
  AC_MSG_ERROR([You must install OCaml findlib (the ocamlfind command)])
fi

AC_CHECK_OCAML_PKG([lablgtk2])
if test "$OCAML_PKG_lablgtk2" = "no"; then
  AC_MSG_ERROR([Please install OCaml findlib module 'lablgtk2'.])
fi

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Makefile.am

The second file controls automake. In fact we’re not really using automake’s capabilities, so our Makefile.am looks almost like an ordinary Makefile:

bin_SCRIPTS = gtk_test$(EXEEXT)

PACKAGES = -package lablgtk2
LIBS     = gtkInit.cmo -linkpkg

gtk_test$(EXEEXT): gtk_test.cmo
	$(OCAMLFIND) ocamlc $(PACKAGES) $(LIBS) $^ -o $@

.ml.cmo:
	ocamlfind ocamlc $(PACKAGES) -c $< -o $@

EXTRA_DIST = gtk_test.ml

CLEANFILES = *.cmi *.cmo $(bin_SCRIPTS) *~

m4/ocaml.m4

Those are the only two files you have to write, but you will also need to copy the OCaml autoconf macros (ocaml.m4) into an m4/ subdirectory:

mkdir m4
cp /usr/share/aclocal/ocaml.m4 m4/

autoreconf

Finally run autoreconf to run autoconf and automake and generate the ./configure script. Because automake defaults to assuming your project conforms to all the GNU coding standards, you probably want to add the automake --foreign flag, so:

AUTOMAKE='automake --foreign' autoreconf -i

Finished

Now you’re done. You can copy the gtk_test.ml program into this directory, and try:

./configure
make
make dist
make install

etc.

Download: ocaml-autoconf-example-1.0.tar.gz

Update #1: Zack’s blog posting about ocaml-autoconf

Update #2: See comments.

About these ads

9 Comments

Filed under Uncategorized

9 responses to “Using autoconf for OCaml projects

  1. Do you have a Makefile.am example using camlp4?

  2. rwmj

    Andre: It depends what particular syntax extension(s) one is using. The easiest thing to do is to add the appropriate -package and -syntax parameters to the ocamlfind command line. You can find examples here and here.

  3. Thanks for the post. It is always good to have a quick getting started example. The Makefile.am could use some improving.
    As it stands here you cannot ‘make dist’. This is because automake assumes the source files to build the program are in C. The target to build that binary just happen to work because we have overwritten it.
    I have this in my Makefile.am:

    FINDLIB_FLAG = -package unix -linkpkg
    bin_PROGRAMS = myprog
    myprog_SOURCES = myprog.ml

    SUFFIXES = .ml

    ml.cmx:
    $(OCAMLFIND) opt $(FINDLIB_FLAG) -c $^ -o $@

    LINK = $(OCAMLFIND) opt $(FINDLIB_FLAG) $^ -o $@

    CLEANFILES = *.cmi *.cmx *.annot *.cmo *~

  4. I’m not familiar with autoconf. Granted, this post is not an autoconf tutorial. However, I’ve been using the macros as explained in this post, with a prior aclocal -I m4 for my m4/ocaml.m4 file. I get

    configure.ac:6: error: possibly undefined macro: AC_PROG_OCAML

    Could you quickly explain what to do? If I get this error, no doubt other autocof noobs will encounter when trying to apply this tutorial post.

    • rich

      There could be several reasons for this. I think most likely you’ve missed out this line from configure.ac:

      AC_CONFIG_MACRO_DIR([m4])

      or this one in Makefile.am:

      ACLOCAL_AMFLAGS = -I m4

      • Indeed. This solves the problem. However, the problem with autoconf is not autoconf itself, but rather the workflow it imposes with, say, git.

        When you need to make clean, it’s possible that you do not have your Makefile available. Regenerating the Makefile each time you compile your stuff can take a while… By the way, you distribute the Makefile.in, while keeping only the Makefile.am under source control.

        How do you guys cope with all these little annoyances?

      • rich

        I’m not going to pretend that autoconf is good. But for the Fedora cross-compiler project I had to cross-compile about a hundred packages using all sorts of different build systems, and autoconf/automake-based ones were by far the most reliable. Autoconf isn’t good, but it’s much better than the alternatives once you start needing to compile on many different platforms, cross compile, or integrate with Linux distro packaging systems.

  5. maalosh

    Wow ! I’m finding this great tool just now. I have only one question : how do you use autoconf to install a library so that ocamlfind can use it ?

    Thanks.

    • rich

      You need to write a META file, put everything in a subdirectory of the OCaml directory, and it should just work. If I were you, I’d take a look at some existing library, eg. /usr/lib*/ocaml/pcre/*

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s