#*********************************************************************
#
# File:          Makefile
#
# Project:       Database for Device Servers with sun-rpc
#
# Description:   Makefile for DBAPI-LIB, the Application Programmer's 
#                Interface Library - libdbapi (archive) and libdbapi.so 
#		 (shared).
#
#                Please use the conditional GNU Makefile commands !
#			
#		 e.g. gmake unix=1 linux=1 x86=1 all
#
# Author(s):     Andy Gotz
#
# Original:	 October 1998
#
#
# $Revision$
# $Date$
#
# $Author$
#
# $Log$
#
# Copyright (c) 1998 by European Synchrotron Radiation Facility,
#                       Grenoble, France
#
#                       All Rights Reserved
#
#**********************************************************************

#---------------------------------------------------------------------
# Home Directories
#
#
#       The variables DSHOME is passed to the Makefile
#       as input arguments or via the environment.
#
# external variable DSHOME
DS_HOME   =   	$(DSHOME)
APIVERS=$(shell ident setacc_cli.c | grep Header | awk -f ./version.awk)

LOHOME    =	`pwd`

ifdef vwx86
RM = del
else
RM = rm -rf
endif # vwx86

ifdef _UCC
INSTALL_HOME =  $(DS_HOME)/lib/os9
endif
ifdef __hp9000s700
INSTALL_HOME =  $(DS_HOME)/lib/s700
INSTALL_HOME_SL =  $(DS_HOME)/lib/s700
endif
ifdef __hpux10
INSTALL_HOME =  $(DS_HOME)/lib/hpux10.2
INSTALL_HOME_SL =  $(DS_HOME)/lib/hpux10.2
endif
ifdef sun
INSTALL_HOME =  $(DS_HOME)/lib/sun4
endif
ifdef _solaris
INSTALL_HOME =  $(DS_HOME)/lib/solaris
INSTALL_HOME_SL =  $(DS_HOME)/lib/solaris
endif
ifdef lynx
INSTALL_HOME =  $(DS_HOME)/lib/lynxos
endif
ifdef linux
ifdef x86
INSTALL_HOME =  $(DS_HOME)/lib/linux/x86
INSTALL_HOME_SL =  $(DS_HOME)/lib/linux/x86
endif # x86
ifdef 68k
INSTALL_HOME =  $(DS_HOME)/lib/linux/68k
INSTALL_HOME_SL =  $(DS_HOME)/lib/linux/68k
endif # 68k
endif # linux
ifdef vw68k
INSTALL_HOME =  $(DS_HOME)/lib/vxworks/68k
endif
ifdef vwx86
INSTALL_HOME =  $(DS_HOME)\lib\vxworks\x86
endif



#---------------------------------------------------------------------
ifdef _UCC
#               The C Compiler for OS9

#               ... and his Flags
#                       Includes
INCLDIRS=       -I ../include \
                -I ../../system/api/apilib/include \
                -I $(DS_HOME)/include \
                -I $(DS_HOME)/include/private \
                -I /usr/local/os9/dd/DEFS

#                       Compiler Flags
CFLAGS =        -mode=c89 -g -D EBUG -to osk -tp 020 -x il -e as=. $(INCLDIRS) \
		$(EXTRACFLAGS)
ICODE_CFLAGS =  -mode=c89 -to osk -tp 020 -e il $(INCLDIRS)
NAME =          -o $@
endif

ifdef unix
#               ... and his Flags
#                       Includes
CINCLDIRS=      -I../include \
                -I ../../system/api/apilib/include \
		-I$(DS_HOME)/include \
		-I$(DS_HOME)/include/private

ifndef vwx86
INCLDIRS=	$(CINCLDIRS)
endif
ifdef vw68k
INCLDIRS=	-I/usr/local/tornado/target/h \
		-I/usr/local/tornado/target/h/rpc \
		-I/usr/local/tornado/target/src/config \
		$(CINCLDIRS)
endif # vw68k
ifdef vwx86
INCLDIRS=	$(CINCLDIRS) \
		-IC:\TORNADO\target\h \
		-IC:\TORNADO\target\h\rpc \
		-IC:\TORNADO\target\src\config
endif # vwx86

#                       Libraries
#               No link phase - ergo no libraries to include

NAME    =       -o
endif # unix

#                       Optimizer or ANSI standart?
ifdef __hpux
CFLAGS  =       -Aa -g -DEBUG -Dunix -D_HPUX_SOURCE -c $(INCLDIRS) \
		$(EXTRACFLAGS)
CFLAGS_SL  =    $(CFLAGS) +z
endif
ifdef __hpux10
CFLAGS  =       -Aa -g -DEBUG -Dunix -D_HPUX_SOURCE -D__hpux10 -DBSD=199704 \
		-c $(INCLDIRS) $(EXTRACFLAGS)
