# FreeS/WAN library
# Copyright (C) 1998, 1999  Henry Spencer.
# 
# 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
# 
# 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.
#
# RCSID $Id: Makefile,v 1.20 1999/04/10 23:24:18 henry Exp $

KOBJS=ultoa.o addrtoa.o subnettoa.o subnetof.o goodmask.o datatoa.o rangetoa.o \
	satoa.o
OBJS=$(KOBJS) atoul.o atoaddr.o atosubnet.o atodata.o atoasr.o atosa.o \
	optionsfrom.o
HDRS=freeswan.h internal.h
LIB=libfreeswan.a
KLIB=libkernel.a
CFLAGS=-O3 -fomit-frame-pointer
KCFLAGS=$(CFLAGS) -I.. -I/usr/src/linux/include -D__KERNEL__
ARFLAGS=crvs
EXTHDRS=des.h gmp.h
EXTLIBS=libdes.a libgmp.a
MANTREE=/usr/local/man
MANDIR=$(MANTREE)/man3
MANS=atoaddr.3 atoasr.3 atodata.3 atosa.3 atoul.3 goodmask.3 optionsfrom.3 \
	subnetof.3
INSTALL=install

SHELL=/bin/sh



all:	$(LIB) $(KLIB) $(EXTHDRS) $(EXTLIBS)

install:
	for f in $(MANS) ; \
	do \
		$(INSTALL) $$f $(MANDIR)/ipsec_$$f || exit 1 ; \
	done

$(LIB):	$(OBJS)
	ar $(ARFLAGS) $(LIB) $(OBJS)

$(OBJS):	$(HDRS)

$(KLIB):	ktmp/$(LIB)
	rm -f $(KLIB)
	ln -s ktmp/$(LIB) $(KLIB)

ktmp/$(LIB):	ktmp
	cd ktmp ; $(MAKE) -f ../Makefile $(LIB) OBJS="$(KOBJS)" \
		CFLAGS="$(KCFLAGS)"

ktmp:
	if test ! -d ktmp ; then mkdir ktmp ; fi
	cd ktmp ; \
	for f in $(KOBJS) ; \
	do \
		cf=`basename $$f .o`.c ; \
		if test ! -f $$cf ; then ln -s ../$$cf ; fi ; \
	done ; \
	for f in $(HDRS) ; \
	do \
		if test ! -f $$f ; then ln -s ../$$f ; fi ; \
	done

des.h:
	ln -s ../libdes/des.h

libdes.a:	../libdes/libdes.a
	ln -f -s ../libdes/libdes.a

# yes, that's CFLAG=, not CFLAGS=
../libdes/libdes.a:
	cd ../libdes ; \
	if test " `arch | sed 's/^i[3456]/x/'`" = " x86" ; \
	then $(MAKE) CC='$(CC)' CFLAG='$(CFLAGS)' TESTING='' x86-elf ; \
	else $(MAKE) CC='$(CC)' CFLAG='$(CFLAGS)' libdes.a ; \
	fi

gmp.h:
	ln -s ../gmp/gmp.h

libgmp.a:	../gmp/libgmp.a
	ln -f -s ../gmp/libgmp.a

../gmp/libgmp.a:	../gmp/Makefile
	cd ../gmp ; $(MAKE)

../gmp/Makefile:	../gmp/configure
	cd ../gmp ; ./configure

clean:
	rm -f $(LIB) $(KLIB) *.o try* core *.core $(EXTHDRS) $(EXTLIBS)
	rm -rf ktmp
	cd ../libdes ; $(MAKE) clean
	cd ../gmp ; $(MAKE) clean



# developer-only stuff
l:
	$(MAKE) $(LIB) ARFLAGS=crv CFLAGS=-O
	ranlib $(LIB)

t:	$(LIB)
	cp atosubnet.c try.c
	cc -DATOSUBNET_MAIN try.c $(LIB) -o try
	./try -r
	cp atodata.c try2.c
	cc -DATODATA_MAIN try2.c $(LIB) -o try2
	./try2 -r
	cp atoasr.c try3.c
	cc -DATOASR_MAIN try3.c $(LIB) -o try3
	./try3 -r
	cp atosa.c try4.c
	cc -DATOSA_MAIN try4.c $(LIB) -o try4
	./try4 -r

lt:	$(LIB)
	$(MAKE) t
	cp optionsfrom.c try5.c
	cc -DTEST try5.c $(LIB) -o try5
	echo --foo --bar >try5in1
	echo --optionsfrom >>try5in1
	echo try5in2 >>try5in1
	echo --foo >try5in2
	./try5 --foo --bar --optionsfrom try5in1 --bar something

tar:	clean
	tar -cvf /tmp/lib.tar Makefile [a-z]*
