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