CFLAGS_SL  =    $(CFLAGS) +z
endif
ifdef sun
CFLAGS  =       -Aa -g -DEBUG -Dunix -c $(INCLDIRS) $(EXTRACFLAGS)
endif
ifdef _solaris
ifdef NATIVE
CFLAGS  =       -Xa -g -Dsolaris -DEBUG -c $(INCLDIRS) $(EXTRACFLAGS)
CFLAGS_SL  =    $(CFLAGS) -KPIC
else
CFLAGS  =       -g -Dsolaris -DEBUG -c $(INCLDIRS) $(EXTRACFLAGS)
CFLAGS_SL  =    $(CFLAGS) -fPIC
endif # NATIVE
endif
ifdef lynx
CFLAGS  =      	-Dlynx -Dunix -ansi -X -DEBUG -g -c $(INCLDIRS) $(EXTRACFLAGS)
endif
ifdef linux
CFLAGS  =      	$(INCLDIRS) -Dlinux -Dunix -D_GNU_SOURCE -ansi -DEBUG -g -c \
		$(EXTRACFLAGS)
CFLAGS_SL  =   	$(INCLDIRS) -Dlinux -Dunix -D_GNU_SOURCE -ansi -DEBUG -fPIC -c \
		$(EXTRACFLAGS)
endif
ifdef vw68k
CFLAGS	=	-Dvxworks -Dunix -DCPU=MC68040 -ansi -m68040 \
		-fno-builtin -nostdinc -DEBUG $(INCLDIRS) $(EXTRACFLAGS)
endif 
ifdef vwx86
CFLAGS	=	-c -Dvxworks -Dunix -D__STDC__ -fno-builtin  -nostdinc \
		-fno-defer-pop -mno-486 -DCPU=I80386 -ansi \
		-DEBUG $(INCLDIRS) -g $(EXTRACFLAGS)
endif

#---------------------------------------------------------------------
# RCS options
#
#               RCS lock options
RCSLOCK  =      co -l -r$(VERSION)
#               RCS check out options
RCSCO    =      co -r$(VERSION)
#               RCS check in options
RCSCI    =      ci -u -f -s"Rel" -r$(VERSION) -m"$(LOCKMSG)"
#               RCS unlock options
RCSUNLOCK  =    rcs -u$(VERSION)
#               RCS diff options
RCSDIFF    =    rcsdiff -r$(VERSION)

#---------------------------------------------------------------------
# All files needed for the library
#
#	File list for RCS control
#               all include files
API_INCL   =   	db_setup.h \
		db_xdr.h

#               source files
API_SRC   = 	setacc_cli.c \
	 	setacc_clnt.c \
	 	setacc_xdr.c \
		browse_cli.c \
		reconnect_cli.c \
		tools_cli.c \
		update_cli.c
#
#	source list to make library
#		source files
ifdef vwx86
SRC =		setacc_cli.c \
	 	setacc_clnt.c \
	 	setacc_xdr.c \
                browse_cli.c \
                reconnect_cli.c \
                tools_cli.c \
                update_cli.c

else
SRC =		setacc_cli.c \
	 	setacc_clnt.c \
	 	setacc_xdr.c \
                browse_cli.c \
                reconnect_cli.c \
                tools_cli.c \
                update_cli.c

endif # vwx86

#               object files
ifdef vwx86
OBJS =		setacc_cli.o \
  		setacc_clnt.o \
	 	setacc_xdr.o \
                browse_cli.o \
                reconnect_cli.o \
                tools_cli.o \
                update_cli.o
else
OBJS =		setacc_cli.o \
  		setacc_clnt.o \
	 	setacc_xdr.o \
                browse_cli.o \
                reconnect_cli.o \
                tools_cli.o \
                update_cli.o
endif # vwx86


OBJS_SL =	setacc_cli.sl.o \
  		setacc_clnt.sl.o \
	 	setacc_xdr.sl.o \
                browse_cli.sl.o \
                reconnect_cli.sl.o \
                tools_cli.sl.o \
                update_cli.sl.o

ICODE_OBJS = 	setacc_cli.ic \
  		setacc_clnt.ic \
	 	setacc_xdr.ic \
                browse_cli.ic \
                reconnect_cli.ic \
                tools_cli.ic \
                update_cli.ic


#--------------------------------------------------------------------
# What has to be made
#
LIBNAME =       libdbapi.a

ifdef _solaris
LIBNAME_SL =    libdbapi
SL_EXT     =    so
endif
ifdef __hp9000s700
LIBNAME_SL =    libdbapi
SL_EXT     =    sl
endif
ifdef linux
LIBNAME_SL =    libdbapi
SL_EXT     =    so
endif

ifdef _UCC
ICODE_LIBNAME = libdbapi.il
endif

#               Names of the include files in the 
#		installation directories
#
INCL_INST  = 	../include/db_setup.h \
		../include/db_xdr.h

#---------------------------------------------------------------------
# Build library

ifdef unix
all:	$(OBJS) $(LIBNAME)
all_sl:	$(LIBNAME_SL).$(SL_EXT)
#
# DO NOT force recompilation of all source files
#
#$(OBJS): $(SRC)
#		$(CC) $(CFLAGS) $(SRC)
endif # unix

	

ifdef OSK
#               Making an OS-9 library is concatenating the members
$(LIBNAME):     $(OBJS)
		$(RM) $(LIBNAME)
		cat $(OBJS) > $(LIBNAME)      
