# $Id: Makefile,v 1.1.1.1 1999/08/21 04:25:41 norman Exp $
# $Log: Makefile,v $
# Revision 1.1.1.1  1999/08/21 04:25:41  norman
# Logitech Mouse Diagnostics
#
# Revision 1.3  1999/06/02 17:16:31  norman
# Corrected an unbalanced ")" in the ASM_OBJ line
#
# Revision 1.2  1999/06/01 18:30:44  norman
# This is a general purpose makefile for compiling an entire directory
# of source code into a single executable or into either a static or
# shared library.
#
# Supported Source Code types are: C/C++/Fortran/Mortran/Assembly
# with shared support for C/C++/Fortran and Mortran.
#
# Revision 1.1  1999/06/01 18:25:58  norman
# Initial revision
#
#
#*****************************************************
# General Makefile for Fortran/Mortran/C/C++ Code
#*****************************************************

#-----------------------------------------------------
# Name of the program to produce
#-----------------------------------------------------
EXE		= Mouse_Diag
#-----------------------------------------------------
# Name of Libraries to Produce
#-----------------------------------------------------
FINALLIB	= libmouse_diag
MAJOR_VER	= 0
MINOR_VER	= 1

#-----------------------------------------------------
# Name of any output files that are made
# and should be cleaned up later
#-----------------------------------------------------
OUT	= core a.out

#-----------------------------------------------------
# Compiler Definitions
#-----------------------------------------------------
F77	= g77
MORT	= compavs 
C++	= g++
CC	= gcc
AR	= ar cr
RANLIB	= ranlib

#-----------------------------------------------------
# Preprocessor Flags 
#-----------------------------------------------------
CPP_FLAG = -E

#-----------------------------------------------------
# Various Preprocessors to Use
#-----------------------------------------------------
CPP	= ${CC} $(CPP_FLAG)
C++PP	= ${C++} $(CPP_FLAG)
FPP	= ${F77} $(CPP_FLAG)

#*****************************************************
#-----------------------------------------------------
# Other Compiler Flags
#-----------------------------------------------------

#-----------------------------------------------------
# User Macro Definitions 
#-----------------------------------------------------
DEFINES1	=

#-----------------------------------------------------
# Debugging
#-----------------------------------------------------
DEBUG		= -g
DEFINES2	= -DDEBUG

#-----------------------------------------------------
# CernLib Variables
#-----------------------------------------------------
# Note: this section is only needed if you plan on linking
#       to the cernlib/geant libraries
CERNDIR 	= /cern
CERNVER		= pro
CERN		= $(CERNDIR)/$(CERNVER)
CERNINC		= $(CERN)/include/
CERN_C_INC	= $(CERNINC)/cfortran/
GEANT		= /geant321
CERNLIB_PATH	= -L$(CERN)/lib
CERNLIBS	= -lgraflib -lgrafX11 -lpawlib -lpacklib -lkernlib -lmathlib
CERNDEFINES	= -DCERNLIB_TYPE -UCERNLIB_INCLUDE -DUNIX -Df2cFortran
#CERNDEFINES	= -DUNIX -Df2cFortran
#-----------------------------------------------------
# Optimization Levels
#-----------------------------------------------------
# Uncomment one of these:
# Standard (no optimization)
# OPT_LEVEL	= -O
# Normal Optimization
OPT_LEVEL	= -O2
# Exteme Optimization
# OPT_LEVEL 	= -O3 -fomit-frame-pointer -Wall -fforce-addr -fforce-mem \
#	          -malign-loops=2 -malign-functions=2 -malign-jumps=2 \
#                 -funroll-loops
# Highest Normal Optimization for an Intel machine using gcc/g++/g77
# OPT_LEVEL	= -O6 -m486
# If you are using egcs try using one of these:
# --Intel PPro or PII--
# OPT_LEVEL	= -O3 -mpentiumpro -fomit-frame-pointer -Wall -fforce-addr \
#                 -fforce-mem -malign-loops=4 -malign-functions=4 -malign-jumps=4 \
#                -funroll-loops
# --Intel Pentium--
# OPT_LEVEL	= -O3 -mpentium -fomit-frame-pointer -Wall -fforce-addr \
#                 -fforce-mem -malign-loops=2 -malign-functions=2 -malign-jumps=2 \
#                 -funroll-loops

#-----------------------------------------------------
# Put all the defines together here
#-----------------------------------------------------
DEFINES		= $(CERNDEFINES) $(DEFINES1) $(DEFINES2)

#-----------------------------------------------------
# Linking Options
#-----------------------------------------------------
STATIC		= -static
SHARED		= -fPIC

