#   -*-Makefile-*- template for the CIPE kernel module and driver.
#
#   Copyright 1996 Olaf Titz <olaf@bigred.inka.de>
#
#   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.

# $Id: Makefile-obj.in,v 1.14 2000/12/16 18:27:35 olaf Exp $

BINDIR:=/usr/local/sbin
MODDIR:=/lib/modules/@KVERS@/misc

MAKE=	   @MAKE@
CC=	   @CC@
KCC=	   @KCC@
KCPPFLAGS= @KCPPFLAGS@
KCFLAGS=   @KCFLAGS@
KDEFS=	   @KDEFS@
KLDFLAGS=  @KLDFLAGS@
UCPPFLAGS= @UCPPFLAGS@
UCFLAGS=   @UCFLAGS@
UDEFS=	   @UDEFS@
ULDFLAGS=  @ULDFLAGS@
ULIBS=	   @ULIBS@
KINC=	   @KINC@

CIPED=	   @CIPED@
MODULE=	   @MODULE@
ASMOBJS=   @ASMOBJS@
CCOBJS=	   @CCOBJS@
CRCOBJS=   @CRCOBJS@
CRC32OBJS= @CRC32OBJS@

src:=../cipe
lib:=../lib

.PHONY: all install clean dep depend dummy

all::	$(MODULE) $(CIPED)

install:: all
	-mkdir -p $(MODDIR) $(BINDIR)
	install -m 644 $(MODULE) $(MODDIR)
	install -m 755 $(CIPED) $(BINDIR)
	-depmod -a

KOBJS:=	module.o device.o sock.o output.o encaps.o $(CCOBJS)
OBJS:=	ciped.o options.o ioctl.o
SOBJS:=	crcgen.o $(CRC32OBJS)

$(MODULE): $(KOBJS) $(ASMOBJS) $(CRCOBJS)
	ld $(KLDFLAGS) -o $@ $(KOBJS) $(ASMOBJS) $(CRCOBJS)

ciped: $(CIPED)
	: ciped is really $(CIPED)

$(CIPED): $(OBJS) $(CRCOBJS) $(lib)/libcipe.a
	$(CC) $(ULDFLAGS) -o $@ $(OBJS) $(CRCOBJS) $(ULIBS) -L$(lib) -lcipe

$(ASMOBJS): %.o: $(src)/%.S
	$(KCC) -MD $(KDEFS) $(KCPPFLAGS) $(KCFLAGS) -o $@ -c $<

$(KOBJS): %.o: $(src)/%.c
	$(KCC) -MD $(KDEFS) $(KCPPFLAGS) $(KCFLAGS) -o $@ -c $<

$(KOBJS:.o=.s): %.s: $(src)/%.c dummy
	$(KCC) $(KDEFS) $(KCPPFLAGS) $(KCFLAGS) -fverbose-asm -o $@ -S $<

$(KOBJS:.o=.i): %.i: $(src)/%.c dummy
	$(KCC) $(KDEFS) $(KCPPFLAGS) $(KCFLAGS) -E -dD $< >$@

$(OBJS) $(CRCOBJS) $(SOBJS): %.o: $(src)/%.c
	$(CC) -MD $(UDEFS) $(UCPPFLAGS) -I$(lib) $(UCFLAGS) -o $@ -c $<

$(KOBJS) $(OBJS): version.h

version.h: config.h crcgen
	sed -n '/@api/{n;s,[/*#],,g;s,^,   ,p;}' config.h | ./crcgen - >$@

crcgen: crc32.o crcgen.o
	$(CC) $(ULDFLAGS) -o crcgen crc32.o crcgen.o $(ULIBS)

$(lib)/libcipe.a: $(lib)/*.[hc]
	$(MAKE) -C $(lib) libcipe.a

clean::
	rm -f core *.out *.tmp *.[oaisd] *.dd version.h crcgen $(CIPED)

$(src)/options.c $(src)/options.h:
	$(MAKE) -C .. cipe/options.c

dep depend:
	: nothing to do

DEPS:=$(patsubst %.o,%.dd, $(KOBJS) $(CRCOBJS) $(ASMOBJS) $(OBJS) $(SOBJS))

$(DEPS): %.dd: %.o
	@sed 's#^\($*\)\.o\([ :]*\)#\1.o \1.d \1.s \1.i\2#' $*.d >$@

ifeq (,$(findstring n,$(MAKEFLAGS)))
ifeq (,$(filter clean dep% %.i %.s,$(MAKECMDGOALS)))
-include $(DEPS)
endif
endif
