Add files from parent branch HEAD:
[pkgsrc.git] / mk / subst.mk
1 # $NetBSD: subst.mk,v 1.15 2004/08/23 16:36:00 jlam Exp $
2 #
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.
7 #
8 # The following variables are used:
9 #
10 # SUBST_CLASSES
11 #       A list of class names.  A new class name must be appended (+=).
12 #
13 # SUBST_STAGE.<class>
14 #       "stage" at which we do the text replacement, e.g. pre-configure,
15 #       post-build, etc.
16 #
17 # SUBST_MESSAGE.<class>
18 #       message to display, noting what is being substituted
19 #
20 # SUBST_FILES.<class>
21 #       files on which to run the substitution; these are relative to
22 #       ${WRKSRC}
23 #
24 # SUBST_SED.<class>
25 #       sed(1) substitution expression to run on the specified files
26 #
27 # SUBST_FILTER_CMD.<class>
28 #       filter used to perform the actual substitution on the specified
29 #       files.  Defaults to ${SED} ${SUBST_SED.<class>}.
30 #
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}.
34
35 ECHO_SUBST_MSG?=        ${ECHO}
36
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
40
41 _SUBST_BACKUP_SUFFIX=   .subst.sav
42
43 .for _class_ in ${SUBST_CLASSES}
44 _SUBST_COOKIE.${_class_}=       ${WRKDIR}/.subst_${_class_}_done
45
46 .  if defined(SUBST_SED.${_class_}) && !empty(SUBST_SED.${_class_})
47 SUBST_FILTER_CMD.${_class_}?=   ${SED} ${SUBST_SED.${_class_}}
48 .  else
49 SUBST_FILTER_CMD.${_class_}?=   # empty
50 .  endif
51 SUBST_POSTCMD.${_class_}?=      ${RM} -f $$file${_SUBST_BACKUP_SUFFIX}
52
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
57
58 .ORDER: ${_SUBST_TARGETS.${_class_}}
59
60 .  if defined(SUBST_STAGE.${_class_})
61 ${SUBST_STAGE.${_class_}}: subst-${_class_}
62 .  endif
63
64 .PHONY: subst-${_class_}
65 subst-${_class_}: ${_SUBST_TARGETS.${_class_}}
66
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_}}"
72 .  endif
73
74 .PHONY: subst-${_class_}-cookie
75  subst-${_class_}-cookie:
76         ${_PKG_SILENT}${_PKG_DEBUG}                                     \
77         ${TOUCH} ${TOUCH_FLAGS} ${_SUBST_COOKIE.${_class_}}
78
79 ${_SUBST_COOKIE.${_class_}}:
80 .  if !empty(SUBST_FILTER_CMD.${_class_})
81         ${_PKG_SILENT}${_PKG_DEBUG}                                     \
82         cd ${WRKSRC};                                                   \
83         files="${SUBST_FILES.${_class_}}";                              \
84         case "$$files" in                                               \
85         "")     ;;                                                      \
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_}} \
91                                         > $$file;                       \
92                                 if [ -x $$file${_SUBST_BACKUP_SUFFIX} ]; then \
93                                         ${CHMOD} +x $$file;             \
94                                 fi;                                     \
95                                 if ${CMP} -s $$file${_SUBST_BACKUP_SUFFIX} $$file; then \
96                                         ${MV} -f $$file${_SUBST_BACKUP_SUFFIX} $$file; \
97                                 else                                    \
98                                         ${SUBST_POSTCMD.${_class_}};    \
99                                         ${ECHO} $$file >> ${.TARGET};   \
100                                 fi;                                     \
101                         fi;                                             \
102                 done ;;                                                 \
103         esac
104 .  endif
105 .endfor