aboutsummaryrefslogtreecommitdiff
path: root/testsuite/make.tests
blob: 35d3b71490c2e06017839b504b74c6c601e9a860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/bin/sh

. ./testing.sh

# testing "test name" "command" "expected result" "file input" "stdin"

testing "make basic makefile" \
	"make -f -" "target\n" "" '
target:
	@echo target
'

# .DEFAULT rules with no commands or some prerequisites are ignored.
# .DEFAULT rules with commands can be redefined.
testing "make .DEFAULT rule" \
	"make -f - default" "default2\n" "" '
.DEFAULT: ignored
.DEFAULT:
	@echo default1
.DEFAULT:
	@echo default2
target:
'

# Macros should be expanded before suffix substitution.  The suffixes
# can be obtained by macro expansion.
testing "make macro expansion and suffix substitution" \
	"make -f -" "src1.o src2.o\n" "" '
DOTC = .c
DOTO = .o
SRC1 = src1.c
SRCS = $(SRC1) src2.c
target:
	@echo $(SRCS:$(DOTC)=$(DOTO))
'

# Indeed, everything after the <colon> can be obtained by macro
# macro expansion.
testing "make macro expansion and suffix substitution 2" \
	"make -f -" "src1.o src2.o\n" "" '
DOTS = .c=.o
SRC1 = src1.c
SRCS = $(SRC1) src2.c
target:
	@echo $(SRCS:$(DOTS))
'

# It should be possible for an inference rule to determine that a
# prerequisite can be created using an explicit rule.
mkdir make.tempdir && cd make.tempdir || exit 1
testing "make inference rule with explicit rule for prerequisite" \
	"make -f -" "touch x.p\ncat x.p >x.q\n" "" '
.SUFFIXES: .p .q
x.q:
x.p:
	touch $@
.p.q:
	cat $< >$@
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# A macro created using ::= remembers it's of type immediate-expansion.
# Immediate expansion also occurs when += is used to append to such a macro.
testing "make appending to immediate-expansion macro" \
	"make -f -" \
	"hello 1 2 3\nhello 4 4\n" "" '
world = 1
hello ::= hello $(world)
world = 2
hello += $(world)
world = 3
hello += $(world)
world = 4

world = 1
reset ::= hello $(world)
world = 2
# No longer immediate-expansion
reset = hello $(world)
world = 3
reset += $(world)
world = 4

target:
	@echo $(hello)
	@echo $(reset)
'

# basic pattern macro expansion
testing "make basic pattern macro expansion" \
	"make -f -" \
	"obj/util.o obj/main.o\n" "" '
SRC = src/util.c src/main.c
OBJ = $(SRC:src/%.c=obj/%.o)

target:
	@echo $(OBJ)
'

# pattern macro expansion; match any value
testing "make pattern macro expansion; match any value" \
	"make -f -" \
	"any_value.o\n" "" '
SRC = any_value
OBJ = $(SRC:%=%.o)

target:
	@echo $(OBJ)
'

# pattern macro expansion with empty rvalue
testing "make pattern macro expansion with empty rvalue" \
	"make -f -" \
	"\n" "" '
SRC = util.c main.c
OBJ = $(SRC:%.c=)

target:
	@echo $(OBJ)
'

# pattern macro expansion with multiple <percent> in rvalue
# POSIX requires the first <percent> to be expanded, others
# may or may not be expanded.  Permit either case.
testing "make pattern macro expansion with multiple <percent> in rvalue" \
	"make -f - | sed 's/mainmainmain/main%%/'" \
	"main%%\n" "" '
SRC = main.c
OBJ = $(SRC:%.c=%%%)

target:
	@echo $(OBJ)
'

# pattern macro expansion; zero match
testing "make pattern macro expansion; zero match" \
	"make -f -" \
	"nsnp\n" "" '
WORD = osop
REPL = $(WORD:os%op=ns%np)

target:
	@echo $(REPL)
'

# Check that MAKE will contain argv[0], e.g make in this case
testing "make basic MAKE macro expansion" \
	"make -f -" \
	"make\n" "" '
target:
	@echo $(MAKE)
