#  (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 -- LINUX 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

# Use the g++ compiler
CC= g++

# The required compiler flags
CFLAGS= $(EXTRA_CFLAGS) -I$(DB2PATH)/include

# The required libraries 
LIBS= -L$(DB2PATH)/lib -Wl,-rpath,$(DB2PATH)/lib -ldb2

# To connect to a remote SAMPLE database cataloged on the client machine
# 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 udfspclient
#              2d - make otherremoteclient
#              2e - make allremoteclient(= udfspclient + otherremoteclient)
#              2f - make clean
#              2g - make cleanall
#############################################################################


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

all : \
	allremoteclient \
	localclient \
	udfspserver

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

udfspserver : \
	spserver \
	udf udfsrv

#****************************************************************************
#                  2c - make localclient
#****************************************************************************
localclient : \
	autocfg

#****************************************************************************
#                  2c - make udfspclient
#****************************************************************************

udfspclient : \
	calludf \
	spclient \
	udfcli

#****************************************************************************
#                  2d - make otherremoteclient
#****************************************************************************

otherremoteclient : \
	client cursor \
	static \
	thdsrver \
	updat

#****************************************************************************
#                  2e - make allremoteclient
#****************************************************************************

allremoteclient : \
	udfspclient \
	otherremoteclient

#****************************************************************************
#                  2f - make clean
#****************************************************************************

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

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

#****************************************************************************
#                  2g - make cleanall
#****************************************************************************

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

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



#****************************************************************************
#                  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) -o udf udf.c $(CFLAGS) $(LIBS) -ldb2apie -shared
	$(ERASE) $(DB2PATH)/function/udf
	$(COPY) udf $(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) -o udfsrv udfsrv.c $(CFLAGS) $(LIBS) -ldb2apie -shared
	$(ERASE) $(DB2PATH)/function/udfsrv
	$(COPY) udfsrv $(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) -o spserver spserver.C $(CFLAGS) $(LIBS) -shared
	$(ERASE) $(DB2PATH)/function/spserver
	$(COPY) spserver $(DB2PATH)/function/spserver

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

thdsrver.C : thdsrver.sqC
	./embprep thdsrver $(DB) $(UID) $(PWD)
thdsrver : thdsrver.C
	$(CC) -c thdsrver.C -D_REENTRANT $(CFLAGS)
	$(CC) -o thdsrver thdsrver.o  -lpthread $(LIBS)