Merge branch 'master' into jbrill-msvc-detect
[scons.git] / test / NodeOps.py
bloba2db429dbd41cb4c8338adfc42f4d98ac27d3286
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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:
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 """
37 import sys
38 import os
40 import TestSCons
41 from TestSCons import _exe, lib_, _lib, _obj, dll_, _dll
43 if os.name == 'posix':
44 os.environ['LD_LIBRARY_PATH'] = '.'
45 if sys.platform.find('irix') > -1:
46 os.environ['LD_LIBRARYN32_PATH'] = '.'
48 test = TestSCons.TestSCons()
50 test.subdir('bld', 'src', ['src', 'subsrcdir'])
52 sconstruct = r"""
53 DefaultEnvironment(tools=[]) # test speedup
54 foo = Environment(SHOBJPREFIX='', WINDOWS_INSERT_DEF=1)
55 foo.Append(SHCXXFLAGS = '-DFOO')
56 bar = Environment(SHOBJPREFIX='', WINDOWS_INSERT_DEF=1)
57 bar.Append(SHCXXFLAGS = '-DBAR')
58 src = Dir('src')
59 VariantDir('bld', src, duplicate=1)
60 Nodes=[]
61 Nodes.extend(foo.SharedObject(target = 'foo%(_obj)s', source = 'prog.cpp'))
62 Nodes.extend(bar.SharedObject(target = 'bar%(_obj)s', source = 'prog.cpp'))
63 SConscript('bld/SConscript', ['Nodes'])
64 if %(_E)s:
65 import os
66 derived = [N.is_derived() for N in Nodes]
67 real1 = [os.path.exists(str(N)) for N in Nodes]
68 exists = [N.exists() for N in Nodes]
69 real2 = [os.path.exists(str(N)) for N in Nodes]
70 for N,D,R,E,F in zip(Nodes, derived, real1, exists, real2):
71 print('%%s: %%s %%s %%s %%s'%%(N,D,R,E,F))
72 foo.SharedLibrary(target = 'foo', source = 'foo%(_obj)s')
73 bar.SharedLibrary(target = 'bar', source = 'bar%(_obj)s')
75 fooMain = foo.Clone(LIBS='foo', LIBPATH='.')
76 foo_obj = fooMain.Object(target='foomain', source='main.c')
77 fooMain.Program(target='fooprog', source=foo_obj)
79 barMain = bar.Clone(LIBS='bar', LIBPATH='.')
80 bar_obj = barMain.Object(target='barmain', source='main.c')
81 barMain.Program(target='barprog', source=bar_obj)
83 gooMain = foo.Clone(LIBS='goo', LIBPATH='bld')
84 goo_obj = gooMain.Object(target='goomain', source='main.c')
85 gooMain.Program(target='gooprog', source=goo_obj)
86 """
88 test.write('foo.def', r"""
89 LIBRARY "foo"
91 EXPORTS
92 doIt
93 """)
95 test.write('bar.def', r"""
96 LIBRARY "bar"
98 EXPORTS
99 doIt
100 """)
102 test.write('prog.cpp', r"""
103 #include <stdio.h>
105 extern "C" void
106 doIt()
108 #ifdef FOO
109 printf("prog.cpp: FOO\n");
110 #endif
111 #ifdef BAR
112 printf("prog.cpp: BAR\n");
113 #endif
115 """)
117 sconscript = r"""
118 import os
119 Import('*')
121 def mycopy(env, source, target):
122 with open(str(target[0]), 'wt') as fo, open(str(source[0]), 'rt') as fi:
123 fo.write(fi.read())
125 def exists_test(node):
126 before = os.path.exists(str(node)) # doesn't exist yet in VariantDir
127 via_node = node.exists() # side effect causes copy from src
128 after = os.path.exists(str(node))
129 node.is_derived()
130 import SCons.Script
131 if GetOption('no_exec'):
132 if (before,via_node,after) != (False,False,False):
133 import sys
134 sys.stderr.write('VariantDir exists() populated during dryrun!\n')
135 sys.exit(-2)
136 else:
137 if (before,via_node,after) != (False,True,True):
138 import sys
139 sys.stderr.write('VariantDir exists() population did not occur! (%%s:%%s,%%s,%%s)\n'%%(str(node),before,via_node,after))
140 sys.exit(-2)
142 goo = Environment()
143 goo.Append(CFLAGS = '-DFOO')
144 goof_in = File('goof.in')
145 if %(_E)s:
146 exists_test(goof_in)
147 Nodes.append(goof_in)
148 Nodes.extend(goo.Command(target='goof.c', source='goof.in', action=mycopy))
149 boo_src = File('subsrcdir/boo.c')
150 if %(_E)s:
151 exists_test(boo_src)
152 boo_objs = goo.Object(target='subsrcdir/boo%(_obj)s', source = boo_src)
153 Nodes.extend(boo_objs)
154 Nodes.extend(goo.Object(target='goo%(_obj)s',source='goof.c'))
155 goo.Library(target = 'goo', source = ['goo%(_obj)s'] + boo_objs)
158 test.write(['src', 'goof.in'], r"""
159 #include <stdio.h>
161 extern char *boo_sub();
163 void
164 doIt()
166 #ifdef FOO
167 printf("prog.cpp: %s\n", boo_sub());
168 #endif
170 """)
172 test.write(['src', 'subsrcdir', 'boo.c'], r"""
173 char *
174 boo_sub()
176 return "GOO";
178 """)
180 test.write('main.c', r"""
181 void doIt();
184 main(int argc, char* argv[])
186 doIt();
187 return 0;
189 """)
191 builddir_srcnodes = [ os.path.join('bld', 'goof.in'),
192 os.path.join('bld', 'subsrcdir', 'boo.c'),
195 sub_build_nodes = [ os.path.join('bld', 'subsrcdir','boo' + _obj),
196 os.path.join('bld', 'goo' + _obj),
197 os.path.join('bld', 'goof.c'),
198 os.path.join('bld', lib_ + 'goo' + _lib),
201 build_nodes = ['fooprog' + _exe,
202 dll_ + 'foo' + _dll,
203 'foo' + _obj,
204 'barprog' + _exe,
205 dll_ + 'bar' + _dll,
206 'bar' + _obj,
208 'gooprog' + _exe,
210 ] + builddir_srcnodes + sub_build_nodes
212 def cleanup_test():
213 """cleanup after running a test"""
214 for F in builddir_srcnodes:
215 test.unlink(F) # will be repopulated during clean operation
216 test.run(arguments = '-c')
217 for F in builddir_srcnodes:
218 test.unlink(F)
219 for name in build_nodes:
220 test.must_not_exist(test.workpath(name))
223 ### First pass, make sure everything goes quietly
225 for name in build_nodes:
226 test.must_not_exist(test.workpath(name))
228 _E=0
229 test.write('SConstruct', sconstruct % locals() )
230 test.write(['src', 'SConscript'], sconscript % locals() )
232 test.run(arguments = '.',
233 stderr=TestSCons.noisy_ar,
234 match=TestSCons.match_re_dotall)
236 test.run(program = test.workpath('fooprog'), stdout = "prog.cpp: FOO\n")
237 test.run(program = test.workpath('barprog'), stdout = "prog.cpp: BAR\n")
238 test.run(program = test.workpath('gooprog'), stdout = "prog.cpp: GOO\n")
240 for name in build_nodes:
241 test.must_exist(test.workpath(name))
243 cleanup_test()
245 ### Next pass: add internal Node ops that may have side effects to
246 ### ensure that those side-effects don't interfere with building
248 for name in build_nodes:
249 test.must_not_exist(test.workpath(name))
251 _E=1
252 test.write('SConstruct', sconstruct % locals() )
253 test.write(['src', 'SConscript'], sconscript % locals() )
255 test.run(arguments = '.',
256 stderr=TestSCons.noisy_ar,
257 match=TestSCons.match_re_dotall)
259 test.run(program = test.workpath('fooprog'), stdout = "prog.cpp: FOO\n")
260 test.run(program = test.workpath('barprog'), stdout = "prog.cpp: BAR\n")
261 test.run(program = test.workpath('gooprog'), stdout = "prog.cpp: GOO\n")
263 for name in build_nodes:
264 test.must_exist(test.workpath(name))
266 cleanup_test()
268 ### Next pass: try a dry-run first and verify that it doesn't change
269 ### the buildability.
271 for name in build_nodes:
272 test.must_not_exist(test.workpath(name))
274 _E=1
275 test.write('SConstruct', sconstruct % locals() )
276 test.write(['src', 'SConscript'], sconscript % locals() )
278 test.run(arguments = '-n .',
279 stderr=TestSCons.noisy_ar,
280 match=TestSCons.match_re_dotall)
282 for name in build_nodes:
283 test.must_not_exist(test.workpath(name))
285 test.run(arguments = '.',
286 stderr=TestSCons.noisy_ar,
287 match=TestSCons.match_re_dotall)
289 test.run(program = test.workpath('fooprog'), stdout = "prog.cpp: FOO\n")
290 test.run(program = test.workpath('barprog'), stdout = "prog.cpp: BAR\n")
291 test.run(program = test.workpath('gooprog'), stdout = "prog.cpp: GOO\n")
293 for name in build_nodes:
294 test.must_exist(test.workpath(name))
296 cleanup_test()
298 ### Next pass: do an up-build from a VariantDir src
301 for name in build_nodes:
302 test.must_not_exist(test.workpath(name))
304 _E=0
305 test.write('SConstruct', sconstruct % locals() )
306 test.write(['src', 'SConscript'], sconscript % locals() )
308 test.run(chdir='src', arguments = '-u',
309 stderr=TestSCons.noisy_ar,
310 match=TestSCons.match_re_dotall)
312 for name in build_nodes:
313 if name in sub_build_nodes or name in builddir_srcnodes:
314 test.must_exist(test.workpath(name))
315 else:
316 test.must_not_exist(test.workpath(name))
318 cleanup_test()
320 ### Next pass: do an up-build from a VariantDir src with Node Ops
321 ### side-effects
323 for name in build_nodes:
324 test.must_not_exist(test.workpath(name))
326 _E=1
327 test.write('SConstruct', sconstruct % locals() )
328 test.write(['src', 'SConscript'], sconscript % locals() )
330 test.run(chdir='src', arguments = '-u',
331 stderr=TestSCons.noisy_ar,
332 match=TestSCons.match_re_dotall)
334 for name in build_nodes:
335 if name in sub_build_nodes or name in builddir_srcnodes:
336 test.must_exist(test.workpath(name))
337 else:
338 test.must_not_exist(test.workpath(name))
340 cleanup_test()
342 test.pass_test()
344 # Local Variables:
345 # tab-width:4
346 # indent-tabs-mode:nil
347 # End:
348 # vim: set expandtab tabstop=4 shiftwidth=4: