# =============================================================================
# 
# @(#) Makefile,v 0.7.8.2 2001/12/11 13:31:19 brian Exp
# 
# -----------------------------------------------------------------------------
# 
# Copyright (C) 2001  OpenSS7 Corporation <http://www.openss7.com>
# 
# 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).
# 
# -----------------------------------------------------------------------------
# 
# Last Modified 2001/12/11 13:31:19 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
SCTP_N_CMAJOR ?= 230
SCTP_T_CMAJOR ?= 231
MODCONF ?= /etc/conf.modules

INCLUDES = \
    -I$(INCDIR) \
    -I$(LIS_INCL) \
    -I/usr/src/linux/include \
    -I/usr/include \

MAKEFILE = Makefile

COFLAGS = -l

OFLAGS ?= \
    -O2 \

DFLAGS ?= \
    -D_SAFE \

WFLAGS ?= \
    -Wall \
    -Wunused \
    -Wpointer-arith \
    -Wcast-qual \
    -Wcast-align \
    -Waggregate-return \
    -Wnested-externs \
    -Winline \
    -Wold-style-cast \
    -finline \
    -finline-functions \
    -fno-keep-inline-functions \
    -fomit-frame-pointer \
    -fno-strict-aliasing

#    -Werror \
#    -Wbad-function-cast \

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 $@

# Extract version number from headers.

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

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 \
       sctp_debug.h sctp_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)
	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 $$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)

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 -a
	@ 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: sctp_input.c sctp_debug.h sctp_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: sctp_output.c sctp_debug.h sctp_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: sctp_n.c sctp_debug.h sctp_bufq.h sctp.h sctp_defs.h \
  sctp_hash.h sctp_cache.h sctp_msg.h sctp_n.h
sctp_t.o: sctp_t.c sctp_debug.h sctp_bufq.h sctp.h sctp_defs.h \
  sctp_hash.h sctp_cache.h sctp_msg.h sctp_t.h
sctp.o: sctp.c sctp_debug.h sctp_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: sctp_cookie.c sctp_debug.h sctp_bufq.h sctp.h sctp_defs.h \
  sctp_cookie.h
sctp_hash.o: sctp_hash.c sctp_debug.h sctp_bufq.h sctp.h sctp_defs.h \
  sctp_cache.h sctp_hash.h
sctp_cache.o: sctp_cache.c sctp_debug.h sctp_bufq.h sctp.h sctp_defs.h \
  sctp_hash.h sctp_cache.h sctp_msg.h
sctp_route.o: sctp_route.c sctp_debug.h sctp_bufq.h sctp.h sctp_defs.h \
  sctp_cache.h sctp_route.h sctp_output.h
sctp_msg.o: sctp_msg.c sctp_debug.h sctp_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
