1 # $NetBSD: subst.mk,v 1.15 2004/08/23 16:36:00 jlam Exp $
3 # This Makefile fragment implements a general text replacement facility.
4 # Package makefiles define a ``class'', for each of which a paricular
5 # substitution description can be defined. For each class of files, a
6 # target subst-<class> is created to perform the text replacement.
8 # The following variables are used:
11 # A list of class names. A new class name must be appended (+=).
14 # "stage" at which we do the text replacement, e.g. pre-configure,
17 # SUBST_MESSAGE.<class>
18 # message to display, noting what is being substituted
21 # files on which to run the substitution; these are relative to
25 # sed(1) substitution expression to run on the specified files
27 # SUBST_FILTER_CMD.<class>
28 # filter used to perform the actual substitution on the specified
29 # files. Defaults to ${SED} ${SUBST_SED.<class>}.
31 # SUBST_POSTCMD.<class>
32 # command to clean up after sed(1). Defaults to ${RM} -f
33 # $$file${_SUBST_BACKUP_SUFFIX}. For debugging, set it to ${DO_NADA}.
35 ECHO_SUBST_MSG?= ${ECHO}
37 # _SUBST_IS_TEXT_FILE returns 0 if $${file} is a text file.
38 _SUBST_IS_TEXT_FILE?= \
39 ${FILE_CMD} $${file} | ${EGREP} "(executable .* script|shell script|text)" >/dev/null 2>&1
41 _SUBST_BACKUP_SUFFIX= .subst.sav
43 .for _class_ in ${SUBST_CLASSES}
44 _SUBST_COOKIE.${_class_}= ${WRKDIR}/.subst_${_class_}_done
46 . if defined(SUBST_SED.${_class_}) && !empty(SUBST_SED.${_class_})
47 SUBST_FILTER_CMD.${_class_}?= ${SED} ${SUBST_SED.${_class_}}
49 SUBST_FILTER_CMD.${_class_}?= # empty
51 SUBST_POSTCMD.${_class_}?= ${RM} -f $$file${_SUBST_BACKUP_SUFFIX}
53 SUBST_TARGETS+= subst-${_class_}
54 _SUBST_TARGETS.${_class_}= subst-${_class_}-message
55 _SUBST_TARGETS.${_class_}+= ${_SUBST_COOKIE.${_class_}}
56 _SUBST_TARGETS.${_class_}+= subst-${_class_}-cookie
58 .ORDER: ${_SUBST_TARGETS.${_class_}}
60 . if defined(SUBST_STAGE.${_class_})
61 ${SUBST_STAGE.${_class_}}: subst-${_class_}
64 .PHONY: subst-${_class_}
65 subst-${_class_}: ${_SUBST_TARGETS.${_class_}}
67 .PHONY: subst-${_class_}-message
68 subst-${_class_}-message:
69 . if defined(SUBST_MESSAGE.${_class_})
70 ${_PKG_SILENT}${_PKG_DEBUG} \
71 ${ECHO_SUBST_MSG} "=> ${SUBST_MESSAGE.${_class_}}"
74 .PHONY: subst-${_class_}-cookie
75 subst-${_class_}-cookie:
76 ${_PKG_SILENT}${_PKG_DEBUG} \
77 ${TOUCH} ${TOUCH_FLAGS} ${_SUBST_COOKIE.${_class_}}
79 ${_SUBST_COOKIE.${_class_}}:
80 . if !empty(SUBST_FILTER_CMD.${_class_})
81 ${_PKG_SILENT}${_PKG_DEBUG} \
83 files="${SUBST_FILES.${_class_}}"; \
86 *) for file in $${files}; do \
87 if ${_SUBST_IS_TEXT_FILE}; then \
88 ${MV} -f $$file $$file${_SUBST_BACKUP_SUFFIX} || exit 1; \
89 ${CAT} $$file${_SUBST_BACKUP_SUFFIX} \
90 | ${SUBST_FILTER_CMD.${_class_}} \
92 if [ -x $$file${_SUBST_BACKUP_SUFFIX} ]; then \
95 if ${CMP} -s $$file${_SUBST_BACKUP_SUFFIX} $$file; then \
96 ${MV} -f $$file${_SUBST_BACKUP_SUFFIX} $$file; \
98 ${SUBST_POSTCMD.${_class_}}; \
99 ${ECHO} $$file >> ${.TARGET}; \