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