Thu Apr 23 19:30:29 2020 UTC ()
mk/subst.mk: warn about all noop patterns before erroring out


(rillig)
diff -r1.82 -r1.83 pkgsrc/mk/subst.mk
diff -r1.22 -r1.23 pkgsrc/regress/infra-unittests/subst.sh

cvs diff -r1.82 -r1.83 pkgsrc/mk/subst.mk (expand / switch to unified diff)

--- pkgsrc/mk/subst.mk 2020/04/23 19:16:49 1.82
+++ pkgsrc/mk/subst.mk 2020/04/23 19:30:29 1.83
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: subst.mk,v 1.82 2020/04/23 19:16:49 rillig Exp $ 1# $NetBSD: subst.mk,v 1.83 2020/04/23 19:30:29 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,33 +146,36 @@ _SUBST_IS_TEXT_FILE_CMD.${class}= ${_SUB @@ -146,33 +146,36 @@ _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} \ 159 ${RUN} set -u; \
160 message=${SUBST_MESSAGE.${class}:Q}; \ 160 message=${SUBST_MESSAGE.${class}:Q}; \
161 [ "$$message" ] && ${ECHO_SUBST_MSG} "$$message"; \ 161 [ "$$message" ] && ${ECHO_SUBST_MSG} "$$message"; \
162 \ 162 \
163 cd ${WRKSRC}; \ 163 cd ${WRKSRC}; \
164 patterns=${SUBST_FILES.${class}:Q}; \ 164 patterns=${SUBST_FILES.${class}:Q}; \
165 set -f; \ 165 set -f; \
 166 noop_count=''; \
 167 noop_patterns=''; \
 168 noop_sep=''; \
