[Omake] Local OCaml libraries with C code

Jason Hickey jyh at cs.caltech.edu
Fri Oct 5 09:33:28 PDT 2007


Alain,

I would say that the association between OCaml libraries and C code  
is a matter of convention.  For any particular OCaml library, you  
could have multiple C implementations, or you could have a single C  
library for all your OCaml libraries.  In our projects, we just keep  
track of OCaml and C libraries as if they were independent.

But that's really just a wimp-out--it makes a lot of sense what you  
are trying to do.  I would change how you do it so that, instead of  
collecting the dependencies in the root OMakefile, you collect them  
through a "fake" target.  This is a standard trick for collecting  
global dependencies.

The steps are:
    - AddImplicitCLib(cma, libs) adds the dependency as an explicit rule
      (this can be anywhere in the project)
    - The GetImplicitCLibs(cma) uses the function "dependencies" (or  
"target") to
      collect the dependencies.

Here is an example.

     X = $(file x)

     ImplicitCLibName(name) =
         value $(name).implicit-clib

     AddImplicitCLib(cma, libs) =
         $(ImplicitCLibName $(cma)):: $(libs)
             touch $@

     GetImplicitCLibs(name) =
         dependencies($(ImplicitCLibName $(name)))

     .DEFAULT: $(ImplicitCLibName $X)
         echo Dependencies of $X are $(GetImplicitCLibs $X)

     .SUBDIRS: a
         AddImplicitCLib($X, foo)

     .SUBDIRS: b
         AddImplicitCLib($X, bar)

This prints:

     Dependencies of x are b/bar a/foo

One thing about this example is that it actually creates the file  
x.implicit-clib.  Instead, you could make the clibs to be  
dependencies of the .cma directly.  Then GetImplicitCLibs would need  
to filter out the clibs from the other dependencies, but there would  
be no need for the fake file.  Ideally, OMake would support  
annotations, so you could mark up rules without changing the actual  
dependencies.

     foo.cma: :annotate-clib: foo.o

Jason

On Oct 1, 2007, at 7:34 AM, Alain Frisch wrote:

> Hello,
>
> How do people deal with local OCaml libraries that contain C code in
> OMake?  By local, I mean local to the current project, without findlib
> support.
>
> To build such a library, one can do something like:
>
> OCAMLFLAGS = -cclib -lmylib
> OCAMLCFLAGS = -custom
> OCAMLLIBS = $(OCamlLibrary ocaml_lib, ...)
> $(OCAMLLIBS): libmylib$(EXT_LIB)
>
> But then, every time one links a program against this library, one  
> must
> add an explicit dependency to libmylib$(EXT_LIB) (and extend
> OCAMLINCLUDES with the directory of this file). The reason is that  
> even
> if we tell OMake that ocaml_lib.cma depends on libmylib$(EXT_LIB),  
> this
> is not really the case: if libmylib$(EXT_LIB) changes, ocaml_lib.cma
> will not, but the program must be re-linked.
>
> Currently what I do is to use a global map to store those implicit
> dependencies:
>
> IMPLICIT_OCAML_CLIBS = $(Map)
>
> AddImplicitCLib(dir, cma, libs) =
>   libs = $(file $(add-wrapper $(dir)/, $(EXT_LIB), $(libs)))
>   IMPLICIT_OCAML_CLIBS = $(IMPLICIT_OCAML_CLIBS.add $(file
> $(dir)/$(cma)), $(libs))
>   export
>
> GetAllImplicitCLibs(cmas) =
>   ILIBS=
>   foreach(l, $(cmas))
>     try
>       ILIBS += $(IMPLICIT_OCAML_CLIBS.find $(file $(l)))
>       export
>     catch Exception(v)
>     export
>   return $(ILIBS)
>
>
> The function OCamlProgram is extended with:
>
>    protected.ICLIBS = $(GetAllImplicitCLibs $(MLFI_LIBS))
>    MLFIINCLUDES += $(dir $(set $(dirof $(ICLIBS))))
>
> and ICLIBS is added to the dependencies $(BYTEPROG) and $(OPTPROG).
>
> Now, in the root OMakefile, I have a sequence of calls to
> AddImplicitCLib, like:
>
> AddImplicitCLib($(ROOT)/mylibdir, ocaml_lib, libmylib)
>
>
> This seems to work, though I'd prefer to put this call to
> AddImplicitCLib in the directory where the library is, not in the root
> of the project. (This does not work because of scoping.)
>
> Anyway, I'd like to hear how other people would deal with the case of
> Caml libraries that depend on C libraries.
>
>
>   Alain
>
>
> _______________________________________________
> Omake mailing list
> Omake at metaprl.org
> https://lists.metaprl.org/mailman/listinfo/omake

--
Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257





More information about the Omake mailing list