#------------------------------------------------------------------------------
# General Settings
#------------------------------------------------------------------------------

TOPDIR = ../..

include $(TOPDIR)/Makefile.defs
include $(TOPDIR)/mf/common.defs

.SUFFIXES: .cpp .o .sh.o
.PHONY: all install clean


# Compiler settings
CCXXFLAGS := $(CCXXFLAGS) $(SZLIB_CFLAGS)

# Object files
OBJS = \
	AggrCube.o \
	AggrCubeMaps.o \
	Cartesian.o \
	Cnode.o \
	Cube.o \
	Cube2Parser.o \
	Cube3Parser.o \
	Cube2Scanner.o \
	Cube3Scanner.o \
	Driver.o \
	Machine.o \
	Matrix.o \
	Metric.o \
	Node.o \
	Process.o \
	Region.o \
	Row.o \
	Thread.o \
	Vertex.o \
	cube_error.o \
	services.o

ifneq ($(SZLIB_LIB),)
OBJS += \
	zfstream.o
endif

SHOBJS=$(OBJS:.o=.sh.o)


# Installed header files
USER_HDRS = \
	Cube.h \
	cube_error.h

# Libraries
LIBRARIES = libcube3.a
ifneq ($(ESH_AR),)
  LIBRARIES += libcube3so.so
endif


#------------------------------------------------------------------------------
# Rules
#------------------------------------------------------------------------------

all: $(LIBRARIES) cube-config

install: all
	$(MKDIR) -m 755 $(BINDIR)
	$(INSTALL) -c -m 755 cube-config $(BINDIR)
	$(MKDIR) -m 755 $(LIBDIR)
	$(INSTALL) -c -m 644 libcube3.a $(LIBDIR)
	if test -f libcube3so.so; then \
		$(INSTALL) -c -m 644 libcube3so.so $(LIBDIR) ; \
	fi
	$(MKDIR) -m 755 $(INCDIR)/cube3
	@list='$(USER_HDRS)'; for p in $$list; do \
		echo "$(INSTALL) -c -m 644 $$p $(INCDIR)/cube3" ; \
		eval "$(INSTALL) -c -m 644 $$p $(INCDIR)/cube3" ; \
	done

clean:
	-rm -f cube-config libcube3.a libcube3so.so $(OBJS) $(SHOBJS)


grammar:
	bison -t -v -d -o Cube3Parser.c --skeleton=lalr1.cc Cube3Parser.yy
	cp Cube3Parser.c Cube3Parser.cpp
	rm Cube3Parser.c
	flex -f -o Cube3Scanner.cpp Cube3Scanner.ll
	bison -t -v -d -o Cube2Parser.c --skeleton=lalr1.cc Cube2Parser.yy
	cp Cube2Parser.c Cube2Parser.cpp
	rm Cube2Parser.c
	flex -f -o Cube2Scanner.cpp Cube2Scanner.ll
	sed -e 's:#include <FlexLexer.h>:#include "FlexLexer.h":' Cube2Scanner.cpp \
	    -e 's:^.*isatty.*$$::' > tmp
	cp tmp Cube2Scanner.cpp
	sed -e 's:#include <FlexLexer.h>:#include "FlexLexer.h":' Cube3Scanner.cpp \
	    -e 's:^.*isatty.*$$::' > tmp
	cp tmp Cube3Scanner.cpp
	sed -e 's:PARSER_HEADER_H:PARSER2_HEADER_H:' Cube2Parser.h > tmp
	cp tmp Cube2Parser.h
	sed -e 's:PARSER_HEADER_H:PARSER3_HEADER_H:' Cube3Parser.h > tmp
	cp tmp Cube3Parser.h
	rm tmp
	@echo
	@echo "*** Don't forget to copy the file 'FlexLexer.h'! ***"


.cpp.o:
	$(CCXX) $(CCXXFLAGS) -c $<

.cpp.sh.o:
	$(CCXX) $(CCXXFLAGS) $(ESH_CXXFLAGS) -c $< -o $@

libcube3.a: $(OBJS)
	$(AR) $(ARFLAGS) $@ $?
	@echo "*** $@ built"

