#
# This is the master makefile.  It's responsible for the simple stuff,
# but doesn't actually do any compilation.  That is done by Makefile.sub,
# which is generated by configure (which is generated by GNU autoconf)
include ../Makefile.defs

all: $(U_DIR)/libbn.a $(U_DIR)/bntest progs

$(U_DIR)/libbn.a: $(U_DIR)/Makefile.sub bnsources
	cd $(U_DIR); $(MAKE) -f Makefile.sub libbn.a

$(U_DIR)/bntest: $(U_DIR)/Makefile.sub bnsources
	cd $(U_DIR); $(MAKE) -f Makefile.sub bntest

.INIT:
	@[ -d $(U_DIR) ] || mkdir -p $(U_DIR)

# The "/bin/sh" is needed to cope with a BUG in SCCS, which does
# not properly save and restore the execute flag on files.  (RCS
# gets it right.)
$(U_DIR)/Makefile.sub: configure Makefile.in config.hin lbn16.c
	cd $(U_DIR); /bin/sh ../configure --srcdir=..

# Actual build commented out to prevent confusion by people without autoconf
# Do it manually for now.
configure: configure.in
#	autoconf

# Example programs in the test subdirectory.
progs: $(U_DIR)/libbn.a
	@(echo Compiling test drivers for bn)
	cd test; make

clean:
	@$(RM) -r $(U_DIR)
	@$(RM) -f *32.[ch] *64.[ch]
	@-$(SCCSCLEAN)
	@cd test; make clean

cleanall:
	@$(RM) -rf *bin.* *32.[ch] *64.[ch]
	@-$(SCCSCLEAN)
	@cd test; make cleanall

BNSOURCES = lbn32.c lbn32.h bn32.c bn32.h bninit32.c bntest32.c \
	lbn64.c lbn64.h bn64.c bn64.h bninit64.c bntest64.c

# An explicit target that can be made before distribution for
# machines that don't have sed.
bnsources: $(BNSOURCES)

# The 16-bit versions of the code are the master versions; all else is
# generated from them.  This fiddling about makes them unwriteable
# to discourage improper edits.
%32.c: %16.c
	@test ! -f $@ -o -w $@ || chmod u+w $@ && test -w $@ || rm -f $@
	@sed -e s/32/64/g -e s/16/32/g $< > $@
	@chmod a-w $@

%32.h: %16.h
	@test ! -f $@ -o -w $@ || chmod u+w $@ && test -w $@ || rm -f $@
	@sed -e s/32/64/g -e s/16/32/g $< > $@
	@chmod a-w $@

%64.c: %16.c
	@test ! -f $@ -o -w $@ || chmod u+w $@ && test -w $@ || rm -f $@
	@sed -e s/32/128/g -e s/16/64/g $< > $@
	@chmod a-w $@

%64.h: %16.h
	@test ! -f $@ -o -w $@ || chmod u+w $@ && test -w $@ || rm -f $@
	@sed -e s/32/128/g -e s/16/64/g $< > $@
	@chmod a-w $@

# An explicit target that can be made before distribution for
# machines that don't have sed.
BNSOURCES = lbn32.c lbn32.h bn32.c bn32.h bninit32.c bntest32.c \
	lbn64.c lbn64.h bn64.c bn64.h bninit64.c bntest64.c
bnsources: $(BNSOURCES)
