.include "Makefile.inc"

SRCDIR=			${PICOPB_TOP}/test

CHECKS=			# none yet

SUBDIR+=		disk
SUBDIR+=		dumpdesc
SUBDIR+=		hdaudio

.PHONY: check
check: regress

CHECKS+=		check-picopbc
check-picopbc: .PHONY
	${PICOPBC} -d /dev/null /dev/null

CHECKS+=		check-comperr
CLEANFILES+=		comperr.err
check-comperr: .PHONY comperr.err-good comperr.err
	diff -u ${.ALLSRC}

CHECKS+=		check-compwarn
CLEANFILES+=		compwarn.warn
check-compwarn: .PHONY compwarn.warn-good compwarn.warn
	diff -u ${.ALLSRC}

CHECKS+=		check-compwarn-c
CLEANFILES+=		compwarn.pb.c
check-compwarn-c: .PHONY compwarn.pb.c-good compwarn.pb.c
	diff -u ${.ALLSRC}

CHECKS+=		check-compwarn-h
CLEANFILES+=		compwarn.pb.h
check-compwarn-h: .PHONY compwarn.pb.h-good compwarn.pb.h
	diff -u ${.ALLSRC}

CHECKS+=		check-sort-c
CLEANFILES+=		sort.pb.c
check-sort-c: .PHONY sort.pb.c-good sort.pb.c
	diff -u ${.ALLSRC}

CHECKS+=		check-sort-h
CLEANFILES+=		sort.pb.h
check-sort-h: .PHONY sort.pb.h-good sort.pb.h
	diff -u ${.ALLSRC}

GOOGLE_OBJDIR=		google-dumps
${GOOGLE_OBJDIR}:
	mkdir -p ${GOOGLE_OBJDIR}

GOOGLE_SRCDIR=		google/protobuf

GOOGLE_TESTS+=		descriptor.proto
GOOGLE_TESTS+=		unittest.proto
GOOGLE_TESTS+=		unittest_custom_options.proto
GOOGLE_TESTS+=		unittest_embed_optimize_for.proto
GOOGLE_TESTS+=		unittest_empty.proto
GOOGLE_TESTS+=		unittest_enormous_descriptor.proto
GOOGLE_TESTS+=		unittest_import.proto
GOOGLE_TESTS+=		unittest_import_lite.proto
GOOGLE_TESTS+=		unittest_import_public.proto
GOOGLE_TESTS+=		unittest_import_public_lite.proto
GOOGLE_TESTS+=		unittest_lite.proto
GOOGLE_TESTS+=		unittest_lite_imports_nonlite.proto
GOOGLE_TESTS+=		unittest_mset.proto
GOOGLE_TESTS+=		unittest_no_generic_services.proto
GOOGLE_TESTS+=		unittest_optimize_for.proto

.for _t_ in ${GOOGLE_TESTS}
# Kludges:
# - `cd ${SRCDIR}' to get the working directory right for imports.
# - Ignore errors until everything is supported.

CHECKS+=		check-google-${_t_}-dump
CLEANFILES+=		${GOOGLE_OBJDIR}/${_t_:.proto=.dump}
check-google-${_t_}-dump: ${GOOGLE_SRCDIR}/${_t_:.proto=.dump-good}
check-google-${_t_}-dump: ${GOOGLE_OBJDIR}/${_t_:.proto=.dump}
	diff -u ${.ALLSRC}
${GOOGLE_OBJDIR}/${_t_:.proto=.dump}: ${PICOPBC}
${GOOGLE_OBJDIR}/${_t_:.proto=.dump}: ${GOOGLE_OBJDIR}
${GOOGLE_OBJDIR}/${_t_:.proto=.dump}: ${GOOGLE_SRCDIR}/${_t_}
	(cd ${SRCDIR} && ${PICOPBC} -d - - < ${GOOGLE_SRCDIR}/${_t_} || :) \
	> $@.tmp 2>&1 \
	&& mv -f $@.tmp $@

CHECKS+=		check-google-${_t_}-h
CLEANFILES+=		${GOOGLE_OBJDIR}/${_t_:.proto=.pb.h}
check-google-${_t_}-h: ${GOOGLE_SRCDIR}/${_t_:.proto=.pb.h-good}
check-google-${_t_}-h: ${GOOGLE_OBJDIR}/${_t_:.proto=.pb.h}
	diff -u ${.ALLSRC}
${GOOGLE_OBJDIR}/${_t_:.proto=.pb.h}: ${PICOPBC}
${GOOGLE_OBJDIR}/${_t_:.proto=.pb.h}: ${GOOGLE_OBJDIR}
${GOOGLE_OBJDIR}/${_t_:.proto=.pb.h}: ${GOOGLE_SRCDIR}/${_t_}
	( \
		cd ${SRCDIR} \
		&& ${PICOPBC} -G ${GOOGLE_SRCDIR}/${_t_:.proto=.pb.h} -h - - \
			< ${GOOGLE_SRCDIR}/${_t_} \
		|| : \
	) > $@.tmp 2>&1 \
	&& mv -f $@.tmp $@

CHECKS+=		check-google-${_t_}-c
CLEANFILES+=		${GOOGLE_OBJDIR}/${_t_:.proto=.pb.c}
check-google-${_t_}-c: ${GOOGLE_SRCDIR}/${_t_:.proto=.pb.c-good}
check-google-${_t_}-c: ${GOOGLE_OBJDIR}/${_t_:.proto=.pb.c}
	diff -u ${.ALLSRC}
${GOOGLE_OBJDIR}/${_t_:.proto=.pb.c}: ${PICOPBC}
${GOOGLE_OBJDIR}/${_t_:.proto=.pb.c}: ${GOOGLE_OBJDIR}
${GOOGLE_OBJDIR}/${_t_:.proto=.pb.c}: ${GOOGLE_SRCDIR}/${_t_}
	( \
		cd ${SRCDIR} \
		&& ${PICOPBC} -H ${_t_:.proto=.pb.h} -c - - \
			< ${GOOGLE_SRCDIR}/${_t_} \
		|| : \
	) > $@.tmp 2>&1 \
	&& mv -f $@.tmp $@
.endfor

.SUFFIXES: .dump .err .warn .proto .pb.c .pb.h
.proto.dump: ${PICOPBC}
	${PICOPBC} -d - - < ${.IMPSRC} > $@.tmp 2>&1 && mv -f $@.tmp $@
.proto.err: ${PICOPBC}
	( \
		if ${PICOPBC} -h /dev/null - < ${.IMPSRC}; then \
			echo >&2 expected failure; exit 1; \
		fi; \
	) > $@.tmp 2>&1 && mv -f $@.tmp $@
.proto.warn: ${PICOPBC}
	${PICOPBC} -h /dev/null - < ${.IMPSRC} > $@.tmp 2>&1 && mv -f $@.tmp $@
.proto.pb.c: ${PICOPBC}
	${PICOPBC} -c $@.tmp -H $*.pb.h $< && mv -f $@.tmp $@
.proto.pb.h: ${PICOPBC}
	${PICOPBC} -h $@.tmp -G $*.pb.h $< && mv -f $@.tmp $@

.PHONY: regress
regress: $(CHECKS)

.PHONY: clean
clean:
	-rm -f -- $(CLEANFILES)

.include <bsd.obj.mk>
.include <bsd.subdir.mk>

.PATH: ${SRCDIR}
