# Linux Makefile

#a guess -- the name of the current directory
TARGET = $(notdir $(CURDIR))
#TARGET = lib$(notdir $(CURDIR)).so
TARGET = ptester

DBG=1
#debug or optimize flags; do we strip the executable or not?
#to compile optimized and stripped, make DBG=0
ifeq ($(DBG),1)
DFLAGS = -g -w
STRIP =
else
DFLAGS = -O
#STRIP = -s
endif


SCRIPTS=../makescripts
QTDIR=/usr
MLABDIR=/cfarm/matlab/extern

#libraries to link to
LIBS = -lqt -lmx -leng -lm

#locations of include files
IFLAGS = -I. -I$(QTDIR)/include/qt -I$(MLABDIR)/include

#locations of library files
LFLAGS = -L$(QTDIR)/lib -Wl,-rpath $(QTDIR)/lib -L$(MLABDIR)/lib/glnx86 -Wl,-rpath $(MLABDIR)/lib/glnx86


############# You probably don't have to edit below this line ############# 

#setting some variables -- we don't want any of these overridden by
#environment variables, so we use override

#where are the scripts needed by the Makefile?
override MAKESCRIPT = $(SCRIPTS)

#script to create compile and depend rules from suffixes
override MKSUFFIXRULES = $(MAKESCRIPT)/mksuffixrules

#where are the C and C++ compilers?
override CPATH = /usr
override CC = $(CPATH)/bin/g++
override cc = $(CPATH)/bin/gcc


#how do we figure out dependencies?
override DEPENDFILTER = $(MAKESCRIPT)/dependfilter
override MAKEDEPEND = $(CC) -M

#script to decide whether a header file needs to be run through $(MOC)
override NEEDSMOC = $(MAKESCRIPT)/needsmoc

#what is the file of implicit rules named?
override RULES := Makefile.rules

#what do we use to make a static library?
override AR = /usr/ccs/bin/ar

#how do we invoke the Qt MOC?
override MOC = $(QTDIR)/bin/moc -f

#putting together the compiling and linking flags, respectively
override CFLAGS = $(DFLAGS) $(IFLAGS)
override LDFLAGS = $(LFLAGS) $(LIBS) $(STRIP)

SRCSUFFIXES := .C .cc .cxx .cpp
HDRSUFFIXES := .h .H .hxx .hpp

findbysuffixindir = $(shell find $(1) -name '*''$(2)' -print)
findbysuffix = $(shell find . -name '*''$(1)' -print)
findbyname = $(shell find . -name '$(1)' -print)

#infer the source files
SRCS := $(foreach suf, .c $(SRCSUFFIXES), $(call findbysuffix,$(suf)))

#infer the header files
HDRS := $(foreach suf, $(HDRSUFFIXES), $(call findbysuffix,$(suf)))

#decide on the necessary moc-generated files using $(NEEDSMOC)
MOCS := $(addsuffix _moc.C,$(basename $(shell $(NEEDSMOC) $(HDRS))))

#the object files are the sources and moc-generated sources with the .o suffix
OBJS := $(addsuffix .o,$(basename $(SRCS))) \
	$(addsuffix .o,$(basename $(MOCS)))

#the depend files are generated from each source file
DEPENDS := $(addsuffix .depend,$(basename $(SRCS)))

#don't keep moc-generated source
.INTERMEDIATE: %_moc.C $(MOCS)

#these are the suffixes we handle
.SUFFIXES: .o .c $(SRCSUFFIXES)

#these aren't files, just targets
.PHONY: all tidy clean depend topdir

#primary target
all: $(TARGET)

$(TARGET): $(OBJS)
ifneq ($(strip $(OBJS)),)

ifeq ($(suffix $(TARGET)),.so)
#it's a shared library
	$(CC) -G -o $@ $(OBJS) $(LDFLAGS) -lCstd -lC
else
ifeq ($(suffix $(TARGET)),.a)
#it's a static library
	$(AR) crv $@ $(OBJS) $(foreach name, $(TMPLDB), $(call findbysuffixindir,$(name),.o))
else
#it's an executable
	$(CC) -o $@ $(OBJS) $(LDFLAGS)
endif
endif

else
#no object files?
	@echo Warning: No object files to link!
endif

run: $(TARGET)
	./$(TARGET)

topdir:
	@echo $(CURDIR)

tidy:
	rm -f core ir.out
	rm -rf $(foreach name, $(TMPLDB), $(call findbyname,$(name)))
	rm -f $(shell find . -name '*~' -print)
	rm -f $(DEPENDS) $(OBJS) $(MOCS)
	rm -f $(RULES)
	rm -f errlist

clean: tidy
	rm -f $(TARGET)

TAGS: $(SRCS) $(HDRS)
	@rm -f TAGS
	/cs/bin/gnu/etags -Sw $(SRCS) $(HDRS)

tags: $(SRCS) $(HDRS)
	@rm -f tags
	/cs/bin/gnu/ctags -STtwd $(SRCS) $(HDRS)

%_moc.C: %.h
	$(MOC) $< -o $@

%_moc.C: %.H
	$(MOC) $< -o $@

%_moc.C: %.hxx
	$(MOC) $< -o $@

%_moc.C: %.hpp
	$(MOC) $< -o $@

$(RULES): Makefile $(MKSUFFIXRULES)
	$(MKSUFFIXRULES) $(SRCSUFFIXES) > $@

%.o: %.c
	$(cc) $(CFLAGS) -o $@ -c $<

%.depend: %.c
	$(MAKEDEPEND) $(IFLAGS) $< | $(DEPENDFILTER) $@ > $@

include $(RULES)

# DO NOT DELETE