'

# Check that MAKE defined as environment variable will overwrite default MAKE
testing "make MAKE macro expansion; overwrite with env macro" \
	"MAKE=hello make -f -" \
	"hello\n" "" '
target:
	@echo $(MAKE)
'

# Check that MAKE defined on the command-line will overwrite MAKE defined in
# Makefile
testing "make MAKE macro expansion; overwrite with command-line macro" \
	"make -f - MAKE=hello" \
	"hello\n" "" '
MAKE = test

target:
	@echo $(MAKE)
'

# POSIX draft states that if make was invoked using relative path, MAKE must
# contain absolute path, not just argv[0]
testing "make MAKE macro expansion; turn relative path into absolute" \
	"../runtest-tempdir-links/make -f -" \
	"ok\n" "" '
target:
	@test -e "$(MAKE)" && test "$(MAKE)" = "$$(env which make)" && echo ok
'

# $? contains prerequisites newer than target, file2 in this case
# $^ has all prerequisites, file1 and file2
touch -t 202206171200 file1
touch -t 202206171201 target
touch -t 202206171202 file2
testing "make compare \$? and \$^ internal macros" \
	"make -f -" \
	"file2\nfile1 file2\n" "" '
target: file1 file2
	@echo $?
	@echo $^
'
rm -f target file1 file2

# Phony targets are executed (once) even if a matching file exists.
# A .PHONY target with no prerequisites is ignored.
touch -t 202206171201 target
testing "make phony target" \
	"make -f -" \
	"phony\n" "" '
.PHONY: target
.PHONY:
target:
	@echo phony
'
rm -f target

# Phony targets aren't touched with -t
testing "make phony target not touched" \
	"make -t -f - >/dev/null && test -f target && echo target" \
	"" "" '
.PHONY: target
target:
	@:
'
rm -f target

# Include files are created or brought up-to-date
mkdir make.tempdir && cd make.tempdir || exit 1
testing "make create include file" \
	"make -f -" \
	"made\n" "" '
target:
	@echo $(VAR)
mk:
	@echo "VAR = made" >mk
include mk
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Include files are created or brought up-to-date even when the -n
# option is given.
mkdir make.tempdir && cd make.tempdir || exit 1
testing "make create include file even with -n" \
	"make -n -f -" \
	"echo made\n" "" '
target:
	@echo $(VAR)
mk:
	@echo "VAR = made" >mk
include mk
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Failure to create an include file isn't an error.  (Provided the
# include line is ignoring non-existent files.)
testing "make failure to create include file is OK" \
	"make -f -" \
	"OK\n" "" '
target:
	@echo OK
mk:
	@:
-include mk
'

# $^ skips duplicate prerequisites, $+ doesn't
mkdir make.tempdir && cd make.tempdir || exit 1
touch file1 file2 file3
testing "make skip duplicate entries in \$^ but not \$+" \
	"make -f -" \
	"file1 file2 file3\nfile1 file2 file2 file3 file3\n" "" '
target: file1 file2 file2 file3 file3
	@echo $^
	@echo $+
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Assign the output of a shell command to a macro.
testing "make shell assignment" \
	"make -f -" \
	"1 2 3   4\n" "" '
hello != echo 1; echo 2; echo 3; echo; echo

target:
	@echo "$(hello) 4"
'

# Nested macro expansion is allowed.  This should be compatible
# with other implementations.
testing "make nested macro expansion" \
	"make -f -" "0 bc\n1 d\n2\n3\n4 bcd\n5 bcd\n" "" '
a = b
b = c
c = d
$(a:.q=.v)$(b:.z=.v) = bc
bcd = bcd
target:
	@echo 0 $(bc)
	@echo 1 $($($(a)))
	@echo 2 $($(a) $(b) $(c))
	@echo 3 $($a $b $c)
	@echo 4 $($(a)$(b)$(c))
	@echo 5 $($a$b$c)
'

# .WAIT is allowed as a prerequisite.  Since parallel builds aren't
# implemented it doesn't have any effect.
mkdir make.tempdir && cd make.tempdir || exit 1
touch file1 file2
testing "make .WAIT is allowed as a prerequisite" \
	"make -f -" \
	"file1 file2\n" "" '
