06e69339b4d37aff08e280a5adc226e2dc64308c
[scons.git] / test / CPPPATH.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
28 import sys
29 import TestSCons
30
31 if sys.platform == 'win32':
32     _exe = '.exe'
33 else:
34     _exe = ''
35
36 prog = 'prog' + _exe
37 subdir_prog = os.path.join('subdir', 'prog' + _exe)
38 variant_prog = os.path.join('variant', 'prog' + _exe)
39
40 args = prog + ' ' + subdir_prog + ' ' + variant_prog
41
42 test = TestSCons.TestSCons()
43
44 test.subdir('include', 'subdir', ['subdir', 'include'], 'inc2')
45
46 test.write('SConstruct', """
47 env = Environment(CPPPATH = ['$FOO'],
48                   FOO='include')
49 obj = env.Object(target='foobar/prog', source='subdir/prog.c')
50 env.Program(target='prog', source=obj)
51 SConscript('subdir/SConscript', "env")
52
53 BuildDir('variant', 'subdir', 0)
54 include = Dir('include')
55 env = Environment(CPPPATH=[include])
56 SConscript('variant/SConscript', "env")
57 """)
58
59 test.write(['subdir', 'SConscript'],
60 """
61 Import("env")
62 env.Program(target='prog', source='prog.c')
63 """)
64
65 test.write(['include', 'foo.h'],
66 r"""
67 #define FOO_STRING "include/foo.h 1\n"
68 #include <bar.h>
69 """)
70
71 test.write(['include', 'bar.h'],
72 r"""
73 #define BAR_STRING "include/bar.h 1\n"
74 """)
75
76 test.write(['subdir', 'prog.c'],
77 r"""
78 #include <foo.h>
79 #include <stdio.h>
80
81 int
82 main(int argc, char *argv[])
83 {
84     argv[argc++] = "--";
85     printf("subdir/prog.c\n");
86     printf(FOO_STRING);
87     printf(BAR_STRING);
88     return 0;
89 }
90 """)
91
92 test.write(['subdir', 'include', 'foo.h'],
93 r"""
94 #define FOO_STRING "subdir/include/foo.h 1\n"
95 #include "bar.h"
96 """)
97
98 test.write(['subdir', 'include', 'bar.h'],
99 r"""
100 #define BAR_STRING "subdir/include/bar.h 1\n"
101 """)
102
103
104
105 test.run(arguments = args)
106
107 test.run(program = test.workpath(prog),
108          stdout = "subdir/prog.c\ninclude/foo.h 1\ninclude/bar.h 1\n")
109
110 test.run(program = test.workpath(subdir_prog),
111          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
112
113 test.run(program = test.workpath(variant_prog),
114          stdout = "subdir/prog.c\ninclude/foo.h 1\ninclude/bar.h 1\n")
115
116 # Make sure we didn't duplicate the source file in the variant subdirectory.
117 test.fail_test(os.path.exists(test.workpath('variant', 'prog.c')))
118
119 test.up_to_date(arguments = args)
120
121 test.write(['include', 'foo.h'],
122 r"""
123 #define FOO_STRING "include/foo.h 2\n"
124 #include "bar.h"
125 """)
126
127 test.run(arguments = args)
128
129 test.run(program = test.workpath(prog),
130          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
131
132 test.run(program = test.workpath(subdir_prog),
133          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
134
135 test.run(program = test.workpath(variant_prog),
136          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 1\n")
137
138 # Make sure we didn't duplicate the source file in the variant subdirectory.
139 test.fail_test(os.path.exists(test.workpath('variant', 'prog.c')))
140
141 test.up_to_date(arguments = args)
142
143 #
144 test.write(['include', 'bar.h'],
145 r"""
146 #define BAR_STRING "include/bar.h 2\n"
147 """)
148
149 test.run(arguments = args)
150
151 test.run(program = test.workpath(prog),
152          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 2\n")
153
154 test.run(program = test.workpath(subdir_prog),
155          stdout = "subdir/prog.c\nsubdir/include/foo.h 1\nsubdir/include/bar.h 1\n")
156
157 test.run(program = test.workpath(variant_prog),
158          stdout = "subdir/prog.c\ninclude/foo.h 2\ninclude/bar.h 2\n")
159
160 # Make sure we didn't duplicate the source file in the variant subdirectory.
161 test.fail_test(os.path.exists(test.workpath('variant', 'prog.c')))
162
163 test.up_to_date(arguments = args)
164
165 # Change CPPPATH and make sure we don't rebuild because of it.
166 test.write('SConstruct', """
167 env = Environment(CPPPATH = Split('inc2 include'))
168 obj = env.Object(target='foobar/prog', source='subdir/prog.c')
169 env.Program(target='prog', source=obj)
170 SConscript('subdir/SConscript', "env")
171
172 BuildDir('variant', 'subdir', 0)
173 include = Dir('include')
174 env = Environment(CPPPATH=['inc2', include])
175 SConscript('variant/SConscript', "env")
176 """)
177
178 test.up_to_date(arguments = args)
179
180 #
181 test.write(['inc2', 'foo.h'],
182 r"""
183 #define FOO_STRING "inc2/foo.h 1\n"
184 #include <bar.h>
185 """)
186
187 test.run(arguments = args)
188
189 test.run(program = test.workpath(prog),
190          stdout = "subdir/prog.c\ninc2/foo.h 1\ninclude/bar.h 2\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 2\n")
197
198 test.up_to_date(arguments = args)
199
200 # Check that a null-string CPPPATH doesn't blow up.
201 test.write('SConstruct', """
202 env = Environment(CPPPATH = '')
203 env.Library('foo', source = '')
204 """)
205
206 test.run(arguments = '.')
207
208 test.pass_test()