Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / explain.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test the --debug=explain option.
29 """
30
31 import os.path
32 import string
33 import sys
34 import TestSCons
35
36 _python_ = TestSCons._python_
37
38 test = TestSCons.TestSCons()
39
40 test.subdir('work1', ['work1', 'src'], ['work1', 'src', 'subdir'],
41             'work4', ['work4', 'src'], ['work4', 'src', 'subdir'],
42             'work5')
43
44 subdir_file6 = os.path.join('subdir', 'file6')
45 subdir_file6_in = os.path.join('subdir', 'file6.in')
46 cat_py = test.workpath('cat.py')
47
48 test.write(cat_py, r"""
49 import sys
50
51 def process(outfp, infp):
52     for line in infp.readlines():
53         if line[:8] == 'include ':
54             file = line[8:-1]
55             try:
56                 fp = open(file, 'rb')
57             except IOError:
58                 import os
59                 print "os.getcwd() =", os.getcwd()
60                 raise
61             process(outfp, fp)
62         else:
63             outfp.write(line)
64
65 outfp = open(sys.argv[1], 'wb')
66 for f in sys.argv[2:]:
67     if f != '-':
68         process(outfp, open(f, 'rb'))
69
70 sys.exit(0)
71 """)
72
73 SConstruct_contents = """\
74 import re
75
76 include_re = re.compile(r'^include\s+(\S+)$', re.M)
77
78 def kfile_scan(node, env, target, arg):
79     contents = node.get_contents()
80     includes = include_re.findall(contents)
81     return includes
82
83 kscan = Scanner(name = 'kfile',
84                 function = kfile_scan,
85                 argument = None,
86                 skeys = ['.k'])
87
88 cat = Builder(action = r'%(_python_)s %(cat_py)s $TARGET $SOURCES')
89
90 env = Environment()
91 env.Append(BUILDERS = {'Cat':cat},
92            SCANNERS = kscan)
93
94 Export("env")
95 SConscript('SConscript')
96 env.Install('../inc', 'aaa')
97 env.InstallAs('../inc/bbb.k', 'bbb.k')
98 env.Install('../inc', 'ddd')
99 env.InstallAs('../inc/eee', 'eee.in')
100 """ % locals()
101
102 args = '--debug=explain .'
103
104 #############################################################################
105 test.write(['work1', 'src', 'SConstruct'], SConstruct_contents)
106
107 test.write(['work1', 'src', 'SConscript'], """\
108 Import("env")
109 env.Cat('file1', 'file1.in')
110 env.Cat('file2', 'file2.k')
111 env.Cat('file3', ['xxx', 'yyy', 'zzz'])
112 env.Command('file4', 'file4.in',
113              r'%(_python_)s %(cat_py)s $TARGET $FILE4FLAG $SOURCES',
114              FILE4FLAG='-')
115 env.Cat('file5', 'file5.k')
116 env.Cat('subdir/file6', 'subdir/file6.in')
117 """ % locals())
118
119 test.write(['work1', 'src', 'aaa'], "aaa 1\n")
120 test.write(['work1', 'src', 'bbb.k'], """\
121 bbb.k 1
122 include ccc
123 include ../inc/ddd
124 include ../inc/eee
125 """)
126 test.write(['work1', 'src', 'ccc'], "ccc 1\n")
127 test.write(['work1', 'src', 'ddd'], "ddd 1\n")
128 test.write(['work1', 'src', 'eee.in'], "eee.in 1\n")
129
130 test.write(['work1', 'src', 'file1.in'], "file1.in 1\n")
131
132 test.write(['work1', 'src', 'file2.k'], """\
133 file2.k 1 line 1
134 include xxx
135 include yyy
136 file2.k 1 line 4
137 """)
138
139 test.write(['work1', 'src', 'file4.in'], "file4.in 1\n")
140
141 test.write(['work1', 'src', 'xxx'], "xxx 1\n")
142 test.write(['work1', 'src', 'yyy'], "yyy 1\n")
143 test.write(['work1', 'src', 'zzz'], "zzz 1\n")
144
145 test.write(['work1', 'src', 'file5.k'], """\
146 file5.k 1 line 1
147 include ../inc/aaa
148 include ../inc/bbb.k
149 file5.k 1 line 4
150 """)
151
152 test.write(['work1', 'src', 'subdir', 'file6.in'], "subdir/file6.in 1\n")
153
154 work1_inc_aaa = test.workpath('work1', 'inc', 'aaa')
155 work1_inc_ddd = test.workpath('work1', 'inc', 'ddd')
156 work1_inc_eee = test.workpath('work1', 'inc', 'eee')
157 work1_inc_bbb_k = test.workpath('work1', 'inc', 'bbb.k')
158
159 #
160 expect = test.wrap_stdout("""\
161 scons: building `file1' because it doesn't exist
162 %(_python_)s %(cat_py)s file1 file1.in
163 scons: building `file2' because it doesn't exist
164 %(_python_)s %(cat_py)s file2 file2.k
165 scons: building `file3' because it doesn't exist
166 %(_python_)s %(cat_py)s file3 xxx yyy zzz
167 scons: building `file4' because it doesn't exist
168 %(_python_)s %(cat_py)s file4 - file4.in
169 scons: building `%(work1_inc_aaa)s' because it doesn't exist
170 Install file: "aaa" as "%(work1_inc_aaa)s"
171 scons: building `%(work1_inc_ddd)s' because it doesn't exist
172 Install file: "ddd" as "%(work1_inc_ddd)s"
173 scons: building `%(work1_inc_eee)s' because it doesn't exist
174 Install file: "eee.in" as "%(work1_inc_eee)s"
175 scons: building `%(work1_inc_bbb_k)s' because it doesn't exist
176 Install file: "bbb.k" as "%(work1_inc_bbb_k)s"
177 scons: building `file5' because it doesn't exist
178 %(_python_)s %(cat_py)s file5 file5.k
179 scons: building `%(subdir_file6)s' because it doesn't exist
180 %(_python_)s %(cat_py)s %(subdir_file6)s %(subdir_file6_in)s
181 """ % locals())
182
183 test.run(chdir='work1/src', arguments=args, stdout=expect)
184
185 test.must_match(['work1', 'src', 'file1'], "file1.in 1\n")
186 test.must_match(['work1', 'src', 'file2'], """\
187 file2.k 1 line 1
188 xxx 1
189 yyy 1
190 file2.k 1 line 4
191 """)
192 test.must_match(['work1', 'src', 'file3'], "xxx 1\nyyy 1\nzzz 1\n")
193 test.must_match(['work1', 'src', 'file4'], "file4.in 1\n")
194 test.must_match(['work1', 'src', 'file5'], """\
195 file5.k 1 line 1
196 aaa 1
197 bbb.k 1
198 ccc 1
199 ddd 1
200 eee.in 1
201 file5.k 1 line 4
202 """)
203
204 #
205 test.write(['work1', 'src', 'file1.in'], "file1.in 2\n")
206 test.write(['work1', 'src', 'yyy'], "yyy 2\n")
207 test.write(['work1', 'src', 'zzz'], "zzz 2\n")
208 test.write(['work1', 'src', 'bbb.k'], "bbb.k 2\ninclude ccc\n")
209
210 expect = test.wrap_stdout("""\
211 scons: rebuilding `file1' because `file1.in' changed
212 %(_python_)s %(cat_py)s file1 file1.in
213 scons: rebuilding `file2' because `yyy' changed
214 %(_python_)s %(cat_py)s file2 file2.k
215 scons: rebuilding `file3' because:
216            `yyy' changed
217            `zzz' changed
218 %(_python_)s %(cat_py)s file3 xxx yyy zzz
219 scons: rebuilding `%(work1_inc_bbb_k)s' because:
220            `%(work1_inc_ddd)s' is no longer a dependency
221            `%(work1_inc_eee)s' is no longer a dependency
222            `bbb.k' changed
223 Install file: "bbb.k" as "%(work1_inc_bbb_k)s"
224 scons: rebuilding `file5' because `%(work1_inc_bbb_k)s' changed
225 %(_python_)s %(cat_py)s file5 file5.k
226 """ % locals())
227
228 test.run(chdir='work1/src', arguments=args, stdout=expect)
229
230 test.must_match(['work1', 'src', 'file1'], "file1.in 2\n")
231 test.must_match(['work1', 'src', 'file2'], """\
232 file2.k 1 line 1
233 xxx 1
234 yyy 2
235 file2.k 1 line 4
236 """)
237 test.must_match(['work1', 'src', 'file3'], "xxx 1\nyyy 2\nzzz 2\n")
238 test.must_match(['work1', 'src', 'file5'], """\
239 file5.k 1 line 1
240 aaa 1
241 bbb.k 2
242 ccc 1
243 file5.k 1 line 4
244 """)
245
246 #
247 test.write(['work1', 'src', 'SConscript'], """\
248 Import("env")
249 env.Cat('file3', ['xxx', 'yyy'])
250 """)
251
252 expect = test.wrap_stdout("""\
253 scons: rebuilding `file3' because `zzz' is no longer a dependency
254 %(_python_)s %(cat_py)s file3 xxx yyy
255 """ % locals())
256
257 test.run(chdir='work1/src', arguments=args, stdout=expect)
258
259 test.must_match(['work1', 'src', 'file3'], "xxx 1\nyyy 2\n")
260
261 #
262 test.write(['work1', 'src', 'SConscript'], """\
263 Import("env")
264 env.Cat('file3', ['xxx', 'yyy', 'zzz'])
265 """)
266
267 expect = test.wrap_stdout("""\
268 scons: rebuilding `file3' because `zzz' is a new dependency
269 %(_python_)s %(cat_py)s file3 xxx yyy zzz
270 """ % locals())
271
272 test.run(chdir='work1/src', arguments=args, stdout=expect)
273
274 test.must_match(['work1', 'src', 'file3'], "xxx 1\nyyy 2\nzzz 2\n")
275
276 #
277 test.write(['work1', 'src', 'SConscript'], """\
278 Import("env")
279 env.Cat('file3', ['zzz', 'yyy', 'xxx'])
280 """)
281
282 expect = test.wrap_stdout("""\
283 scons: rebuilding `file3' because the dependency order changed:
284                old: ['xxx', 'yyy', 'zzz']
285                new: ['zzz', 'yyy', 'xxx']
286 %(_python_)s %(cat_py)s file3 zzz yyy xxx
287 """ % locals())
288
289 test.run(chdir='work1/src', arguments=args, stdout=expect)
290
291 test.must_match(['work1', 'src', 'file3'], "zzz 2\nyyy 2\nxxx 1\n")
292
293 #
294 test.write(['work1', 'src', 'SConscript'], """\
295 Import("env")
296 f3 = File('file3')
297 env.Cat(f3, ['zzz', 'yyy', 'xxx'])
298 env.AddPostAction(f3, r'%(_python_)s %(cat_py)s ${TARGET}.yyy $SOURCES yyy')
299 env.AddPreAction(f3, r'%(_python_)s %(cat_py)s ${TARGET}.alt $SOURCES')
300 """ % locals())
301
302 expect = test.wrap_stdout("""\
303 scons: rebuilding `file3' because the build action changed:
304                old: %(_python_)s %(cat_py)s $TARGET $SOURCES
305                new: %(_python_)s %(cat_py)s ${TARGET}.alt $SOURCES
306                     %(_python_)s %(cat_py)s $TARGET $SOURCES
307                     %(_python_)s %(cat_py)s ${TARGET}.yyy $SOURCES yyy
308 %(_python_)s %(cat_py)s file3.alt zzz yyy xxx
309 %(_python_)s %(cat_py)s file3 zzz yyy xxx
310 %(_python_)s %(cat_py)s file3.yyy zzz yyy xxx yyy
311 """ % locals())
312
313 test.run(chdir='work1/src', arguments=args, stdout=expect)
314
315 test.must_match(['work1', 'src', 'file3'], "zzz 2\nyyy 2\nxxx 1\n")
316 test.must_match(['work1', 'src', 'file3.alt'], "zzz 2\nyyy 2\nxxx 1\n")
317 test.must_match(['work1', 'src', 'file3.yyy'], "zzz 2\nyyy 2\nxxx 1\nyyy 2\n")
318
319 #
320 test.write(['work1', 'src', 'SConscript'], """\
321 Import("env")
322 f3 = File('file3')
323 env.Cat(f3, ['zzz', 'yyy', 'xxx'])
324 env.AddPostAction(f3, r'%(_python_)s %(cat_py)s ${TARGET}.yyy $SOURCES xxx')
325 env.AddPreAction(f3, r'%(_python_)s %(cat_py)s ${TARGET}.alt $SOURCES')
326 """ % locals())
327
328 expect = test.wrap_stdout("""\
329 scons: rebuilding `file3' because the build action changed:
330                old: %(_python_)s %(cat_py)s ${TARGET}.alt $SOURCES
331                     %(_python_)s %(cat_py)s $TARGET $SOURCES
332                     %(_python_)s %(cat_py)s ${TARGET}.yyy $SOURCES yyy
333                new: %(_python_)s %(cat_py)s ${TARGET}.alt $SOURCES
334                     %(_python_)s %(cat_py)s $TARGET $SOURCES
335                     %(_python_)s %(cat_py)s ${TARGET}.yyy $SOURCES xxx
336 %(_python_)s %(cat_py)s file3.alt zzz yyy xxx
337 %(_python_)s %(cat_py)s file3 zzz yyy xxx
338 %(_python_)s %(cat_py)s file3.yyy zzz yyy xxx xxx
339 """ % locals())
340
341 test.run(chdir='work1/src', arguments=args, stdout=expect)
342
343 test.must_match(['work1', 'src', 'file3'], "zzz 2\nyyy 2\nxxx 1\n")
344 test.must_match(['work1', 'src', 'file3.alt'], "zzz 2\nyyy 2\nxxx 1\n")
345 test.must_match(['work1', 'src', 'file3.yyy'], "zzz 2\nyyy 2\nxxx 1\nxxx 1\n")
346
347 #
348 test.write(['work1', 'src', 'SConscript'], """\
349 Import("env")
350 env.Command('file4', 'file4.in',
351             r'%(_python_)s %(cat_py)s $TARGET $FILE4FLAG $SOURCES',
352             FILE4FLAG='')
353 """ % locals())
354
355 expect = test.wrap_stdout("""\
356 scons: rebuilding `file4' because the contents of the build action changed
357                action: %(_python_)s %(cat_py)s $TARGET $FILE4FLAG $SOURCES
358 %(_python_)s %(cat_py)s file4 file4.in
359 """ % locals())
360
361 test.run(chdir='work1/src',arguments=args, stdout=expect)
362
363 test.must_match(['work1', 'src', 'file4'], "file4.in 1\n")
364
365 test.up_to_date(chdir='work1/src',arguments='.')
366
367 # Test the transition when you turn ON SConsignFile().
368 # This will (or might) rebuild things, but we don't care what,
369 # we just want to make sure we don't blow up.
370 test.write(['work1', 'src', 'SConstruct'],
371            "SConsignFile()\n" + SConstruct_contents)
372
373 test.run(chdir='work1/src', arguments=args)
374
375 test.up_to_date(chdir='work1/src',arguments='.')
376
377 #############################################################################
378 # Test that the --debug=explain information gets saved by default.
379 test.write(['work4', 'src', 'SConstruct'], SConstruct_contents)
380
381 test.write(['work4', 'src', 'SConscript'], """\
382 Import("env")
383 env.Cat('file1', 'file1.in')
384 env.Cat('file2', 'file2.k')
385 env.Cat('file3', ['xxx', 'yyy', 'zzz'])
386 env.Command('file4', 'file4.in', r'%(_python_)s %(cat_py)s $TARGET - $SOURCES')
387 env.Cat('file5', 'file5.k')
388 env.Cat('subdir/file6', 'subdir/file6.in')
389 """ % locals())
390
391 test.write(['work4', 'src', 'aaa'], "aaa 1\n")
392 test.write(['work4', 'src', 'bbb.k'], """\
393 bbb.k 1
394 include ccc
395 include ../inc/ddd
396 include ../inc/eee
397 """)
398 test.write(['work4', 'src', 'ccc'], "ccc 1\n")
399 test.write(['work4', 'src', 'ddd'], "ddd 1\n")
400 test.write(['work4', 'src', 'eee.in'], "eee.in 1\n")
401
402 test.write(['work4', 'src', 'file1.in'], "file1.in 1\n")
403
404 test.write(['work4', 'src', 'file2.k'], """\
405 file2.k 1 line 1
406 include xxx
407 include yyy
408 file2.k 1 line 4
409 """)
410
411 test.write(['work4', 'src', 'file4.in'], "file4.in 1\n")
412
413 test.write(['work4', 'src', 'xxx'], "xxx 1\n")
414 test.write(['work4', 'src', 'yyy'], "yyy 1\n")
415 test.write(['work4', 'src', 'zzz'], "zzz 1\n")
416
417 test.write(['work4', 'src', 'file5.k'], """\
418 file5.k 1 line 1
419 include ../inc/aaa
420 include ../inc/bbb.k
421 file5.k 1 line 4
422 """)
423
424 test.write(['work4', 'src', 'subdir', 'file6.in'], "subdir/file6.in 1\n")
425
426 #
427 test.run(chdir='work4/src', arguments='.')
428
429 test.must_match(['work4', 'src', 'file1'], "file1.in 1\n")
430 test.must_match(['work4', 'src', 'file2'], """\
431 file2.k 1 line 1
432 xxx 1
433 yyy 1
434 file2.k 1 line 4
435 """)
436 test.must_match(['work4', 'src', 'file3'], "xxx 1\nyyy 1\nzzz 1\n")
437 test.must_match(['work4', 'src', 'file4'], "file4.in 1\n")
438 test.must_match(['work4', 'src', 'file5'], """\
439 file5.k 1 line 1
440 aaa 1
441 bbb.k 1
442 ccc 1
443 ddd 1
444 eee.in 1
445 file5.k 1 line 4
446 """)
447
448 #
449 test.write(['work4', 'src', 'file1.in'], "file1.in 2\n")
450 test.write(['work4', 'src', 'yyy'], "yyy 2\n")
451 test.write(['work4', 'src', 'zzz'], "zzz 2\n")
452 test.write(['work4', 'src', 'bbb.k'], "bbb.k 2\ninclude ccc\n")
453
454 work4_inc_bbb_k = test.workpath('work4', 'inc', 'bbb.k')
455 work4_inc_ddd = test.workpath('work4', 'inc', 'ddd')
456 work4_inc_eee = test.workpath('work4', 'inc', 'eee')
457
458 test.run(chdir='work4/src', arguments=args, stdout=test.wrap_stdout("""\
459 scons: rebuilding `file1' because `file1.in' changed
460 %(_python_)s %(cat_py)s file1 file1.in
461 scons: rebuilding `file2' because `yyy' changed
462 %(_python_)s %(cat_py)s file2 file2.k
463 scons: rebuilding `file3' because:
464            `yyy' changed
465            `zzz' changed
466 %(_python_)s %(cat_py)s file3 xxx yyy zzz
467 scons: rebuilding `%(work4_inc_bbb_k)s' because:
468            `%(work4_inc_ddd)s' is no longer a dependency
469            `%(work4_inc_eee)s' is no longer a dependency
470            `bbb.k' changed
471 Install file: "bbb.k" as "%(work4_inc_bbb_k)s"
472 scons: rebuilding `file5' because `%(work4_inc_bbb_k)s' changed
473 %(_python_)s %(cat_py)s file5 file5.k
474 """ % locals()))
475
476 test.must_match(['work4', 'src', 'file1'], "file1.in 2\n")
477 test.must_match(['work4', 'src', 'file2'], """\
478 file2.k 1 line 1
479 xxx 1
480 yyy 2
481 file2.k 1 line 4
482 """)
483 test.must_match(['work4', 'src', 'file3'], "xxx 1\nyyy 2\nzzz 2\n")
484 test.must_match(['work4', 'src', 'file5'], """\
485 file5.k 1 line 1
486 aaa 1
487 bbb.k 2
488 ccc 1
489 file5.k 1 line 4
490 """)
491
492
493
494 test.write(['work5', 'SConstruct'], """\
495 import shutil
496
497 env = Environment()
498 mode = int(ARGUMENTS.get('mode'))
499 if mode:
500     def DifferentCopy(target, source, env):
501         tgt = str(target[0])
502         src = str(source[0])
503         shutil.copy(src, tgt)
504     def AltCopyStage2(target, source, env):
505         pass
506     MyCopy = Builder(action = [DifferentCopy, AltCopyStage2])
507
508     def ChangingCopy(target, source, env):
509         tgt = str(target[0])
510         src = str(source[0])
511         shutil.copy(src, tgt)
512     ChangingCopy = Builder(action = ChangingCopy)
513 else:
514     MyCopy = Builder(action = Copy('$TARGET', '$SOURCE'))
515     def ChangingCopy(target, source, env):
516         tgt = str(target[0].abspath)
517         src = str(source[0].abspath)
518         shutil.copy(src, tgt)
519     ChangingCopy = Builder(action = ChangingCopy)
520
521 env['BUILDERS']['MyCopy'] = MyCopy
522 env['BUILDERS']['ChangingCopy'] = ChangingCopy
523
524 env.MyCopy('f1.out', 'f1.in')
525 env.ChangingCopy('f2.out', 'f2.in')
526 """)
527
528 test.write(['work5', 'f1.in'], "work5/f1.in\n")
529 test.write(['work5', 'f2.in'], "work5/f2.in\n")
530
531 test.run(chdir = 'work5', arguments = "mode=0 .")
532
533 test.must_match(['work5', 'f1.out'], "work5/f1.in\n")
534 test.must_match(['work5', 'f2.out'], "work5/f2.in\n")
535
536 test.run(chdir = 'work5',
537          arguments = "--debug=explain mode=1 .",
538          stdout = test.wrap_stdout("""\
539 scons: rebuilding `f1.out' because the build action changed:
540                old: Copy("$TARGET", "$SOURCE")
541                new: DifferentCopy(target, source, env)
542                     AltCopyStage2(target, source, env)
543 DifferentCopy(["f1.out"], ["f1.in"])
544 AltCopyStage2(["f1.out"], ["f1.in"])
545 scons: rebuilding `f2.out' because the contents of the build action changed
546                action: ChangingCopy(target, source, env)
547 ChangingCopy(["f2.out"], ["f2.in"])
548 """))
549
550
551
552 test.pass_test()