help-make
[Top][All Lists]
Advanced

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

Re: vpath-using makefile can build a target when steps are invoked indiv


From: Philip Guenther
Subject: Re: vpath-using makefile can build a target when steps are invoked individually, but can't chain them
Date: Wed, 19 May 2021 16:35:55 -0900

On Wed, May 19, 2021 at 1:26 PM Britton Kerin <britton.kerin@gmail.com>
wrote:

> Maybe the below makes sense for some reason I don't understand but it
> sure seems weird.
> Seems like $< ends up with a different value depending on where the
> request for the target comes from:
>
> $ ls
> Makefile
> $ cat Makefile
> OBJS = foo.o
>
> FC = cp # Fake Compile
> FL = cp # Fake Link
>
> vpath %.o objdir
>
> %.o: %.c
>         $(FC) $< objdir/$@
>

This violates Paul's second rules for Makefiles, as enumerated here:
    http://make.mad-scientist.net/papers/rules-of-makefiles/
namely:
   2. Every non-.PHONY rule must update a file with the exact name of its
target.
       Make sure every command script touches the file “$@“–not “../$@“, or
“$(notdir $@)“, but exactly $@.
       That way you and GNU make always agree.

As soon as you do that, you've gone off the road and the results (and
informational messages) may be unexpected.  Maybe it works 'mostly', but
you've given up reliability.



> foo: foo.o
>         $(FL) $< $@
>
> clean:
>         rm -f *.o
>         rm -f objdir/*.o
>         rm foo
> $ mkdir objdir
> $ touch foo.c
> $ make foo.o
> cp  foo.c objdir/foo.o
> $ make foo
> cp  objdir/foo.o foo
> $ make clean
> rm -f *.o
> rm -f objdir/*.o
> rm foo
> $ make foo
> cp  foo.c objdir/foo.o
> cp  foo.o foo
> cp: cannot stat 'foo.o': No such file or directory
> make: *** [Makefile:12: foo] Error 1
> 2 $
>

Yep, you told make it can create foo.o IN THE CURRENT DIRECTORY from foo.c
by invoking the first recipe, but that's a lie and make will trip on your
lie if you try have make chain from that false fact, while if you tell the
lie to one make which doesn't need that info and let a second make discover
the real state of the world for itself then it 'works'.


You should probably also check out Paul's whitepaper on VPATH:
     http://make.mad-scientist.net/papers/how-not-to-use-vpath/

because what you're appearing to try to do (use VPATH/vpath to place
targets) is walked through in that paper.


Philip Guenther


reply via email to

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