# -*- Mode:makefile; Mode:font-lock; -*-
##############################################################################
# RCS: ProMAX $Id: Makefile 49775 2014-01-08 04:07:00Z Craig Artley $ $Revision: 49775 $ $Date: 2014-01-07 21:07:00 -0700 (Tue, 07 Jan 2014) $
##############################################################################
#
# To build and link with C code, specify "language=C" on the make command;
# no "language=..." parameter, or "language" set to any value other than "C" 
# builds the default Fortran.
language := C

include advance.make

JAVA_HOME = /usr/java

ifeq ($(purify),yes)
   master := yes
endif

ifeq ($(clink),yes)
   srcs := cexec_main.c exec.f $(newsrcs)
else
   CFLAGS := -DCXXLINK $(CFLAGS)
   FPPFLAGS := -DCXXLINK $(FPPFLAGS)
   srcs := exec_main.$(CXX_SUF) exec.f $(newsrcs)
endif

ifeq ($(language),C)
  srcs += toolcall.c
  toolcallsrc := toolcall.c
else
  srcs += toolcall.f
  toolcallsrc := toolcall.f
endif

parentdir := exec
ifneq (,$(findstring linux,$(machtype)))
ifeq ($(intel),yes)
depend_include := $(parentdir)_icc.depend
else
depend_include := $(parentdir)_gcc.depend
endif
else
depend_include := $(parentdir).depend
endif
srcdir := srcexedir
objdir := objexedir

usrcs := $(define_usrcs)
uobjs := $(define_uobjs)
asrcs := $(define_asrcs)
aobjs := $(define_aobjs)

ifeq ($(master),yes)
exe := $(exedir)/exec.exe
else
exe := $(exedir)/$(altprefix)exec.exe
endif


tools_db := $(firstword $(wildcard $($(srcdir))/$(parentdir)/tools.db \
				   $(m$(srcdir))/$(parentdir)/tools.db \
				   $(a$(srcdir))/$(parentdir)/tools.db))

libtoolcallname := toolcall
lib := $(libdir)/lib$(libtoolcallname)$(lib_suf)

# Solaris
ifeq ($(machtype),solaris)
ifeq ($(clink),yes)
libs := $(libmaxtoolc) $(libmaxtool1) $(libmaxtool2) $(libmaxtool3) $(libmaxexec) \
	$(libesi) $(libmultitable) $(libidaStub) $(libGeom) $(AGCLIBS) \
	$(libfmhelp) $(libpropath) $(libeispack) $(libpar) $(libagl) \
	$(libXutil) $(LIBLAM) $(LIBLIC) $(libcfortran)
CLDFLAGS += -zmuldefs
else   # clink = no
libs := $(libmaxtoolc) $(libmaxtool1) $(libmaxtool2) $(libmaxtool3) $(libagPilite) $(libmaxexec) \
	$(libesi) $(libmultitable) $(libida) $(libGeom) $(AGCXXLIBS) \
	$(libfmhelp) $(libpropath) $(libeispack) $(libpar) $(libagl) \
	$(libXutil) $(LIBLAM) $(LIBLIC) $(libcfortran)
CXXLDFLAGS += -zmuldefs
endif  # from clink check
TOOLLIBPATH := -L$(libdir) -L$(alibdir)
TOOLLIBDELTA := -ljtool -lthread
endif  # end Solaris

# Linux or Linux64
ifneq ($(findstring $(machtype),linux linux64),)
ifeq ($(clink),yes)
libs := $(libmaxtoolc) $(libmaxtool1) $(libmaxtool2) $(libmaxtool3) \
	$(libmaxexec) $(libesi) \
	$(libmultitable) $(libidaStub) $(libGeom) $(libagl) $(LIBLAM) $(AGCLIBS) \
	$(libfmhelp) $(libpropath) $(libeispack) $(libpar) \
	$(libXutil) $(LIBLAM) $(LIBLIC) $(libcfortran)
