# Version 1.3 (15/05/00, ar)
# Version 1.2 (09/12/98, ajw)
#
# This makefile is written for the GNU version of make.
# If you encounter any problems please feel free to contact us.
# Email your problem report to 
# 	progres@i3.informatik.rwth-aachen.de
#
# Building prototypes requires the installation
# of gcc/g++/libstdc++ version 2.8.1! or higher
#
# PLEASE CHECK THE FOLLOWING LINES!
#

# ----- User specific variables -----

# Note that some of the values of the following variables 
# (PROGRESROOT, IMPORTLIB, GENDIR, and PROTOTYPE) are 
# taken from the environment if set.

# Definition of the PROGRES environment's root directory
# which contains all installed PROGRES files.
ifndef PROGRESROOT
  PROGRESROOT = $(HOME)
endif

# The included makefile determines the operating
# system's name and version. Do not change!
include $(PROGRESROOT)/prototype/Makefile.system 

# Library of imported types and functions.
ifndef IMPORTINCLUDE
  IMPORTINCLUDE = $(PROGRESROOT)/import/src
endif
ifndef IMPORTLIB
  IMPORTLIB = $(PROGRESROOT)/import/lib/$(OSVERSION)/libImportedFunctions.so
endif

# MULTIUSER should be set to TRUE if you intend to use 
# different prototype instances on the same working graph.
# On SOLARIS machines, only!
# Default is single user mode.
MULTIUSER = FALSE

# PREPARE_MENU may be a file containing pairs of operation
# prefixes and menu names. Each line contains one pair. 
# The prefix and the menu name are separated by space. 
# The effect is that all operations with one of these 
# prefixes are inserted into the menu with the corresponding 
# menu name. The prefix is removed from the command name 
# when it is inserted into the menu.
# Default is that no PREPARE_MENU file is given.
PREPARE_MENU =

# GENDIR denotes to the directory where the generated code is
# located. Default is the current directory
ifndef GENDIR
  GENDIR = $(shell pwd)
  export GENDIR  # for prepare script
endif

# Definition of the target binary that will finally be created.
# Should be changed to avoid confusion with other prototypes.
ifndef PROTOTYPE
  PROTOTYPE = Proto
endif

# ----- Installation specific variables -----

# Adapt the following variables according to your installation of X11.
ifeq ($(OSVERSION), LINUXLIBC6)
# LINUXLIBC6 X11 header and library files.
  X11_INCLUDE    = /usr/X11R6/include
  X11_LIB        = /usr/X11R6/lib
else
# Other (Solaris 2.x) X11 header and library files.
  X11_INCLUDE    = /usr/openwin/include
  X11_LIB        = /usr/openwin/lib
endif

# Check this variable according to your installation of Tcl 8.0 
# and Tk 8.0 (newer versions are not supported). Note that we provide 
# all files that are necessary for our prototype. So you don't
# really need to change that variable.
TCL_LIB_PATH = $(PROGRESROOT)/lib/$(OSVERSION)


# ----- There should be no reason to change anything below this line -----

SHELL = /bin/sh
CC = gcc -I$(IMPORTINCLUDE)
ifeq ($(OSVERSION), LINUXLIBC6)
  CPP = g++
else
  CPP = g++ -V 2.8.1
endif

STARTPROTO = StartProto        # Please do not change.
TIMEFLAG   = DateOfGeneration  # Please do not change.

SRCDIR = .
OBJDIR = $(GENDIR)/obj/$(OSVERSION)
BINDIR = $(GENDIR)/bin/$(OSVERSION)

# Pattern rules to create objs in specific subdirectory

$(OBJDIR)/%.o: %.cc $(TIMEFLAG)
	$(CPP) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

$(OBJDIR)/%.o: %.c  $(TIMEFLAG)
	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

MENUE = config.ecu
SETTINGS = NoSettings
CSOURCE = ProtoAppMain.c
CPPSOURCE = ProtoMain.cc
OBJS = $(OBJDIR)/ProtoAppMain.o $(OBJDIR)/ProtoMain.o

#
# include a user specific part (if a UserMakefile exists)
#

ifeq (UserMakefile,$(wildcard UserMakefile))
include UserMakefile
endif


PROTOINCLUDE       = $(PROGRESROOT)/prototype/include
PROTOSYSTEMINCLUDE = $(PROGRESROOT)/prototype/include/$(OSVERSION)
PROTOLIB           = $(PROGRESROOT)/lib/$(OSVERSION)
#STDIMPORTINCLUDE   = $(PROGRESROOT)/import/include
#STDIMPORTLIB       = $(PROGRESROOT)/import/lib/$(OSVERSION)

ifeq ($(MULTIUSER), TRUE)
  GEXEDLIBS = -lgraph_access_c
else
  GEXEDLIBS = -lge_graph_access -lgraph_access
endif

ifdef M2RTSystem
  GRASLIBS = -lm2gras -lkernel -lm3SETs -lm3LISTs -lm3system -lSETs -lLISTs -lsystem
  RTLIBS = -lPGCRTInitLib -lrt_init -lSM2 -lCEM2
else
  GRASLIBS = -lm3gras -lkernel -lm3SETs -lm3LISTs -lm3system
  RTLIBS = -lPGCRTInitLib
endif


# ----- Environment variables are now set. Build compile and link flags.

