YACC=@YACC@
LEX=@LEX@
LEX_OUTPUT_ROOT=@LEX_OUTPUT_ROOT@
CC=@CC@
CFLAGS=@CFLAGS@
CPPFLAGS=@CPPFLAGS@
AR=@AR@
RANLIB=@RANLIB@
DOXYGEN=@DOXYGEN@
DOT=@DOT@

STRIPAT=../buildtools/build/stripat

INSTALL=@INSTALL@
#MKDIR_P=mkdir -p
MKDIR_P=@MKDIR_P@

prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@
bindir=@bindir@
libdir=@libdir@
docdir=@docdir@
includedir=@includedir@

# We should use libtool -- this library file name won't work everywhere
UNITYLIB=libunity.a

PACKAGE_VERSION=@PACKAGE_VERSION@
LIBRARY_HOMEPAGE=@LIBRARY_HOMEPAGE@

PARSERLIST=@PARSERLIST@
PARSERS=$(patsubst %,unity-%,$(PARSERLIST))
# The OBJS are the object files which go in the library
# (ie, they don't include unity.o)
# Each of these OBJS has a corresponding .h file
OBJS=function-definitions unit-definitions unitylib writers
# These extra headers don't correspond to .c/.o files
EXTRA_HEADERS=known-syntaxes.h unity-constants.h unity.tab.h

# Create the parser code from the syntax-specific version of the grammar.
# We need some gymnastics to let us have multiple parsers in the same library.
# Yacc option -p would help here, but byaccj doesn't have that option
# (it would help if byaccj declared a few more variables static...).
# We need yylex, yyerror and yylval to be common to all parsers, so define
# ${type}_yyfoo to be yyfoo for each of those cases.

# These aren't terribly precious, but we want the making of libunity.a, which
# depends on these, to implicitly create these files, too
.PRECIOUS: $(PARSERS:=.tab.c)
#.PRECIOUS: $(patsubst %,%.tab.c,$(PARSERS))


unity: unity.o $(UNITYLIB) $(EXTRA_HEADERS)
	$(CC) -o $@ $(CFLAGS) unity.o -L. -lunity -lm

unity.o: unity.h
unit-definitions.o: unit-definitions.h known-syntaxes.h
unitylib.o: known-syntaxes.h
writers.o: known-syntaxes.h unit-definitions.h
function-definitions.o: function-definitions.h known-syntaxes.h

unit-definitions.c:
	cd ../grammar; make unit-definitions.c
	ln -s ../grammar/unit-definitions.c

function-definitions.c:
	cd ../grammar; make function-definitions.c
	ln -s ../grammar/function-definitions.c

# Yes, we should use libtool...
$(UNITYLIB): $(OBJS:=.o) $(PARSERS:=.tab.o) unity-lexer.o
	rm -f $@
	$(AR) -r $@ $(OBJS:=.o) $(PARSERS:=.tab.o) unity-lexer.o
	$(RANLIB) $@

unity.h: $(OBJS:=.h) known-syntaxes.h
	rm -f $@
	{ echo "/* GENERATED by Makefile.in */"; echo "/* DO NOT EDIT */"; \
	    echo "#ifndef UNITY_H_LOADED"; echo "#define UNITY_H_LOADED 1"; \
	    echo "/* From known-syntaxes.h */"; cat known-syntaxes.h; \
	    for f in $(OBJS:=.h); do \
	        echo; echo "/* From $$f ... */"; grep -v '^#include "' $$f; echo "/* ...end $$f */"; \
	    done; \
	    echo "#endif"; } >$@

# The following doesn't really depend on which unity-*.tab.c is generated
unity.tab.h: unity-fits.tab.c
	mv y.tab.h unity.tab.h

# The flex output includes a yyinput(), which is redundant in this
# case (and so causes a compilation error with -Wall -Werror), but
# which is omitted if (undocumented) YY_NO_INPUT is defined.
unity-lexer.o: unity-lexer.c unity.tab.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -DYY_NO_INPUT=1 -c -o unity-lexer.o unity-lexer.c






install: unity docs
	for d in $(bindir) $(includedir) $(libdir) $(docdir); do test -d $$d || $(MKDIR_P) $$d; done
	$(INSTALL) unity $(bindir)
	$(INSTALL) unitylib.h $(includedir)
	$(INSTALL) $(UNITYLIB) $(libdir)
	if test -d docs/html; then $(MKDIR_P) $(docdir)/unity && cp -R docs/* $(docdir)/unity; else :; fi

Makefile: Makefile.in ../../config.status
	cd ../..; ./config.status src/c/Makefile

check: $(UNITYLIB)
	cd test; $(MAKE) check

clean:
	rm -f *.o $(UNITYLIB) unity
	rm -Rf dist
	rm -f unity-*.c unity.tab.c y.tab.c unity.tab.h unity.h *.y
	cd test; $(MAKE) clean
