# 
# Main makefile for the btparse distribution.  This applies equally to my
# development version and the offical distribution; the differences are
# isolated in Makefile.defs (which is a symlink to Makefile.defs.linux for
# development, and just an ordinary file for distribution).  Makefile.dirs
# defines the installation directories, which you should check if you're
# going to "make install".
#
# Thus, you shouldn't have to change anything here.
#

include Makefile.defs		# CC, OPT, CFLAGS, etc.
include Makefile.dirs		# installation directories
include Makefile.version        # for building documentation and distribution


# Input files

GRAMMAR = bibtex

LIBSRC =\
   init.c\
   input.c\
   $(SCAN).c\
   $(ERR).c\
   $(GRAMMAR).c\
   error.c\
   lex_auxiliary.c\
   parse_auxiliary.c\
   bibtex_ast.c\
   sym.c\
   util.c\
   post_parse.c\
   macros.c\
   traversal.c\
   names.c\
   tex_tree.c

LIBOBJ  = $(LIBSRC:.c=.o)
LIB     = libbtparse.a
SHLIB   = libbtparse.so
INCLUDE = btparse.h

# Directories to descend for "make clean"
DIRS    = doc progs t


# Lexer-related files generated by antlr:

TOKENS = tokens.h
DLG_FILE = parser.dlg

# Files generated by dlg

SCAN = scan
ERR = err

# Files generated by antlr from the grammar (apart from $(GRAMMAR).c)

MODE = mode.h


# The outputs of antlr and dlg

GRAMMAR_SPAWN = $(GRAMMAR).c $(ERR).c stdpccts.h
LEXER_SPAWN = $(DLG_FILE) $(TOKENS)
DLG_SPAWN = $(SCAN).c $(MODE)
PCCTS_SRC = $(SCAN).c $(ERR).c $(GRAMMAR).c
PCCTS_OBJ = $(PCCTS_SRC:.c=.o)

lib : $(LIB)
	@echo $(LIB) is up to date.

shlib : $(SHLIB)
	@echo $(SHLIB) is up to date.

$(LIB) : $(LIBOBJ)
	$(AR) $(ARFLAGS) $@ $?
	@if [ -n "$(RANLIB)" ]; then \
	  echo $(RANLIB) $(LIB) ; \
	  $(RANLIB) $(LIB) ; \
	fi

$(SHLIB) : $(LIBOBJ)
	$(CC) -shared -o $(SHLIB) $(LIBOBJ)

$(GRAMMAR).o : $(GRAMMAR).c $(MODE) $(TOKENS)
	$(CC) $(CFLAGS2) -c $<
$(SCAN).o : $(SCAN).c $(MODE) $(TOKENS)
	$(CC) $(CFLAGS2) -c $<

$(LEXER_SPAWN) $(GRAMMAR_SPAWN) $(DLG_SPAWN) : $(GRAMMAR).g
	$(PERL) $(RUN_PCCTS) $(AFLAGS) $(GRAMMAR).g

man:
	cd doc ; $(MAKE) man

test: $(LIB)
	cd t ; $(MAKE) test

install: $(LIB)
	cp $(INCLUDE) $(INSTALL_INC)
	cp $(LIB) $(INSTALL_LIB)
	cd doc ; $(MAKE) install

clean:
	rm -f $(LIBOBJ) core logfile
	for d in $(DIRS) ; do cd $$d ; $(MAKE) clean ; cd .. ; done

scrub: clean
	rm -f $(LIB) $(SHLIB) $(GRAMMAR_SPAWN) $(LEXER_SPAWN) $(DLG_SPAWN)
	cat < /dev/null > .depend
	rm -rf tmp
	for d in $(DIRS) ; do cd $$d ; $(MAKE) scrub ; cd .. ; done

dep: $(LIBSRC)
	$(CC) $(CFLAGS) -M $(SRC) $(LIBSRC) > .depend

debug:
	$(MAKE) OPT="-g -Wall" OPT2="-g"     # assumes gcc!

# stuff for making a release (VERSION comes from Makefile.version)

DISTRIB = btparse
RELEASE = $(DISTRIB)-$(VERSION)
ARCHIVE = $(RELEASE).tar.gz

dist : man $(GRAMMAR_SPAWN) $(LEXER_SPAWN) $(DLG_SPAWN)
	if [ -d $(RELEASE) ]; then rm -rf $(RELEASE); fi
	mkdir $(RELEASE)
	tar -chf - -T MANIFEST | (cd $(RELEASE) ; tar xf -)
	cd $(RELEASE) ;\
	  cat < /dev/null > .depend ;\
	  ln -s Makefile.defs.dist Makefile.defs
	cd $(RELEASE)/progs ;\
	  cat < /dev/null > .depend ;\
	  ln -s Makefile.dist Makefile
	tar -czf $(ARCHIVE) $(RELEASE)
	mv $(ARCHIVE) ../..
	rm -rf $(RELEASE)

# header dependencies (generated by "make dep")
include .depend
