Escape path names to fix regular expression matches on Windows
[scons.git] / test / NodeOps.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 # This test is used to verify that the Buildability of a set of nodes
28 # is unaffected by various querying operations on those nodes:
29 #
30 # 1) Calling exists() on a Node (e.g. from find_file) in a VariantDir
31 #    will cause that node to be duplicated into the builddir.
32 #    However, this should *not* occur during a dryrun (-n).  When not
33 #    performed during a dryrun, this should not affect buildability.
34 # 2) Calling is_derived() should not affect buildability.
35
36 import sys
37 import TestSCons
38 import os
39 import string
40
41 _exe = TestSCons._exe
42 lib_ = TestSCons.lib_
43 _lib = TestSCons._lib
44 _obj = TestSCons._obj
45 dll_ = TestSCons.dll_
46 _dll = TestSCons._dll
47     
48 if os.name == 'posix':
49     os.environ['LD_LIBRARY_PATH'] = '.'
50 if string.find(sys.platform, 'irix') > -1:
51     os.environ['LD_LIBRARYN32_PATH'] = '.'
52
53 test = TestSCons.TestSCons()
54
55 test.subdir('bld', 'src', ['src', 'subsrcdir'])
56
57 sconstruct = r"""
58 foo = Environment(SHOBJPREFIX='', WINDOWS_INSERT_DEF=1)
59 foo.Append(SHCXXFLAGS = '-DFOO')
60 bar = Environment(SHOBJPREFIX='', WINDOWS_INSERT_DEF=1)
61 bar.Append(SHCXXFLAGS = '-DBAR')
62 src = Dir('src')
63 VariantDir('bld', src, duplicate=1)
64 Nodes=[]
65 Nodes.extend(foo.SharedObject(target = 'foo%(_obj)s', source = 'prog.cpp'))
66 Nodes.extend(bar.SharedObject(target = 'bar%(_obj)s', source = 'prog.cpp'))
67 SConscript('bld/SConscript', ['Nodes'])
68 if %(_E)s:
69   import os
70   derived = map(lambda N: N.is_derived(), Nodes)
71   real1 = map(lambda N: os.path.exists(str(N)), Nodes)
72   exists = map(lambda N: N.exists(), Nodes)
73   real2 = map(lambda N: os.path.exists(str(N)), Nodes)
74   for N,D,R,E,F in map(None, Nodes, derived, real1, exists, real2):
75     print '%%s: %%s %%s %%s %%s'%%(N,D,R,E,F)
76 foo.SharedLibrary(target = 'foo', source = 'foo%(_obj)s')
77 bar.SharedLibrary(target = 'bar', source = 'bar%(_obj)s')
78
79 fooMain = foo.Clone(LIBS='foo', LIBPATH='.')
80 foo_obj = fooMain.Object(target='foomain', source='main.c')
81 fooMain.Program(target='fooprog', source=foo_obj)
82
83 barMain = bar.Clone(LIBS='bar', LIBPATH='.')
84 bar_obj = barMain.Object(target='barmain', source='main.c')
85 barMain.Program(target='barprog', source=bar_obj)
86
87 gooMain = foo.Clone(LIBS='goo', LIBPATH='bld')
88 goo_obj = gooMain.Object(target='goomain', source='main.c')
89 gooMain.Program(target='gooprog', source=goo_obj)
90 """
91
92 test.write('foo.def', r"""
93 LIBRARY        "foo"
94
95 EXPORTS
96    doIt
97 """)
98
99 test.write('bar.def', r"""
100 LIBRARY        "bar"
101
102 EXPORTS
103    doIt
104 """)
105
106 test.write('prog.cpp', r"""
107 #include <stdio.h>
108
109 extern "C" void
110 doIt()
111 {
112 #ifdef FOO
113         printf("prog.cpp:  FOO\n");
114 #endif
115 #ifdef BAR
116         printf("prog.cpp:  BAR\n");
117 #endif
118 }
119 """)
120
121 sconscript = r"""
122 import os
123 Import('*')
124
125 import __builtin__
126 try:
127     __builtin__.True
128 except AttributeError:
129     __builtin__.True = 1
130     __builtin__.False = 0
131
132 def mycopy(env, source, target):
133     open(str(target[0]),'w').write(open(str(source[0]),'r').read())
134
135 def exists_test(node):
136     before = os.path.exists(str(node))  # doesn't exist yet in VariantDir
137     via_node = node.exists()            # side effect causes copy from src
138     after = os.path.exists(str(node))
139     node.is_derived()
140     import SCons.Script
141     if GetOption('no_exec'):
142         if (before,via_node,after) != (False,False,False):
143             import sys
144             sys.stderr.write('VariantDir exists() populated during dryrun!\n')
145             sys.exit(-2)
146     else:
147         if (before,via_node,after) != (False,True,True):
148             import sys
149             sys.stderr.write('VariantDir exists() population did not occur! (%%s:%%s,%%s,%%s)\n'%%(str(node),before,via_node,after))
150             sys.exit(-2)
151
152 goo = Environment()
153 goo.Append(CFLAGS = '-DFOO')
154 goof_in = File('goof.in')
155 if %(_E)s:
156     exists_test(goof_in)
157 Nodes.append(goof_in)
158 Nodes.extend(goo.Command(target='goof.c', source='goof.in', action=mycopy))
159 boo_src = File('subsrcdir/boo.c')
160 if %(_E)s:
161     exists_test(boo_src)
162 boo_objs = goo.Object(target='subsrcdir/boo%(_obj)s', source = boo_src)
163 Nodes.extend(boo_objs)
164 Nodes.extend(goo.Object(target='goo%(_obj)s',source='goof.c'))
165 goo.Library(target = 'goo', source = ['goo%(_obj)s'] + boo_objs)
166 """
167
168 test.write(['src', 'goof.in'], r"""
169 #include <stdio.h>
170
171 extern char *boo_sub();
172
173 void
174 doIt()
175 {
176 #ifdef FOO
177         printf("prog.cpp:  %s\n", boo_sub());
178 #endif
179 }
180 """)
181
182 test.write(['src', 'subsrcdir', 'boo.c'], r"""
183 char *
184 boo_sub()
185 {
186     return "GOO";
187 }
188 """)
189
190 test.write('main.c', r"""
191 void doIt();
192
193 int
194 main(int argc, char* argv[])
195 {
196     doIt();
197     return 0;
198 }
199 """)
200
201 builddir_srcnodes = [ os.path.join('bld', 'goof.in'),
202                       os.path.join('bld', 'subsrcdir', 'boo.c'),
203                     ]
204
205 sub_build_nodes = [ os.path.join('bld', 'subsrcdir','boo' + _obj),
206                     os.path.join('bld', 'goo' + _obj),
207                     os.path.join('bld', 'goof.c'),
208                     os.path.join('bld', lib_ + 'goo' + _lib),
209 ]
210
211 build_nodes = ['fooprog' + _exe,
212                dll_ + 'foo' + _dll,
213                'foo' + _obj,
214                'barprog' + _exe,
215                dll_ + 'bar' + _dll,
216                'bar' + _obj,
217
218                'gooprog' + _exe,
219
220                ] + builddir_srcnodes + sub_build_nodes
221
222 def cleanup_test():
223     "cleanup after running a test"
224     for F in builddir_srcnodes:
225         test.unlink(F)  # will be repopulated during clean operation
226     test.run(arguments = '-c')
227     for F in builddir_srcnodes:
228         test.unlink(F)
229     for name in build_nodes:
230         test.must_not_exist(test.workpath(name))
231
232
233 ### First pass, make sure everything goes quietly
234
235 for name in build_nodes:
236     test.must_not_exist(test.workpath(name))
237
238 _E=0
239 test.write('SConstruct', sconstruct % locals() )
240 test.write(['src', 'SConscript'], sconscript % locals() )
241
242 test.run(arguments = '.',
243          stderr=TestSCons.noisy_ar,
244          match=TestSCons.match_re_dotall)
245
246 test.run(program = test.workpath('fooprog'), stdout = "prog.cpp:  FOO\n")
247 test.run(program = test.workpath('barprog'), stdout = "prog.cpp:  BAR\n")
248 test.run(program = test.workpath('gooprog'), stdout = "prog.cpp:  GOO\n")
249
250 for name in build_nodes:
251     test.must_exist(test.workpath(name))
252
253 cleanup_test()
254
255 ### Next pass: add internal Node ops that may have side effects to
256 ### ensure that those side-effects don't interfere with building
257
258 for name in build_nodes:
259     test.must_not_exist(test.workpath(name))
260
261 _E=1
262 test.write('SConstruct', sconstruct % locals() )
263 test.write(['src', 'SConscript'], sconscript % locals() )
264
265 test.run(arguments = '.',
266          stderr=TestSCons.noisy_ar,
267          match=TestSCons.match_re_dotall)
268
269 test.run(program = test.workpath('fooprog'), stdout = "prog.cpp:  FOO\n")
270 test.run(program = test.workpath('barprog'), stdout = "prog.cpp:  BAR\n")
271 test.run(program = test.workpath('gooprog'), stdout = "prog.cpp:  GOO\n")
272
273 for name in build_nodes:
274     test.must_exist(test.workpath(name))
275
276 cleanup_test()
277
278 ### Next pass: try a dry-run first and verify that it doesn't change
279 ### the buildability.
280
281 for name in build_nodes:
282     test.must_not_exist(test.workpath(name))
283
284 _E=1
285 test.write('SConstruct', sconstruct % locals() )
286 test.write(['src', 'SConscript'], sconscript % locals() )
287
288 test.run(arguments = '-n .',
289          stderr=TestSCons.noisy_ar,
290          match=TestSCons.match_re_dotall)
291
292 for name in build_nodes:
293     test.must_not_exist(test.workpath(name))
294
295 test.run(arguments = '.',
296          stderr=TestSCons.noisy_ar,
297          match=TestSCons.match_re_dotall)
298
299 test.run(program = test.workpath('fooprog'), stdout = "prog.cpp:  FOO\n")
300 test.run(program = test.workpath('barprog'), stdout = "prog.cpp:  BAR\n")
301 test.run(program = test.workpath('gooprog'), stdout = "prog.cpp:  GOO\n")
302
303 for name in build_nodes:
304     test.must_exist(test.workpath(name))
305
306 cleanup_test()
307
308 ### Next pass: do an up-build from a VariantDir src
309
310
311 for name in build_nodes:
312     test.must_not_exist(test.workpath(name))
313
314 _E=0
315 test.write('SConstruct', sconstruct % locals() )
316 test.write(['src', 'SConscript'], sconscript % locals() )
317
318 test.run(chdir='src', arguments = '-u',
319          stderr=TestSCons.noisy_ar,
320          match=TestSCons.match_re_dotall)
321
322 for name in build_nodes:
323     if name in sub_build_nodes or name in builddir_srcnodes:
324         test.must_exist(test.workpath(name))
325     else:
326         test.must_not_exist(test.workpath(name))
327
328 cleanup_test()
329
330 ### Next pass: do an up-build from a VariantDir src with Node Ops
331 ### side-effects
332
333 for name in build_nodes:
334     test.must_not_exist(test.workpath(name))
335
336 _E=1
337 test.write('SConstruct', sconstruct % locals() )
338 test.write(['src', 'SConscript'], sconscript % locals() )
339
340 test.run(chdir='src', arguments = '-u',
341          stderr=TestSCons.noisy_ar,
342          match=TestSCons.match_re_dotall)
343
344 for name in build_nodes:
345     if name in sub_build_nodes or name in builddir_srcnodes:
346         test.must_exist(test.workpath(name))
347     else:
348         test.must_not_exist(test.workpath(name))
349
350 cleanup_test()
351
352 test.pass_test()