target: file1 .WAIT file2
	@echo $^
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

testing "make double-colon rule" \
	"make -f -" "target1\ntarget2\n" "" '
target::
	@echo target1
target::
	@echo target2
'

# There was a bug whereby the modification time of a file created by
# double-colon rules wasn't correctly updated.  This test checks that
# the bug is now fixed.
mkdir make.tempdir && cd make.tempdir || exit 1
touch -t 202206171200 file1
touch -t 202206171201 intermediate
touch -t 202206171202 target
touch -t 202206171203 file2
testing "make target depends on prerequisite updated by double-colon rule" \
	"make -f -" \
	"file2\n" "" '
target: intermediate
	@cat intermediate
intermediate:: file1
	@echo file1 >>intermediate
intermediate:: file2
	@echo file2 >>intermediate
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Use chained inference rules to determine prerequisites.
mkdir make.tempdir && cd make.tempdir || exit 1
touch target.p
testing "make chained inference rules" \
	"make -s -f - target.s" \
	"target.q\ntarget.r\ntarget.s\n" "" '
.SUFFIXES: .p .q .r .s
.p.q:
	@cp $< $*.q
	@echo $*.q
.q.r:
	@cp $< $*.r
	@echo $*.r
.r.s:
	@cp $< $*.s
	@echo $*.s
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# make supports *, ? and [] wildcards in targets and prerequisites
mkdir make.tempdir && cd make.tempdir || exit 1
touch -t 202206171201 t1a t2aa t3b
touch s1a s2aa s3b
testing "make expand wildcards in filenames" \
	"make -f - t1a t2aa t3b" \
	"t1a s1a s2aa s3b\nt2aa s1a s2aa s3b\nt3b s1a s2aa s3b\n" "" '
t1? t2* t3[abc]: s1? s2* s3[abc]
	@echo $@ $?
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Skip duplicate entries in $? and $^
mkdir make.tempdir && cd make.tempdir || exit 1
touch -t 202206171200 file1 file3
touch -t 202206171201 target
touch -t 202206171202 file2
testing "make skip duplicate entries in \$? and \$^" \
	"make -f -" \
	"file2\nfile1 file2 file3\n" "" '
target: file1 file2 file2 file3 file3
	@echo $?
	@echo $^
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Skip duplicate entries in $? and $^, with each double-colon rule
# handled separately
mkdir make.tempdir && cd make.tempdir || exit 1
touch -t 202206171200 file1 file3
touch -t 202206171201 target
touch -t 202206171202 file2
testing "make skip duplicate entries: double-colon rules" \
	"make -f -" \
	"file2\nfile1 file3 file2\nfile2\nfile2 file3\n" "" '
target:: file1 file3 file1 file2 file3
	@echo $?
	@echo $^
target:: file2 file3 file3
	@echo $?
	@echo $^
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Skip duplicate entries in $? and $^, with each double-colon rule
# handled separately.  No prerequisites out-of-date in the first.
mkdir make.tempdir && cd make.tempdir || exit 1
touch -t 202206171200 file1 file3
touch -t 202206171201 target
touch -t 202206171202 file2
testing "make skip duplicate entries: double-colon rules, only second invoked" \
	"make -f -" \
	"file2\nfile2 file3\n" "" '
target:: file1 file3 file1 file3
	@echo $?
	@echo $^
target:: file2 file3 file3
	@echo $?
	@echo $^
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null

# Double-colon rules didn't work properly if their target was phony:
# - they didn't ignore the presence of a file matching the target name;
# - they were also invoked as if they were a single-colon rule.
mkdir make.tempdir && cd make.tempdir || exit 1
touch -t 202206171200 file1
touch -t 202206171201 target
testing "make phony target of double-colon rule" \
	"make -f - 2>&1" \
	"unconditional\nconditional\n" "" '
.PHONY: target
target::
	@echo unconditional
target:: file1
	@echo conditional
file1:
	@touch file1
'
cd .. || exit 1; rm -rf make.tempdir 2>/dev/null