#
# Maketmpl Makefile template for the Yorick-based codes
# If this is Maketmpl.in, run configure (in ..) to create Maketmpl
#
# $Id$
#
# Generated automatically from Maketmpl.in by configure.
#
# Note: symbols between two ats @some thing@ are filled in by configure

# The configure script in the Yorick distribution should be able to do
# most of this correctly.  The major exception is the Fortran section.
# Wait until you have installed Maketmpl in Y_HOME before you fix that;
# Yorick itself uses no FORTRAN.

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

# Variables to be set in Makefile before including this template:

# CODE_NAME
# NON_SHARABLE
#   the name of the code you want to build
#   use the NON_SHARABLE for if you are not building a library
# CODE_LIBRARY
#   libpkg.a to build a library called pkg
#
# your all:: target should be $(CODE_LIBRARY) $(CODE_NAME)
#                          or $(NON_SHARABLE)
# depending on whether you want a library or not
#
# OBJS
#   list of object files to be built (use \ to continue)
# PKG_LIBS
#   -lpkg at least, put any other package libraries here too
# PKG_OBJS
#   if you are building without a library, at least $(OBJS) $(YWRAP_O)
# Y_INCLUDE
#   startup include files in this directory
# Y_OTHERS
#   startup include files for other packages (in Y_HOME/startup or
#   Y_HOME/contrib)
# YWRAP_O
#   ywrap.o if there are any PROTOTYPE comments in Y_INCLUDE files
#   else nil
# SYS_LIBS
#   additional system libraries (beyond -lX11 -lm -lc)
#   use $(FORTRAN_LIBS) to get Fortran libraries
# FORTRAN_STYLE
#   $(WKS_FORTRAN) or $(CRAY_FORTRAN) -- latter is deprecated
# CLEAN_UP
# TIDY_UP
#   any special filenames for the clean and tidy targets
#   $(CODE_NAME) or $(NON_SHARABLE) should go in CLEAN_UP

# many of these can be generated automatically by running
# the make function defined in Y_SITE/include/make.i

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

# 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:

# I don't know whether these do what they're supposed to do (for autoconf).
# They actually confuse GNU make sometimes, since they cause Yorick/ycode.c
# to be found when making the MathC and Drat directories.
srcdir = .

# Directory for the architecture-independent parts of the public Yorick.
# This
Y_SITE = /usr/lib/yorick/1.4

# Directory for the architecture-dependent parts of the public Yorick.
# Y_HOME affects builds of future custom versions of Yorick, but has no
# effect at runtime.
Y_HOME = /usr/lib/yorick/1.4/i386-debian-linux
Y_BINDIR = /usr/bin

Y_VERSION = 1.4
D_Y_VERSION = '-DY_VERSION="$(Y_VERSION)"'

# Directories for the architecture-dependent parts of Yorick
Y_LIBDIR = $(Y_HOME)/lib
Y_INCDIR = $(Y_HOME)/h
Y_CONTRIBDIR = $(Y_LIBDIR)/contrib

# Typical values:
# Y_SITE = /usr/local/share/yorick/1.3
# Y_HOME = /usr/local/lib/yorick/1.3
# Y_BINDIR = /usr/local/bin

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

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

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

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

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

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

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

# Usage:   $(Y_LD) $(OBJS) $(LIBS)
Y_LD = $(CXX) $(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)

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

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

# Repeat for FORTRAN compiler
FC = g77
FFLAGS = $(F_OPTIMIZE) $(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_

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

# Repeat for C++ compiler
CXX = c++
CCC = $(CXX)
CXXFLAGS = $(CXX_OPTIMIZE) $(DEFS) -I$(srcdir) $(YORINCL)
CCFLAGS = $(CXXFLAGS)

# If C++, YORMAIN will be just main.o and Y_LD will use CXX instead of CC
# Thus, main.o needs a rule to build it:
CXXMAIN = main.o
$(CXXMAIN):
	$(CXX) $(CPPFLAGS) -g -c $(Y_INCDIR)/main.c

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

# 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 weird operating system flags
D_HAS_LIBMSET = 
D_HAS_GETCWD = -DHAS_GETCWD
D_NO_STRTOUL = 

# 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

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

# No user serviceable parts beyond this point.

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

CODGER = $(Y_LIBDIR)/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 = yinit.o $(PKG_OBJS)
YORMAIN = /main.o

# ---------------------------------------------------------------------------
# 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)

$(CODE_NAME): $(MAIN_OBJS) $(CODE_LIBRARY)
	$(Y_LD) $(YORMAIN) $(MAIN_OBJS) $(ALL_LIBS)
	$(RM) ycode.c

$(NON_SHARABLE): $(MAIN_OBJS)
	$(Y_LD) $(YORMAIN) $(MAIN_OBJS) $(ALL_LIBS)
	$(RM) ycode.c

# 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) $@

# 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_HOME) $(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 -t *.h *.c *.y

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

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