#
#  Generic make commands for C-language library module test programs
#  For GNU make; will not work with most other make programs
#
#	PROGRAMS = a b ...
#	FILTER = | filter_log  # optional
#	include ../Make_test
#


# $Id: Make_lib_test,v 1.12 1996/04/28 libtsp-V2R7a $

SHELL = /bin/sh

ifndef tspdir
  tspdir = /usr/local
endif

exec_prefix = $(tspdir)
libdir = $(exec_prefix)/lib
includedir = $(tspdir)/include

# Set the default library name
ifeq ($(CC),CC)
  DEF_LIBNAME = libtspCC
else
  ifeq ($(CC),gcc)
    DEF_LIBNAME = libtspgcc
  else
    DEF_LIBNAME = libtsp
  endif
endif

# Library name
ifndef LIBNAME
  LIBNAME = $(DEF_LIBNAME)
endif
LIB = $(addsuffix .a,$(addprefix $(libdir)/,$(LIBNAME)))

# Compile and link flags
ifndef CFLAGS
  ifeq ($(CC),gcc)
    CFLAGS = -O3 -Wall
  else
    CFLAGS = -O2
  endif
endif
ifndef LDFLAGS
  LDFLAGS = -s
endif

LOADLIBES = $(LIB) -lm

CI = ci

fdir := $(dir $(shell pwd))	# Lop off last directory component, i.e. test
DIR := $(notdir $(fdir:/=))	# Remove trailing /, pick off last component
DIR := $(strip $(DIR))

TEST_SCRIPT = t$(DIR)routines
LOG_FILE = t$(DIR).log
REF_FILE = t$(DIR).ref

# Put bins in the bin/ subdirectory
BINS = $(addprefix bin/, $(PROGRAMS))

.PHONY:	all test reference clean

all:	$(BINS)

# Static rules expand to one rule for each program
$(BINS): bin/%: %.c $(LIB)
	@-test -d bin || mkdir bin
	$(CC) -I$(includedir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) \
		$< $(LOADLIBES) -o $@

test:	$(BINS)
	$(TEST_SCRIPT) $(FILTER) > $(LOG_FILE)
	diff $(LOG_FILE) $(REF_FILE)
	$(RM) $(LOG_FILE)

reference:
	$(CO) -M -l $(REF_FILE)
	$(TEST_SCRIPT) $(FILTER) > $(REF_FILE)
	$(CI) -M -u -m"- update" $(REF_FILE)

clean:
	$(RM) *.o
