#
# Makefile template for a Yorick-based code
#
# $Id: Makefile,v 1.1 1993/09/02 22:48:38 munro Exp munro $

# See $(Y_HOME)/Maketmpl for documentation on this Makefile

# ---------------------------------------------------------------------------

exec_prefix = 
Y_HOME = $(exec_prefix)/yorhome

CODE_NAME = drat
NON_SHARABLE = unused

C_OPTIMIZE = -O
F_OPTIMIZE = -g

OBJS = bound.o track.o trans.o drat.o ydrat.o

CODE_LIBRARY = libdrat.a

PKG_LIBS = -ldrat -lyorm $(FORTRAN_LIBS)
PKG_OBJS =

Y_INCLUDE = drat.i
Y_OTHERS = fft.i matrix.i

# Use
# Y_OTHERS = drat.i
# PKG_LIBS = -ldrat
# to put the drat routines into another custom version of Yorick

YWRAP_O =

SYS_LIBS =

CLEAN_UP = drat
TIDY_UP=

# ---------------------------------------------------------------------------

# all:: $(NON_SHARABLE)
all:: $(CODE_LIBRARY) $(CODE_NAME) startup

startup::
	if test ! -d ../Yorick/startup; then mkdir ../Yorick/startup; fi
	cp $(Y_INCLUDE) ../Yorick/startup

check: all
	./drat -batch check.i

install: $(CODE_LIBRARY) installdirs
	cp $(Y_INCLUDE) $(Y_SITE)/startup
	if test -x drat; then cp drat $(Y_BINDIR)/yorick; fi
	cp libdrat.a $(Y_LIBDIR)
	$(RANLIB) $(Y_LIBDIR)/libdrat.a

uninstall:
	cd $(Y_SITE)/startup; $(RM) $(Y_INCLUDE);
	$(RM) $(Y_LIBDIR)/libdrat.a

installdirs:
	if test ! -d $(Y_SITE); then mkdir $(Y_SITE); fi
	if test ! -d $(Y_SITE)/startup; then mkdir $(Y_SITE)/startup; fi
	if test ! -d $(Y_BINDIR); then mkdir $(Y_BINDIR); fi
	if test ! -d $(Y_LIBDIR); then mkdir $(Y_LIBDIR); fi

distclean: clean
	$(SED) -n -e "1,/^# --END-CODE/p" Makefile >M.1
	cat M.1 >Makefile
	$(RM) M.1

# Note: indirectly included headers separated by three spaces

bound.o: bound.h
track.o: track.h   bound.h
trans.o: trans.h   track.h bound.h
drat.o: drat.h   trans.h track.h bound.h
ydrat.o: drat.h   trans.h track.h bound.h

# my_code2.o: my_code2.c my_code.h
# 	$(CC) $(CFLAGS) -DSPECIAL_SWITCH -c my_code2.c

# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------

# Do not modify or remove the following line.
# --END-CODE-SPECIFIC-SECTION-----------------END-CODE-SPECIFIC-SECTION--
# -BEGIN-SITE-SPECIFIC-SECTION---------------BEGIN-SITE-SPECIFIC-SECTION-
# Do not modify or remove the preceding line.

# If you are creating a Makefile starting from the Maketmpl in order
# to build a special version of Yorick, then

#       TRY HARD NOT TO MODIFY ANYTHING BELOW THIS POINT.

# The configure script in the Yorick distribution should be able to do
# most of this correctly.  Exceptions are the Fortran section and the
# Y_LD variable; see the README in the top level distribution directory.

# ---------------------------------------------------------------------------
# This Makefile is divided into two (hopefully independent) parts:
# This second section is a site-specific section, which should not
# differ among various Yorick-based codes at a particular site.
# The first section describes how to build the Yorick-based code
# contained in this directory.
# The two parts divide between the lines which look like
#                 END-CODE-SPECIFIC-SECTION           and 
#                BEGIN-SITE-SPECIFIC-SECTION

# Generic make targets defined below--
# $(CODE_NAME):     (builds my_code custom version of Yorick)
# $(CODE_LIBRARY):  (builds my_code library)
# clean::           (removes everything except source code)
# tidy::            (removes backups and object files)
# Makefile::        (to port this Makefile to another site)
# tags:             (make TAGS file for GNU Emacs)
# index:            (make index file for vgrind C beautifier)

# ---------------------------------------------------------------------------