# These options are at least needed for successful compilation.
CFLAGS    = -g -ggdb -gstabs+ -DDEBUG -Wall -Wid-clash-31 -Wpointer-arith -Wcast-qual -Wtraditional -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Winline -w -Wno-unused -Woverloaded-virtual -fdollars-in-identifiers -fshort-enums -DEPC_MODULA -D$(OSVERSION) -I$(PROTOINCLUDE) 
CXXFLAGS = -g

INCLUDES  = -I. -I$(IMPORTINCLUDE) -I$(PROTOINCLUDE)\
	-I$(PROTOSYSTEMINCLUDE) -I$(X11_INCLUDE)
CPPFLAGS = $(USER_DEFS) $(INCLUDES)

ifeq ($(OSVERSION), LINUXLIBC6)
  LDFLAGS   =  -L$(PROTOLIB) -L$(TCL_LIB_PATH)\
   -L$(X11_LIB) -Wl,-R$(PROTOLIB) -Wl,-R$(IMPORTLIBPATH) -Wl,-R$(TCL_LIB_PATH)\
   -Wl,-R$(X11_LIB) $(USER_LIBPATH) \
   -Wl,--wrap,adjtime,--wrap,getdirentries,--wrap,readv,--wrap,utimes,--wrap,wait3
  LDLIBS    = -lecu -lecu_common -lFFGraph -lFFGraphic -lFFlayout\
   -lFFcontainer -lPGCExecLib $(IMPORTLIB) $(GRASLIBS) $(GEXEDLIBS)\
   -lcommon -lcollections -lcom -lxdr -lgetopt -lm2libs $(RTLIBS) -lm3\
   -lm3core -lgcc -lc -lm -lg++ -lstdc++ -lX11 -lXpm -ldl -ltk8.0\
   -ltcl8.0 -ltix4.1.8.0 $(USER_LIBS)
else
  LDFLAGS   =  -L$(PROTOLIB) -L$(TCL_LIB_PATH)\
   -L$(X11_LIB) -R$(PROTOLIB) -R$(IMPORTLIBPATH) -R$(TCL_LIB_PATH)\
   -R$(X11_LIB) $(USER_LIBPATH)
  LDLIBS    = -lecu -lecu_common -lFFGraph -lFFGraphic -lFFlayout\
    -lFFcontainer -lPGCExecLib $(IMPORTLIB) $(GRASLIBS) $(GEXEDLIBS)\
    -lcommon -lcollections -lcom -lxdr -lgetopt -lm2libs $(RTLIBS) -lm3\
    -lm3core -lgcc -lc -lm -lg++-compat -lstdc++ -lX11 -lXpm -lsocket\
    -lnsl -ldl -ltk8.0 -ltcl8.0 -ltix4.1.8.0 $(USER_LIBS)
endif

# ----- Now everything should be all right, so we try to compile ... -----

all: $(GENDIR) prepare $(OBJDIR) $(BINDIR)/$(PROTOTYPE) $(GENDIR)/$(STARTPROTO)

prepare:
	@LD_LIBRARY_PATH=$(PROGRESROOT)/lib/$(OSVERSION) \
	TCL_LIBRARY=$(PROGRESROOT)/lib/$(OSVERSION)/tcl8.0 \
	TK_LIBRARY=$(PROGRESROOT)/lib/$(OSVERSION)/tk8.0 \
	$(PROGRESROOT)/bin/$(OSVERSION)/wish8.0 $(PROGRESROOT)/browser/tcl/prepare_ecu.tcl $(PREPARE_MENU)

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(GENDIR):
	mkdir -p $(GENDIR)

$(GENDIR)/$(STARTPROTO): 
	@if [ ! -f $(GENDIR)/$(STARTPROTO) ] ; \
	   then \
	     cp $(PROGRESROOT)/prototype/bin/$(STARTPROTO) $(GENDIR)/$(STARTPROTO) ; \
	 fi

$(CSOURCE):
	@if [ ! -f $(GENDIR)/$(CSOURCE) ] ; \
	   then \
	     cp $(PROGRESROOT)/prototype/src/$(CSOURCE) $(GENDIR) ; \
	 fi

$(CPPSOURCE):
	@if [ ! -f $(GENDIR)/$(CPPSOURCE) ] ; \
	   then \
	     cp $(PROGRESROOT)/prototype/src/$(CPPSOURCE) $(GENDIR) ; \
	 fi

$(BINDIR)/$(PROTOTYPE): $(CSOURCE) $(CPPSOURCE) $(OBJS) $(USER_OBJS)
	@if [ ! -d $(BINDIR) ] ; \
	   then \
	     mkdir -p $(BINDIR) ; \
	 fi
	$(CPP) $(LDFLAGS) $(OBJS) $(USER_OBJS) $(LDLIBS) -o $(BINDIR)/$(PROTOTYPE)

clean: cleanbin cleansetting cleanmenue

cleanbin: 
	rm -f menu.clean core;\
	rm -f $(OBJS) $(USER_OBJS);\
	rm -f $(BINDIR)/$(PROTOTYPE);

cleanmenue:
	@if [ -f $(MENUE) ] ; \
	   then \
	     mv  -f $(MENUE) $(MENUE).backup ; \
	fi ; \

cleansetting:
	 @if [ -d $(SETTINGS) ] ; \
	    then \
	      rm -rf $(SETTINGS).backup ; \
	      mv $(SETTINGS) $(SETTINGS).backup ; \
	fi
