#  (C) COPYRIGHT International Business Machines Corp. 1995, 2000
#  All Rights Reserved.
#
#  US Government Users Restricted Rights - Use, duplication or
#  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.


# makefile for DB2 Universal Database 
# C++ sample programs for the PTX operating system
                         

# Enter one of the following commands 
#
#   make <app_name>        - Builds the program designated by <app_name>
#
#   make all               - Builds the all the supplied sample programs
#   make udfspserver       - Builds the examples that implement
#                            stored procedures and UDFs (on the server)
#   make localclient       - Builds the examples that can only be run
#                            successfully on a server 
#   make udfspclient       - Builds the examples that call stored 
#                            procedures and UDFs ( they can run remotely )
#   make otherremoteclient - Builds the examples that will run successfully
#                            on a client platform ( others than udfspclient )
#   make allremoteclient   - Builds programs in udfspclient and otherremoteclient
#                            categories
#
#   make clean             - Erases intermediate files
#   make cleanall          - Erases all files produced in the build process,
#                            except the original source files

# The makefile contains the following sections:
#    1 -- COMPILERS + VARIABLES
#    2 -- MAKE CATEGORIES
#    3 -- COMMANDS TO MAKE INDIVIDUAL SAMPLES


##################################################################################
#                  1 -- COMPILERS + VARIABLES                                     
##################################################################################

# This file assumes the DB2 instance path is defined by the variable HOME.
# It also assumes DB2 is installed under the DB2 instance.
# If these statements are not correct, update the variable DB2PATH. 
DB2PATH = $(HOME)/sqllib

# The following compile and link options are for the ptx/C++ compiler.
CC=c++

CFLAGS=$(EXTRA_CFLAGS) -I$(DB2PATH)/include -D_RWSTD_COMPILE_INSTANTIATE=0

CFLAGSMT=$(EXTRA_CFLAGS) -I$(DB2PATH)/include -D_RWSTD_COMPILE_INSTANTIATE=0 -Kthread

LIBS=-L$(DB2PATH)/lib -ldb2
# Linked Libraries for creating UDFs.
LIBSUDF=-L$(DB2PATH)/lib -ldb2 -ldb2apie

LIBSMT=-L$(DB2PATH)/lib -ldb2 -Kthread 

# linker for shared library
LINKSL=c++ -shared -G

# To connect to a database with another name, update the DB variable.
DB=sample
# Set UID and PWD if neccesary  
UID=
PWD=

COPY=cp
ERASE=rm -f

#############################################################################
#  2 -- MAKE CATEGORIES
#              2a - make all(= allremoteclient + localclient +udfspserver)
#              2b - make udfspserver
#              2c - make localclient  
#              2d - make udfspclient
#              2e - make otherremoteclient
#              2f - make allremoteclient(= udfspclient + otherremoteclient)
#              2g - make clean
#              2h - make cleanall
#############################################################################


#****************************************************************************
#                  2a - make all
#****************************************************************************

all : \
	allremoteclient \
	localclient \
	udfspserver

#****************************************************************************
#                  2b - make udfspserver
#****************************************************************************

udfspserver : \
	spserver \
	udf  udfsrv

#****************************************************************************
#                  2c - make localclient 
#****************************************************************************

localclient : \
	autocfg

#****************************************************************************
#                  2d - make udfspclient
#****************************************************************************

udfspclient : \
	calludf \
	spclient \
	udfcli

#****************************************************************************
#                  2e - make otherremoteclient
#****************************************************************************

otherremoteclient : \
	client cursor \
	static \
	thdsrver \
	updat

#****************************************************************************
#                  2f - make allremoteclient
#****************************************************************************

allremoteclient : \
	udfspclient \
	otherremoteclient

#****************************************************************************
#                  2g - make clean
#****************************************************************************

clean :	\
	cleangen \
	cleanemb
 
cleangen :
	$(ERASE) *.o *.map

cleanemb :
	$(ERASE) autocfg.C 
	$(ERASE) calludf.C cursor.C
	$(ERASE) spclient.C spserver.C static.C
	$(ERASE) thdsrver.C
	$(ERASE) udfcli.C updat.C utilemb.C

#****************************************************************************
#                  2h - make cleanall
#****************************************************************************

cleanall : \
	clean
	$(ERASE) *.bnd
	$(ERASE) autocfg
	$(ERASE) calludf client cursor
	$(ERASE) spclient spserver.so static
	$(ERASE) thdsrver 
	$(ERASE) updat udf.so udfcli udfsrv.so

	$(ERASE) $(DB2PATH)/function/spserver	
	$(ERASE) $(DB2PATH)/function/udf
	$(ERASE) $(DB2PATH)/function/udfsrv