# The following paths may be set by the configure script:

# This should ALWAYS be absolute path names beginning with /
# NEVER relative path names beginning with . or ..
prefix = /usr/local

# I don't know whether these do what they're supposed to do (for autoconf).
srcdir = .
VPATH = .

# Directory which will contain the architecture-independent components of
# the public Yorick for this architecture.
Y_SITE = $(prefix)/Yorick

# Directories for the architecture-dependent parts of Yorick
Y_BINDIR = $(exec_prefix)/bin
Y_LIBDIR = $(exec_prefix)/lib
Y_CONTRIBDIR = $(exec_prefix)/contrib

# These are separate variables so they can be
# overridden on the make command line
YORINCL = -I$(Y_HOME)
YORLIB = -L$(Y_LIBDIR)
GISTINCL = -I$(Y_HOME)
GISTLIB = -L$(Y_LIBDIR)
Y_CONTRIB_LIB = -L$(Y_CONTRIBDIR)

# Filled in by configure script
XLOAD = -L/usr/X11R6/lib -lX11

# ---------------------------------------------------------------------------

# Redefine these to nil to get a no-graphics version of Yorick.
GRAPH_LIBS = $(GISTLIB) -lgist $(XLOAD)
# Solaris needs:    -lsocket -lnsl -lw -lintl -ldl
# when loading with -Bstatic   -- however, Sun doesn't supply libdl.a!?
GRAPH_I = graph.i
GRAPH_O = graph.o

# ---------------------------------------------------------------------------

# Choose C compiler appropriate to this machine - must be an ANSI C compiler
CC = shlicc
DEFS =  
CFLAGS = $(C_OPTIMIZE) $(DEFS) -I$(srcdir) $(YORINCL)

LDOPTS = 
LDFLAGS = $(LD_OPTIMIZE) $(LDOPTS)

# Usage:   $(Y_LD) $(OBJS) $(LIBS)
Y_LD = $(CC) $(LDFLAGS) -o $@

# The lowest level (last) libraries required on the load line go here.
# LOWLIBS = -lm -lc            (if Y_LD is not your C compiler)
LOWLIBS = -lm 

.c.o:
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
.c:
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)

# ---------------------------------------------------------------------------

# Repeat for FORTRAN compiler
FC = 
FFLAGS = $(F_OPTIMIZE) $(FORTRAN_STYLE)

# See comment at top of Maketmpl about FORTRAN_STYLE
# CRAY_FORTRAN = -r8 -i4       Sun SPARC workstation
# CRAY_FORTRAN = +autodbl4     HP PA-RISC workstation (OS<=8)
# CRAY_FORTRAN = -R8           HP PA-RISC workstation (OS>=9)
#             doesn't really work, and +autodblpad is totally wrong
# This is the most important case, since Crays are the only deviants:
# CRAY_FORTRAN =
# WKS_FORTRAN = -dp            UNICOS Crays
CRAY_FORTRAN = -r8 -i4
WKS_FORTRAN =

# (May be best to leave these commented out...)
# .f.o:
# 	$(FC) $(FFLAGS) -c $<
# .f:
# 	$(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)

# Since the rule used to build the custom Yorick version generally
# uses the C compiler to load, the user will need to select the
# FORTRAN libraries explicitly.  Yorick packages should generally
# avoid FORTRAN I/O, so I don't put the I/O library here...
# FORTRAN_LIBS = -lf77                Sun SPARC workstation
#      On Suns, use ar to remove the file main.o from the libF77.a
#      in /usr/lang.  Call the result libf77.a and put it in Y_HOME.
#      Also, Sun FORTRAN uses yacc to stupidly cause a yyparse and
#      yylex to appear if you use any formatted I/O -- needless to
#      say, this conflicts with Yorick's parser.  I could also use
#      macros to redefine these names in yorick.y/yorick.c...
# FORTRAN_LIBS = -lF77                Sun SPARC workstation (someday)
# FORTRAN_LIBS = -lM77 -lF77          Solaris
# FORTRAN_LIBS = -lvec -lcl           HP PA-RISC workstation
# FORTRAN_LIBS = -l??                 IBM workstations
# FORTRAN_LIBS = -lf                  UNICOS Crays
FORTRAN_LIBS = 

