# =============================================================================
# 
# @(#) $Id: Makefile,v 0.7.4.1 2001/02/18 09:44:54 brian Exp $
# 
# -----------------------------------------------------------------------------
# 
# Copyright (C) 1997-2001  Brian Bidulock <bidulock@dallas.net>
# 
# All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
# 
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 675 Mass
# Ave, Cambridge, MA 02139, USA.
# 
# -----------------------------------------------------------------------------
# 
# Last Modified $Date: 2001/02/18 09:44:54 $ by $Author: brian $
# 
# =============================================================================

INCDIR ?= $(shell ( cd ../include; pwd ))
SRCDIR ?= $(shell ( cd ../test;    pwd ))
LISINCL ?= /usr/src/LiS/include
LISLFLAGS ?= '-lLiS'

INCLUDES = -I$(LISINCL) -I$(INCDIR)
MAKEFILE = Makefile

COFLAGS = -l

DFLAGS ?= \
    -D_DEBUG \

WFLAGS ?= \
    -Wall \
    -Werror \
    -Wunused \
    -Wpointer-arith \
    -Wbad-function-cast \
    -Wcast-qual \
    -Wcast-align \
    -Waggregate-return \
    -Wstrict-prototypes \
    -Wnested-externs \
    -Winline \
    -Wold-style-cast \

MFLAGS ?= \
    -m486 \
    -malign-loops=2 \
    -malign-jumps=2 \
    -malign-functions=2 \

CFLAGS ?= \
    -DLINUX \
    -D__KERNEL__ \
    -DMODULE \
    -DMODVERSIONS \
    $(OFLAGS) \
    $(WFLAGS) \
    $(INCLUDES) \
    -O3 \
    -pipe \

CCFLAGS = \
    -nostdlib \
    -D__KERNEL__ \
    -DMODULE \
    -DMODVERSIONS \
    -DEXPORT_SYMTAB \
    -O3 \
    -W \
    -Wall \
    -Winline \
    $(INCLUDES)

CC = gcc
CCC = g++

%_i.c : %.c ; $(CC) -E $(DFLAGS) $(CFLAGS) $< | sed -e "/^ *$$/d" > $@
%.s : %.c ; $(CC) -S $(DFLAGS) $(CFLAGS) $< -o $@
%.o : %.c ; $(CC) -c $(DFLAGS) $(CFLAGS) $< -o $@
%_i.cc : %.cc ; $(CCC) -E $(DFLAGS) $(CCFLAGS) $< | sed -e "/^ *$$/d" > $@
%.s : %.cc ; $(CCC) -S $(DFLAGS) $(CCFLAGS) $< -o $@
%.o : %.cc ; $(CCC) -c $(DFLAGS) $(CCFLAGS) $< -o $@

# Extract version number from headers.