#############################################################################
#  3 -- COMMANDS TO MAKE INDIVIDUAL SAMPLES
#                  3a -  utilities
#                  3b -  non embedded SQL, non client/server samples
#                  3c -  embedded SQL, non client/server samples
#                  3d -  client/server samples (mixed)
#                  3e -  multi-threaded samples
#############################################################################

#****************************************************************************
#                  3a -  utilities
#****************************************************************************

utilapi.o : utilapi.C
	$(CC) -c utilapi.C $(CFLAGS)

utilemb.C : utilemb.sqC
	./embprep utilemb $(DB) $(UID) $(PWD)
utilemb.o : utilemb.C
	$(CC) -c utilemb.C $(CFLAGS)

#****************************************************************************
#                  3b -  non embedded SQL, non client/server samples
#****************************************************************************

client : client.C utilapi.o
	$(CC) -o client client.C utilapi.o $(CFLAGS) $(LIBS)

#****************************************************************************
#                  3c -  embedded SQL non client/server samples
#****************************************************************************
autocfg.C : autocfg.sqC
	./embprep autocfg $(DB) $(UID) $(PWD)
autocfg : autocfg.C utilemb.o
	$(CC) -o autocfg autocfg.C utilemb.o $(CFLAGS) $(LIBS)

cursor.C : cursor.sqC
	./embprep cursor $(DB) $(UID) $(PWD)
cursor : cursor.C utilemb.o
	$(CC) -o cursor cursor.C utilemb.o $(CFLAGS) $(LIBS)

static.C : static.sqC
	./embprep static $(DB) $(UID) $(PWD)
static : static.C utilemb.o
	$(CC) -o static static.C utilemb.o $(CFLAGS) $(LIBS)

updat.C : updat.sqC
	./embprep updat $(DB) $(UID) $(PWD)
updat : updat.C utilemb.o
	$(CC) -o updat updat.C utilemb.o $(CFLAGS) $(LIBS)

#****************************************************************************
#                  3d -  client/server samples (mixed)   
#****************************************************************************

#--------------------calludf/udf--------------------------------------------#

calludf.C : calludf.sqC
	./embprep calludf $(DB) $(UID) $(PWD)
calludf : calludf.C utilemb.o
	$(CC) -o calludf calludf.C utilemb.o $(CFLAGS) $(LIBS)

udf : udf.c
	cc -c udf.c $(CFLAGS)
	$(LINKSL) -o udf udf.o $(LIBSUDF)
	$(ERASE) $(DB2PATH)/function/udf
	$(COPY) udf.so $(DB2PATH)/function/udf

#--------------------  udfcli/udfsrv----------------------------------------#
udfcli.C : udfcli.sqC
	./embprep udfcli $(DB) $(UID) $(PWD)
udfcli : udfcli.C utilemb.o
	$(CC) -o udfcli udfcli.C utilemb.o $(CFLAGS) $(LIBS)

udfsrv : udfsrv.c
	$(CC) -c udfsrv.c $(CFLAGS)
	$(LINKSL) -o udfsrv udfsrv.o $(LIBSUDF)  
	$(ERASE) $(DB2PATH)/function/udfsrv
	$(COPY) udfsrv.so $(DB2PATH)/function/udfsrv

#--------------------spclient/spserver--------------------------------------#

spclient.C : spclient.sqC
	./embprep spclient $(DB) $(UID) $(PWD)
spclient : spclient.C utilemb.o
	$(CC) -o spclient spclient.C utilemb.o $(CFLAGS) $(LIBS)

spserver.C : spserver.sqC
	./embprep spserver $(DB) $(UID) $(PWD)

spserver : spserver.C 
	$(CC) -c spserver.C $(CFLAGS)
	$(LINKSL) -o spserver spserver.o $(LIBS) 
	$(ERASE) $(DB2PATH)/function/spserver
	$(COPY) spserver.so $(DB2PATH)/function/spserver

#****************************************************************************
#                  3e -  multi-threaded samples
#****************************************************************************

thdsrver.C : thdsrver.sqC
	./embprep thdsrver $(DB) $(UID) $(PWD)
thdsrver : thdsrver.C
	$(CC) -c thdsrver.C $(CFLAGSMT)
	$(CC) -o thdsrver thdsrver.o  $(LIBSMT)