# Codger, Yorick's automatic wrapper and initialization code generator,
# understands four possible FORTRAN external name linkage conventions:
# All lowercase or all uppercase, with or without a trailing underscore.
# Use exactly one of the following lines:
# FORTRAN_LINKAGE = -Df_linkage
# FORTRAN_LINKAGE = -Df_linkage_
# FORTRAN_LINKAGE = -DF_LINKAGE
# FORTRAN_LINKAGE = -DF_LINKAGE_
FORTRAN_LINKAGE = -Df_linkage_

# ---------------------------------------------------------------------------

# The timing routines in sysdep.c are a perennial problem.  The default
# is POSIX 1003.1 standard, but this doesn't work on Suns...
# You will need to edit sysdep.c if neither of these works.
# HAS_BSD_TIMER = -DBSD_TIMER
# HAS_BSD_TIMER =
HAS_BSD_TIMER = 

# Other utilities

SHELL = /bin/sh
MAKE = make
RM = rm -f
MV = mv
SED = sed

AR = ar
# If your ar does not take the "l" flag (local temporary files), use this.
# ARFLAGS = rc
ARFLAGS = rcl

RANLIB = ranlib

# ---------------------------------------------------------------------------

# If you are editing the original Yorick Makefile, you are done.

# ---------------------------------------------------------------------------

CODGER = $(Y_HOME)/codger

# A generic main.o is located in Y_HOME, along with main.c.
# yinit.o is made by Codger from ycode.c, which will include all the
# packages specified as Y_INCLUDE, plus the std.i package in Y_HOME.
MAIN_OBJS = $(Y_HOME)/main.o yinit.o $(PKG_OBJS)

# ---------------------------------------------------------------------------
# The first three targets are generic rules for building the custom code
# and the package library required for inserting this package into any
# version of Yorick, in combination with other packages.

OTHER_LIBS = $(GRAPH_LIBS) $(SYS_LIBS) $(LOWLIBS)
ALL_LIBS = -L. $(Y_CONTRIB_LIB) $(YORLIB) $(PKG_LIBS) -lyor $(OTHER_LIBS)

$(NON_SHARABLE): $(MAIN_OBJS)
	$(Y_LD) $(MAIN_OBJS) $(ALL_LIBS)

$(CODE_NAME): $(MAIN_OBJS) $(CODE_LIBRARY)
	$(Y_LD) $(MAIN_OBJS) $(ALL_LIBS)

# The ywrap.o file is generated by Codger from ycode.c, as controlled
# by the Y_INCLUDE variable.
$(CODE_LIBRARY): $(OBJS) $(YWRAP_O)
	$(AR) $(ARFLAGS) $@ $(OBJS) $(YWRAP_O)
	$(RANLIB) $@

Makefile::
	@echo "Merging $(Y_HOME)/Maketmpl with ./Makefile"
	$(SED) -n -e "1,/^# --END-CODE/p" ./Makefile >M.1
	$(SED) -n -e "/^# -BEGIN-SITE/,$$$$p" $(Y_HOME)/Maketmpl >M.2
	cat M.1 M.2 >Makefile
	$(RM) M.1 M.2

# The Codger code generator produces a single output file ycode.c
# which must be compiled into two object files -- ywrap.o to be
# included in an object file library for a Yorick package, and
# yinit.o to be loaded into a specific version of Yorick.
ycode.c: $(Y_INCLUDE)
	$(CODGER) $(Y_SITE) $(Y_INCLUDE) - $(GRAPH_I) $(Y_OTHERS)
ywrap.o: ycode.c $(Y_INCLUDE)
	$(CC) $(CPPFLAGS) $(CFLAGS) -DYWRAP_C -c ycode.c
	$(MV) ycode.o ywrap.o
yinit.o: ycode.c $(Y_INCLUDE)
	$(CC) $(CPPFLAGS) $(CFLAGS) -DYINIT_C -c ycode.c
	$(MV) ycode.o yinit.o

# Housekeeping targets--
#   make clean        to wipe everything but source files
#   make tidy         to clean out junk, but leave executables and libraries
clean: tidy
	$(RM) *.a $(CLEAN_UP)

tidy:
	$(RM) ycode.c *.o *~ *.bak core $(TIDY_UP)

# TAGS table for Emacs
tags:
	etags -tw *.h *.c *.y

# index for vgrind (a pretty C printing routine)
index:
	etags -twv *.h *.c >index

# ---------------------------------------------------------------------------
