#				Makefile
# 		to build and test my working environment
#	(including my i/o library and an arithmetic coding classlib)
#
# To build the library, you may want to edit the list of the library modules
# below (MODULES). Say, if you don't need/want voc, remove all the names
# that contain voc from MODULES= below. Then point ./c++ to the right
# script: g++.2.95 or eg++.2.91.
# Then make the modules you chose into the library by
#		make lib
# simple 'make' would suffice, too.
#
# To verify the library, do
#	make check-all
# or, more specifically,
#	make vmyenv		(checks myenv utility functions)
#	make vendian_io		(checks Endion I/O and bitstream i/o)
#	make vendian_io_ext	(the same but with extended file names)
#	make vhistogram		(checks histogram operations)
#	make varithm		(checks arithmetic compression/decompression)
#	make vTCPstream		(checks TCP streams)
#	make vTCPstream_server	(checks TCP streams on a server side)
#	make vvoc		(checks vocabularies)
#
# Type
#	make lib tcp-trans
# to make the TCP transactor tool.
#
# Note, this Makefile was built (and works under) GNU make 3.71+
#
# For the vTCPstream and vendian_io_ext tests, you need to check and set
# the ECHO_HOST variable defined below. It should be the name or an
# IP address of a host that accepts TCP connections on ports 7 and 13
# (echo and date ports). ECHO_HOST is currently set to localhost. Note,
# you MUST change this setting if you are working on a Linux or a
# FreeBSD platform as 'localhost' normally does not allow connections
# on these ports.
#
# A test case vendian_io_ext is not enabled by default: this is because
# wedging of sys_open() in place of open(2) is rather platform-specific.
# It has to be stressed that _every_ linker is capable of necessary
# operations, but pertinent linker flags vary from platform
# to platform. The only place that needs adjustment is an open_ext.o
# rule at the end of this file. 
# This Makefile specifies procedures that work on
# "HP-UX B.10.10."
# "SunOS 5.6 Generic sun4u sparc SUNW,Ultra-2"
# "FreeBSD3.2" and "FreeBSD4.0"
# "Linux 2.2.x"
# You may need to adjust the rule for your platform, and then type
# 	make vendian_io_ext
#
# Test case vTCPstream_server is also not enabled by default because
# the test is interactive. The test runs a TCP server that listens on a
# port specified by an env variable VTCP_PORT (see below). After you
# launch the test, you need to connect to the port (for example, using
# telnet). After accepting the connection, the server will print the
# current timestamp. The server will then wait for you to type an
# arbitrary string of ASCII characters, terminated with a newline.
# The server will echo the string back to you. The server will exit
# after two connections.
#
# $Id: Makefile,v 4.2 2000/11/19 22:57:06 oleg Exp oleg $

CC=./c++
CCL=g++ -pipe
.SUFFIXES: .cc
MODULES=myenv.cc endian_io.cc \
	arithm_coding.cc arithm_model.cc arithm_modadapt.cc \
	arithm_modadh.cc histogram.cc \
	TCPstream.cc voc.cc voc_io.cc

LIBRARY=libcppadvio.a
CLDEF=			# Other flags for the linker, if needed
LIBS=-lm		# Other libraries to use, if needed
# See the platform-specific section below for the following
#RANLIB = (ar d $(LIBRARY) __.SYMDEF || true); ranlib $(LIBRARY) # for BSD
#RANLIB = /bin/true 	# for Solaris 2.x, HP/UX and other SysV-based
#NETLIBS = -lnsl -lsocket # For Solaris 2.x
#NETLIBS = # for HP and Linux, where networking libs are "standard"
ECHO_HOST=localhost	# A host that accepts connections on ports 7 and 13

#	Rules, new style

%.o : %.cc
	$(CC) $*.cc

$(LIBRARY)(%.o) : %.cc
	$(CC) $*.cc
	ar rcv $(LIBRARY) $*.o
	rm -f $*.o

% : %.o $(LIBRARY)
	$(CCL) $< $(CLDEF) $(LIBRARY) $(LIBS) -o $@
	./$@

% :: %.cc
	$(CC) $*.cc
	$(CCL) $*.o $(CLDEF) $(LIBRARY) $(LIBS) -o $@
	./$@

#	Rules, old style
#.o:	$*.o $(LIBRARY)
#	$(CCL) $*.o $(CLDEF) $(LIBRARY) $(LIBS) -o $*
#	./$*
#.cc: 	$*.cc $(LIBRARY)
#	$(CC) $*.cc
#	$(CCL) $*.o $(CLDEF) $(LIBRARY) $(LIBS) -o $*
#	./$*
#.cc.o:
#	$(CC) $*.cc
#

# Primary goal

# Library

lib:	$(LIBRARY)
.PHONY: 	lib
.PRECIOUS:	$(LIBRARY)

# 			Compile the source files that have been changed 

$(LIBRARY):    $(LIBRARY)($(MODULES:.cc=.o))
	$(RANLIB)

#$(LIBRARY)::    $(MODULES)
# 			Compile the source files that have been changed 
#	$(CC) $?
#	listobj=`echo $? | sed s/.cc/.o/g` ; \
#	ar rv $(LIBRARY) $$listobj &&	\
#	rm $$listobj
#	$(RANLIB)

# Verification routines
check-all:	lib vmyenv vendian_io varithm vhistogram vvoc \
	vTCPstream

