Go to the first, previous, next, last section, table of contents.

Using `make'

It is reasonably straightforward to set up a `Makefile' to use with GHC, assuming you name your source files the same as your modules. Thus:
HC      = ghc
HC_OPTS = -cpp $(EXTRA_HC_OPTS)

SRCS = Main.lhs Foo.lhs Bar.lhs
OBJS = Main.o   Foo.o   Bar.o

.SUFFIXES : .o .hi .lhs .hc .s

cool_pgm : $(OBJS)
        $(RM) $@
        $(HC) -o $@ $(HC_OPTS) $(OBJS)

# Standard suffix rules
.o.hi:
        @:

.lhs.o:
        $(RM) $@
        $(HC) -c $< $(HC_OPTS)

.hs.o:
        $(RM) $@
        $(HC) -c $< $(HC_OPTS)

# Optional
.hc.o:
        $(RM) $@
        $(HC) -c $< $(HC_OPTS)

# Optional
.s.o:
        $(RM) $@
        $(HC) -c $< $(HC_OPTS)


# Inter-module dependencies
Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
(Sophisticated `make' variants may achieve some of the above more elegantly. Notably, `gmake''s pattern rules let you write the more comprehensible:
%.o : %.lhs
        $(RM) $@
        $(HC) -c $< $(HC_OPTS)
What we've shown should work with any `make'.) Note the cheesy `.o.hi' rule: It records the dependency of the interface (`.hi') file on the source. The rule says a `.hi' file can be made from a `.o' file by doing... nothing. Which is true. Note the inter-module dependencies at the end of the Makefile, which take the form
Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
They tell `make' that if any of `Foo.o', `Foo.hc' or `Foo.s' have an earlier modification date than `Baz.hi', then the out-of-date file must be brought up to date. To bring it up to date, `make' looks for a rule to do so; one of the preceding suffix rules does the job nicely. Putting inter-dependencies of the form `Foo.o : Bar.hi' into your `Makefile' by hand is rather error-prone. Don't worry -- never fear, `mkdependHS' is here! (and is distributed as part of GHC) Add the following to your `Makefile':
depend :
        mkdependHS -- $(HC_OPTS) -- $(SRCS)
Now, before you start compiling, and any time you change the `imports' in your program, do `make depend' before you do `make cool_pgm'. `mkdependHS' will append the needed dependencies to your `Makefile'. `mkdependHS' is fully describe in Section See section Makefile dependencies in Haskell: using `mkdependHS'. A few caveats about this simple scheme: (a) You may need to compile some modules explicitly to create their interfaces in the first place (e.g., `make Bar.o' to create `Bar.hi'). (b) You may have to type `make' more than once for the dependencies to have full effect. However, a `make' run that does nothing does mean "everything's up-to-date." (c) This scheme will work with mutually-recursive modules but, again, it may take multiple iterations to "settle."
Go to the first, previous, next, last section, table of contents.