Thu Apr 23 19:06:09 2020 UTC ()
mk/subst.mk: refactor main code

The indentation of the inner loop has been fixed.

The chmod is only run if the file has actually changed. In the other
case, the file would have been removed right after the chmod, which made
the chmod unnecessary.

For compatibility with ancient operating systems whose /bin/sh still does
not understand negated conditions (SunOS), these conditions have been
avoided and were written using && and || instead.

The inner loop has been flattened a bit, to compensate for the
indentation of the outer loop.


(rillig)
diff -r1.80 -r1.81 pkgsrc/mk/subst.mk

cvs diff -r1.80 -r1.81 pkgsrc/mk/subst.mk (expand / switch to unified diff)

--- pkgsrc/mk/subst.mk 2020/04/23 18:06:13 1.80
+++ pkgsrc/mk/subst.mk 2020/04/23 19:06:09 1.81
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: subst.mk,v 1.80 2020/04/23 18:06:13 rillig Exp $ 1# $NetBSD: subst.mk,v 1.81 2020/04/23 19:06:09 rillig Exp $
2# 2#
3# The subst framework replaces text in one or more files in the WRKSRC 3# The subst framework replaces text in one or more files in the WRKSRC
4# directory. Packages can define several ``classes'' of replacements. 4# directory. Packages can define several ``classes'' of replacements.
5# Each such class defines: 5# Each such class defines:
6# 6#
7# - in which stage of the build process the replacement happens 7# - in which stage of the build process the replacement happens
8# - which files are affected by the replacement 8# - which files are affected by the replacement
9# - which text or pattern is replaced by which replacement text 9# - which text or pattern is replaced by which replacement text
10# 10#
11# A typical example is: 11# A typical example is:
12# 12#
13# SUBST_CLASSES+= prefix 13# SUBST_CLASSES+= prefix
14# SUBST_STAGE.prefix= pre-configure 14# SUBST_STAGE.prefix= pre-configure
@@ -146,54 +146,55 @@ _SUBST_IS_TEXT_FILE_CMD.${class}= ${_SUB @@ -146,54 +146,55 @@ _SUBST_IS_TEXT_FILE_CMD.${class}= ${_SUB
146. endif 146. endif
147 147
148. if defined(SUBST_STAGE.${class}) 148. if defined(SUBST_STAGE.${class})
149${SUBST_STAGE.${class}}: subst-${class} 149${SUBST_STAGE.${class}}: subst-${class}
150. else 150. else
151# SUBST_STAGE.* does not need to be defined. 151# SUBST_STAGE.* does not need to be defined.
152#PKG_FAIL_REASON+= "SUBST_STAGE missing for ${class}." 152#PKG_FAIL_REASON+= "SUBST_STAGE missing for ${class}."
153. endif 153. endif
154 154
155.PHONY: subst-${class} 155.PHONY: subst-${class}
156subst-${class}: ${_SUBST_COOKIE.${class}} 156subst-${class}: ${_SUBST_COOKIE.${class}}
157 157
158${_SUBST_COOKIE.${class}}: 158${_SUBST_COOKIE.${class}}:
159 ${RUN} message=${SUBST_MESSAGE.${class}:Q}; \ 
160 if [ "$$message" ]; then ${ECHO_SUBST_MSG} "$$message"; fi 
161 ${RUN} \ 159 ${RUN} \
 160 message=${SUBST_MESSAGE.${class}:Q}; \
 161 [ "$$message" ] && ${ECHO_SUBST_MSG} "$$message"; \
 162 \
162 cd ${WRKSRC}; \ 163 cd ${WRKSRC}; \
163 patterns=${SUBST_FILES.${class}:Q}; \ 164 patterns=${SUBST_FILES.${class}:Q}; \
164 set -f; \ 165 set -f; \
165 for pattern in $$patterns; do \ 166 for pattern in $$patterns; do \
166 set +f; \ 167 set +f; \
167 changed=no; \ 168 changed=no; \
168 for file in $$pattern; do \ 169 for file in $$pattern; do \
169 case $$file in /*) ;; *) file="./$$file";; esac; \ 170 case $$file in /*) ;; *) file="./$$file";; esac; \
170 tmpfile="$$file.subst.sav"; \ 171 tmpfile="$$file.subst.sav"; \
171 if [ ! -f "$$file" ]; then \ 172 [ -d "$$file" ] && continue; \
172 [ -d "$$file" ] || ${_SUBST_WARN.${class}} "Ignoring non-existent file \"$$file\"."; \ 173 [ -f "$$file" ] || { \
173 elif ${_SUBST_IS_TEXT_FILE_CMD.${class}}; then \ 174 ${_SUBST_WARN.${class}} "Ignoring non-existent file \"$$file\"."; \
174 ${SUBST_FILTER_CMD.${class}} \ 175 continue; \
175 < "$$file" \ 176 }; \
176 > "$$tmpfile"; \ 177 ${_SUBST_IS_TEXT_FILE_CMD.${class}} || { \
177 if ${TEST} -x "$$file"; then \ 178 ${_SUBST_WARN.${class}} "Ignoring non-text file \"$$file\"."; \
178 ${CHMOD} +x "$$tmpfile"; \ 179 continue; \
179 fi; \ 180 }; \
180 if ${CMP} -s "$$tmpfile" "$$file"; then \ 181 ${SUBST_FILTER_CMD.${class}} < "$$file" > "$$tmpfile"; \
 182 ${CMP} -s "$$tmpfile" "$$file" && { \
181 ${_SUBST_WARN.${class}} "Nothing changed in $$file."; \ 183 ${_SUBST_WARN.${class}} "Nothing changed in $$file."; \
182 ${RM} -f "$$tmpfile"; \ 184 ${RM} -f "$$tmpfile"; \
183 else \ 185 continue; \
184 changed=yes; \ 186 }; \
185 ${_SUBST_KEEP.${class}}; \ 187 [ -x "$$file" ] && ${CHMOD} +x "$$tmpfile"; \
186 ${MV} -f "$$tmpfile" "$$file"; \ 188 changed=yes; \
187 ${ECHO} "$$file" >> ${.TARGET}.tmp; \ 189 ${_SUBST_KEEP.${class}}; \
188 fi; \ 190 ${MV} -f "$$tmpfile" "$$file"; \
189 else \ 191 ${ECHO} "$$file" >> ${.TARGET}.tmp; \
190 ${_SUBST_WARN.${class}} "Ignoring non-text file \"$$file\"."; \ 192 done; \
191 fi; \ 193 \
192 done; \ 194 [ "$$changed,${SUBST_NOOP_OK.${class}:tl}" = no,no ] \
 195 && ${FAIL_MSG} "[subst.mk:${class}] The filename pattern \"$$pattern\" has no effect."; \
 196 done; \
193 \ 197 \
194 if ${TEST} "$$changed,${SUBST_NOOP_OK.${class}:tl}" = no,no; then \ 198 ${TOUCH} ${TOUCH_FLAGS} ${.TARGET}.tmp; \
195 ${FAIL_MSG} "[subst.mk:${class}] The filename pattern \"$$pattern\" has no effect."; \ 199 ${MV} ${.TARGET}.tmp ${.TARGET}
196 fi; \ 
197 done 
198 ${RUN} ${TOUCH} ${TOUCH_FLAGS} ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET} 
199.endfor 200.endfor