#
# Makefile.in for the Tiny COBOL compiler runtime routines library
#

#
# Debug flags are set in htconfig.h
# -DPICTURE_TESTING -DDEBUG_RTS
#
CCX=gcc
ASM=as

INCLUDES=-I/usr/include -I/usr/local/include -I/usr/include/glibc -I/c/tcl/include -I../lib -I.. -I. -I/c/PostgreSQL/include 

CCXFLAGS=${INCLUDES} -Wall 

WRES=windres
WRESFLAGS=-O coff

RANLIB=ranlib
AR=ar

lib_dir=C:/TinyCOBOL

INSTRC=${lib_dir}
RCFILE=htrtconf

INSTALL=install.exe
INSTALL_DATA=${INSTALL}

MV   = mv -f
RM   = rm -f
LNK  = ln -sf
MKDIR = mkdir -p

#

LIBS1=-L/usr/local/lib -L/c/mingw/lib -lpdcurses
LIBS2=-L/usr/local/lib /d/tinycobol/tcltk84/tcl84.dll /d/tinycobol/tcltk84/tk84.dll 
LIBS3=-L/usr/local/lib -L/c/PostgreSQL/lib -lpq

#
# Default install directory is set in htconfig.h
#

lib_version=0.73.0

lib_name1=tcroutines
lib_name2=tctcl
lib_name3=tcpgres

#
# Libraries to build
#
STATIC_LIB1=lib${lib_name1}.a
SHARED_LIB1=${lib_name1}.dll
SHARED_LIB1A=${lib_name1}.dll.a

STATIC_LIB2=lib${lib_name2}.a
SHARED_LIB2=${lib_name2}.dll
SHARED_LIB2A=${lib_name2}.dll.a

STATIC_LIB3=lib${lib_name3}.a
SHARED_LIB3=${lib_name3}.dll
SHARED_LIB3A=${lib_name3}.dll.a

#
# Sources list
#
SRC1  = screen.c dir.c files.c globals.c system.c
OBJS1 = $(SRC1:.c=.o)

SRC2  = tctcl.c
OBJS2 = $(SRC2:.c=.o)

SRC3  = sql_clear_query.c sql_connect_db.c sql_disconnect_db.c sql_exec_query.c \
		sql_free_memory.c sql_get_tuple.c sql_get_value.c sql_max_field.c sql_max_tuple.c sql_status_message.c
OBJS3 = $(SRC3:.c=.o)

#
# Rules for compiling .c .s sources
#
.SUFFIX: .c .o .lo
.c.o:
	$(CCX) $(CCXFLAGS) -c $<

%.lo: %.c
	$(CCX) $(CCXFLAGS1) -c $< -o $@

#
# Notes on building and using the shared htcobol library.
# - The linker will try to use the shared libraries by default. 
#   If not available it will try to use the static libraries.
# - When linking with a shared htcobol library, all other libraries must be 
#   named, whether they are used in the cobol program or not. 
#   (i.e. -lncurses -ldb)
# - To run a cobol program built with a shared htcobol library, and the
#   htcobol library is not installed in one of the standard locations, use 
#   the $LD_LIBRARY_PATH environmental variable to add this path.
#   (i.e. export LD_LIBRARY_PATH=this path:$LD_LIBRARY_PATH )
# - For testing purposes, it is recommended that only a static htcobol library 
#   be used, since gdb will not work properly with shared libraries.
#

all: static-libs
devel: static-libs shared-libs

static-libs: ${STATIC_LIB1} ${STATIC_LIB2} ${STATIC_LIB3}
shared-libs: ${SHARED_LIB1} ${SHARED_LIB2} ${SHARED_LIB3}

#
# Rules for building the static libraries
#
${STATIC_LIB1}: ${OBJS1}
	${AR} cr ${STATIC_LIB1} ${OBJS1}
	${RANLIB} ${STATIC_LIB1}

${STATIC_LIB2}: ${OBJS2}
	${AR} cr ${STATIC_LIB2} ${OBJS2}
	${RANLIB} ${STATIC_LIB2}

${STATIC_LIB3}: ${OBJS3}
	${AR} cr ${STATIC_LIB3} ${OBJS3}
	${RANLIB} ${STATIC_LIB3}

#
# Rules for building the shared libraries
#
${SHARED_LIB1}: ${OBJS1}
	${CCX} -shared -Wl,--out-implib,${lib_name1}.dll.a,--output-def,${lib_name1}.def  -o ${SHARED_LIB1} $(OBJS1) ${LIBS1}

${SHARED_LIB2}: ${OBJS2}
	${CCX} -shared -Wl,--out-implib,${lib_name2}.dll.a,--output-def,${lib_name2}.def  -o ${SHARED_LIB2} $(OBJS2) ${LIBS2}

${SHARED_LIB3}: ${OBJS3}
	${CCX} -shared -Wl,--out-implib,${lib_name3}.dll.a,--output-def,${lib_name3}.def  -o ${SHARED_LIB3} $(OBJS3) ${LIBS3}

#
# Rule for cleaning
#
clean:
	@${RM} ${OBJS1} ${OBJS2} ${OBJS3} \
	${STATIC_LIB1} ${SHARED_LIB1} ${SHARED_LIB1A} \
	${STATIC_LIB2} ${SHARED_LIB2} ${SHARED_LIB2A} \
	${STATIC_LIB3} ${SHARED_LIB3} ${SHARED_LIB3A} \
	core *.bak *.o *.lo

#
# Rule for installing
#
install: install-dir install-static install-shared

install-static: ${STATIC_LIB1} ${STATIC_LIB2} ${STATIC_LIB3}
#	${INSTALL} ${STATIC_LIB1} ${lib_dir}/${STATIC_LIB1}
#	${INSTALL} ${STATIC_LIB2} ${lib_dir}/${STATIC_LIB2}
#	${INSTALL} ${STATIC_LIB3} ${lib_dir}/${STATIC_LIB3}

install-shared: ${SHARED_LIB1} ${SHARED_LIB2} ${SHARED_LIB3}
	${INSTALL} ${SHARED_LIB1} ${lib_dir}/${SHARED_LIB1}
	${INSTALL} ${SHARED_LIB2} ${lib_dir}/${SHARED_LIB2}
	${INSTALL} ${SHARED_LIB3} ${lib_dir}/${SHARED_LIB3}
#	${INSTALL} ${SHARED_LIB1A} ${lib_dir}/${SHARED_LIB1A}
#	${INSTALL} ${SHARED_LIB2A} ${lib_dir}/${SHARED_LIB2A}
#	${INSTALL} ${SHARED_LIB3A} ${lib_dir}/${SHARED_LIB3A}

install-dir: 
	$(MKDIR) $(INSTRC)

