SHELL=/bin/sh
MAKE=make
include ../Make.cfg

CFLAGS=$(COPTIONS) -I. -I../gist -I../play
X11LIB=$(XLIB) -lX11
CCLOAD=$(CC) $(LDOPTIONS) $(LDFLAGS)

REQ_DIRS=gist fft matrix
PKG_DIRS=$(REQ_DIRS) $(OPT_DIRS)
ALL_OPTS=drat hex

PKG_I=
PKG_LIBS=
SYS_LIBS=$(X11LIB) $(MATHLIB) $(FPELIB)
ALL_LIBS=$(PKG_LIBS) -L. -lyor $(SYS_LIBS)
D_Y_VERSION='-DY_VERSION="$(Y_VERSION)"'

STD_PKGS=../i0/graph.i ../i0/matrix.i ../i0/fft.i
MAIN_OBJS=main.o yinit.o

OBJS = task.o yorick.o yinput.o parse.o ydata.o yio.o fnctn.o array.o \
  range.o ops.o ops0.o ops1.o ops2.o ops3.o opsv.o nonc.o debug.o \
  std0.o std1.o std2.o ascio.o defmem.o yhash.o  yrdwr.o bcast.o \
  binio.o binobj.o binstd.o cache.o convrt.o binpdb.o clog.o \
  graph.o graph0.o style.o list.o pathfun.o fortrn.o ywrap.o

CLEAN_CRUFT=main.c ycode.c codger yorick pkg-*
PKG_MAKE=CODGER=../yorick/codger CODGER_I0=-i0=../i0 \
  YORINCL=-I../yorick PLAYINCL=-I../play C_OPTIMIZE="$(COPTIONS)" libyor
PKG_NUKE=MAKE_TEMPLATE=../Make.cfg yorclean
PKG_CLEAN=ZAP_DOTI= $(PKG_NUKE)

all: yorexe

libyor: codger
	@rm -f pkg-lib pkg-i; touch pkg-lib pkg-i
	@for d in $(PKG_DIRS); do ( cd ../$$d; $(MAKE) $(PKG_MAKE); ); done
	@p=`cat pkg-lib`; echo $$p >pkg-lib1
	@p=`cat pkg-i`; echo $$p >pkg-i1
	@echo ------------------ packages: `cat pkg-i1`
	@echo ------------------ libraries: `cat pkg-lib1`
	@$(MAKE) PKG_LIBS="`cat pkg-lib1`" PKG_I="`cat pkg-i1`" make.done
	@echo ------------------ made libyor target

yorexe: libyor
	@echo ------------------ packages: `cat pkg-i1`
	@echo ------------------ libraries: `cat pkg-lib1`
	@$(MAKE) PKG_LIBS="`cat pkg-lib1`" PKG_I="`cat pkg-i1`" yorick
	@echo ------------------ made yorexe target
	@rm -f pkg-*

make.done: $(OBJS) libyor.a
	$(AR) r libyor.a $(OBJS)
	$(RANLIB) libyor.a
	@touch $@

yorick: libyor.a $(MAIN_OBJS)
	$(CCLOAD) -o $@ $(MAIN_OBJS) $(ALL_LIBS)

check: yorexe
	./yorick -batch testfull.i

config:
	./yconfig.sh

optclean:
	@for d in $(ALL_OPTS); do ( cd ../$$d; $(MAKE) $(PKG_CLEAN); ); done

optnuke:
	@for d in $(ALL_OPTS); do ( cd ../$$d; $(MAKE) $(PKG_NUKE); ); done

clean:: optclean
	rm -f *~ '#'* *.o *.a core a.out cfg.* *.done $(CLEAN_CRUFT)

distclean:: optnuke
	rm -f *~ '#'* *.o *.a core a.out cfg.* *.done $(CLEAN_CRUFT)
	rm -f prmtyp.h Maketmpl Make-cxx .gdbinit

echofc:
	echo "$(FC)" >cfg.tmp

# -------------- auxilliary codes and files ---------------

codger: codger.c
	$(CC) $(CPPFLAGS) $(CFLAGS) $(FORTRAN_LINKAGE) -o codger codger.c

ycode.c: codger ../i0/std.i $(STD_PKGS) $(PKG_I)
	./codger -i0=../i0 $(Y_HOME) $(Y_SITE) $(STD_PKGS) - $(PKG_I)

yinit.o: ycode.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -DYINIT_C -c ycode.c
	mv ycode.o yinit.o

ywrap.o: ycode.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -DYWRAP_C -c ycode.c
	mv ycode.o ywrap.o

