# Makefile by Branden J. Moore <bmoore@cse.nd.edu>

PROG = program
SRCDIR = .
CCODE = main.c
INCDIRS = $(SRCDIR)
LIBDIRS =
LIBS =
LDFLAGS =

CC = gcc
CFLAGS = -Wall -Werror
COPT = -O3
CDEBUG =


# Should not need to edit below this line

SHELL = /bin/sh
OS = $(shell uname -s)

CCVER = `$(CC) -v`
ifeq "$(findstring gcc,$(CCVER))" "gcc"
    DEPFLAG = -MM
else
    DEPFLAG = -M
endif

SOURCE = $(CCODE:%=$(SRCDIR)/%)
OBJECTS = $(SOURCE:.c=.o)
INCLUDES = $(INCDIRS:%=-I%)
LDLIBS = $(LIBDIRS:%=-L%) $(LIBS)
DEPENDENCIES = $(SOURCE:.c=.d)

ifeq "$(findstring debug,$(MAKECMDGOALS))" "debug"
    CFLAGS += -g -DEBUG
    COPT = -O0
endif

.PHONY: all depend debug clean distclean update

.SUFFIXES:
.SUFFIXES: .d .c .o

.DEFAULT: all

CFLAGS += $(INCLUDES) $(COPT) $(CDEBUG)

all debug:  depend $(PROG)

$(PROG): $(OBJECTS)
    $(LINK.o) $(LDFLAGS) $^ -o $@ $(LDLIBS)

%.o : %.c
    $(CC) $(CFLAGS) -c $<

%.d : %.c
    @set -e; rm -f $@; \
        $(CC) $(DEPFLAG) $(CPPFLAGS) $(CFLAGS) $< > $@.$$$$; \
        sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
        $(RM) $@.$$$$
    @echo $@

clean:
    $(RM) $(OBJECTS)

distclean: clean
    $(RM) $(DEPENDENCIES) $(PROG)

depend: $(DEPENDENCIES)
ifeq ($(DEPDONE),1)
    $(MAKE) DEPDONE=1 $(MFLAGS) $(subst depend,,$(MAKECMDGOALS))
endif

update:
ifeq "$(wildcard CVS)" "CVS"
    @cvs update -dP .
else
    @echo "Not in CVS"
endif

ifneq "$(wildcard $(SRCDIR)/*.d)" ""
ifneq "$(findstring clean,$(MAKECMDGOALS))" "clean"
include $(wildcard $(SRCDIR)/*.d)
endif
endif