# A good place to get information on 
# how to create a Makefile is at
# http://vertigo.hsrl.rutgers.edu/ug/make_help.html
#
# Define the Compiler and Linker variables
CC = gcc
LD = g77

# Define the archiver and indexer
AR = /usr/bin/ar
RANLIB = /usr/bin/ranlib


# Define program's object files
PROG_OBJS = test.o

# Define program's executable
PROG = ./test

# library's archive file
LIB_FILE = libutil.a 

# library's object files.
#LIB_OBJS = stuafh.o formFactorsPlus.o dataParse.o tantrumFileIO.o dcs.o
LIB_OBJS = libTest.o

# library to use when linking the main program
# the '-L.' option is used to tell the compiler to look for libraries
# in the current directory, as well as the normal system library locations.
LIBS = -L.
#LIBS += -L/usr/local/clas/builds/PRODUCTION/lib/LinuxRHEL3 
#LIBS += -L/cern/pro/lib/ 
#LIBS += -lpacklib -lutil

INCLUDE = -I. 
#INCLUDE += -I/cern/pro/include/cfortran
#INCLUDE += -I/usr/local/clas/builds/PRODUCTION/packages/include


# top-level rule
all: $(LIB_FILE) $(PROG)


# create our library
$(LIB_FILE): $(LIB_OBJS)
	$(AR) rc $(LIB_FILE) $(LIB_OBJS)
	$(RANLIB) $(LIB_FILE)


$(PROG): $(PROG_OBJS)
	$(LD) -O2 -mcpu=pentium -fno-automatic -finit-local-zero -ffixed-line-length-none -fno-second-underscore \
	 $(PROG_OBJS) $(INCLUDE) $(LIBS)  -o $(PROG)

# NOTES 
# The lines with the colon ":" in them are dependency lines.
# These determine when the target is to be rebuilt.
# To the left of the colon is the target that is to be built from the 
# dependencies. To the right of the colon are the objects
# Thus,
# $(PROG): $(PROG_OBJS)
# Says that $(PROG) depends on sources $(PROG_OBJS)
#
# The indented (Using a TAB) line after the dependency line
# is called a shell line. The shell line tells the make command
# how to build the target. For this example the shell line is
# $(LD) $(PROG_OBJS) -o $(PROG)
#
# Since we have defined:
# CC = g77
# LD = g77
# PROG_OBJS = main.o
# PROG = prog
#
# Then the dependency line realy says->
# prog: main.o
#
# In english-> The executable called prog is built from the
# object called main.o
#
# The shell line realy says->
# g77 main.o -o prog
# 
# In english-> The executable called prog is created by
# using the g77 linker from the program object called
# main.o

# compile source files into object files.
%.o: %.c
	$(CC) -O2 -mcpu=pentium   -DLinux $(INCLUDE) -c $<

# NOTE
# The above lines are comprised of a dependency line
# followed by the shell line.
# The dependency line uses the wildcard character "%". 
# The wildcard matches anything. Make will look at the 
# definition of PROG_OBJS and LIB_OBJS to see if any
# match "%.o". In our example main.o matches and so
# the dependency file says that main.o is dependent
# on main.f.
#
# The shell line is
# $(CC) -c $<
# This uses the character combination "$<".
# This character combination is the name of the 
# related file that caused the action. 
# Since the dependency line caused the action and 
# the target of that line is %.o, then the $<
# will be equal to %.o. The make command is smart
# enough to know that the source file %.f is to 
# be compiled. This is known as an Implicit Rule. 


# Define variable RM
RM = /bin/rm -f

# define the standard input variable clean
clean:
	$(RM) $(PROG_OBJS) $(PROG) $(LIB_FILE) $(LIB_OBJS)


