win32 test portability fixes (Anthony Roach)
[scons.git] / test / AS.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001, 2002 Steven Knight
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 = sys.executable
33
34 if sys.platform == 'win32':
35     _exe = '.exe'
36 else:
37     _exe = ''
38
39 test = TestSCons.TestSCons()
40
41
42
43 if sys.platform == 'win32':
44
45     test.write('mylink.py', r"""
46 import string
47 import sys
48 args = sys.argv[1:]
49 while args:
50     a = args[0]
51     if a[0] != '/':
52         break
53     args = args[1:]
54     if string.lower(a[:5]) == '/out:': out = a[5:]
55 infile = open(args[0], 'rb')
56 outfile = open(out, 'wb')
57 for l in infile.readlines():
58     if l[:5] != '#link':
59         outfile.write(l)
60 sys.exit(0)
61 """)
62
63     test.write('myas.py', r"""
64 import sys
65 args = sys.argv[1:]
66 inf = None
67 while args:
68     a = args[0]
69     args = args[1:]
70     if a[0] != '/':
71         if not inf:
72             inf = a
73         continue
74     if a[:3] == '/Fo': out = a[3:]
75 infile = open(inf, 'rb')
76 outfile = open(out, 'wb')
77 for l in infile.readlines():
78     if l[:3] != '#as':
79         outfile.write(l)
80 sys.exit(0)
81 """)
82
83 else:
84
85     test.write('mylink.py', r"""
86 import getopt
87 import sys
88 opts, args = getopt.getopt(sys.argv[1:], 'o:')
89 for opt, arg in opts:
90     if opt == '-o': out = arg
91 infile = open(args[0], 'rb')
92 outfile = open(out, 'wb')
93 for l in infile.readlines():
94     if l[:5] != '#link':
95         outfile.write(l)
96 sys.exit(0)
97 """)
98
99     test.write('myas.py', r"""
100 import getopt
101 import sys
102 opts, args = getopt.getopt(sys.argv[1:], 'co:')
103 for opt, arg in opts:
104     if opt == '-o': out = arg
105 infile = open(args[0], 'rb')
106 outfile = open(out, 'wb')
107 for l in infile.readlines():
108     if l[:3] != '#as':
109         outfile.write(l)
110 sys.exit(0)
111 """)
112
113 test.write('SConstruct', """
114 env = Environment(LINK = r'%s mylink.py',
115                   AS = r'%s myas.py',
116                   CC = r'%s myas.py')
117 env.Program(target = 'test1', source = 'test1.s')
118 env.Program(target = 'test2', source = 'test2.S')
119 env.Program(target = 'test3', source = 'test3.asm')
120 env.Program(target = 'test4', source = 'test4.ASM')
121 env.Program(target = 'test5', source = 'test5.spp')
122 env.Program(target = 'test6', source = 'test6.SPP')
123 """ % (python, python, python))
124
125 test.write('test1.s', r"""This is a .s file.
126 #as
127 #link
128 """)
129
130 test.write('test2.S', r"""This is a .S file.
131 #as
132 #link
133 """)
134
135 test.write('test3.asm', r"""This is a .asm file.
136 #as
137 #link
138 """)
139
140 test.write('test4.ASM', r"""This is a .ASM file.
141 #as
142 #link
143 """)
144
145 test.write('test5.spp', r"""This is a .spp file.
146 #as
147 #link
148 """)
149
150 test.write('test6.SPP', r"""This is a .SPP file.
151 #as
152 #link
153 """)
154
155 test.run(arguments = '.', stderr = None)
156
157 test.fail_test(test.read('test1' + _exe) != "This is a .s file.\n")
158
159 test.fail_test(test.read('test2' + _exe) != "This is a .S file.\n")
160
161 test.fail_test(test.read('test3' + _exe) != "This is a .asm file.\n")
162
163 test.fail_test(test.read('test4' + _exe) != "This is a .ASM file.\n")
164
165 test.fail_test(test.read('test5' + _exe) != "This is a .spp file.\n")
166
167 test.fail_test(test.read('test6' + _exe) != "This is a .SPP file.\n")
168
169
170 as = test.detect('AS', 'as')
171
172 if as:
173
174     test.write("wrapper.py",
175 """import os
176 import string
177 import sys
178 open('%s', 'wb').write("wrapper.py\\n")
179 os.system(string.join(sys.argv[1:], " "))
180 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
181
182     test.write('SConstruct', """
183 aaa = Environment()
184 bbb = aaa.Copy(AS = r'%s wrapper.py ' + WhereIs('as'))
185 aaa.Program(target = 'aaa', source = ['aaa.s', 'aaa_main.c'])
186 bbb.Program(target = 'bbb', source = ['bbb.s', 'bbb_main.c'])
187 """ % python)
188
189     test.write('aaa.s', 
190 """        .file   "aaa.s"
191 .data
192 .align 4
193 .globl name
194 name:
195         .ascii  "aaa.s"
196         .byte   0
197 """)
198
199     test.write('bbb.s', 
200 """        .file   "bbb.s"
201 .data
202 .align 4
203 .globl name
204 name:
205         .ascii  "bbb.s"
206         .byte   0
207 """)
208
209     test.write('aaa_main.c', r"""
210 extern char name[];
211
212 int
213 main(int argc, char *argv[])
214 {
215         argv[argc++] = "--";
216         printf("aaa_main.c %s\n", name);
217         exit (0);
218 }
219 """)
220
221     test.write('bbb_main.c', r"""
222 extern char name[];
223
224 int
225 main(int argc, char *argv[])
226 {
227         argv[argc++] = "--";
228         printf("bbb_main.c %s\n", name);
229         exit (0);
230 }
231 """)
232
233     test.run(arguments = 'aaa' + _exe, stderr = None)
234
235     test.run(program = test.workpath('aaa'), stdout =  "aaa_main.c aaa.s\n")
236
237     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
238
239     test.run(arguments = 'bbb' + _exe)
240
241     test.run(program = test.workpath('bbb'), stdout =  "bbb_main.c bbb.s\n")
242
243     test.fail_test(test.read('wrapper.out') != "wrapper.py\n")
244
245     test.unlink('wrapper.out')
246
247
248
249 ml = test.where_is('ml')
250
251 if ml:
252
253     test.write("wrapper.py",
254 """import os
255 import string
256 import sys
257 open('%s', 'wb').write("wrapper.py\\n")
258 os.system(string.join(sys.argv[1:], " "))
259 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
260
261     test.write('SConstruct', """
262 import os
263 ccc = Environment(tools = ['msvc', 'mslink', 'masm'],
264                   ASFLAGS = '/nologo /coff')
265 ccc['ENV']['PATH'] = os.environ['PATH']
266 ddd = ccc.Copy(AS = r'%s wrapper.py ' + ccc['AS'])
267 ccc.Program(target = 'ccc', source = ['ccc.asm', 'ccc_main.c'])
268 ddd.Program(target = 'ddd', source = ['ddd.asm', 'ddd_main.c'])
269 """ % python)
270
271     test.write('ccc.asm', 
272 """
273 DSEG    segment
274         PUBLIC _name
275 _name   byte "ccc.asm",0
276 DSEG    ends
277         end
278 """)
279
280     test.write('ddd.asm', 
281 """        
282 DSEG    segment
283         PUBLIC _name
284 _name   byte "ddd.asm",0
285 DSEG    ends
286         end
287 """)
288
289     test.write('ccc_main.c', r"""
290 extern char name[];
291
292 int
293 main(int argc, char *argv[])
294 {
295         argv[argc++] = "--";
296         printf("ccc_main.c %s\n", name);
297         exit (0);
298 }
299 """)
300
301     test.write('ddd_main.c', r"""
302 extern char name[];
303
304 int
305 main(int argc, char *argv[])
306 {
307         argv[argc++] = "--";
308         printf("ddd_main.c %s\n", name);
309         exit (0);
310 }
311 """)
312
313     test.run(arguments = 'ccc' + _exe, stderr = None)
314
315     test.run(program = test.workpath('ccc'), stdout =  "ccc_main.c ccc.asm\n")
316
317     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
318
319     test.run(arguments = 'ddd' + _exe)
320
321     test.run(program = test.workpath('ddd'), stdout =  "ddd_main.c ddd.asm\n")
322
323     test.fail_test(test.read('wrapper.out') != "wrapper.py\n")
324
325
326
327
328 nasm = test.where_is('nasm')
329
330 if nasm:
331
332     test.write("wrapper.py",
333 """import os
334 import string
335 import sys
336 open('%s', 'wb').write("wrapper.py\\n")
337 os.system(string.join(sys.argv[1:], " "))
338 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
339
340     test.write('SConstruct', """
341 eee = Environment(tools = ['gcc', 'gnulink', 'nasm'],
342                   ASFLAGS = '-f aout')
343 fff = eee.Copy(AS = r'%s wrapper.py ' + WhereIs('nasm'))
344 eee.Program(target = 'eee', source = ['eee.asm', 'eee_main.c'])
345 fff.Program(target = 'fff', source = ['fff.asm', 'fff_main.c'])
346 """ % python)
347
348     test.write('eee.asm', 
349 """
350 global name
351 name:
352         db 'eee.asm',0
353 """)
354
355     test.write('fff.asm', 
356 """        
357 global name
358 name:
359         db 'fff.asm',0
360 """)
361
362     test.write('eee_main.c', r"""
363 extern char name[];
364
365 int
366 main(int argc, char *argv[])
367 {
368         argv[argc++] = "--";
369         printf("eee_main.c %s\n", name);
370         exit (0);
371 }
372 """)
373
374     test.write('fff_main.c', r"""
375 extern char name[];
376
377 int
378 main(int argc, char *argv[])
379 {
380         argv[argc++] = "--";
381         printf("fff_main.c %s\n", name);
382         exit (0);
383 }
384 """)
385
386     test.run(arguments = 'eee' + _exe, stderr = None)
387
388     test.run(program = test.workpath('eee'), stdout =  "eee_main.c eee.asm\n")
389
390     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
391
392     test.run(arguments = 'fff' + _exe)
393
394     test.run(program = test.workpath('fff'), stdout =  "fff_main.c fff.asm\n")
395
396     test.fail_test(test.read('wrapper.out') != "wrapper.py\n")
397
398
399
400 test.pass_test()