# =============================================================================
# 
# @(#) Makefile,v SCTPBeta2_4(0.8.2.4) 2002/04/09 10:48:06
#
# -----------------------------------------------------------------------------
#
# Copyright (c) 2001-2002  OpenSS7 Corporation <http://www.openss7.com>
# Copyright (c) 1997-2000  Brian F. G. 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.
#
# -----------------------------------------------------------------------------
#
# U.S. GOVERNMENT RESTRICTED RIGHTS.  If you are licensing this Software on
# behalf of the U.S. Government ("Government"), the following provisions apply
# to you.  If the Software is supplied by the Department of Defense ("DoD"), it
# is classified as "Commercial Computer Software" under paragraph 252.227-7014
# of the DoD Supplement to the Federal Acquisition Regulations ("DFARS") (or any
# successor regulations) and the Government is acquiring only the license rights
# granted herein (the license rights customarily provided to non-Government
# users).  If the Software is supplied to any unit or agency of the Government
# other than DoD, it is classified as "Restricted Computer Software" and the
# Government's rights in the Software are defined in paragraph 52.227-19 of the
# Federal Acquisition Regulations ("FAR") (or any success regulations) or, in
# the cases of NASA, in paragraph 18.52.227-86 of the NASA Supplement to the FAR
# (or any successor regulations).
#
# -----------------------------------------------------------------------------
#
# Commercial licensing and support of this software is available from OpenSS7
# Corporation at a fee.  See http://www.openss7.com/
#
# -----------------------------------------------------------------------------
#
# Last Modified 2002/04/09 10:48:06 by brian
#
# =============================================================================

-include /usr/src/LiS/config.in
-include ../../.config.in

INCDIR ?= $(shell ( cd ../../include; pwd ))
SRCDIR ?= $(shell ( pwd ))

LIS_HOME ?= /usr/src/LiS
LIS_INCL ?= $(LIS_HOME)/include

MODCONF ?= /etc/modules.conf

SCTP_N_CMAJOR ?= 240
SCTP_T_CMAJOR ?= 241

VER ?= $(shell uname -r)

INCLUDES = \
    -I$(INCDIR) \
    -I$(LIS_INCL) \
    -I/lib/modules/$(VER)/build/include \

MAKEFILE = Makefile

COFLAGS = -l

OFLAGS ?= \
    -O3 \

DFLAGS ?= \
    -D_SAFE \

WFLAGS ?= \
    -Wall \

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

CFLAGS = \
    -DLINUX \
    -D__KERNEL__ \
    -DMODULE \
    -DMODVERSIONS \
    $(OFLAGS) \
    $(WFLAGS) \
    $(INCLUDES) \
    -pipe \
    -DSCTP_N_CMAJOR=$(SCTP_N_CMAJOR)
    -DSCTP_T_CMAJOR=$(SCTP_T_CMAJOR)

CCFLAGS = \
    -nostdlib \
    -D__KERNEL__ \
    -DMODULE \
    -DMODVERSIONS \
    -DEXPORT_SYMTAB \
    $(OFLAGS) \
    -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 $@

XCCP =
XPRG =
CTST =
TSTS =
OBJS = $(TSTS) $(CTST) $(XPRG) $(XCPP)
BINS = sctp_input.o sctp_output.o sctp_n.o sctp_t.o sctp.o sctp_cookie.o sctp_hash.o sctp_cache.o sctp_route.o sctp_msg.o
SRCS = sctp_input.c sctp_output.c sctp_n.c sctp_t.c sctp.c sctp_cookie.c sctp_hash.c sctp_cache.c sctp_route.c sctp_msg.c
INCS = sctp_input.h sctp_output.h sctp_n.h sctp_t.h sctp.h sctp_cookie.h sctp_hash.h sctp_cache.h sctp_route.h sctp_msg.h \
       ../debug.h ../bufq.h sctp_defs.h

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

all:: streams-sctp.o tags

#$(CVSS):
#	cvs update $@

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

streams-sctp.o: $(BINS)
	$(LD) -r $^ -o $@

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

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

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