clean:
	rm -f core *.o vmyenv vendian_io vendian_io_ext varithm \
	vhistogram vvoc vTCPstream vTCPstream_server tcp-trans

dist-clean:	clean
	rm -f $(LIBRARY)

#vendian_io:	vendian_io.o $(LIBRARY)
#	$(CCL) vendian_io.o  $(LIBRARY) -o vendian_io -lm
#	./vendian_io

# Specific dependent goals

# Optimization causes internal compiler error...
#vmyenv.o: vmyenv.cc
#	$(CC) -O0 vmyenv.cc

vTCPstream: vTCPstream.o $(LIBRARY)
	$(CCL) $^ -o $@ $(NETLIBS) -lm
	ECHO_HOST=$(ECHO_HOST) ./$@

vTCPstream_server.o: vTCPstream_server.cc
	$(CC) -DDO_FORK vTCPstream_server.cc 

vTCPstream_server: vTCPstream_server.o $(LIBRARY)
	$(CCL) $^ -o $@ $(NETLIBS) -lm
	VTCP_PORT=9003 ./$@


tcp-trans: tcp-trans.o $(LIBRARY)
	$(CCL) $^ -o $@ $(NETLIBS) -lm

# Dependence rules

myenv.cc:		myenv.h
arithm_coding.cc:	arithm.h
arithm_model.cc:	arithm.h
arithm_modadh.cc:	arithm.h arithm_modadh.h


vendian_io_ext:	sys_open_glue.o open_ext.o
	$(CC) -DEXT_OPEN=1 -DEXT_NETIO=$(ECHO_HOST) vendian_io.cc
	$(CCL) vendian_io.o $^ $(LIBRARY) $(NETLIBS) -o $@ -lm
	./$@

# Platform-specific parts


# Specific dependent goals. $(OSTYPE) is an environment variable
# commonly set up by a shell when it starts up

PLATFORM:= $(shell uname -s)

ifeq "$(PLATFORM)" "Linux"

# Uncomment the following if running on a system with versioned
# shared libraries (e.g., S.u.S.E., 6.4+)
#
#open_ext.o:	sys_open.o
#	ar xv /usr/lib/libc.a open.o open64.o sysdep.o fileops.o #close.o lstat.o stat.o
#	ld -r -x -wrap open -wrap open64 -defsym __wrap_open=__libc_open \
#	-defsym __open=sys_open -defsym __open64=sys_open \
#	$^ open.o open64.o sysdep.o -o open_ext1.o
#	ar xv /usr/lib/libc.a genops.o mmap.o munmap.o llseek.o
#	ld -v -r -x open_ext1.o genops.o mmap.o munmap.o llseek.o fileops.o  -o open_ext.o
#	rm -f open_ext1.o

# Two-stage linking above is necessary to properly overload versioned symbols.
# fileops.o in libc.so refers to a specific version of an open function,
# (e.g.,  __open@GLIBC_2.0). OTH, fileops.o in libc.a contains an unqualified
# external reference, __open, which we can intercept.

open_ext.o:	sys_open.o
	ar xv /usr/lib/libc.a open.o sysdep.o #close.o lstat.o stat.o
	ld -r -x -wrap open -defsym __wrap_open=__libc_open \
	-defsym __open=sys_open \
	$^ open.o sysdep.o -o open_ext.o

#	ld -r -x -wrap open -defsym __wrap_open=__libc_open \
#	$^ open.o sysdep.o -o open_ext.o
#	ar xv /usr/lib/libc.a __open.o

#CFLAGS=$(CDEFS) -fno-inline
RANLIB = /bin/true
NETLIBS=
LDFLAGSI=
LIBSI=$(NETLIBS) -lm

else
ifeq "$(PLATFORM)" "SunOS"

open_ext.o:	sys_open.o
	ar xv /usr/lib/libc.a libc_open.o open.o open64.o fdopen.o
	ld -r -B local -z redlocsym $^ libc_open.o open.o open64.o \
	fdopen.o -o open_ext.o

#CFLAGS=$(CDEFS)
ECHO_HOST=`hostname`
RANLIB = /bin/true
NETLIBS=-lnsl -lsocket
LDFLAGSI=-Wl,-z,muldefs
LIBSI=/usr/lib/libc.a $(NETLIBS) -ldl -lm

else
ifeq "$(PLATFORM)" "FreeBSD"

open_ext.o:	sys_open.o
	ld -r -x -wrap open \
	$^ -o open_ext.o

#CFLAGS=$(CDEFS) -fno-inline
RANLIB = /usr/bin/true
NETLIBS=
LDFLAGSI=
LIBSI=$(NETLIBS) -lm

else
ifeq "$(PLATFORM)" "HPUX"

open_ext.o:	sys_open.o
	ar xv /usr/lib/libc.a t_open.o
	ld -r -h open -v -B immediate sys_open.o t_open.o -o open_ext.o

#CFLAGS=$(CDEFS)
ECHO_HOST=`hostname`
RANLIB = /bin/true
NETLIBS=
LIBS=$(NETLIBS) -ldl -lm
LDFLAGSI=-Wl,-z,muldefs
LIBSI=/usr/lib/libc.a $(NETLIBS) -ldl -lm

else
open_ext.o:	sys_open.o
	echo "Adjustments are required for this platform $(PLATFORM) !"
endif
endif
endif
endif
