Debian Codegraph @

debian/Explanations

raw
1 This file is a presentation of explanations of make's behaviour that
2 users have found puzzling over time, along with explanations of the
3 behaviour by make's author.
4
5 ######################################################################
6 ######################################################################
7 ######################################################################
8
9 Case 1:
10
11 > I have always thought that a normal (non-pattern) rule with multiple
12 > targets gets run multiple times, once per each target that is out of
13 > date. At least, that's what the manual says. Sometimes one would
14 > like a different behavior, namely to update all the targets at once
15 > with one run, but as long as it's documented and established I am
16 > willing to live with it. But there seems to be more to it.
17
18 ==============================================================================
19 MODULES := \
20 Blah \
21 Bleh \
22
23 SOURCES := $(addsuffix .sml,$(MODULES))
24
25 TEX := $(addsuffix .tex,$(MODULES))
26
27 modules.tex : $(TEX)
28 cat $(TEX) > modules.tex
29
30 $(TEX) : $(SOURCES)
31 ./bin/plit $(SOURCES)
32 ==============================================================================
33
34 > I have files Blah.sml and Bleh.sml.
35 > Surprise #1: make modules.tex runs the last rule _once_ (which
36 > happens to be which happens in this case).
37
38 That's because of the way make processes the makefile. First, it builds
39 Blah.tex, but that command also updates Bleh.tex. So, when make comes
40 around looking to decide if Bleh.tex needs to be updated, it doesn't
41 have to be because it's already up to date, so make doesn't run the rule
42 again.
43
44 The best way to see this problem is to try a parallel build: in that
45 case often make will try to invoke the script once for each target, in
46 parallel.
47
48 > Surprise #2: add a flag to the command, so now the last rule is
49 > $(TEX) : $(SOURCES)
50 > ./bin/plit -n $(SOURCES)
51 > touch one of the .sml files, make modules.tex again. Now plit runs twice!
52
53 I don't know what the -n flag does so I can't say: I can't find any docs
54 for "plit" via Google or on my system. Maybe it doesn't actually update
55 the files with -n?
56
57 Needless to say I can't reproduce this behavior with a simplified
58 makefile that doesn't invoke plit but rather just uses touch, etc. to
59 try to mimic its behavior.
60
61 >
62 > Surprise #3: refactor like this
63 > $(TEX) : junk
64 > junk: $(SOURCES)
65 > ./bin/plit -n $(SOURCES)
66 > touch Blah.sml. Now make modules.tex runs the last rule (once), but
67 > not the first rule (for modules.tex itself), even though it clearly
68 > is out of date!
69
70 I can only assume that you created a file "junk" by hand in this
71 directory during your testing: no other explanation fits the facts as
72 you describe them.
73
74 This makefile is broken, because you never have any command that updates
75 the file "junk".
76
77 If that file "junk" doesn't exist already then make will always run the
78 plit script every time, regardless of the relative timestamps of the
79 .sml files, and then it will always update modules.tex.
80
81 If that file "junk" does exist, then it will re-run "plit" if any of the
82 .sml files is newer than the file "junk". But since the script doesn't
83 update "junk" itself, make doesn't consider the two .tex files out of
84 date (they are still newer than their prerequisite, "junk") so it
85 doesn't rebuild modules.tex.
86
87 If you change your rule so that it updates "junk", all will work as you
88 expect:
89
90 $(TEX): junk
91
92 junk: $(SOURCES)
93 ./bin/plit -n $(SOURCES)
94 @touch $@
95
96 ######################################################################
97 ######################################################################
98 ######################################################################
99
100 Case 2
101
102 > In some cases, GNU make ignores some double-colon rules when dry-run.
103 ======================================================================
104 all: foo.1 foo.2
105 foo.1:
106 touch foo.1
107 foo.2:
108 touch foo.2
109
110 install.man:: foo.1
111 @echo install foo.1
112
113 install.man:: foo.2
114 @echo install foo.2
115
116 install.man::
117 @echo install done
118 ======================================================================
119 > I've got the following results
120
121 > % make
122 > touch foo.1
123 > touch foo.2
124 > % make -n install.man
125 > echo install foo.1
126 > echo install done
127
128 > I expect "echo install foo.2", but make dry-run shows not to
129 > run "install foo.2". However, if I run make actually
130
131 > % make install.man
132 > install foo.1
133 > install foo.2
134 > install done
135
136 > "install foo.2" is executed. Is this an intentional behavior or a
137 > bug of GNU make?
138
139 It is intentional behavior. When GNU make runs with -n it must assume
140 that your command script does what you say it will do: since it doesn't
141 run the script itself it can't know what the script _really_ does.
142
143 For a rule like this:
144
145 install.man:: foo.1
146 <some script>
147
148 GNU make _must_ assume that <some script> updates the target
149 "install.man"... how can it know that it really doesn't?
150
151 So, when you run with -n it assumes that the target is now updated,
152 which automatically makes it newer than "foo.2", so the rule depending
153 on "foo.2" does not get run.
154
155 The last install.man rule is run because there is a special condition
156 for double-colon rules, that if they have no prerequisites the command
157 is run even if the target exists (see the section "Double-Colon Rules"
158 in the GNU make manual).
159
160 However, double-colon rules that _DO_ have prerequisites are only
161 invoked if the prerequisites are newer than the target, just as with
162 normal rules.
163
164 You can see the behavior "make -n" is emulating without using -n if you
165 have your scripts actually create the target your makefile says it will:
166
167 install.man:: foo.1
168 @echo install foo.1
169 touch $@
170 install.man:: foo.2
171 @echo install foo.2
172 touch $@
173 install.man::
174 @echo install done
175 touch $@
176
177
178 $ make -n
179 install foo.1
180 install done
181
182 $ make
183 install foo.1
184 install done
185
186 ms> Note that pmake run as I expected
187
188 ms> % pmake -n install.man
189 ms> echo install foo.1
190 ms> echo install foo.2
191 ms> echo install done
192
193 Most likely pmake behaves differently; it may always run all
194 double-colon scripts, even if there's a satisfied prerequisite
195 relationship.
196
197 Try the above makefile with the touch lines in it: does it run all three
198 rules even though the touch lines exist?
199
200 ######################################################################
201 ######################################################################
202 ######################################################################
203
204 arch-tag: b9738368-cd5f-43ad-aed0-6853f020811f