166 for pattern in $$patterns; do \ 169 for pattern in $$patterns; do \
167 set +f; \ 170 set +f; \
168 changed=no; \ 171 changed=no; \
169 for file in $$pattern; do \ 172 for file in $$pattern; do \
170 case $$file in ([!A-Za-z0-9/]*) file="./$$file";; esac; \ 173 case $$file in ([!A-Za-z0-9/]*) file="./$$file";; esac; \
171 tmpfile="$$file.subst.sav"; \ 174 tmpfile="$$file.subst.sav"; \
172 [ -d "$$file" ] && continue; \ 175 [ -d "$$file" ] && continue; \
173 [ -f "$$file" ] || { \ 176 [ -f "$$file" ] || { \
174 ${_SUBST_WARN.${class}} "Ignoring non-existent file \"$$file\"."; \ 177 ${_SUBST_WARN.${class}} "Ignoring non-existent file \"$$file\"."; \
175 continue; \ 178 continue; \
176 }; \ 179 }; \
177 ${_SUBST_IS_TEXT_FILE_CMD.${class}} || { \ 180 ${_SUBST_IS_TEXT_FILE_CMD.${class}} || { \
178 ${_SUBST_WARN.${class}} "Ignoring non-text file \"$$file\"."; \ 181 ${_SUBST_WARN.${class}} "Ignoring non-text file \"$$file\"."; \
@@ -181,20 +184,28 @@ ${_SUBST_COOKIE.${class}}: @@ -181,20 +184,28 @@ ${_SUBST_COOKIE.${class}}:
181 ${SUBST_FILTER_CMD.${class}} < "$$file" > "$$tmpfile"; \ 184 ${SUBST_FILTER_CMD.${class}} < "$$file" > "$$tmpfile"; \
182 ${CMP} -s "$$tmpfile" "$$file" && { \ 185 ${CMP} -s "$$tmpfile" "$$file" && { \
183 ${_SUBST_WARN.${class}} "Nothing changed in \"$$file\"."; \ 186 ${_SUBST_WARN.${class}} "Nothing changed in \"$$file\"."; \
184 ${RM} -f "$$tmpfile"; \ 187 ${RM} -f "$$tmpfile"; \
185 continue; \ 188 continue; \
186 }; \ 189 }; \
187 [ -x "$$file" ] && ${CHMOD} +x "$$tmpfile"; \ 190 [ -x "$$file" ] && ${CHMOD} +x "$$tmpfile"; \
188 changed=yes; \ 191 changed=yes; \
189 ${_SUBST_KEEP.${class}}; \ 192 ${_SUBST_KEEP.${class}}; \
190 ${MV} -f "$$tmpfile" "$$file"; \ 193 ${MV} -f "$$tmpfile" "$$file"; \
191 ${ECHO} "$$file" >> ${.TARGET}.tmp; \ 194 ${ECHO} "$$file" >> ${.TARGET}.tmp; \
192 done; \ 195 done; \
193 \ 196 \
194 [ "$$changed,${SUBST_NOOP_OK.${class}:tl}" = no,no ] \ 197 [ "$$changed,${SUBST_NOOP_OK.${class}:tl}" = no,no ] && { \
195 && ${FAIL_MSG} "[subst.mk:${class}] The filename pattern \"$$pattern\" has no effect."; \ 198 noop_count="$$noop_count+"; \
196 done; \ 199 noop_patterns="$$noop_patterns$$noop_sep$$pattern"; \
 200 noop_sep=" "; \
 201 }; \
 202 done; \
197 \ 203 \
 204 case $$noop_count in \
 205 ('') ;; \
 206 (+) ${FAIL_MSG} "[subst.mk:${class}] The filename pattern \"$$noop_patterns\" has no effect.";; \
 207 (*) ${FAIL_MSG} "[subst.mk:${class}] The filename patterns \"$$noop_patterns\" have no effect."; \
 208 esac; \
198 ${TOUCH} ${TOUCH_FLAGS} ${.TARGET}.tmp; \ 209 ${TOUCH} ${TOUCH_FLAGS} ${.TARGET}.tmp; \
199 ${MV} ${.TARGET}.tmp ${.TARGET} 210 ${MV} ${.TARGET}.tmp ${.TARGET}
200.endfor 211.endfor

cvs diff -r1.22 -r1.23 pkgsrc/regress/infra-unittests/subst.sh (expand / switch to unified diff)

--- pkgsrc/regress/infra-unittests/subst.sh 2020/04/23 19:16:49 1.22
+++ pkgsrc/regress/infra-unittests/subst.sh 2020/04/23 19:30:29 1.23
@@ -928,52 +928,53 @@ if test_case_begin "pattern matches only @@ -928,52 +928,53 @@ if test_case_begin "pattern matches only
928 "=> Substituting \"dir\" in sub*" \ 928 "=> Substituting \"dir\" in sub*" \
929 'fail: [subst.mk:dir] The filename pattern "sub*" has no effect.' \ 929 'fail: [subst.mk:dir] The filename pattern "sub*" has no effect.' \
930 "*** Error code 1" \ 930 "*** Error code 1" \
931 "" \ 931 "" \
932 "Stop." \ 932 "Stop." \
933 "$make: stopped in $PWD" 933 "$make: stopped in $PWD"
934 assert_that "stderr" --file-is-empty 934 assert_that "stderr" --file-is-empty
935 assert_that "$exitcode" --equals 1 935 assert_that "$exitcode" --equals 1
936 936
937 test_case_end 937 test_case_end
938fi 938fi
939 939
940 940
941if test_case_begin "first filename pattern has no effect"; then 941if test_case_begin "two filename patterns have no effect"; then
942 942
943 # All patterns of SUBST_FILES should be applied before erroring out. 943 # All patterns of SUBST_FILES should be applied before erroring out,
944 # TODO: also warn about file2 944 # to give a complete picture of the situation.
945 945
946 create_file_lines "testcase.mk" \ 946 create_file_lines "testcase.mk" \
947 'SUBST_CLASSES+= id' \ 947 'SUBST_CLASSES+= id' \
948 'SUBST_STAGE.id= pre-configure' \ 948 'SUBST_STAGE.id= pre-configure' \
949 'SUBST_FILES.id= file1 file2' \ 949 'SUBST_FILES.id= file1 file2' \
950 'SUBST_VARS.id= A B' \ 950 'SUBST_VARS.id= A B' \
951 'SUBST_NOOP_OK.id= no' \ 951 'SUBST_NOOP_OK.id= no' \
952 'A= a-value' \ 952 'A= a-value' \
953 'B= b-value' \ 953 'B= b-value' \
954 '' \ 954 '' \
955 '.include "prepare-subst.mk"' \ 955 '.include "prepare-subst.mk"' \
956 '.include "mk/subst.mk"' 956 '.include "mk/subst.mk"'
957 create_file_lines "file1" "nothing to replace" 957 create_file_lines "file1" "nothing to replace"
958 create_file_lines "file2" "nothing to replace" 958 create_file_lines "file2" "nothing to replace"
959 959
960 run_bmake "testcase.mk" "pre-configure" 1> "$tmpdir/out" 2>&1 \ 960 run_bmake "testcase.mk" "pre-configure" 1> "$tmpdir/out" 2>&1 \
961 && exitcode=0 || exitcode=$? 961 && exitcode=0 || exitcode=$?
962 962
963 assert_that "out" --file-is-lines \ 963 assert_that "out" --file-is-lines \
964 '=> Substituting "id" in file1 file2' \ 964 '=> Substituting "id" in file1 file2' \
965 'warning: [subst.mk:id] Nothing changed in "file1".' \ 965 'warning: [subst.mk:id] Nothing changed in "file1".' \
966 'fail: [subst.mk:id] The filename pattern "file1" has no effect.' \ 966 'warning: [subst.mk:id] Nothing changed in "file2".' \
 967 'fail: [subst.mk:id] The filename patterns "file1 file2" have no effect.' \
967 '*** Error code 1' \ 968 '*** Error code 1' \
968 '' \ 969 '' \
969 'Stop.' \ 970 'Stop.' \
970 "$make: stopped in $PWD" 971 "$make: stopped in $PWD"
971 972
972 test_case_end 973 test_case_end
973fi 974fi
974 975
975 976
976if test_case_begin "empty SUBST_FILES"; then 977if test_case_begin "empty SUBST_FILES"; then
977 978
978 # An empty SUBST_FILES section is ok. 979 # An empty SUBST_FILES section is ok.
979 # It may have been produced by a shell command like find(1). 980 # It may have been produced by a shell command like find(1).