Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / CXX / CXX.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 _python_ = TestSCons._python_
32 _exe   = TestSCons._exe
33
34 test = TestSCons.TestSCons()
35
36
37
38 if sys.platform == 'win32':
39
40     test.write('mylink.py', r"""
41 import sys
42 args = sys.argv[1:]
43 while args:
44     a = args[0]
45     if a[0] != '/':
46         break
47     args = args[1:]
48     if a[:5].lower() == '/out:': out = a[5:]
49 infile = open(args[0], 'rb')
50 outfile = open(out, 'wb')
51 for l in infile.readlines():
52     if l[:8] != '/*link*/':
53         outfile.write(l)
54 sys.exit(0)
55 """)
56
57     test.write('myc++.py', r"""
58 import sys
59 args = sys.argv[1:]
60 inf = None
61 while args:
62     a = args[0]
63     args = args[1:]
64     if a[0] != '/':
65         if not inf:
66             inf = a
67         continue
68     if a[:3] == '/Fo': out = a[3:]
69 infile = open(inf, 'rb')
70 outfile = open(out, 'wb')
71 for l in infile.readlines():
72     if l[:7] != '/*c++*/':
73         outfile.write(l)
74 sys.exit(0)
75 """)
76
77 else:
78
79     test.write('mylink.py', r"""
80 import getopt
81 import sys
82 opts, args = getopt.getopt(sys.argv[1:], 'o:')
83 for opt, arg in opts:
84     if opt == '-o': out = arg
85 infile = open(args[0], 'rb')
86 outfile = open(out, 'wb')
87 for l in infile.readlines():
88     if l[:8] != '/*link*/':
89         outfile.write(l)
90 sys.exit(0)
91 """)
92
93     test.write('myc++.py', r"""
94 import getopt
95 import sys
96 opts, args = getopt.getopt(sys.argv[1:], 'co:')
97 for opt, arg in opts:
98     if opt == '-o': out = arg
99 infile = open(args[0], 'rb')
100 outfile = open(out, 'wb')
101 for l in infile.readlines():
102     if l[:7] != '/*c++*/':
103         outfile.write(l)
104 sys.exit(0)
105 """)
106
107 test.write('SConstruct', """
108 env = Environment(LINK = r'%(_python_)s mylink.py',
109                   LINKFLAGS = [],
110                   CXX = r'%(_python_)s myc++.py',
111                   CXXFLAGS = [])
112 env.Program(target = 'test1', source = 'test1.cc')
113 env.Program(target = 'test2', source = 'test2.cpp')
114 env.Program(target = 'test3', source = 'test3.cxx')
115 env.Program(target = 'test4', source = 'test4.c++')
116 env.Program(target = 'test5', source = 'test5.C++')
117 """ % locals())
118
119 test.write('test1.cc', r"""This is a .cc file.
120 /*c++*/
121 /*link*/
122 """)
123
124 test.write('test2.cpp', r"""This is a .cpp file.
125 /*c++*/
126 /*link*/
127 """)
128
129 test.write('test3.cxx', r"""This is a .cxx file.
130 /*c++*/
131 /*link*/
132 """)
133
134 test.write('test4.c++', r"""This is a .c++ file.
135 /*c++*/
136 /*link*/
137 """)
138
139 test.write('test5.C++', r"""This is a .C++ file.
140 /*c++*/
141 /*link*/
142 """)
143
144 test.run(arguments = '.', stderr = None)
145
146 test.must_match('test1' + _exe, "This is a .cc file.\n")
147
148 test.must_match('test2' + _exe, "This is a .cpp file.\n")
149
150 test.must_match('test3' + _exe, "This is a .cxx file.\n")
151
152 test.must_match('test4' + _exe, "This is a .c++ file.\n")
153
154 test.must_match('test5' + _exe, "This is a .C++ file.\n")
155
156 if TestSCons.case_sensitive_suffixes('.c', '.C'):
157
158     test.write('SConstruct', """
159 env = Environment(LINK = r'%(_python_)s mylink.py',
160                   LINKFLAGS = [],
161                   CXX = r'%(_python_)s myc++.py',
162                   CXXFLAGS = [])
163 env.Program(target = 'test6', source = 'test6.C')
164 """ % locals())
165
166     test.write('test6.C', r"""This is a .C file.
167 /*c++*/
168 /*link*/
169 """)
170
171     test.run(arguments = '.', stderr = None)
172
173     test.must_match('test6' + _exe, "This is a .C file.\n")
174
175
176
177
178 test.write("wrapper.py",
179 """import os
180 import sys
181 open('%s', 'wb').write("wrapper.py\\n")
182 os.system(" ".join(sys.argv[1:]))
183 """ % test.workpath('wrapper.out').replace('\\', '\\\\'))
184
185 test.write('SConstruct', """
186 foo = Environment()
187 cxx = foo.Dictionary('CXX')
188 bar = Environment(CXX = r'%(_python_)s wrapper.py ' + cxx)
189 foo.Program(target = 'foo', source = 'foo.cxx')
190 bar.Program(target = 'bar', source = 'bar.cxx')
191 """ % locals())
192
193 test.write('foo.cxx', r"""
194 #include <stdio.h>
195 #include <stdlib.h>
196 int
197 main(int argc, char *argv[])
198 {
199         argv[argc++] = (char *)"--";
200         printf("foo.cxx\n");
201         exit (0);
202 }
203 """)
204
205 test.write('bar.cxx', r"""
206 #include <stdio.h>
207 #include <stdlib.h>
208 int
209 main(int argc, char *argv[])
210 {
211         argv[argc++] = (char *)"--";
212         printf("foo.cxx\n");
213         exit (0);
214 }
215 """)
216
217
218 test.run(arguments = 'foo' + _exe)
219
220 test.must_not_exist(test.workpath('wrapper.out'))
221
222 test.run(arguments = 'bar' + _exe)
223
224 test.must_match('wrapper.out', "wrapper.py\n")
225
226 test.pass_test()
227
228 # Local Variables:
229 # tab-width:4
230 # indent-tabs-mode:nil
231 # End:
232 # vim: set expandtab tabstop=4 shiftwidth=4: