caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Jeff Shaw <shawjef3@msu.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] A Tutorial on GNU Make with OCaml
Date: Fri, 21 May 2010 10:28:19 +0200	[thread overview]
Message-ID: <87k4qxveu4.fsf@frosties.localdomain> (raw)
In-Reply-To: <4BF603AA.4030703@msu.edu> (Jeff Shaw's message of "Thu, 20 May 2010 23:53:14 -0400")

Jeff Shaw <shawjef3@msu.edu> writes:

> Dear OCamlers,
> I spent quite a lot of time today getting better acquainted with GNU
> make and decided I to share my experience and results. It's a bit on
> the newbie friendly side of instruction.
>
> http://shawjeff.blogspot.com/2010/05/this-is-storytutorial-about-how-i_20.html
>
> I hope someone finds it helpful!
>
> Jeff Shaw
> shawjef3@msu.edu

Some comments:

- For things you don't want to be deleted even though make sees they
will never be reused on subsequent make calls look up .PRECIOUS

- Targets that have no dependencies and create no file are implicitly
PHONY. No need to specify it as such. A reason for why one might do it
anyway would be to protect against someone doing "touch clean" by
accident:

% touch clean
% make clean
make: `clean' is up to date.

A .PHONY: clean would prevent that and run clean anyway.

- Don't create interface files if there are none

If you start writing a module and haven't decided on the exact interface
it will be a pain to delete the generated interface file every time. It
is also completly useless as you can generate the cmi file from the .ml
file directly. No need to create .mli first.

- Use ocamldep

Ocamldep figures out wich modules depend on other modules and generates
that in Makefile syntax for you. Unfortunately ocamldep lacks an option
to generate a dependency line for the final binary so SOURCES below must
be set by hand and in the right order.

- List source files, not generated files and use pattern substitution to
get the later


Here is my own version of a Makefile for a trivial project. I hardcoded
it for native code but you already know how to use 'TARGET=byte' from
your Makefile. Merging the two is left as an execise.

PROG       := prog
LIBS       := graphics unix
SOURCES    := foo.ml bar.ml baz.ml

# No user servicable parts below
INTERFACES := $(wildcard *.mli)
OBJS       := $(patsubst %.ml,%.cmx,$(SOURCES))
LIBS       := $(patsubst %,%.cmxa,$(LIBS))

# Count number of cores and use them all, no idea how do to that for windows
PARALLEL   := -j$(shell cat /proc/cpuinfo | grep processor | wc -l)

all:
	$(MAKE) $(PARALLEL) $(PROG)

$(PROG): $(OBJS)
        ocamlopt -o $@ $(LIBS) $(OBJS)

clean:
        rm -rf $(PROG) *.o *.cmx *.cmi *~

%.cmx: %.ml
        ocamlopt -c $*.ml

%.cmi: %.mli
        ocamlopt -c $*.mli

.depend: $(SOURCES) $(INTERFACES)
        ocamldep -native $(SOURCES) $(INTERFACES) >.depend

include .depend


      parent reply	other threads:[~2010-05-21  8:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-21  3:53 Jeff Shaw
2010-05-21  5:18 ` [Caml-list] " Michael Grünewald
2010-05-21  8:17   ` David Allsopp
2010-05-21  8:42     ` Michaël Grünewald
2010-05-21  8:28 ` Goswin von Brederlow [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k4qxveu4.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=shawjef3@msu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).