From 770bad1fe3c369da32b53d790a26f82c012666e7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 10 Aug 2024 10:17:35 +0100 Subject: make: disallow inference rules for phony targets GNU make doesn't allow inference rules or the .DEFAULT target to be used for phony targets. POSIX is unclear on the matter but there doesn't seem to be an explicit prohibition of inference rules or .DEFAULT. Follow the GNU make behaviour as a non-POSIX extension. Adds 48-80 bytes. (pdpmake GitHub issue 56) --- miscutils/make.c | 13 ++++++++----- testsuite/make.tests | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/miscutils/make.c b/miscutils/make.c index 1dfbd8c44..8c74d7113 100644 --- a/miscutils/make.c +++ b/miscutils/make.c @@ -2611,9 +2611,10 @@ make(struct name *np, int level) if (!(np->n_flag & N_DOUBLE)) { // Find the commands needed for a single-colon rule, using - // an inference rule or .DEFAULT rule if necessary + // an inference rule or .DEFAULT rule if necessary (but, + // as an extension, not for phony targets) sc_cmd = getcmd(np); - if (!sc_cmd) { + if (!sc_cmd && (posix || !(np->n_flag & N_PHONY))) { impdep = dyndep(np, &imprule); if (impdep) { sc_cmd = imprule.r_cmd; @@ -2623,7 +2624,8 @@ make(struct name *np, int level) // As a last resort check for a default rule if (!(np->n_flag & N_TARGET) && np->n_tim.tv_sec == 0) { - sc_cmd = getcmd(findname(".DEFAULT")); + if (posix || !(np->n_flag & N_PHONY)) + sc_cmd = getcmd(findname(".DEFAULT")); if (!sc_cmd) { if (doinclude) return 1; @@ -2634,10 +2636,11 @@ make(struct name *np, int level) } else { // If any double-colon rule has no commands we need - // an inference rule + // an inference rule (but, as an extension, not for phony targets) for (rp = np->n_rule; rp; rp = rp->r_next) { if (!rp->r_cmd) { - impdep = dyndep(np, &imprule); + if (posix || !(np->n_flag & N_PHONY)) + impdep = dyndep(np, &imprule); if (!impdep) { if (doinclude) return 1; diff --git a/testsuite/make.tests b/testsuite/make.tests index 5485233f6..c92c69340 100755 --- a/testsuite/make.tests +++ b/testsuite/make.tests @@ -738,4 +738,18 @@ file1: ' cd .. || exit 1; rm -rf make.tempdir 2>/dev/null +# GNU make and BSD make don't allow the use of inference rules +# for phony targets. In POSIX mode the output is "phony.xyz\n". +mkdir make.tempdir && cd make.tempdir || exit 1 +touch phony.xyz +testing "make don't use inference rule for phony target" \ + "make -f -" "make: nothing to be done for phony\n" "" ' +.PHONY: phony +.SUFFIXES: .xyz +.xyz: + @echo $< +phony: +' +cd .. || exit 1; rm -rf make.tempdir 2>/dev/null + exit $FAILCOUNT -- cgit v1.2.3-55-g6feb