#-----------------------------------------------------
# Complilation Options Generating Object Code
#-----------------------------------------------------
F_OBJFLAG	= -c $(OPT_LEVEL) $(DEBUG) $(DEFINES)
C_OBJFLAG	= -c $(OPT_LEVEL) $(DEBUG) $(DEFINES)
C++_OBJFLAG	= -c $(OPT_LEVEL) $(DEBUG) $(DEFINES)
FOPTS		= -FOPTS="$(OPT_LEVEL)"

#-----------------------------------------------------
# Library Paths and Libraries to Link to
#-----------------------------------------------------
LIBPATH	=
LIB	=

#-----------------------------------------------------
# Final Compile and Linking Flags
#-----------------------------------------------------
F_FLAGS	  = $(OPT_LEVEL) $(DEFINES)
C_FLAGS   = $(OPT_LEVEL) $(DEFINES)
C++_FLAGS = $(OPT_LEVEL) $(DEFINES)

#-----------------------------------------------------
# Compiler to use for final Linking Loading
# Note: to use cernlibs you MUST use F77 to link 
#       or you will get undefined symbols
#-----------------------------------------------------
FINAL		= $(CC)
FINAL_FLAGS	= $(C_FLAGS)

#-----------------------------------------------------
# Header Include Path
#-----------------------------------------------------
INC	=