main.c: ../play/unix/pmain.c
	cp ../play/unix/pmain.c main.c

../play/all/numfmt:
	@cd ../play; $(MAKE) numfmt

prmtyp.h: ../play/all/numfmt ../play/all/numfmt.c
	../play/all/numfmt prmtyp.h

# ------------- dependencies -------------

# Note: indirectly included headers separated by three spaces
# ydata.h: binio.h hash.h
# binio.h: hash.h
# yio.h: hash.h pstdio.h
# defmem.h: pstdlib.h
# bcast.h: ydata.h   binio.h hash.h

PLAY=../play/play.h
PSLIB=../play/pstdlib.h
PSIO=../play/pstdio.h
PLAYALL=$(PLAY) $(PSLIB) $(PSIO)
HGIST=../gist/gist.h ../gist/hlevel.h

array.o: ydata.h   binio.h hash.h
ascio.o: ydata.h yio.h defmem.h $(PSLIB) $(PLAY)   binio.h hash.h $(PSIO)
bcast.o: bcast.h   ydata.h binio.h hash.h
binio.o: ydata.h yio.h defmem.h $(PSLIB)   binio.h hash.h $(PSIO)
binobj.o: bcast.h defmem.h $(PSLIB)   ydata.h binio.h hash.h
binpdb.o: ydata.h yio.h $(PSLIB)   binio.h hash.h $(PSIO)
binstd.o: binio.h $(PSLIB)   hash.h
cache.o: binio.h $(PSLIB) $(PSIO)   hash.h
clog.o: ydata.h yio.h $(PSLIB)   binio.h hash.h $(PSIO)
convrt.o: binio.h $(PSLIB)   hash.h
debug.o: ydata.h yio.h $(PSLIB) $(PLAY)   binio.h hash.h $(PSIO)
defmem.o: defmem.h   $(PSLIB)
fnctn.o: ydata.h   binio.h hash.h
fortrn.o: fortrn.c yasync.h $(PSLIB)
	$(CC) $(CPPFLAGS) $(CFLAGS) $(FORTRAN_LINKAGE) -c fortrn.c
graph.o: graph.c ydata.h yio.h   binio.h hash.h $(PLAYALL) $(HGIST)
graph0.o: ydata.h   binio.h hash.h
yhash.o: hash.h defmem.h $(PSLIB)
list.o: ydata.h defmem.h   binio.h hash.h
nonc.o: nonc.c
	$(CC) $(CPPFLAGS) $(CFLAGS) $(NO_HYPOT) -c nonc.c
ops.o: bcast.h $(PSLIB)   ydata.h binio.h hash.h
ops0.o: ydata.h   binio.h hash.h
ops1.o: ydata.h   binio.h hash.h
ops2.o: ydata.h   binio.h hash.h $(PSLIB)
ops3.o: bcast.h $(PSLIB)   ydata.h binio.h hash.h
opsv.o: ydata.h $(PSLIB) prmtyp.h   binio.h hash.h
parse.o: parse.h ydata.h $(PSLIB)   binio.h hash.h
pathfun.o: yio.h $(PSLIB)   hash.h $(PSIO)
range.o: ydata.h   binio.h hash.h
std0.o: std0.c bcast.h yio.h defmem.h $(PSLIB) $(PLAY)   ydata.h binio.h hash.h $(PSIO)
	$(CC) $(CPPFLAGS) $(CFLAGS) $(D_Y_VERSION) -c std0.c
std1.o: ydata.h $(PSLIB) $(PLAY)   binio.h hash.h
std2.o: ydata.h yio.h $(PSLIB) $(PLAY)   binio.h hash.h $(PSIO)
style.o: style.c ydata.h $(PSLIB) $(HGIST) ../gist/draw.h   binio.h hash.h
task.o: ydata.h yio.h $(PSLIB) $(PLAY)   binio.h hash.h $(PSIO)
ydata.o: bcast.h defmem.h $(PSLIB)   ydata.h binio.h hash.h
yinput.o: yio.h parse.h hash.h $(PSLIB) $(PLAY)   $(PSIO)
yio.o: yio.h defmem.h ydata.h $(PSLIB) $(PLAY)   binio.h hash.h $(PSIO)
yorick.o: yorick.c parse.h $(PSLIB) $(PLAY)
	$(CC) $(CPPFLAGS) $(CFLAGS) $(D_NO_STRTOUL) -c yorick.c
yrdwr.o: bcast.h $(PSLIB)   ydata.h binio.h hash.h

# Always compile main.c with -g so new packages can be debugged.
main.o: main.c
	$(CC) $(CPPFLAGS) -I../play -g -c main.c
