# This makefile is adapted from Peter Miller's article
# "Recursive Make Considered Harmful"
#
# http://www.pcug.org.au/~millerp/rmch/recu-make-cons-harm.html

MODULES := Common Parser SynTree SymTab ResolvExpr CodeGen ControlStruct GenPoly Tuples InitTweak Designators #Try ArgTweak Explain
TARGET := cfa-cpp

all: $(TARGET)

# look for include files in each of the modules
CXX := g++
CXXFLAGS += -Wno-deprecated -Wall -g -DDEBUG_ALL -I. -I Common -MMD
INSTALL=/usr/bin/install -c

# this is the back-end compiler, used to compile libcfa & builtins to link with user code
BACKEND_CC := /u/pabuhr/bin/gcc

# uncomment the definition of this variable to enable purify
#  (do a "purerun" first)
#PURIFY := purify --cache-dir=$(HOME)/tmp

# extra libraries if required
LIBS :=

# each module will add to this
SRC := main.cc MakeLibCfa.cc

# other things that ought to be cleaned up
EXTRA_OUTPUT := core

# include the description for each module
include $(patsubst %,%/module.mk,$(MODULES))

# determine the object files
OBJ := $(patsubst %.cc,%.o,$(filter %.cc,$(SRC))) \
       $(patsubst %.y,%.tab.o,$(filter %.y,$(SRC))) \
       $(patsubst %.l,%.yy.o,$(filter %.l,$(SRC)))

# include the C include dependencies
DEPS := $(OBJ:.o=.d)
-include $(DEPS)

# link the program
$(TARGET): $(OBJ)
	$(PURIFY) $(CXX) -o $@ $(OBJ) $(LIBS)

#installing
install: $(TARGET)
	$(INSTALL) -d /u/pabuhr/software/cfa/cfa-cc/lib
	$(INSTALL) $(TARGET) /u/pabuhr/software/cfa/cfa-cc/lib

# clean-up rule
clean:
	rm -f $(OBJ) $(DEPS) $(TARGET) tags $(EXTRA_OUTPUT)
	find . -name "Expected*" -prune -o \( -name "*.tst" -o -name "report" \) -print | xargs rm -f
	find . -name "core*" -print | xargs rm -f

distclean: clean