VER = $(shell awk -F\" '/REL/ {print $$2}' /usr/src/linux/include/linux/version.h)

XCCP =
XPRG =
CTST =
TSTS = test-sdludp test-sdt test-sl test-sdlacb test-q781
OBJS = $(TSTS) $(CTST) $(XPRG) $(XCPP)
BINS = 
SRCS = 
INCS = 

RMES = # README.thingstodo README.plan
CVSS = $(SRCS) $(INCS) $(RMES) $(TSTS:%=%.c) $(XPRG:%=%.c) $(CTST:%=%.cc) $(XCPP:%=%.cc) $(MAKEFILE)
LOGS = $(CVSS:%=%.log)

all:: $(TSTS) # tags

#$(CVSS):
#	cvs update $@

#$(SRCS): $(MAKEFILE)
#$(INCS): $(MAKEFILE)

$(XCPP): %: %.cc
	$(CCC) -Wall -ggdb -O3 -I/usr/X11/include -L/usr/X11/lib -lXaw -lc $< -o $@
$(XCPP:%=%.s): %.s: %.cc
	$(CCC) -S -Wall -ggdb -O3 -I/usr/X11/include -L/usr/X11/lib $< -o $@
$(XCPP:%=%_i.cc): %_i.cc: %.cc
	$(CCC) -E -Wall -ggdb -O3 -I/usr/X11/include -L/usr/X11/lib $< | sed -e "/^ *$$/d" > $@

$(XPRG): %: %.c
	$(CC) -Wall -ggdb -O3 -I/usr/X11/include -L/usr/X11/lib -lXaw -lc $< -o $@
$(XPRG:%=%.s): %.s: %.c
	$(CC) -S -Wall -ggdb -O3 -I/usr/X11/include -L/usr/X11/lib $< -o $@
$(XPRG:%=%_i.c): %_i.c: %.c
	$(CC) -E -Wall -ggdb -O3 -I/usr/X11/include -L/usr/X11/lib $< | sed -e "/^ *$$/d" > $@

$(TSTS): %: %.c
	$(CC) -Wall -ggdb -O3 $(INCLUDES) $(LISLFLAGS) $< -o $@
$(TSTS:%=%.s): %.s: %.c
	$(CC) -S -Wall -ggdb -O3 $(INCLUDES) $(LISLFLAGS) $< -o $@
$(TSTS:%=%_i.c): %_i.c: %.c
	$(CC) -E -Wall -ggdb -O3 $(INCLUDES) $(LISLFLAGS) $< | sed -e "/^ *$$/d" >  $@

$(CTST): %: %.cc
	$(CCC) -Wall -ggdb -O3 -lpanel -lncurses $< -o $@
$(CTST:%=%.s): %.s: %.cc
	$(CCC) -S -Wall -ggdb -O3 $< -o $@
$(CTST:%=%_i.cc): %_i.cc: %.cc
	$(CCC) -E -Wall -ggdb -O3 $< | sed -e "/^ *$$/d" > $@

assem:: $(BINS:%.o=%.s) $(TSTS:%=%.s) $(CTST:%=%.s) $(XPRG:%=%.s) $(XCPP:%=%.s)
prep:: $(BINS:%.o=%_i.c) $(TSTS:%=%_i.c) $(CTST:%=%_i.ci) $(XPRG:%=%_i.c) $(XCPP:%=%_i.cc)
tags: $(SRCS) $(INCS) $(wildcard $(INCDIR)/ss7/*.h)
	ctags $(SRCS) $(INCS) $(wildcard $(INCDIR)/ss7/*.h)

$(LOGS): %.log: %
	cvs log $< | sed -e "1d" > $@
logs:: $(LOGS)

$(MAKEFILE): $(SRCS) $(INCS) $(TSTS:%=%.c)
	mv $(MAKEFILE) $(MAKEFILE).bak
	cat $(MAKEFILE).bak | sed -e "/^# Depend/,$$ d" > $(MAKEFILE)
	echo "# Dependencies" >> $(MAKEFILE)
	echo "" >> $(MAKEFILE)
	for i in $(SRCS); do $(CC) -MM $(INCLUDES) $(MOREINCLUDE) $(CFLAGS) $$i >> $(MAKEFILE); done
	for i in $(CTST); do $(CCC) -MM $$i.cc | sed -e "s/\.o:/:/" >> $(MAKEFILE); done
	for i in $(TSTS); do $(CC) -MM $(INCLUDES) $$i.c | sed -e "s/\.o:/:/" >> $(MAKEFILE); done
	for i in $(XPRG); do $(CC) -MM -I/usr/X11/include $$i.c | sed -e "s/\.o:/:/" >> $(MAKEFILE); done
	for i in $(XCPP); do $(CC) -MM -I/usr/X11/include $$i.cc | sed -e "s/\.o:/:/" >> $(MAKEFILE); done
	mv $(MAKEFILE) $(MAKEFILE)~
	cat $(MAKEFILE)~ | sed -e "/^# Depend/,$$ s/:/: $(MAKEFILE)/" > $(MAKEFILE)
	rm $(MAKEFILE)~

depend:: $(MAKEFILE)

install::

clean::
	rm -f *.o *.s *_i.c *_i.cc *.log *~ core $(TSTS) $(CTST) $(XPRG) $(XCPP) Makefile.bak

realclean::
	cvs commit
	rm -f $(SRCS) $(INCS) $(RMES) $(BINS) $(OBJS) \
		$(BINS:%.o=%.s) $(BINS:%.o=%_i.c) \
		$(MAKEFILE).bak *~ core \
	$(TSTS) $(TSTS:%=%.c) $(TSTS:%=%.s) $(TSTS:%=%_i.c) \
	$(XPRG) $(XPRG:%=%.c) $(XPRG:%=%.s) $(XPRG:%=%_i.c) \
	$(XCPP) $(XCPP:%=%.cc) $(XCPP:%=%.s) $(XCPP:%=%_i.cc) \
	$(CTST) $(CTST:%=%.cc) $(CTST:%=%.s) $(CTST:%=%_i.cc)

.lines:   $(SRCS) $(INCS) $(MAKEFILE)
	cat $^ | wc -l > .lines
	cat .lines

# Dependencies

test-sdludp: Makefile test-sdludp.c
test-sdt: Makefile test-sdt.c
test-sl: Makefile test-sl.c
test-sdlacb: Makefile test-sdlacb.c
test-q781: Makefile test-q781.c
