1246149Ssjg# $Id: yacc.mk,v 1.6 2011/06/10 22:45:46 sjg Exp $
2246149Ssjg
3246149Ssjg#
4246149Ssjg#	@(#) Copyright (c) 1999-2011, Simon J. Gerraty
5246149Ssjg#
6246149Ssjg#	This file is provided in the hope that it will
7246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
8246149Ssjg#	Permission to copy, redistribute or otherwise
9246149Ssjg#	use this file is hereby granted provided that 
10246149Ssjg#	the above copyright notice and this notice are
11246149Ssjg#	left intact. 
12246149Ssjg#      
13246149Ssjg#	Please send copies of changes and bug-fixes to:
14246149Ssjg#	sjg@crufty.net
15246149Ssjg#
16246149Ssjg
17246149Ssjg# this file contains rules to DTRT when SRCS contains foo.y or foo.c
18246149Ssjg# when only a foo.y exists.
19246149Ssjg
20246149SsjgYACC?= yacc
21246149SsjgYFLAGS?= -v -t
22246149SsjgRM?= rm
23246149Ssjg
24246149SsjgYACC.y?= ${YACC} ${YFLAGS}
25246149Ssjg
26246149Ssjg.if ${YACC.y:M-d} == "" || defined(NO_RENAME_Y_TAB_H)
27246149Ssjg
28246149Ssjg.y.c:
29246149Ssjg	${YACC.y} ${.IMPSRC}
30246149Ssjg	[ ! -s y.tab.c ] || mv y.tab.c ${.TARGET}
31246149Ssjg	${RM} -f y.tab.[!h]
32246149Ssjg
33246149Ssjg.else
34246149Ssjg
35246149Ssjg# the touch of the .c is to ensure it is newer than .h (paranoia)
36246149Ssjg.y.h:
37246149Ssjg	${YACC.y} ${.IMPSRC}
38246149Ssjg	[ ! -s y.tab.c ] || mv y.tab.c ${.TARGET:T:R}.c
39246149Ssjg	[ ! -s y.tab.h ] || cmp -s y.tab.h ${.TARGET:T:R}.h \
40246149Ssjg		|| mv y.tab.h ${.TARGET:T:R}.h
41246149Ssjg	touch ${.TARGET:T:R}.c
42246149Ssjg	${RM} -f y.tab.*
43246149Ssjg
44246149Ssjg# Normally the .y.h rule does the work - to avoid races.
45246149Ssjg# If for any reason the .c is lost but the .h remains,
46246149Ssjg# regenerate the .c
47246149Ssjg.y.c:	${.TARGET:T:R}.h
48246149Ssjg	[ -s ${.TARGET} ] || { \
49246149Ssjg		${YACC.y} ${.IMPSRC} && \
50246149Ssjg		{ [ ! -s y.tab.c ] || mv y.tab.c ${.TARGET}; \
51246149Ssjg		${RM} y.tab.*; }; }
52246149Ssjg.endif
53246149Ssjg
54246149Ssjgbeforedepend:	${SRCS:T:M*.y:S/.y/.c/g}
55246149Ssjg
56246149SsjgCLEANFILES+= ${SRCS:T:M*.y:S/.y/.[ch]/g}
57246149SsjgCLEANFILES+= y.tab.[ch]
58