#-----------------------------------------------------
# Source Code definitions
# Types: Fortran, Mortran, C, C++, Assembly
#-----------------------------------------------------
SRCDIR	= .
HEADERS	= $(wildcard $(SRCDIR)/*.h)
ASM_SRC	= $(wildcard $(SRCDIR)/*.s)
F_SRC1	= $(wildcard $(SRCDIR)/*.f)
F_SRC2	= $(wildcard $(SRCDIR)/*.F) 
F_SRC3	= $(wildcard $(SRCDIR)/*.for)
M_SRC	= $(wildcard $(SRCDIR)/*.prepmort)
C_SRC	= $(wildcard $(SRCDIR)/*.c)
C++_SRC1 = $(wildcard $(SRCDIR)/*.C)
C++_SRC2 = $(wildcard $(SRCDIR)/*.cc)
SRC	= $(ASM_SRC) $(F_SRC1) $(F_SRC2) $(F_SRC3) $(C_SRC) $(C++_SRC1) $(C++_SRC2)

#-----------------------------------------------------
# Object Code Definitions
#-----------------------------------------------------
ASM_OBJ	= $(ASM_SRC:.s=.o)
F_OBJ1	= $(F_SRC1:.f=.o)
F_OBJ2	= $(F_SRC2:.F=.o)
F_OBJ3	= $(F_SRC3:.for=.o)
M_OBJ	= $(M_SRC:.prepmort=.o)
C_OBJ	= $(C_SRC:.c=.o)
C++_OBJ1 = $(C++_SRC1:.C=.o)
C++_OBJ2 = $(C++_SRC2:.cc=.o)
OBJ	= $(ASM_OBJ) $(F_OBJ1) $(F_OBJ2) $(F_OBJ3) $(C_OBJ) $(C++_OBJ1) $(C++_OBJ2)

#-----------------------------------------------------
# Shared Object Code
#-----------------------------------------------------
SHARED_OBJ = $(OBJ:.o=.so)

#-----------------------------------------------------
# Compilation Rules for Different Types
# of source Code
#-----------------------------------------------------
# Supported Sections are FORTRAN/MORTRAN/C/C++

#-----------------------------------------------------
# Fortran Code
#-----------------------------------------------------
.F.o:	$(*F).F
	@echo -n "Compiling $(*F).F"
	@${F77} ${F_OBJFLAG} ${STATIC} -I. ${INC} $(*F).F
	@echo -e "\t- DONE!"

.f.o:	$(*F).f
	@echo -n "Compiling $(*F).f"
	@${F77} ${F_OBJFLAG} ${STATIC} -I. ${INC} $(*F).f
	@echo -e "\t- DONE!"

.for.o:	$(*F).for 
	@echo -n "Compiling $(*F).for"
	@${F77} ${F_OBJFLAG} ${STATIC} -I. ${INC} $(*F).for
	@echo -e "\t- DONE!"
#-----------------------------------------------------
# Shared Fortran Code
#-----------------------------------------------------
%.so:	%.F
	@echo -n "Compiling $(*F).F"
	@${F77} ${F_OBJFLAG} -o $(*F).so ${SHARED} -I. ${INC} $(*F).F
	@echo -e "\t- DONE!"

%.so:	%.f
	@echo -n "Compiling $(*F).f"
	@${F77} ${F_OBJFLAG} -o $(*F).so ${SHARED} -I. ${INC} $(*F).f
	@echo -e "\t- DONE!"

%.so:	%.for 
	@echo -n "Compiling $(*F).for"
	@${F77} ${F_OBJFLAG} -o $(*F).so ${SHARED} -I. ${INC} $(*F).for
	@echo -e "\t- DONE!"


#-----------------------------------------------------
# Mortran Code
#-----------------------------------------------------
.prepmort.o:	$(*F).prepmort
	@echo -n "Compiling $(*F).prepmort"
	@compavs $(DEBUG) $(FOPTS) -I. -I$(KLUEINC) $(*F).prepmort
	@echo -e "\t- DONE!"
#-----------------------------------------------------
# Shared Mortran Code
#-----------------------------------------------------
%.so:	%.prepmort
	@echo -n "Compiling $(*F).prepmort"
	@compavs -FOPTS="$(DEBUG) $(OPT_LEVEL) $(SHARED) -o $(*F).so"  -I. -I$(KLUEINC) $(*F).prepmort
	@echo -e "\t- DONE!"

#-----------------------------------------------------
# C++ Code
#-----------------------------------------------------
.C.o:	$(*F).C ${HEADERS}
	@echo -n "Compiling $(*F).C"
	@${C++} ${C_OBJFLAG} ${STATIC}-I. ${INC} $(*F).C
	@echo -e "\t- DONE!"

.cc.o:	$(*F).cc ${HEADERS}
	@echo -n "Compiling $(*F).cc"
	@$(C++) $(C_OBJFLAG) ${STATIC} -I. $(INC) $(*F).cc
	@echo -e "\t- DONE!"

#-----------------------------------------------------
# C++ Code
#-----------------------------------------------------

.c.o:	$(*F).c ${HEADERS}
	@echo -n "Compiling $(*F).c"
	@${CC} ${C_OBJFLAG} ${STATIC} -I. ${INC} $(*F).c
	@echo -e "\t- DONE!"

#-----------------------------------------------------
# Shared C++ Code
#-----------------------------------------------------
%.so:	%.C ${HEADERS}
	@echo -n "Compiling $(*F).C"
	@${C++} ${C_OBJFLAG} -o $(*F).so ${SHARED} -I. ${INC} $(*F).C
	@echo -e "\t- DONE!"

%.so:	%.cc ${HEADERS}
	@echo -n "Compiling $(*F).cc"
	@$(C++) $(C_OBJFLAG) -o $(*F).so ${SHARED} -I. $(INC) $(*F).cc
	@echo -e "\t- DONE!"
#-----------------------------------------------------
# Shared C Code
#-----------------------------------------------------

%.so:	%.c ${HEADERS}
	@echo -n "Compiling $(*F).c"
	@${CC} ${C_OBJFLAG} -o $(*F).so ${SHARED} -I. ${INC} $(*F).c
	@echo -e "\t- DONE!"

#-----------------------------------------------------
# Standard Targets
#-----------------------------------------------------
all:	main

clean:	;@rm -f ${OBJ} ${SHARED_OBJ} ${OUT}
	@echo "Clean completed"

proper:	;@rm -f ${OBJ} ${SHARED_OBJ} ${OUT} ${EXE} ${FINALLIB}.a \
	${FINALLIB}.so.${MAJOR_VER}.${MINOR_VER}
	@echo "Proper clean completed"

#-----------------------------------------------------
# Non-Standard Targets
#-----------------------------------------------------

main:	${HEADERS} ${SRC} ${OBJ}
	@echo -n "Creating Executable: ${EXE}"
	@${FINAL} -o ${EXE} ${FINAL_FLAGS} ${OBJ} ${LIBPATH} ${LIB}
	@echo -e "\t- DONE!"

lib:	${HEADERS} ${SRC} ${OBJ}
	@echo -n "Creating Static Library: ${FINALLIB}"
	@${AR} ${FINALLIB}.a ${OBJ}
	@${RANLIB} ${FINALLIB}.a
	@echo -e "\t- DONE!"

shared:	${HEADERS} ${SRC} ${SHARED_OBJ}
	@echo -n "Creating Shared Library: ${FINALLIB}.so.${MAJOR_VER}.${MINOR_VER}"
	@$(CC) -shared -Wl,-soname,${FINALLIB}.so.${MAJOR_VER} \
	-o ${FINALLIB}.so.${MAJOR_VER}.${MINOR_VER} ${SHARED_OBJ}
	@echo -e "\t- DONE!"