endif

ifdef unix
# define a rule for making shared library objects (suffice .sl.o)
.SUFFIXES: .sl.o .c
.c.sl.o:
		$(CC) $(CFLAGS_SL) $< -o $*.sl.o
endif

ifdef _UCC
.SUFFIXES: .ic .c
.c.ic:
	        $(CC) $(ICODE_CFLAGS) $(NAME) -efe $<
.SUFFIXES: .ic .C
.C.ic:

#
# DO NOT recompile all icode files
#
#$(ICODE_OBJS):	
#	        $(CC) $(ICODE_CFLAGS) $(NAME) -efe $<


$(LIBNAME):     $(OBJS) $(ICODE_OBJS)
		libgen $(OBJS) -co=$(LIBNAME)
		$(CC) $(ICODE_CFLAGS) $(ICODE_OBJS) -Wi,-m -f $(ICODE_LIBNAME)

endif


ifdef unix
#		Make HPUX library
$(LIBNAME):     $(OBJS)
ifdef vw68k
		ar68k -rc $(LIBNAME) $(OBJS) 
		ar68k -s $(LIBNAME) $(OBJS) 
#		ranlib68k $(LIBNAME)
else
ifdef vwx86
		ar386 -rc $(LIBNAME) $(OBJS) 
		ar386 -s $(LIBNAME) $(OBJS) 
#		ld386 -r -o $(LIBNAME) $(OBJS)
else
		ar rv $(LIBNAME) $(OBJS)
endif # vwx86
endif # vw68k

$(LIBNAME_SL).$(SL_EXT):	$(OBJS_SL)
ifdef APIVERS
		echo "making shared library VERSION $(APIVERS) ..."
ifdef _solaris
		ld -G -o $(LIBNAME_SL).$(APIVERS).$(SL_EXT) $(OBJS_SL)
endif # _solaris
ifdef __hp9000s700
		ld -b -o $(LIBNAME_SL).$(APIVERS).$(SL_EXT) $(OBJS_SL)
endif # __hp9000s700
ifdef linux
		$(CC) -shared -Wl,-soname,$(LIBNAME_SL).$(SL_EXT) \
		-o $(LIBNAME_SL).$(APIVERS).$(SL_EXT) $(OBJS_SL)
endif # linux
		$(RM) $(LIBNAME_SL).$(SL_EXT)
		ln -s $(LIBNAME_SL).$(APIVERS).$(SL_EXT) $(LIBNAME_SL).$(SL_EXT)
else
		echo "cannot make shared library, APIVERS not defined !"
endif # APIVERS
endif # unix

install_incl:	
	cp -f ../include/db_setup.h $(DSHOME)/include
	chmod 664 $(DSHOME)/include/db_setup.h
	cp -f ../include/db_xdr.h $(DSHOME)/include
	chmod 664 $(DSHOME)/include/db_xdr.h
		

install :       install_incl
#		install library
		cp $(LIBNAME) $(INSTALL_HOME)
ifdef _UCC
		cp $(ICODE_LIBNAME) $(INSTALL_HOME)
endif

install_sl :
ifdef APIVERS
		echo "installing shared library VERSION $(APIVERS) ..."
		cp $(LIBNAME_SL).$(APIVERS).$(SL_EXT) $(INSTALL_HOME_SL)
		$(RM) $(INSTALL_HOME_SL)/$(LIBNAME_SL).$(SL_EXT)
		cd $(INSTALL_HOME_SL); \
		ln -s $(LIBNAME_SL).$(APIVERS).$(SL_EXT) $(LIBNAME_SL).$(SL_EXT)
else
		echo "cannot install shared library, APIVERS not defined !"
endif # APIVERS

clean :
		$(RM) $(OBJS)
ifndef vxworks
		$(RM) $(OBJS_SL)
endif # !vxworks
ifdef _UCC
		rm -rf $(ICODE_OBJS)
endif # _UCC

clobber:	clean
		$(RM) $(LIBNAME) 
ifdef LIBNAME_SL
		$(RM) $(LIBNAME_SL).$(SL_EXT)
		$(RM) $(LIBNAME_SL).$(APIVERS).$(SL_EXT)
endif
ifdef _UCC
		$(RM) $(ICODE_LIBNAME)
		$(RM) $(ICODE_LIBNAME)
endif


lock:
		$(RCSLOCK) $(API_SRC)
		cd ../include; $(RCSLOCK) $(API_INCL)
		cd $(LOHOME)

co:
		$(RCSCO) $(API_SRC)
		cd ../include; $(RCSCO) $(API_INCL); cd ../src
		cd $(LOHOME)

ci:
		$(RCSCI) $(API_SRC)
		cd ../include; $(RCSCI) $(API_INCL); cd ../src
		cd $(LOHOME)

unlock:
		$(RCSUNLOCK) $(API_SRC)
		cd ../include; $(RCSUNLOCK) $(API_INCL)
		cd $(LOHOME)

diff:
		$(RCSDIFF) $(API_SRC)
		cd ../include; $(RCSDIFF) $(API_INCL)
		cd $(LOHOME)