else   # clink = no
libs := $(libmaxtoolc) $(libmaxtool1) $(libmaxtool2) $(libmaxtool3) \
	$(libagPilite) $(libmaxexec) $(libesi) \
	$(libmultitable) $(libida) $(libGeom) $(libagl) $(LIBLAM) $(AGCXXLIBS) \
	$(libfmhelp) $(libpropath) $(libeispack) $(libpar) \
	$(libXutil) $(LIBLAM) $(LIBLIC) $(libcfortran)
#CXXLDFLAGS := $(CXXLDFLAGS) -Xlinker -noinhibit-exec
endif  # from clink check
TOOLLIBPATH := -L$(libdir) -L$(alibdir)
TOOLLIBDELTA := -ljtool
endif  # end linux/linux64

# Enables JavaSeis tables.
ifeq ($(machtype),linux64)
  CXXFLAGS += -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
  SLIB := $(libXpm)
  JVMDIR = amd64
endif
ifeq ($(machtype),linux)
  CXXFLAGS += -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
  JVMDIR = i386
endif
ifeq ($(machtype),solaris)
  CXXFLAGS += -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/solaris
  JVMDIR = sparc
endif

ARCHIVE_EXTRA_LIBS := $(CLDFLAGS) $(patsubst lib%,-l%,$(basename $(notdir $(filter-out %.o,$(filter-out %.a,$(libs))))))
ARCHIVE_EXTRA_LIBS += $(XMLIBS) $(SYSLIBS)

ifneq ($(findstring $(machtype), linux linux64 solaris),)
ifeq ($(master),yes)
	TOOLLIBS := $(shell cat $(srcexedir)/exec/libfile.tmp)
endif
endif

libs := $(lib) $(libs)

libs := $(filter-out $(nolibmaxtool1),$(libs))
libs := $(filter-out $(nolibmaxtool2),$(libs))
libs := $(filter-out $(nolibmaxtool3),$(libs))
libs := $(filter-out $(nolibmaxtoolc),$(libs))


lgclibs := $(TOOLLIBS) $(TOOLLIBDELTA)
# $(OWLIBS) $(ORACLELIBS)

# build exec and lib 
all : $(toolcallsrc) $(lib)
	$(UPDATE_LIBRARIES)
	$(MAKE) $(exe)

utc:
	$(UPDATE_LIBRARIES)
	if [ -f $($(srcdir))/$(parentdir)/toolcall* ]; then \
	/bin/rm $($(srcdir))/$(parentdir)/toolcall* 2> /dev/null; fi
	$(MAKE) $(exe)

# Rule to make sure exec_main.o, exec.o, toolcall.o are there.

$(lib): $(objexedir)/exec/toolcall.o
ifneq ($(findstring $(machtype), linux linux64 solaris),)
	$(ARCHIVE_CREATE) $(TOOLLIBPATH) $(shell cat $(srcexedir)/exec/libfile.tmp) $(TOOLLIBDELTA) 
else
	$(ARCHIVE_CREATE)
endif

objs: $(uobjs)
	@echo "Object(s): $^ Created."

ifeq ($(master),yes)
ifeq ($(clink),yes)
$(exe) : $(filter-out %/toolcall.o,$(aobjs)) $(libs)
	$(C_LINK) $(CLIBS) $(XMLIBS) $(SYSLIBS) $(lgclibs) \
	-L$(JAVA_HOME)/jre/lib/$(JVMDIR)/server -ljvm || $(BACKUP_ON_ERROR)
else
$(exe) :  $(filter-out %/toolcall.o,$(aobjs)) $(libs)
	$(FFT_LINK) $(CXXLIBS) $(XMLIBS) $(SYSXXLIBS) $(lgclibs) \
	-L$(JAVA_HOME)/jre/lib/$(JVMDIR)/server -ljvm || $(BACKUP_ON_ERROR)
endif

else

