summaryrefslogtreecommitdiff
path: root/mk/subst.mk
blob: 2a707ab7415b5d942494226d40b63675e218eb4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# $NetBSD: subst.mk,v 1.7 2003/12/28 15:57:29 jmmv Exp $
#
# This Makefile fragment implements a general text replacement facility
# for different classes of files in ${WRKSRC}.  For each class of files,
# a target subst-<class> is created to perform the text replacement.
#
# The following variables are used:
#
# SUBST_STAGE.<class>
#	"stage" at which we do the text replacement, e.g. pre-configure,
#	post-build, etc.
#
# SUBST_MESSAGE.<class>
#	message to display, noting what is being substituted
#                                      
# SUBST_FILES.<class>
#	files on which to run the substitution; these are relative to
#	${WRKSRC}
#
# SUBST_SED.<class>
#	sed(1) substitution expression to run on the specified files
#
# SUBST_FILTER_CMD.<class>
#	filter used to perform the actual substitution on the specified
#	files.  Defaults to ${SED} ${SUBST_SED.<class>}.

ECHO_SUBST_MSG?=	${ECHO}

.for _class_ in ${SUBST_CLASSES}
_SUBST_COOKIE.${_class_}=	${WRKDIR}/.subst_${_class_}_done

.if defined(SUBST_SED.${_class_}) && !empty(SUBST_SED.${_class_})
SUBST_FILTER_CMD.${_class_}?=	${SED} ${SUBST_SED.${_class_}}
.else
SUBST_FILTER_CMD.${_class_}?=	# empty
.endif

SUBST_TARGETS+=			subst-${_class_}
_SUBST_TARGETS.${_class_}=	subst-${_class_}-message
_SUBST_TARGETS.${_class_}+=	${_SUBST_COOKIE.${_class_}}
_SUBST_TARGETS.${_class_}+=	subst-${_class_}-cookie

.ORDER: ${_SUBST_TARGETS.${_class_}}

.  if defined(SUBST_STAGE.${_class_})
${SUBST_STAGE.${_class_}}: subst-${_class_}
.  endif

.PHONY: subst-${_class_}
subst-${_class_}: ${_SUBST_TARGETS.${_class_}}

.PHONY: subst-${_class_}-message
 subst-${_class_}-message:
.if defined(SUBST_MESSAGE.${_class_})
	${_PKG_SILENT}${_PKG_DEBUG}					\
	${ECHO_SUBST_MSG} "=> ${SUBST_MESSAGE.${_class_}}"
.endif

.PHONY: subst-${_class_}-cookie
 subst-${_class_}-cookie:
	${_PKG_SILENT}${_PKG_DEBUG}					\
	${TOUCH} ${TOUCH_FLAGS} ${_SUBST_COOKIE.${_class_}}

${_SUBST_COOKIE.${_class_}}:
.  if !empty(SUBST_FILTER_CMD.${_class_})
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cd ${WRKSRC};							\
	files="${SUBST_FILES.${_class_}}";				\
	case "$$files" in						\
	"")	;;							\
	*)	for file in $${files}; do				\
			${MV} -f $$file $$file.subst.sav || exit 1;	\
			${CAT} $$file.subst.sav 			\
				| ${SUBST_FILTER_CMD.${_class_}}	\
				> $$file;				\
			if [ -x $$file.subst.sav ]; then		\
				${CHMOD} +x $$file;			\
			fi;						\
			if ${CMP} -s $$file.subst.sav $$file; then	\
				${MV} -f $$file.subst.sav $$file;	\
			else						\
				${ECHO} $$file >> ${.TARGET};		\
			fi;						\
		done ;;							\
	esac
.  endif
.endfor