f3085796d33592defc61be2bf08b1aaca8a6f9d6
[scons.git] / test / BuildDir / BuildDir.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 import os.path
28 import string
29 import sys
30 import time
31 import TestSCons
32
33 _exe = TestSCons._exe
34 fortran_runtime = TestSCons.fortran_lib
35
36 test = TestSCons.TestSCons()
37
38 fortran = test.detect('FORTRAN')
39
40 foo11 = test.workpath('work1', 'build', 'var1', 'foo1' + _exe)
41 foo12 = test.workpath('work1', 'build', 'var1', 'foo2' + _exe)
42 foo21 = test.workpath('work1', 'build', 'var2', 'foo1' + _exe)
43 foo22 = test.workpath('work1', 'build', 'var2', 'foo2' + _exe)
44 foo31 = test.workpath('work1', 'build', 'var3', 'foo1' + _exe)
45 foo32 = test.workpath('work1', 'build', 'var3', 'foo2' + _exe)
46 foo41 = test.workpath('work1', 'build', 'var4', 'foo1' + _exe)
47 foo42 = test.workpath('work1', 'build', 'var4', 'foo2' + _exe)
48 foo51 = test.workpath('build', 'var5', 'foo1' + _exe)
49 foo52 = test.workpath('build', 'var5', 'foo2' + _exe)
50
51 bar11 = test.workpath('work1', 'build', 'var1', 'bar1' + _exe)
52 bar12 = test.workpath('work1', 'build', 'var1', 'bar2' + _exe)
53 bar21 = test.workpath('work1', 'build', 'var2', 'bar1' + _exe)
54 bar22 = test.workpath('work1', 'build', 'var2', 'bar2' + _exe)
55 bar31 = test.workpath('work1', 'build', 'var3', 'bar1' + _exe)
56 bar32 = test.workpath('work1', 'build', 'var3', 'bar2' + _exe)
57 bar41 = test.workpath('work1', 'build', 'var4', 'bar1' + _exe)
58 bar42 = test.workpath('work1', 'build', 'var4', 'bar2' + _exe)
59 bar51 = test.workpath('build', 'var5', 'bar1' + _exe)
60 bar52 = test.workpath('build', 'var5', 'bar2' + _exe)
61
62 test.subdir('work1', 'work2', 'work3')
63
64 test.write(['work1', 'SConstruct'], """
65 src = Dir('src')
66 var2 = Dir('build/var2')
67 var3 = Dir('build/var3')
68 var4 = Dir('build/var4')
69 var5 = Dir('../build/var5')
70 var6 = Dir('../build/var6')
71
72 env = Environment(BUILD = 'build', SRC = 'src')
73
74 BuildDir('build/var1', src)
75 BuildDir(var2, src)
76 BuildDir(var3, src, duplicate=0)
77 env.BuildDir("$BUILD/var4", "$SRC", duplicate=0)
78 BuildDir(var5, src, duplicate=0)
79 BuildDir(var6, src)
80
81 env = Environment(CPPPATH='#src', FORTRANPATH='#src')
82 SConscript('build/var1/SConscript', "env")
83 SConscript('build/var2/SConscript', "env")
84
85 env = Environment(CPPPATH=src, FORTRANPATH=src)
86 SConscript('build/var3/SConscript', "env")
87 SConscript(File('SConscript', var4), "env")
88
89 env = Environment(CPPPATH='.', FORTRANPATH='.')
90 SConscript('../build/var5/SConscript', "env")
91 SConscript('../build/var6/SConscript', "env")
92 """)
93
94 test.subdir(['work1', 'src'])
95 test.write(['work1', 'src', 'SConscript'], """
96 import os
97 import os.path
98
99 def buildIt(target, source, env):
100     if not os.path.exists('build'):
101         os.mkdir('build')
102     f1=open(str(source[0]), 'r')
103     f2=open(str(target[0]), 'w')
104     f2.write(f1.read())
105     f2.close()
106     f1.close()
107     return 0
108 Import("env")
109 env.Command(target='f2.c', source='f2.in', action=buildIt)
110 env.Program(target='foo2', source='f2.c')
111 env.Program(target='foo1', source='f1.c')
112 env.Command(target='f3.h', source='f3h.in', action=buildIt)
113 env.Command(target='f4.h', source='f4h.in', action=buildIt)
114 env.Command(target='f4.c', source='f4.in', action=buildIt)
115
116 env2=env.Clone(CPPPATH='.')
117 env2.Program(target='foo3', source='f3.c')
118 env2.Program(target='foo4', source='f4.c')
119
120 try:
121     fortran = env.subst('$FORTRAN')
122 except:
123     fortran = None
124
125 if fortran and env.Detect(fortran):
126     env.Command(target='b2.f', source='b2.in', action=buildIt)
127     env.Clone(LIBS = %s).Program(target='bar2', source='b2.f')
128     env.Clone(LIBS = %s).Program(target='bar1', source='b1.f')
129 """ % (fortran_runtime, fortran_runtime))
130
131 test.write(['work1', 'src', 'f1.c'], r"""
132 #include <stdio.h>
133 #include <stdlib.h>
134
135 #include "f1.h"
136
137 int
138 main(int argc, char *argv[])
139 {
140         argv[argc++] = "--";
141         printf(F1_STR);
142         exit (0);
143 }
144 """)
145
146 test.write(['work1', 'src', 'f2.in'], r"""
147 #include <stdio.h>
148 #include <stdlib.h>
149
150 #include "f2.h"
151
152 int
153 main(int argc, char *argv[])
154 {
155         argv[argc++] = "--";
156         printf(F2_STR);
157         exit (0);
158 }
159 """)
160
161 test.write(['work1', 'src', 'f3.c'], r"""
162 #include <stdio.h>
163 #include <stdlib.h>
164
165 #include "f3.h"
166
167 int
168 main(int argc, char *argv[])
169 {
170         argv[argc++] = "--";
171         printf(F3_STR);
172         exit (0);
173 }
174 """)
175
176 test.write(['work1', 'src', 'f4.in'], r"""
177 #include <stdio.h>
178 #include <stdlib.h>
179
180 #include "f4.h"
181
182 int
183 main(int argc, char *argv[])
184 {
185         argv[argc++] = "--";
186         printf(F4_STR);
187         exit (0);
188 }
189 """)
190
191 test.write(['work1', 'src', 'f1.h'], r"""
192 #define F1_STR "f1.c\n"
193 """)
194
195 test.write(['work1', 'src', 'f2.h'], r"""
196 #define F2_STR "f2.c\n"
197 """)
198
199 test.write(['work1', 'src', 'f3h.in'], r"""
200 #define F3_STR "f3.c\n"
201 """)
202
203 test.write(['work1', 'src', 'f4h.in'], r"""
204 #define F4_STR "f4.c\n"
205 """)
206
207 test.write(['work1', 'src', 'b1.f'], r"""
208       PROGRAM FOO
209       INCLUDE 'b1.for'
210       STOP
211       END
212 """)
213
214 test.write(['work1', 'src', 'b2.in'], r"""
215       PROGRAM FOO
216       INCLUDE 'b2.for'
217       STOP
218       END
219 """)
220
221 test.write(['work1', 'src', 'b1.for'], r"""
222       PRINT *, 'b1.for'
223 """)
224
225 test.write(['work1', 'src', 'b2.for'], r"""
226       PRINT *, 'b2.for'
227 """)
228
229 # Some releases of freeBSD seem to have library complaints about
230 # tempnam().  Filter out these annoying messages before checking for
231 # error output.
232 def blank_output(err):
233     if not err:
234         return 1
235     stderrlines = filter(lambda l: l, string.split(err, '\n'))
236     msg = "warning: tempnam() possibly used unsafely"
237     stderrlines = filter(lambda l, msg=msg: string.find(l, msg) == -1,
238                          stderrlines)
239     return len(stderrlines) == 0
240
241 test.run(chdir='work1', arguments = '. ../build', stderr=None)
242
243 test.fail_test(not blank_output(test.stderr()))
244
245 test.run(program = foo11, stdout = "f1.c\n")
246 test.run(program = foo12, stdout = "f2.c\n")
247 test.run(program = foo21, stdout = "f1.c\n")
248 test.run(program = foo22, stdout = "f2.c\n")
249 test.run(program = foo31, stdout = "f1.c\n")
250 test.run(program = foo32, stdout = "f2.c\n")
251 test.run(program = foo41, stdout = "f1.c\n")
252 test.run(program = foo42, stdout = "f2.c\n")
253 test.run(program = foo51, stdout = "f1.c\n")
254 test.run(program = foo52, stdout = "f2.c\n")
255
256 if fortran:
257     test.run(program = bar11, stdout = " b1.for\n")
258     test.run(program = bar12, stdout = " b2.for\n")
259     test.run(program = bar21, stdout = " b1.for\n")
260     test.run(program = bar22, stdout = " b2.for\n")
261     test.run(program = bar31, stdout = " b1.for\n")
262     test.run(program = bar32, stdout = " b2.for\n")
263     test.run(program = bar41, stdout = " b1.for\n")
264     test.run(program = bar42, stdout = " b2.for\n")
265     test.run(program = bar51, stdout = " b1.for\n")
266     test.run(program = bar52, stdout = " b2.for\n")
267
268 test.run(chdir='work1', arguments='. ../build', stdout=test.wrap_stdout("""\
269 scons: `.' is up to date.
270 scons: `%s' is up to date.
271 """ % test.workpath('build')))
272
273 import os
274 import stat
275 def equal_stats(x,y):
276     x = os.stat(x)
277     y = os.stat(y)
278     return (stat.S_IMODE(x[stat.ST_MODE]) == stat.S_IMODE(y[stat.ST_MODE]) and
279             x[stat.ST_MTIME] ==  y[stat.ST_MTIME])
280
281 # Make sure we did duplicate the source files in build/var2,
282 # and that their stats are the same:
283 test.must_exist(['work1', 'build', 'var2', 'f1.c'])
284 test.must_exist(['work1', 'build', 'var2', 'f2.in'])
285 test.fail_test(not equal_stats(test.workpath('work1', 'build', 'var2', 'f1.c'), test.workpath('work1', 'src', 'f1.c')))
286 test.fail_test(not equal_stats(test.workpath('work1', 'build', 'var2', 'f2.in'), test.workpath('work1', 'src', 'f2.in')))
287  
288 # Make sure we didn't duplicate the source files in build/var3.
289 test.must_not_exist(['work1', 'build', 'var3', 'f1.c'])
290 test.must_not_exist(['work1', 'build', 'var3', 'f2.in'])
291 test.must_not_exist(['work1', 'build', 'var3', 'b1.f'])
292 test.must_not_exist(['work1', 'build', 'var3', 'b2.in'])
293
294 # Make sure we didn't duplicate the source files in build/var4.
295 test.must_not_exist(['work1', 'build', 'var4', 'f1.c'])
296 test.must_not_exist(['work1', 'build', 'var4', 'f2.in'])
297 test.must_not_exist(['work1', 'build', 'var4', 'b1.f'])
298 test.must_not_exist(['work1', 'build', 'var4', 'b2.in'])
299
300 # Make sure we didn't duplicate the source files in build/var5.
301 test.must_not_exist(['build', 'var5', 'f1.c'])
302 test.must_not_exist(['build', 'var5', 'f2.in'])
303 test.must_not_exist(['build', 'var5', 'b1.f'])
304 test.must_not_exist(['build', 'var5', 'b2.in'])
305
306 # verify that header files in the source directory are scanned properly:
307 test.write(['work1', 'src', 'f1.h'], r"""
308 #define F1_STR "f1.c 2\n"
309 """)
310
311 test.write(['work1', 'src', 'f3h.in'], r"""
312 #define F3_STR "f3.c 2\n"
313 """)
314
315 test.write(['work1', 'src', 'f4h.in'], r"""
316 #define F4_STR "f4.c 2\n"
317 """)
318
319 test.run(chdir='work1', arguments = '../build/var5', stderr=None)
320
321 test.fail_test(not blank_output(test.stderr()))
322
323 test.run(program = foo51, stdout = "f1.c 2\n")
324 test.run(program = test.workpath('build', 'var5', 'foo3' + _exe),
325                                  stdout = "f3.c 2\n")
326 test.run(program = test.workpath('build', 'var5', 'foo4' + _exe),
327                                  stdout = "f4.c 2\n")
328
329 test.run(chdir='work1', arguments='../build/var5', stdout=test.wrap_stdout("""\
330 scons: `%s' is up to date.
331 """ % test.workpath('build', 'var5')))
332
333 #
334 test.write(['work2', 'SConstruct'], """\
335 env = Environment()
336 env.Program('prog.c')
337 """)
338
339 test.write(['work2', 'prog.c'], r"""
340 #include <stdio.h>
341 #include <stdlib.h>
342
343 int
344 main(int argc, char *argv[])
345 {
346         argv[argc++] = "--";
347         printf("work2/prog.c\n");
348         exit (0);
349 }
350 """)
351
352 test.run(chdir='work2', arguments='.')
353
354 test.up_to_date(chdir='work2', arguments='.')
355
356 #
357 test.write(['work2', 'SConstruct'], """\
358 env = Environment()
359 BuildDir('build', '.')
360 Export('env')
361 SConscript('build/SConscript')
362 """)
363
364 test.write(['work2', 'SConscript'], """\
365 Import('env')
366 env.Program('prog.c')
367 """)
368
369 test.run(chdir='work2', arguments='.', stderr=None)
370
371 test.fail_test(not blank_output(test.stderr()))
372
373 test.run(chdir='work2', arguments='.',
374          stdout=test.wrap_stdout("""\
375 scons: building associated BuildDir targets: build
376 scons: `.' is up to date.
377 """))
378
379 test.write( ['work3', 'SConstruct'], """\
380 SConscriptChdir(0)
381 BuildDir('build', '.', duplicate=1 ) 
382 SConscript( 'build/SConscript' )
383 """)
384
385 test.write( ['work3', 'SConscript'], """\
386 import sys
387 headers = ['existing.h', 'non_existing.h']
388 for header in headers:
389     h = File( header )
390     contents = h.get_contents()
391     sys.stderr.write( '%s:%s\\n' % (header, contents))
392 """)
393
394 test.write( ['work3', 'existing.h'], """\
395 /* a header file */\
396 """)
397
398 test.run(chdir='work3',
399          stdout=test.wrap_stdout("""\
400 scons: building associated BuildDir targets: build
401 scons: `.' is up to date.
402 """),
403          stderr="""\
404 existing.h:/* a header file */
405 non_existing.h:
406 """)
407
408 test.pass_test()