Adam Miller
Adam Miller

Reputation: 1783

How to use OCamlMakefile to generate a library consumed by subproject?

Using OCamlMakefile one can specify sources and it automatically determines the suffix and alleviates much of the headache in determining what the file suffixes should be for various targets. So if I want to use some ml source to build to byte code while developing and then deploy to native code once I'm done, it's just:

make bc

and then:

make nc

I have an issue where I want two different binaries to be generated and they have a single common piece of source. Originally, the makefile was organized like this:

ifndef SUBPROJS
    export SUBPROJS = somesubproj 
endif

define PROJ_somesubproj
    RESULT=OnlyResult
    SOURCES = common.ml containsmain.ml
    DOC_FILES=$(SOURCES)
endef
export PROJ_somesubproj

doing make on it just built OnlyResult. But now I have another result I want to produce that has common.ml as a consumed item, but it's built from a different directory in a different project; common.ml is part of this directory but not the other thing that consumes it by library organization, so it's not right to put common.ml as a source in the other project that is now consuming it. I want to split the subproj into two separate goals, one that produces a common.cma or common.cmxa and then another that consumes the common library. Then in my out of directory build, I can just put common as a lib, as it should be.

How do I do that in make though? How do I specify that common be build as a library without containsmain?

Upvotes: 1

Views: 317

Answers (1)

Markus Mottl
Markus Mottl

Reputation: 121

I would strongly suggest considering more modern alternatives to the venerable old OCamlMakefile. It works fine for small to possibly medium-sized projects, but does not really scale so well to complex projects with many subprojects due to limitations of "make". It also isn't actively developed anymore.

If you need a high degree of portability, "ocamlbuild", which is part of the OCaml standard distribution, is a reasonable choice. You may want to even generate "ocamlbuild" files automatically using Oasis for packaging. This is especially recommended if you are intending to distribute your work as open source. Oasis can deal with some complex build scenarios, e.g. libraries shared between several executables it has to build.

Another mature build tool is OMake, which scales to large, complex projects and is quite easy to learn if you already know "make".

You may even want to consider Jenga, which, AFAIK, is not considered stable enough for public use yet, but may eventually exceed OMake in terms of capabilities and ease of use.

Upvotes: 1

Related Questions