libcube3so.so: $(SHOBJS)
	$(ESH_AR) $(ESH_ARFLAGS) -o $@ $? 
	@echo "*** $@ built"

cube-config: cube-config.in
	sed -e "s|@VERSION@|$(CUBEVER)|" \
	    -e "s|@PREFIX@|$(PREFIX)|" \
	    -e "s|@INCDIR@|$(INCDIR)|" \
	    -e "s|@LIBDIR@|$(LIBDIR)|" \
	    -e "s|@CXX@|$(CCXX)|" \
	    -e "s|@ZLIB@|$(SZLIB_LIB)|" \
	    -e "s|@SHARED@|$(if $(ESH_AR),yes)|" \
	    -e "s|@RPATH@|$(ESH_RPATH)|" \
	    cube-config.in > cube-config


#------------------------------------------------------------------------------
# Dependencies
#
# Generated by:
#   g++ -MM *.cpp
#------------------------------------------------------------------------------

services.o: services.h services.cpp
AggrCube.o: AggrCube.cpp AggrCube.h Cube.h Cnode.h Vertex.h IdentObject.h \
  cube_error.h Machine.h Sysres.h Metric.h Matrix.h Region.h
AggrCubeMaps.o: AggrCubeMaps.cpp AggrCubeMaps.h AggrCube.h Cube.h
Cartesian.o: Cartesian.cpp Cartesian.h cube_error.h Machine.h Sysres.h \
  Vertex.h IdentObject.h Node.h Process.h Thread.h
Cnode.o: Cnode.cpp Cnode.h Vertex.h IdentObject.h Region.h
Cube.o: Cube.cpp Cartesian.h Cnode.h Vertex.h IdentObject.h Cube.h \
  cube_error.h Machine.h Sysres.h Matrix.h Metric.h Node.h Process.h \
  Region.h Row.h Thread.h Driver.h
Cube2Parser.o: Cube2Parser.cpp Cube2Parser.h stack.hh ParseContext.h \
  Cube.h location.hh position.hh Driver.h Cube2Scanner.h FlexLexer.h \
  Metric.h Matrix.h Vertex.h IdentObject.h Cnode.h
Cube2Scanner.o: Cube2Scanner.cpp FlexLexer.h Cube2Parser.h stack.hh \
  ParseContext.h Cube.h location.hh position.hh Cube2Scanner.h
Cube3Parser.o: Cube3Parser.cpp Cube3Parser.h stack.hh ParseContext.h \
  Cube.h location.hh position.hh Driver.h Cube3Scanner.h FlexLexer.h \
  Metric.h Matrix.h Vertex.h IdentObject.h Cnode.h
Cube3Scanner.o: Cube3Scanner.cpp FlexLexer.h Cube3Parser.h stack.hh \
  ParseContext.h Cube.h location.hh position.hh Cube3Scanner.h
Driver.o: Driver.cpp Driver.h Cube.h Cube2Parser.h stack.hh \
  ParseContext.h location.hh position.hh Cube3Parser.h Cube2Scanner.h \
  FlexLexer.h Cube3Scanner.h
Machine.o: Machine.cpp Machine.h Sysres.h Vertex.h IdentObject.h Node.h
Matrix.o: Matrix.cpp Matrix.h Row.h Cnode.h Vertex.h IdentObject.h \
  Thread.h Sysres.h
Metric.o: Metric.cpp Metric.h Matrix.h Vertex.h IdentObject.h
Node.o: Node.cpp Node.h Sysres.h Vertex.h IdentObject.h Process.h
Process.o: Process.cpp Process.h Sysres.h Vertex.h IdentObject.h Thread.h
Region.o: Region.cpp Region.h Vertex.h IdentObject.h
Row.o: Row.cpp Row.h Thread.h Sysres.h Vertex.h IdentObject.h
Thread.o: Thread.cpp Thread.h Sysres.h Vertex.h IdentObject.h
Vertex.o: Vertex.cpp cube_error.h Vertex.h IdentObject.h
cube_error.o: cube_error.cpp cube_error.h
zfstream.o: zfstream.cpp zfstream.h