$(exe) : $(libmaxtoolc) $(lib)
	echo " "
	echo "Devkit maxtoolc lib is $(libmaxtoolc)"
	echo "Devkit toolcall lib is $(lib)"
	echo " "
	if [ ! -f $(libmaxtoolc) ] ; then \
	   echo "$(libmaxtoolc) is missing or out of date." ; \
           echo "Will build toolcall library anyway." ; \
	   echo "Try manually building $(libmaxtoolc)" ; fi
	$(BACKUP)
	@echo "Linking $(exe)"
	$(asrcexedir)/exec/$(machtype)/make_altexec.sh $(exe) $(asrcexedir)/exec/$(machtype)/exec_hdr $(lib) $(libmaxtoolc) || $(BACKUP_ON_ERROR)
	@echo " "
	@echo '*******************************************************************************'
	@echo "WARNING: Do not replace the installed \"$(PROMAX_HOME)/sys/exe/exec.exe\"!"
	@echo "Use \"$(exe)\" only as an Alternate Executive."
	@echo '*******************************************************************************'
	@echo " "

endif

CPPFLAGS := $(OWINCPATH) $(CPPFLAGS)
CXXPPFLAGS := $(OWINCPATH) $(CXXPPFLAGS)

include: $(globalincs)

depend: 
	$(AMAKEDEPEND)

toolcall.f: $(srcexedir)/exec/tools_to_delete $(srcexedir)/exec/tools_to_add $(tools_db)
	@echo Building $@
	@/bin/rm -f $@ 2> /dev/null
	@/bin/sh $(aportdir)/bin/buildtoolcall $^ >$@

toolcall.c: $(srcexedir)/exec/tools_to_delete $(srcexedir)/exec/tools_to_add $(tools_db)
	@echo Building $@
	@/bin/rm -f $@ 2> /dev/null
	@/bin/sh $(aportdir)/bin/buildtoolcall -C $^ >$@

$(srcexedir)/exec/tools_to_delete :
	@touch $@

$(srcexedir)/exec/tools_to_add :
	@touch $@

$(objexedir)/exec/cexec_main.o : cexec_main.c
	@echo "Ansi C Compiling: $<"
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

ifeq ($(language),C)
$(objexedir)/exec/toolcall.o : toolcall.c
	@/bin/rm -f $(objexedir)/exec/toolcall* 2> /dev/null
	$(ANSIC_COMPILE)
else
$(objexedir)/exec/toolcall.o : toolcall.f
	@/bin/rm -f $(objexedir)/exec/toolcall* 2> /dev/null
	$(F_COMPILE)
endif


$(objexedir)/exec/%.o : %.c
	$(ANSIC_COMPILE)
$(objexedir)/exec/%.o : %.$(CXX_SUF)
	$(CXX_COMPILE)
$(objexedir)/exec/%.o : %.f
	$(subst -ip,-ip-no-inlining,$(F_COMPILE))

cleanexe:
	@echo "Removing: $(exe)"; \
	/bin/rm -f $(exe) 2> /dev/null

ifeq ($(master),yes)

# NOTE: WE do not want to get rid of $(objexedir)/($exe) since
# there are objects from clink=yes that need to be there...
cleanobjs:
	@echo "Removing: $(uobjs)"; \
	/bin/rm -rf $(uobjs) 2> /dev/null
else
cleanobjs:
	@echo "Removing: $(objexedir)/exec/toolcall.o"; \
	/bin/rm -rf $(objexedir)/exec/toolcall.o 2> /dev/null
endif
cleanlib:
	@echo "Removing $(lib)"; \
	/bin/rm -f $(lib) 2> /dev/null

cleantool:
	@echo "Removing: $(srcexedir)/exec/toolcall*"; \
	/bin/rm -f $(srcexedir)/exec/toolcall* 2> /dev/null
#	@echo "Removing: $(srcexedir)/exec/libfile.tmp"; \
#	/bin/rm -f $(srcexedir)/exec/libfile.tmp 2> /dev/null

clean: cleanexe cleanobjs cleanlib cleantool

# Include the dependency include file if it exists.
ifneq ($(wildcard $(depend_include)),)
   include $(depend_include)
endif

.SILENT:
.phony: depend cleanexe cleanobjs clean $(libs) exec_main
