xref: /freebsd/contrib/bmake/mk/yacc.mk (revision 02e9120893770924227138ba49df1edb3896112a)
1# $Id: yacc.mk,v 1.8 2022/06/22 04:51:06 sjg Exp $
2
3#
4#	@(#) Copyright (c) 1999-2011, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17# this file contains rules to DTRT when SRCS contains foo.y or foo.c
18# when only a foo.y exists.
19
20YACC?= yacc
21YFLAGS?= -v -t
22RM?= rm
23
24YACC.y?= ${YACC} ${YFLAGS}
25
26# first deal with explicit *.y in SRCS
27.for y in ${SRCS:M*.y}
28.if ${YACC.y:M-d} == "" || defined(NO_RENAME_Y_TAB_H)
29.ORDER: ${y:T:R}.c y.tab.h
30y.tab.h: .NOMETA
31${y:T:R}.c y.tab.h: $y
32	${YACC.y} ${.IMPSRC}
33	[ ! -s y.tab.c ] || mv y.tab.c ${.TARGET}
34	${RM} -f y.tab.[!h]
35.else
36.ORDER: ${y:T:R}.c ${y:T:R}.h
37${y:T:R}.h: .NOMETA
38${y:T:R}.c ${y:T:R}.h: $y
39	${YACC.y} ${.IMPSRC}
40	[ ! -s y.tab.c ] || mv y.tab.c ${.TARGET:T:R}.c
41	[ ! -s y.tab.h ] || cmp -s y.tab.h ${.TARGET:T:R}.h \
42		|| mv y.tab.h ${.TARGET:T:R}.h
43	${RM} -f y.tab.*
44.endif
45.endfor
46
47.if ${SRCS:M*.y} == ""
48.if ${YACC.y:M-d} == "" || defined(NO_RENAME_Y_TAB_H)
49
50.y.c:
51	${YACC.y} ${.IMPSRC}
52	[ ! -s y.tab.c ] || mv y.tab.c ${.TARGET}
53	${RM} -f y.tab.[!h]
54
55.else
56
57# the touch of the .c is to ensure it is newer than .h (paranoia)
58.y.h:
59	${YACC.y} ${.IMPSRC}
60	[ ! -s y.tab.c ] || mv y.tab.c ${.TARGET:T:R}.c
61	[ ! -s y.tab.h ] || cmp -s y.tab.h ${.TARGET:T:R}.h \
62		|| mv y.tab.h ${.TARGET:T:R}.h
63	touch ${.TARGET:T:R}.c
64	${RM} -f y.tab.*
65
66# Normally the .y.h rule does the work - to avoid races.
67# If for any reason the .c is lost but the .h remains,
68# regenerate the .c
69.y.c:	${.TARGET:T:R}.h
70	[ -s ${.TARGET} ] || { \
71		${YACC.y} ${.IMPSRC} && \
72		{ [ ! -s y.tab.c ] || mv y.tab.c ${.TARGET}; \
73		${RM} y.tab.*; }; }
74.endif
75.endif
76
77beforedepend:	${SRCS:T:M*.y:S/.y/.c/g}
78
79CLEANFILES+= ${SRCS:T:M*.y:S/.y/.[ch]/g}
80CLEANFILES+= y.tab.[ch]
81
82