#
# Makefile for Yorick FFT and Matrix analysis packages
#
# $Id: Makefile,v 1.1 1993/08/06 23:08:27 munro Exp munro $
#

# NOTE: The LAPACK function DLAMC2 (in dgecon.f here) contains a
#       write statement and its format statement, which must be
#       commented out.  Their sole function is to print a warning
#       that the machine type has not been certainly determined.

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

# If you've played your cards right, this is the only line that you
# need to change in order to port your custom version of Yorick to a
# new platform.
exec_prefix = /usr/local/yorick/1.0
Y_HOME = $(exec_prefix)/yorhome

C_OPTIMIZE = -O
F_OPTIMIZE = -O

CODE_NAME = yorm
NON_SHARABLE = unused

OBJS = cfft2.o cfftb.o cfftf.o cffti.o roll2.o \
       dgyor.o dgtsv.o dgesv.o dgecon.o dgblas.o

CODE_LIBRARY = libyorm.a

# PKG_LIBS =
# PKG_OBJS = $(OBJS) $(YWRAP_O)
PKG_LIBS = -lyorm
PKG_OBJS =

Y_INCLUDE = fft.i matrix.i
Y_OTHERS =

# Use
# Y_OTHERS = fft.i matrix.i
# PKG_LIBS = -lyorm
# to put the fft and matrix routines into another custom version of Yorick

# YWRAP_O =
YWRAP_O = ywrap.o

SYS_LIBS = $(FORTRAN_LIBS)

FORTRAN_STYLE = $(WKS_FORTRAN)

# Set any special files to be zapped by the clean and tidy targets.
# (make clean removes both TIDY_UP and CLEAN_UP)
CLEAN_UP = yorm
TIDY_UP=

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

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

startup::
	echo "Run yorm in the current directory."

check: all
	./yorm -batch check.i

install: $(CODE_LIBRARY)
	echo "Run yorm in the current directory.  Not installed."

uninstall:
	echo "Not uninstalled -- I don't know how."

# Note: indirectly included headers separated by three spaces

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

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

# 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 an absolute path name beginning with /
# NEVER relative path names beginning with . or ..
prefix = @prefix@

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

# Directory which will contain the architecture-independent parts of
# the public Yorick.
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 = @XLOAD@

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

# 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 graph0.o

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

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

LDOPTS = @LDOPTS@
LD_OPTIMIZE = $(C_OPTIMIZE)
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 = @LOWLIBS@

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

# Some versions of make have an implicit rule to make .i from .c
.c.i:
	@touch $<

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

# Repeat for FORTRAN compiler
FC = @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 = @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 = @FORTRAN_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 = @D_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

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