Imported Upstream version 4.4
[platform/upstream/make.git] / tests / scripts / features / include
1 #                                     -*-mode: perl; rm-trailing-spaces: nil-*-
2
3 $description = "Test various forms of the GNU make 'include' command.";
4
5 $details = "\
6 Test include, -include, sinclude and various regressions involving them.
7 Test extra whitespace at the end of the include, multiple -includes and
8 sincludes (should not give an error) and make sure that errors are reported
9 for targets that were also -included.";
10
11 create_file('incl.mk', "ANOTHER: ; \@echo This is another included makefile\n");
12
13 run_make_test(qq!#Extra space at the end of the following file name
14 include incl.mk    ! . q!
15 all: ; @echo There should be no errors for this makefile.
16
17 -include nonexistent.mk
18 -include nonexistent.mk
19 sinclude nonexistent.mk
20 sinclude nonexistent-2.mk
21 -include makeit.mk
22 sinclude makeit.mk
23
24 error: makeit.mk
25 !,
26               "all", "There should be no errors for this makefile.\n");
27
28 run_make_test(undef, "ANOTHER", "This is another included makefile\n");
29
30 unlink('incl.mk');
31
32 # Try to build the "error" target; this will fail since we don't know
33 # how to create makeit.mk, but we should also get a message (even though
34 # the -include suppressed it during the makefile read phase, we should
35 # see one during the makefile run phase).
36
37 run_make_test
38   ('
39 -include foo.mk
40 error: foo.mk ; @echo $@
41 ',
42    '',
43    "#MAKE#: *** No rule to make target 'foo.mk', needed by 'error'.  Stop.\n",
44    512
45   );
46
47 # The same as above with an additional include directory.
48
49 mkdir('hellod', 0777);
50
51 run_make_test
52   ('
53 -include foo.mk
54 error: foo.mk ; @echo $@
55 ',
56    '-Ihellod',
57    "#MAKE#: *** No rule to make target 'foo.mk', needed by 'error'.  Stop.\n",
58    512
59   );
60
61 rmdir('hellod');
62
63 # Make sure that target-specific variables don't impact things.  This could
64 # happen because a file record is created when a target-specific variable is
65 # set.
66
67 run_make_test
68   ('
69 bar.mk: foo := baz
70 -include bar.mk
71 hello: ; @echo hello
72 ',
73    '',
74    "hello\n"
75   );
76
77
78 # Test inheritance of dontcare flag when rebuilding makefiles.
79 #
80 run_make_test('
81 .PHONY: all
82 all: ; @:
83
84 -include foo
85
86 foo: bar; @:
87 ', '', '');
88
89
90 # Make sure that we don't die when the command fails but we dontcare.
91 # (Savannah bug #13216).
92 #
93 run_make_test('
94 .PHONY: all
95 all:; @:
96
97 -include foo
98
99 foo: bar; @:
100
101 bar:; @exit 1
102 ', '', '');
103
104 # Check include, sinclude, -include with no filenames.
105 # (Savannah bug #1761).
106
107 run_make_test('
108 .PHONY: all
109 all:; @:
110 include
111 -include
112 sinclude', '', '');
113
114
115 # Test that the diagnostics is issued even if the target has been
116 # tried before with the dontcare flag (direct dependency case).
117 #
118 run_make_test('
119 -include foo
120
121 all: bar
122
123 foo: baz
124 bar: baz
125 ',
126 '',
127 "#MAKE#: *** No rule to make target 'baz', needed by 'bar'.  Stop.\n",
128 512);
129
130 # Test that the diagnostics is issued even if the target has been
131 # tried before with the dontcare flag (indirect dependency case).
132 #
133 run_make_test('
134 -include foo
135
136 all: bar
137
138 foo: baz
139 bar: baz
140 baz: end
141 ',
142 '',
143 "#MAKE#: *** No rule to make target 'end', needed by 'baz'.  Stop.\n",
144 512);
145
146 # Test include of make-able file doesn't show an error (Savannah #102)
147 run_make_test(q!
148 .PHONY: default
149 default:; @echo DONE
150
151 inc1:; echo > $@
152 include inc1
153 include inc2
154 inc2:; echo > $@
155 !,
156               '', "echo > inc1\necho > inc2\nDONE\n");
157
158 rmfiles('inc1', 'inc2');
159
160 # Test include of make-able file doesn't show an error.
161 # Specify an additional include directory.
162
163 mkdir('hellod', 0777);
164
165 run_make_test(q!
166 .PHONY: default
167 default:; @echo DONE
168
169 inc1:; echo > $@
170 include inc1
171 include inc2
172 inc2:; echo > $@
173 !,
174               '-Ihellod', "echo > inc1\necho > inc2\nDONE\n");
175
176 rmfiles('inc1', 'inc2');
177
178 # Test include of make-able file doesn't show an error.
179 # inc1 and inc2 are present in the specified include directory.
180 touch('hellod/inc1');
181 touch('hellod/inc2');
182
183 run_make_test(q!
184 .PHONY: default
185 default:; @echo DONE
186
187 inc1:; echo > $@
188 include inc1
189 include inc2
190 inc2:; echo > $@
191 !,
192               '-Ihellod', "DONE\n");
193
194 rmfiles('inc1', 'inc2', 'hellod/inc1', 'hellod/inc2');
195
196 rmdir('hellod');
197
198 # No target gets correct error
199 run_make_test("\n", '', '#MAKE#: *** No targets.  Stop.', 512);
200
201 # No target in included file either, still gets correct error.
202 touch('inc1.mk');
203 run_make_test('include inc1.mk', '', '#MAKE#: *** No targets.  Stop.', 512);
204 rmfiles('inc1.mk');
205
206 # Include same file multiple times
207
208 run_make_test(q!
209 default:; @echo DEFAULT
210 include inc1
211 inc1:; echo > $@
212 include inc1
213 !,
214               '', "echo > inc1\nDEFAULT\n");
215
216 rmfiles('inc1');
217
218 if (defined $ERR_no_such_file) {
219
220     # Test that the diagnostics is issued even if the target has been
221     # tried before with the dontcare flag (include/-include case).
222     #
223     run_make_test('
224 include bar
225 -include foo
226
227 all:
228
229 foo: baz
230 bar: baz
231 baz: end
232 ',
233 '',
234                   "#MAKEFILE#:2: bar: $ERR_no_such_file\n#MAKE#: *** No rule to make target 'end', needed by 'baz'.  Stop.\n",
235                   512);
236
237     # Test include of non-make-able file does show an error (Savannah #102)
238     run_make_test(q!
239 .PHONY: default
240 default:; @echo DONE
241
242 inc1:; echo > $@
243 include inc1
244 include inc2
245 !,
246                   '', "echo > inc1\n#MAKEFILE#:7: inc2: $ERR_no_such_file\n#MAKE#: *** No rule to make target 'inc2'.  Stop.\n", 512);
247
248     rmfiles('inc1');
249
250     # Included file has a prerequisite that fails to build
251
252     run_make_test(q!
253 default:; @echo DEFAULT
254 include inc1
255 inc1: foo; echo > $@
256 foo:; exit 1
257 !,
258                   '', "exit 1\n#MAKEFILE#:3: inc1: $ERR_no_such_file\n#MAKE#: *** [#MAKEFILE#:5: foo] Error 1\n", 512);
259
260     rmfiles('inc1');
261
262     # Included file has a prerequisite we don't know how to build
263
264     run_make_test(q!
265 default:; @echo DEFAULT
266 include inc1
267 inc1: foo; echo > $@
268 !,
269                   '', "#MAKEFILE#:3: inc1: $ERR_no_such_file\n#MAKE#: *** No rule to make target 'foo', needed by 'inc1'.  Stop.\n", 512);
270
271     rmfiles('inc1');
272
273     # Check that included double-colon targets with no prerequisites aren't
274     # built.  This should fail as hello.mk doesn't exist
275
276     run_make_test(q!
277 .PHONY: default
278 default:;@echo 'FOO=$(FOO)'
279 include hello.mk
280 hello.mk:: ; echo 'FOO=bar' > $@
281 !,
282                   '', "#MAKEFILE#:4: hello.mk: $ERR_no_such_file", 512);
283
284     # Check that included phony targets aren't built.
285     # This should fail as hello.mk doesn't exist
286
287     run_make_test(q!
288 .PHONY: default
289 default:;@echo 'FOO=$(FOO)'
290 include hello.mk
291 hello.mk: ; echo 'FOO=bar' > $@
292 .PHONY: hello.mk
293 !,
294                   '', "#MAKEFILE#:4: hello.mk: $ERR_no_such_file", 512);
295 }
296
297 if (defined $ERR_unreadable_file) {
298     # Including files that can't be read should show an error
299     unlink('inc1');
300     create_file('inc1', 'FOO := foo');
301     chmod 0000, 'inc1';
302
303     run_make_test(q!
304 include inc1
305 all:;@echo $(FOO)
306 !,
307                   '', "#MAKEFILE#:2: inc1: $ERR_unreadable_file\n#MAKE#: *** No rule to make target 'inc1'.  Stop.", 512);
308
309     # Including files that can't be read should show an error, even when there
310     # is a readable file in a subsequent include directory.
311     mkdir('hellod', 0777);
312     touch("hellod/inc1");
313
314     run_make_test(q!
315 include inc1
316 all:;@echo $(FOO)
317 !,
318                   '-Ihellod', "#MAKEFILE#:2: inc1: $ERR_unreadable_file\n#MAKE#: *** No rule to make target 'inc1'.  Stop.", 512);
319
320     # Unreadable files that we know how to successfully recreate should work
321
322     run_make_test(sprintf(q!
323 all:;@echo $(FOO)
324 include inc1
325 inc1:; @%s $@ && echo FOO := bar > $@
326 !, $CMD_rmfile),
327                   '', "bar");
328
329     # Unreadable files that we know how to successfully recreate should work.
330     # Even when there is a readable file in an additional include directory.
331
332     unlink('inc1');
333     create_file('inc1', 'FOO := foo');
334     chmod 0000, 'inc1';
335
336     run_make_test(sprintf(q!
337 all:;@echo $(FOO)
338 include inc1
339 inc1:; @%s $@ && echo FOO := bar > $@
340 !, $CMD_rmfile),
341                   '-Ihellod', "bar");
342
343     rmfiles('inc1', 'hellod/inc1');
344     rmdir('hellod');
345 }
346
347 # Check that the order of remaking include files is correct: should remake
348 # them in the same order they were encountered in the makefile.  SV 58735
349
350 run_make_test(q!
351 -include i1 i2
352 -include i3
353 -include i4
354 %:;@echo $@
355 all:;
356 !,
357               '', "i1\ni2\ni3\ni4\n#MAKE#: 'all' is up to date.\n");
358
359 # Check that included files work if created after the first include failed
360 # https://savannah.gnu.org/bugs/?57676
361
362 run_make_test(q!
363 default:; @echo $(hello)
364 -include hello.mk
365 $(shell echo hello=world >hello.mk)
366 include hello.mk
367 !,
368               '', "world\n");
369
370 unlink('hello.mk');
371
372 # Check that included double-colon targets with no prerequisites aren't built.
373 # This should succeed since hello.mk already exists
374
375 touch('hello.mk');
376
377 run_make_test(q!
378 .PHONY: default
379 default:;@echo 'FOO=$(FOO)'
380 include hello.mk
381 hello.mk:: ; echo 'FOO=bar' > $@
382 !,
383               '', 'FOO=');
384
385 unlink('hello.mk');
386
387 # Check that included double-colon targets with no prerequisites aren't built.
388 # This should succeed due to -include
389
390 run_make_test(q!
391 .PHONY: default
392 default:;@echo 'FOO=$(FOO)'
393 -include hello.mk
394 hello.mk:: ; echo 'FOO=bar' > $@
395 !,
396               '', 'FOO=');
397
398 # Check that phony targets aren't built.
399 # This should succeed since hello.mk already exists
400
401 touch('hello.mk');
402
403 run_make_test(q!
404 .PHONY: default
405 default:;@echo 'FOO=$(FOO)'
406 include hello.mk
407 hello.mk: ; echo 'FOO=bar' > $@
408 .PHONY: hello.mk
409 !,
410               '', 'FOO=');
411
412 unlink('hello.mk');
413
414 # Check that included double-colon targets with no prerequisites aren't built.
415 # This should succeed due to -include
416
417 run_make_test(q!
418 .PHONY: default
419 default:;@echo 'FOO=$(FOO)'
420 -include hello.mk
421 hello.mk: ; echo 'FOO=bar' > $@
422 .PHONY: hello.mk
423 !,
424               '', 'FOO=');
425
426 # SV 56301 Verify pattern rules creating optional includes.
427 # -k shouldn't matter when creating include files.
428
429 run_make_test(q!
430 all:; @echo hello
431 -include inc_a.mk
432 include inc_b.mk
433 %_a.mk %_b.mk:; exit 1
434 !,
435               '', "exit 1\n#MAKEFILE#:4: Failed to remake makefile 'inc_b.mk'.\n", 512);
436
437 run_make_test(undef, '-k', "exit 1\n#MAKEFILE#:4: Failed to remake makefile 'inc_b.mk'.\n", 512);
438
439 # It seems wrong to me that this gives a different error message, but at
440 # least it doesn't keep going.
441 run_make_test(q!
442 all:; @echo hello
443 include inc_a.mk
444 -include inc_b.mk
445 %_a.mk %_b.mk:; exit 1
446 !,
447               '', "exit 1\n#MAKEFILE#:3: inc_a.mk: $ERR_no_such_file\n#MAKE#: *** [#MAKEFILE#:5: inc_a.mk] Error 1\n", 512);
448
449 run_make_test(undef, '-k', "exit 1\n#MAKEFILE#:3: inc_a.mk: $ERR_no_such_file\n#MAKE#: *** [#MAKEFILE#:5: inc_a.mk] Error 1\n#MAKEFILE#:3: Failed to remake makefile 'inc_a.mk'.\n", 512);
450
451 # Check the default makefiles... this requires us to invoke make with no
452 # arguments.  Also check MAKEFILES
453
454 if ($port_type eq 'W32') {
455     $defaults = "GNUmakefile\nmakefile\nMakefile\nmakefile.mak";
456 } else {
457     $defaults = "GNUmakefile\nmakefile\nMakefile";
458 }
459
460 $ENV{MAKEFILES} = 'foobar barfoo';
461 run_make_with_options(undef, ['-E', '%:;@echo $@', '-E', 'all:;', '-E', '-include bizbaz', '-E', '-include bazbiz'], get_logfile(0));
462 $answer = "bizbaz\nbazbiz\nfoobar\nbarfoo\n$defaults\n#MAKE#: 'all' is up to date.\n";
463 &compare_output(subst_make_string($answer), &get_logfile(1));
464
465 1;