views:

119

answers:

3

Is there a mechanism in make to allow for default global implicit rules that are available anywhere, similar to the built-in rules?

Make provides some built-inimplicit rules for compiling C/C++/Fortran files, without even requiring a Makefile for simple cases. However, when compiling other languages (e.g. Go programming language files), a Makefile is always required. I would like to extend my Makeenvironment to have implicit rules available by default.

+3  A: 

This is not normally desirable, as it would cause your Makefile to be less portable; it wouldn't work on somebody else's machine if they didn't have it set up that way.

However, if you want to do this, create a "global" Makefile somewhere with your default rules for Go files, then add its path to the MAKEFILES environment variable. This global Makefile will be processed before any Makefile when you run "make", just as if you had included its source at the top of the file.

dmazzoni
Thanks! Agreed with your caution. However, I would like to compile throw-away code without writing Makefiles all the times.
notnoop
A: 

I'm assuming you're referring to the fact that you can do

make hello.o

and make will automatically know how to make the .o from a .c file (or indeed from a .f or .p, if one exists) - but you want to do this for custom file types (say, building a .bar from a .foo.

The most portable way of doing this is as follows (in your Makefile):

.SUFFIXES: .foo .bar
.foo.bar:
        foo2bar -in $> -out $@

The first line (.SUFFIXES) warns make that you'll be treating these as special suffixes; the second line says "here's a recipe for making a .bar from a .foo. The third line gives the command for doing this - $> and $@ get changed by make to the input and output filenames.

NOTE: The indent for the third line MUST be a tab character.

A much more flexible method, that only works with GNU make, is to use its support for implicit rules. If you can guarantee you'll be using GNU make then this is probably to be recommended.

psmears
A: 

While I agree with dmazzoni, I just though I'd add my make recipe for a Go Makefile:

# Include default Golang Make magic
include $(GOROOT)/src/Make.$(GOARCH)

# Hack the following line    
your_program: your_program.$O
    $(LD) -o $@ $^

# Compiles .go-files into architecture-specific binaries
%.$O: %.go
    $(GC) -o $@ $^

clean:
    rm your_program *.$O

(Note: the $O is DOLLAR + UPPERCASE-o - not zero!)

While I haven't tested it on all the machines I have available, i believe it should port fairly well.

Morten Siebuhr
Ah, even simpler: http://github.com/jlouis/go-blip/blob/master/Makefile (haven't tested it though...)
Morten Siebuhr