bugmake - Bugs: bug #28983, forcing a target matching a...

 
 

bug #28983: forcing a target matching a pattern rule shadows the rule's actions

Submitter:  Ilguiz Latypov <ilgiz>
Submitted:  Mon 22 Feb 2010 08:35:05 PM UTC
   
 
Severity:  3 - Normal Item Group:  Bug
Status:  Not A Bug Privacy:  Public
Assigned to:  None Open/Closed:  Closed
Component Version:  3.81 Operating System:  MS Windows
Fixed Release:  None Triage Status:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Fri 02 Jul 2010 09:43:41 PM UTC, comment #8: 

A few things.  First, I cannot reproduce at all the behavior you show in your original bug description, where sometimes using a pattern rule make will rebuild file.o.  If you use a pattern rule, make should NEVER rebuild file.o (if it's declared .PHONY) and that's exactly the behavior I see.  If you can reproduce that behavior, that would indeed be a bug.  Please provide more details in that case.

Second, the behavior difference you see is a direct consequence of one of the primary purposes of the .PHONY feature, and is described clearly in the manual:

   Since it knows that phony targets do not name actual files that
could be remade from other files, `make' skips the implicit rule search
for phony targets (*note Implicit Rules::).  This is why declaring a
target phony is good for performance, even if you are not worried about
the actual file existing.


So, in your examples with pattern rules, no pattern rule search is done and thus there is no recipe to build file.o so make doesn't build it.  This is identical to the behavior you'd see if there were no pattern rule at all.  If you define a target explicitly for file.o, then make will see that and try to build it (every time, because it's marked PHONY).

Because this is behaving exactly as described in the manual I'm marking this as "Not a Bug".  However, if you have an alternative behavior that you think would make more sense please feel free to comment here; if you convince me I'll be happy to reopen this and make it an enhancement request instead.

Paul D. Smith <psmith>
Group administrator
Sat 08 May 2010 12:10:10 AM UTC, comment #7: 


The following tests show that .PHONY's pre-requisites are normally rebuilt regardless of existence of a file with the same name.

This works,


$ cat test-force.mak

default: file.o

.PHONY: file.o

file.o:
        echo Building $@... > $@

$ make -f test-force.mak
echo Building file.o... > file.o

$ ls -al file.o
-rw-r--r-- 1 ilatypov Domain Users 19 May  7 19:58 file.o

$ rm file.o

$ make -f test-force.mak
echo Building file.o... > file.o

$ ls -al file.o
-rw-r--r-- 1 ilatypov Domain Users 19 May  7 19:58 file.o

$ make -f test-force.mak
echo Building file.o... > file.o


And this works,


$ cat test-force2.mak

default: file.o

.PHONY: file.o

file.c:
        echo Auto-generating $@... > $@

file.o: file.c
        @echo Building $@ from $^...
        cat $^ > $@

$ rm file.c file.o

$ make -f test-force2.mak
echo Auto-generating file.c... > file.c
Building file.o from file.c...
cat file.c > file.o

$ make -f test-force2.mak
Building file.o from file.c...
cat file.c > file.o

$ make -f test-force2.mak
Building file.o from file.c...
cat file.c > file.o

$ rm file.o

$ make -f test-force2.mak
Building file.o from file.c...
cat file.c > file.o


But this does not,


$ cat test-force3.mak

default: file.o

.PHONY: file.o

file.c:
        echo Auto-generating $@... > $@

%.o: %.c
        @echo Building $@ from $^...
        cat $^ > $@

$ rm -f file.c file.o

$ make -f test-force3.mak
make: Nothing to be done for `default'.


This allows me to think that my expectation in the original bug report was valid.  And a pattern rule should not be affected by the phoniness of a target.

Ilguiz Latypov <ilgiz>
Sat 08 May 2010 12:08:10 AM UTC, comment #6: 


The following tests show that .PHONY's pre-requisites are normally rebuilt regardless of existence of a file with the same name.

This works,


$ cat test-force.mak

default: file.o

.PHONY: file.o

file.o:
        echo Building $@... > $@

$ make -f test-force.mak
echo Building file.o... > file.o

$ ls -al file.o
-rw-r--r-- 1 ilatypov Domain Users 19 May  7 19:58 file.o

$ rm file.o

$ make -f test-force.mak
echo Building file.o... > file.o

$ ls -al file.o
-rw-r--r-- 1 ilatypov Domain Users 19 May  7 19:58 file.o

$ make -f test-force.mak
echo Building file.o... > file.o

+verbatim-

And this works,

+verbatim+
$ cat test-force2.mak

default: file.o

.PHONY: file.o

file.c:
        echo Auto-generating $@... > $@

file.o: file.c
        @echo Building $@ from $^...
        cat $^ > $@

$ rm file.c file.o

$ make -f test-force2.mak
echo Auto-generating file.c... > file.c
Building file.o from file.c...
cat file.c > file.o

$ make -f test-force2.mak
Building file.o from file.c...
cat file.c > file.o

$ make -f test-force2.mak
Building file.o from file.c...
cat file.c > file.o

$ rm file.o

$ make -f test-force2.mak
Building file.o from file.c...
cat file.c > file.o
+verbatim-

But this does not,

+verbatim+
$ cat test-force3.mak

default: file.o

.PHONY: file.o

file.c:
        echo Auto-generating $@... > $@

%.o: %.c
        @echo Building $@ from $^...
        cat $^ > $@

$ rm -f file.c file.o

$ make -f test-force3.mak
make: Nothing to be done for `default'.
+verbatim-

This allows me to think that my expectation in the original bug report was valid.  And a pattern rule should not be affected by the phoniness of a target.

Ilguiz Latypov <ilgiz>
Fri 07 May 2010 11:11:45 PM UTC, comment #5: 

I don't understand how the second quote defeats my statement.  The point is that marking the "clean" target phony prevents the accidental existence of a file by that name from confusing the build system.  If you're trying to argue from that quote that marking a target phony is a general way to force it to be rebuilt, you're wrong.

I don't understand your objection to the FORCE idiom either.  Here is your makefile rewritten to use FORCE:


default: file.o

FORCE:
.PHONY: FORCE
file.o: FORCE

file.c:
        echo Auto-generating "$@"...
        touch "$@"

%.o: %.c
        echo Making "$@" from "$^"...
        touch "$@"


The only "pre-requisite of .PHONY" (let's just call it a phony target) is FORCE itself, and the purpose of marking it phony is to not be affected by a file of the that name, though such a file should never exist anyway.  What is the potential name clash you mention?

Matt McCutchen <hashproduct>
Fri 07 May 2010 06:08:58 PM UTC, comment #4: 

The GNU make manual both supports and defeats Matt's statement.

  "A phony target is one that is not really the name of a file."

  [..]

  "Once this is done, `make clean' will run the commands regardless of whether there is a file named clean."

It would be a nuisance for the builds of pre-requisites of .PHONY to depend on existence of the files by the same name.

Introducing an intermediate dependency can only reduce but not completely eliminate the possibility of a name clash.

Ilguiz Latypov <ilgiz>
Fri 07 May 2010 04:33:49 PM UTC, comment #3: 

The technique in comment #1 is actually the recommended solution, with the dummy target named FORCE.  Phoniness is only for targets that are not files.

Matt McCutchen <hashproduct>
Mon 22 Feb 2010 09:57:06 PM UTC, comment #2: 

Correction: the semicolon at the end of the work-around lines should be removed because it assigned an empty set of actions to the artificial target.  GNU make will complain if another rule assigns any actions to the same target,

.PHONY: foo.o ;

.PHONY: bar.o ;

makefile:XXX: warning: overriding commands for target `.PHONY'

Ilguiz Latypov <ilgiz>
Mon 22 Feb 2010 08:47:27 PM UTC, comment #1: 

The work-around, as I see it in one build system, is to add an extra dummy target,

=================================
ALWAYS-BUILD: ;

file.o: ALWAYS-BUILD
=================================

Same with the explicit noisy target,

=================================
.PHONY: ALWAYS-BUILD ;

file.o: ALWAYS-BUILD
=================================

Ilguiz Latypov <ilgiz>
Mon 22 Feb 2010 08:35:05 PM UTC, original submission:  


With the makefile piece below I could get file.o only once.  If I remove it from the file system, the .PHONY rule seems to shadow the pattern rule.  I could not find a stipulation justifying this behavior in the GNU make documentation.  Using a Cygwin build 3.81-2 here.

=====================================================
default: file.o

.PHONY: file.o

file.c:
        echo Auto-generating "$@"...
        touch "$@"

%.o: %.c
        echo Making "$@" from "$^"...
        touch "$@"

=====================================================

$ make -f force-build-of-a-target-matching-a-pattern-rule.mak
echo Auto-generating "file.c"...
Auto-generating file.c...
touch "file.c"
echo Making "file.o" from "file.c"...
Making file.o from file.c...
touch "file.o"

$ rm file.o

$ make -f force-build-of-a-target-matching-a-pattern-rule.mak
make: Nothing to be done for `default'.

Ilguiz Latypov <ilgiz>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by psmith (Posted a comment)
  • -email is unavailable- added by hashproduct (Posted a comment)
  • -email is unavailable- added by ilgiz (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2010-07-02 psmith StatusNone Not A Bug
        Open/ClosedOpen Closed
    2010-02-22 ilgiz Attached File- Added force-build-of-a-target-matching-a-pattern-rule.mak, #19776

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code