help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: manual section 4.7 says something strange


From: Paul Smith
Subject: Re: manual section 4.7 says something strange
Date: Mon, 02 Jan 2017 14:36:24 -0500

On Mon, 2017-01-02 at 09:51 -0900, Britton Kerin wrote:
> This may be at least mostly  true for the particular case of the clean
> target, which is generally invoked explicitly.  However, its not true
> for the common case of an object file that one wants to always
> rebuild.

I'm not sure anyone would say object files that one wants to always
rebuild could be considered a "common case".  In fact, I've never
written (or seen) a (correct) makefile like that in my life: if you
declare an object file to be phony, or forced, then you can never have a
"do nothing" build; every time you run make the object file will always
be rebuilt and the binary will always be re-linked, even if no code
changes have happened.  That's not something I've ever known to be
desirable in a makefile; certainly it's not common.

Anyway...

> This Makefile:
> 
>      %.o: %.c
>          cp $< address@hidden # Simulate compile
> 
>      #.PHONY: test.o
> 
>      test.o: FORCE
>      FORCE:
> 
>      test.o: Makefile
> 
>      fooprog: test.o
>          cp $< address@hidden # Simulate link
> 
> Will always recompile test.c into test.o as it is, but if the test.o:
> FORCE and FORCE: rules are replaced with the .PHONY: test.o rule
> (commented out above), then test.o will never be built at all.

That's because of this feature of .PHONY, described in the section of
the GNU make manual about Phony Targets:

>    The implicit rule search (*note Implicit Rules::) is skipped for
> '.PHONY' targets.  This is why declaring a target as '.PHONY' is good
> for performance, even if you are not worried about the actual file
> existing.

Because your test.o has no explicit rule and .PHONY disables implicit
rule lookup, when make goes to try to build "test.o" it finds no recipe
for that target and simply declares it to be up to date. 

> I think the description in 4.7 goes too far in implying equivalency.
> Either it shouldn't say the two are equivalent, or it should be more
> careful to qualify that that they are equivalent only in the
> particular case of the explicitly invoked phony target which doesn't
> have additional dependencies expressed in other rules.

It doesn't have anything to do with additional dependencies, as I
mention above.  If you were to rewrite your test.o rule so that it was
explicit instead of implicit:

    test.o: test.c Makefile
            cp $< $@

You'd see that the .PHONY and FORCE variants worked identically in all
senses.

It may be worth alluding to this difference in the section on FORCE,
just to be clear.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]