$(CTST): %: %.cc
	$(CCC) -Wall -ggdb $(OFLAGS) -lpanel -lncurses $< -o $@
$(CTST:%=%.s): %.s: %.cc
	$(CCC) -S -Wall -ggdb $(OFLAGS) $< -o $@
$(CTST:%=%_i.cc): %_i.cc: %.cc
	$(CCC) -E -Wall -ggdb $(OFLAGS) $< | 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)/sys/*.h)
	ctags $(SRCS) $(INCS) $(wildcard $(INCDIR)/sys/*.h)

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

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

depend:: $(MAKEFILE)

root-check::
	@ if [ $$(id -u) -ne 0 ]; then \
	    echo -e "\nYou must be superuser to install!\n"; \
	    exit 1; \
	fi

install-headers:: root-check
	install -C $(INCDIR)/sys/xti_ip.h   $(LIS_INCL)/sys
	install -C $(INCDIR)/sys/xti_sctp.h $(LIS_INCL)/sys
	install -C $(INCDIR)/sys/npi.h      $(LIS_INCL)/sys
	install -C $(INCDIR)/sys/npi_sctp.h $(LIS_INCL)/sys
	install -C $(INCDIR)/sys/tpi.h      $(LIS_INCL)/sys
	install -C $(INCDIR)/sys/tpi_sctp.h $(LIS_INCL)/sys

install:: install-headers streams-sctp.o
	install -d /lib/modules/$(VER)/misc /lib/modules/misc
	install -C streams-sctp.o /lib/modules/$(VER)/misc
	install -C streams-sctp.o /lib/modules/misc
	rm -f /dev/sctp_n; mknod -m 666 /dev/sctp_n c $(SCTP_N_CMAJOR) 0
	rm -f /dev/sctp_t; mknod -m 666 /dev/sctp_t c $(SCTP_T_CMAJOR) 0
	cat $(MODCONF) | sed -e "/^[ \t]*alias[ \t]*char-major-\($(SCTP_N_CMAJOR)\|$(SCTP_T_CMAJOR)\)/d" > $(MODCONF).new
	echo "alias char-major-$(SCTP_N_CMAJOR)	streams-sctp		# driver sctp npi" >> $(MODCONF).new
	echo "alias char-major-$(SCTP_T_CMAJOR)	streams-sctp		# driver sctp tpi" >> $(MODCONF).new
	mv -fb $(MODCONF).new $(MODCONF)
	depmod -ae
	if grep "streams-sctp" /proc/modules >/dev/null; then \
		rmmod streams-sctp; \
	fi

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

sctp_input.o: Makefile sctp_input.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_hash.h sctp_cache.h sctp_route.h sctp_msg.h sctp_input.h \
 sctp_output.h
sctp_output.o: Makefile sctp_output.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_hash.h sctp_cache.h sctp_route.h sctp_msg.h sctp_output.h
sctp_n.o: Makefile sctp_n.c ../debug.h ../bufq.h sctp.h sctp_defs.h sctp_hash.h \
 sctp_cache.h sctp_msg.h sctp_n.h
sctp_t.o: Makefile sctp_t.c ../debug.h ../bufq.h sctp.h sctp_defs.h sctp_hash.h \
 sctp_cache.h sctp_msg.h sctp_t.h
sctp.o: Makefile sctp.c ../debug.h ../bufq.h sctp.h sctp_defs.h sctp_hash.h \
 sctp_cache.h sctp_n.h sctp_t.h sctp_input.h
sctp_cookie.o: Makefile sctp_cookie.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_cookie.h
sctp_hash.o: Makefile sctp_hash.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_cache.h sctp_hash.h
sctp_cache.o: Makefile sctp_cache.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_hash.h sctp_cache.h sctp_msg.h
sctp_route.o: Makefile sctp_route.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_cache.h sctp_route.h sctp_output.h
sctp_msg.o: Makefile sctp_msg.c ../debug.h ../bufq.h sctp.h sctp_defs.h \
 sctp_hash.h sctp_cache.h sctp_route.h sctp_cookie.h sctp_output.h \
 sctp_msg.h
