#  This file is part of cqual.
#  Copyright (C) 2000-2001 The Regents of the University of California.
#
# cqual 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, or (at your option)
# any later version.
#
# cqual 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 cqual; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

CC = gcc
CFLAGS += -g -O9 -Werror -Wall -Wno-char-subscripts -I. -Ilibcompat -Ddeletes= -Dtraditional= -Dsameregion= -Dparentptr= -Wpointer-arith
LDFLAGS +=
LIBS = -lm

POBJS = AST.o AST_utils.o array.o builtins.o c-parse.tab.o c-lex.o	\
	callcc1.o constants.o cval.o env.o errors.o expr.o hash.o	\
	input.o libcompat/profile.o libcompat/regions.o mbchar.o	\
	semantics.o stmt.o types.o unparse.o utils.o

AST_H = AST.h AST_types.h AST_defs.h qualifiers.h

PHDRS = AST_utils.h array.h bool.h builtins.h c-lex.h c-parse.h		\
	c-gperf.h c-parse.tab.h callcc1.h config.h constants.h		\
	cstring.h cval.h decls.h env.h errors.h expr.h flags.h hash.h	\
	input.h linkage.h parser.h qualifiers.h libcompat/profile.h	\
	libcompat/regions.h semantics.h stmt.h types.h unparse.h	\
	utils.h $(AST_H)

HDRS = analyze.h buffer.h containers.h cqual.h pam.h qtype.h quals.h	\
	set.h util.h $(PHDRS)

OBJS = analyze.o buffer.o dd_list.o lattice-parse.o lattice-lex.o	\
	main.o pam.o qtype.o quals.o set.o util.o

IOBJS = buffer.o dd_list.o iquals-lex.o iquals.tab.o lattice-lex.o	\
	lattice-parse.o quals.o libcompat/profile.o			\
	libcompat/regions.o set.o util.o

all: parser cqual iquals

clean:
	rm -f *.o cqual parser
	rm -f AST_defs.c AST_defs.h AST_types.h AST_print.c c-gperf.h
	(cd libcompat; make clean)

parser: $(POBJS) toplev.o
	$(CC) -o parser $(LDFLAGS) $(POBJS) toplev.o $(LIBS)

cqual: $(OBJS) $(POBJS) parser
	$(CC) -o cqual $(LDFLAGS) $(POBJS) $(OBJS) $(LIBS)

iquals: $(IOBJS)
	$(CC) -o iquals $(LDFLAGS) $(IOBJS) $(LIBS)

iquals.tab.c iquals.tab.h: iquals.y $(HDRS)
	bison -d iquals.y

iquals-lex.c: iquals.tab.h iquals.l $(HDRS)
	flex -oiquals-lex.c iquals.l

c-parse.tab.c c-parse.tab.h: c-parse.y
	bison -d c-parse.y

AST_defs.c AST_defs.h AST_types.h: nodetypes.def build-types.el build-basics.el
	emacs -batch -load build-types.el

containers.h: containers.def build-containers.el
	emacs -batch -load build-containers.el

AST_print.c: nodetypes.def build-print.el build-basics.el
	emacs -batch -load build-print.el

c-gperf.h: c-parse.gperf qualifiers.h
	m4 c-parse.gperf | grep -v '^$$' | \
	gperf -p -j1 -i 1 -g -o -t -G -N is_reserved_word -k1,3,$$ >c-gperf.h

lattice-parse.c lattice-parse.h: lattice-parse.y $(HDRS)
	bison -d -plyy -olattice-parse.c lattice-parse.y

lattice-lex.c: lattice-parse.h lattice-parse.l $(HDRS)
	flex -olattice-lex.c -Plyy lattice-parse.l

libcompat/regions.o libcompat/profile.o: libcompat/*.c libcompat/*.h
	(cd libcompat; make)

AST.o: AST.c AST_defs.c AST_print.c $(PHDRS)
AST_utils.o: AST_utils.c $(PHDRS)
analyze.o: analyze.c $(HDRS)
array.o: array.c $(PHDRS)
buffer.o: buffer.c $(HDRS)
builtins.o: builtins.c $(PHDRS)
c-lex.o: c-lex.c $(PHDRS)
c-parse.tab.o: c-parse.tab.c $(PHDRS)
callcc1.o: callcc1.c $(PHDRS)
constants.o: constants.c $(PHDRS)
cval.o: cval.c $(PHDRS)
env.o: env.c $(PHDRS)
errors.o: errors.c $(PHDRS)
expr.o: expr.c $(PHDRS)
hash_info.o: hash_info.c $(HDRS)
input.o: input.c $(PHDRS)
main.o: main.c $(HDRS)
mbchar.o: mbchar.c $(PHDRS)
pam.o: pam.c $(HDRS)
qtype.o: qtype.c $(HDRS)
quals.o: quals.c $(HDRS)
semantics.o: semantics.c $(PHDRS)
set.o: set.c $(HDRS)
stmt.o: stmt.c $(PHDRS)
toplev.o: toplev.c $(PHDRS)
types.o: types.c $(PHDRS)
unparse.o: unparse.c $(PHDRS)
util.o: util.c $(HDRS)
utils.o: utils.c $(PHDRS)

#%.d: %.c $(AST_H)
#	$(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
#		| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
#		[ -s $@ ] || rm -f $@'
#
#include $(OBJS:.o=.d)
