Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / implicit-cache / basic.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
29 import TestSCons
30
31 _exe = TestSCons._exe
32 _obj = TestSCons._obj
33
34 prog = 'prog' + _exe
35 subdir_prog = os.path.join('subdir', 'prog' + _exe)
36 variant_prog = os.path.join('variant', 'prog' + _exe)
37
38 args = prog + ' ' + subdir_prog + ' ' + variant_prog
39
40 test = TestSCons.TestSCons()
41
42 test.subdir('include', 'subdir', ['subdir', 'include'], 'inc2')
43
44 test.write('SConstruct', """
45 env = Environment(CPPPATH = Split('inc2 include'))
46 obj = env.Object(target='prog', source='subdir/prog.c')
47 env.Program(target='prog', source=obj)
48 SConscript('subdir/SConscript', "env")
49
50 BuildDir('variant', 'subdir', 0)
51 include = Dir('include')
52 env = Environment(CPPPATH=['inc2', include])
53 SConscript('variant/SConscript', "env")
54
55 def copy(target, source, env):
56     open(str(target[0]), 'wt').write(open(str(source[0]), 'rt').read())
57 nodep = env.Command('nodeps.c', 'nodeps.in', action=copy)
58 env.Program('nodeps', 'nodeps.c')
59
60 env.Object(['one', 'two'], ['one.c'])
61 """)
62
63 test.write(['subdir', 'SConscript'],
64 """
65 Import("env")
66 env.Program(target='prog', source='prog.c')
67 """)
68
69 test.write('nodeps.in', r"""
70 int
71 main(int argc, char *argv[])
72 {
73     argv[argc++] = "--";
74     return 0;
75 }
76 """)
77
78
79 test.write(['include', 'foo.h'], r"""
80 #define FOO_STRING "include/foo.h 1\n"
81 #include <bar.h>
82 """)
83
84 test.write(['include', 'bar.h'], r"""
85 #define BAR_STRING "include/bar.h 1\n"
86 """)
87
88 test.write(['include', 'baz.h'], r"""
89 #define BAZ_STRING "include/baz.h 1\n"
90 """)
91
92 test.write(['subdir', 'prog.c'], r"""
93 #include <foo.h>
94 #include <stdio.h>
95
96 int
97 main(int argc, char *argv[])
98 {
99     argv[argc++] = "--";
100     printf("subdir/prog.c\n");
101     printf(FOO_STRING);
102     printf(BAR_STRING);
103     return 0;
104 }
105 """)
106
107 test.write(['subdir', 'include', 'foo.h'], r"""
108 #define FOO_STRING "subdir/include/foo.h 1\n"
109 #include "bar.h"
110 """)
111
112 test.write(['subdir', 'include', 'bar.h'], r"""
113 #define BAR_STRING "subdir/include/bar.h 1\n"
114 """)
115
116 test.write('one.c' , r"""
117 #include <foo.h>
118
119 void one(void) { }
120 """)
121
122 test.run(arguments = "--implicit-cache " + args)
123
124 test.run(program = test.workpath(prog),
125          stdout = "subdir/prog.c\ninclude/foo.h 1\ninclude/bar.h 1\n")
126
127 test.run(program = test.workpath(subdir_prog),
128          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
129
130 test.run(program = test.workpath(variant_prog),
131          stdout = "subdir/prog.c\ninclude/foo.h 1\ninclude/bar.h 1\n")
132
133 test.up_to_date(arguments = args)
134
135
136
137 # Make sure implicit dependencies work right when one is modifed:
138 test.write(['include', 'foo.h'], r"""
139 #define FOO_STRING "include/foo.h 2\n"
140 #include "bar.h"
141 """)
142
143 test.run(arguments = "--implicit-cache " + args)
144
145 test.run(program = test.workpath(prog),
146          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
147
148 test.run(program = test.workpath(subdir_prog),
149          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
150
151 test.run(program = test.workpath(variant_prog),
152          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
153
154 test.up_to_date(arguments = args)
155
156
157
158 # Make sure that changing the order of includes causes rebuilds and
159 # doesn't produce redundant rebuilds:
160 test.write(['include', 'foo.h'], r"""
161 #define FOO_STRING "include/foo.h 2\n"
162 #include "bar.h"
163 #include "baz.h"
164 """)
165
166 test.run(arguments = "--implicit-cache " + args)
167
168 test.run(program = test.workpath(prog),
169          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
170
171 test.run(program = test.workpath(subdir_prog),
172          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
173
174 test.run(program = test.workpath(variant_prog),
175          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
176
177 test.up_to_date(arguments = args)
178
179
180
181 test.write(['include', 'foo.h'], r"""
182 #define FOO_STRING "include/foo.h 2\n"
183 #include "baz.h"
184 #include "bar.h"
185 """)
186
187 test.run(arguments = "--implicit-cache " + args)
188
189 test.run(program = test.workpath(prog),
190          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
191
192 test.run(program = test.workpath(subdir_prog),
193          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
194
195 test.run(program = test.workpath(variant_prog),
196          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
197
198 test.up_to_date(arguments = args)
199
200
201
202 # Add inc2/foo.h that should shadow include/foo.h, but
203 # because of implicit dependency caching, scons doesn't
204 # detect this:
205 test.write(['inc2', 'foo.h'], r"""
206 #define FOO_STRING "inc2/foo.h 1\n"
207 #include <bar.h>
208 """)
209
210 test.run(arguments = "--implicit-cache " + args)
211
212 test.run(program = test.workpath(prog),
213          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
214
215 test.run(program = test.workpath(subdir_prog),
216          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
217
218 test.run(program = test.workpath(variant_prog),
219          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
220
221
222
223 # Now modifying include/foo.h should make scons aware of inc2/foo.h
224 test.write(['include', 'foo.h'], r"""
225 #define FOO_STRING "include/foo.h 3\n"
226 #include "bar.h"
227 """)
228
229 test.run(arguments = "--implicit-cache " + args)
230
231 test.run(program = test.workpath(prog),
232          stdout = "subdir/prog.c\ninc2/foo.h 1\ninclude/bar.h 1\n")
233
234 test.run(program = test.workpath(subdir_prog),
235          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
236
237 test.run(program = test.workpath(variant_prog),
238          stdout = "subdir/prog.c\ninclude/foo.h 3\ninclude/bar.h 1\n")
239
240
241
242 # test a file with no dependencies where the source file is generated:
243 test.run(arguments = "--implicit-cache nodeps%s"%_exe)
244
245 test.write('nodeps.in', r"""
246 #include <foo.h>
247
248 int
249 main(int argc, char *argv[])
250 {
251     argv[argc++] = "--";
252     return 0;
253 }
254 """)
255
256 test.run(arguments = "--implicit-cache one%s"%_obj)
257
258
259
260 # Test forcing of implicit caching:
261 test.write(['include', 'foo.h'], r"""
262 #define FOO_STRING "include/foo.h 3\n"
263 #include "bar.h"
264 """)
265
266 test.run(arguments = "--implicit-cache " + args)
267
268 test.write(['include', 'foo.h'], r"""
269 #define FOO_STRING "include/foo.h 3\n"
270 #include "baz.h"
271 #include "bar.h"
272 """)
273
274 test.not_up_to_date(options = "--implicit-deps-unchanged",
275                     arguments = variant_prog)
276
277 test.write(['include', 'baz.h'], r"""
278 #define BAZ_STRING "include/baz.h 2\n"
279 """)
280
281 test.up_to_date(options = "--implicit-deps-unchanged",
282                 arguments = variant_prog)
283
284 test.not_up_to_date(arguments = variant_prog)
285
286
287
288 # Test forcing rescanning:
289 test.write(['include', 'foo.h'], r"""
290 #define FOO_STRING "include/foo.h 3\n"
291 #include "bar.h"
292 """)
293
294 test.run(arguments = "--implicit-cache " + args)
295
296 test.write(['include', 'foo.h'], r"""
297 #define FOO_STRING "include/foo.h 3\n"
298 #include "baz.h"
299 #include "bar.h"
300 """)
301
302 test.not_up_to_date(options = "--implicit-deps-unchanged",
303                     arguments = variant_prog)
304
305 test.write(['include', 'baz.h'], r"""
306 #define BAZ_STRING "include/baz.h 2\n"
307 """)
308
309 test.up_to_date(options = "--implicit-deps-unchanged",
310                 arguments = variant_prog)
311
312 test.not_up_to_date(options = "--implicit-deps-changed",
313                     arguments = variant_prog)
314
315
316
317 test.pass_test()