Dumb Questions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dumb Questions

Lee Eric
Hi,

New to Make and after reading some pages from the GNU Make book I'm
confused about some explanations.

1. From the O'Reilly book it mentions "Normally, phony targets will be
always be executed ...". I don't quite follow it as "clean" is a
typical phony target but it's not executed every time until we tell
Make. So my question is, if a Makefile has multiple targets, what's
the rule of Make to process the targets? Because after reading the
book I have no idea what targets would be executed.

2. What's the actual/practical use of Empty Targets? The example in that book is

prog: size prog.o
    $(CC) $(LDFLAGS) -o $@ $^
size: prog.o
    size $^
    touch size

but I didn't see the use of that size target, because even I change it to

prog: prog.o
    $(CC) $(LDFLAGS) -o $@ $^

is still valid. So why we need to use empty targets?

Thanks and I do really appreciate someone who can give me details about those.

Eric

Reply | Threaded
Open this post in threaded view
|

Re: Dumb Questions

Paul Smith-20
On Sun, 2019-11-10 at 23:18 -0800, Lee Eric wrote:
> Hi,
>
> New to Make and after reading some pages from the GNU Make book I'm
> confused about some explanations.

Just to be clear, the O'Reilly book was not written by the FSF or the
GNU project so we can't really speak for it.  I personally don't think
I even own a copy.

We do take responsibility for the GNU make user's manual, which you can
find in various forms here: https://www.gnu.org/software/make/manual/
and, it's free!  If you want a dead tree version please purchase it
from the FSF/GNU rather than somewhere else: that way the funding goes
to the FSF/GNU project.

There are other books out there as well such as The GNU Make Book by
John Graham-Cumming (obdisclosure: I was a technical reviewer for this
book but I don't get any royalties :)): https://nostarch.com/gnumake

Not that I have anything against the O'Reilly book: I'm sure it's very
good like all their books.  Just to say on this list we can't be called
on to justify it or even know what it says beyond what you've quoted
here.

> 1. From the O'Reilly book it mentions "Normally, phony targets will be
> always be executed ...". I don't quite follow it as "clean" is a
> typical phony target but it's not executed every time until we tell
> Make. So my question is, if a Makefile has multiple targets, what's
> the rule of Make to process the targets? Because after reading the
> book I have no idea what targets would be executed.

The GNU make manual describes how make processes targets:

https://www.gnu.org/software/make/manual/html_node/How-Make-Works.html

You might want to just read the entire introductory chapter:

https://www.gnu.org/software/make/manual/html_node/Introduction.html

I think the O'Reilly book means, _if_ that rule needs to be executed,
then it will _always_ be executed regardless of whether the target
appears to be out of date or not.

> 2. What's the actual/practical use of Empty Targets? The example in that book is
>
> prog: size prog.o
>     $(CC) $(LDFLAGS) -o $@ $^
> size: prog.o
>     size $^
>     touch size
>
> but I didn't see the use of that size target, because even I change it to
>
> prog: prog.o
>     $(CC) $(LDFLAGS) -o $@ $^
>
> is still valid. So why we need to use empty targets?

In this case, it's used to run the size(1) program which will print out
the size info for the input file (use "man size" to see what it does).

So it's not intended to change the build output (which is why not
running it doesn't matter) it's meant to show the user some information
about the built artifact.


Reply | Threaded
Open this post in threaded view
|

Dumb Questions

Gnu - Make - Help mailing list
In reply to this post by Lee Eric
Lee Eric writes:
 > Hi,
 >
 > New to Make and after reading some pages from the GNU Make book I'm
 > confused about some explanations.
 >
 > 1. From the O'Reilly book it mentions "Normally, phony targets will
 >    be always be executed ...". I don't quite follow it as "clean"
 >    is a typical phony target but it's not executed every time until
 >    we tell Make. So my question is, if a Makefile has multiple
 >    targets, what's the rule of Make to process the targets? Because
 >    after reading the book I have no idea what targets would be
 >    executed.

 A phony target is executed each time it is mentioned on the command
 line, or as a prerequisite of another target that is being built.

 Essentially, a phony target is never considered 'up-to-date' each
 time Make is started.

 >
 > 2. What's the actual/practical use of Empty Targets? The example in that book is
 >
 > prog: size prog.o
 >     $(CC) $(LDFLAGS) -o $@ $^
 > size: prog.o
 >     size $^
 >     touch size
 >
 > but I didn't see the use of that size target, because even I change it to
 >
 > prog: prog.o
 >     $(CC) $(LDFLAGS) -o $@ $^
 >
 > is still valid. So why we need to use empty targets?

 The 'size' target here is used to encapsulate a sequence of commands
 that you might like to execute frequently during development.  In
 this case, it's showing the size of the contents of the prerequisite
 object files -- facilitating tracking code & data bloat.

 This 'size' is a little bit smart -- it only runs when the object
 file has changed since the last time 'size' was executed.  As an
 experiment, try making it phony and executing it without having
 prog.o change.


--
It's not about where you're from, it's wear your hat.


Reply | Threaded
Open this post in threaded view
|

Re: Dumb Questions

Kaz Kylheku (gmake)
In reply to this post by Lee Eric
On 2019-11-10 23:18, Lee Eric wrote:

> Hi,
>
> New to Make and after reading some pages from the GNU Make book I'm
> confused about some explanations.
>
> 1. From the O'Reilly book it mentions "Normally, phony targets will be
> always be executed ...". I don't quite follow it as "clean" is a
> typical phony target but it's not executed every time until we tell
> Make. So my question is, if a Makefile has multiple targets, what's
> the rule of Make to process the targets? Because after reading the
> book I have no idea what targets would be executed.

The direct rules in a Makefile form the familiar directed acyclic graph
(DAG). The target in a Makefile that is not a prerequisite of other
targets is the root of the DAG. If there are multiple such targets then
there are multiple roots. In that case we can choose the root by giving
a target on the command line. If no target is given on the command line,
then the first one that occurs in the Makefile is updated.

A phony target's recipe is always executed if that phony target is
involved in a dependency tree that is part of an update.

A phony target that is not part of a tree being updated, of course,
doesn't have its recipe executed. (Like your "clean" target example
when "make clean" isn't being run).

Because a phony target isn't a real file, it is always considered
to be missing. Whenever it is required as a prerequisite, or explicitly
required as a goal from the command line, its recipe is executed.

However, it is executed only once. A phony target is in fact a real
object; just not one in the filesystem. It's an object that exists in
make's memory somehow. When the recipe for that object runs, it is
then considered up-to-date, even though no file was created or touched.
when evaluation continues and other rules are processed which require
that same target, the rule is not run again; the target is known to be
up-to-date.

Therefore, it is subtly misleading to say "the phony target always
executes".

If a phony target is required by three rules that all execute,
it doesn't execute three times, as "always" would imply.