b6d61a3169907ed92a0850ea4784aeb62169873d
[scons.git] / test / AS / nasm.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 """
28 Verify correct use of the live 'nasm' assembler.
29 """
30
31 import os
32 import string
33 import sys
34
35 import TestSCons
36
37 _python_ = TestSCons._python_
38 _exe   = TestSCons._exe
39
40 test = TestSCons.TestSCons()
41
42 nasm = test.where_is('nasm')
43
44 if not nasm:
45     test.skip_test('nasm not found; skipping test\n')
46
47 if string.find(sys.platform, 'linux') == -1:
48     test.skip_test("skipping test on non-Linux platform '%s'\n" % sys.platform)
49
50 try:
51     import popen2
52     stdout = popen2.popen2('nasm -v')[0]
53 except OSError:
54     test.skip_test('could not determine nasm version; skipping test\n')
55 else:
56     version = string.split(stdout.read())[2]
57     if version[:4] != '0.98':
58         test.skip_test("skipping test of nasm version %s\n" % version)
59
60     machine = os.uname()[4]
61     if not machine in ('i386', 'i486', 'i586', 'i686'):
62         fmt = "skipping test of nasm %s on non-x86 machine '%s'\n"
63         test.skip_test(fmt % (version, machine))
64
65 # Allow flexibility about the type of object/executable format
66 # needed on different systems.  Format_map is a dict that maps
67 # sys.platform substrings to the correct argument for the nasm -f
68 # option.  The default is "elf," which seems to be a reasonable
69 # lowest common denominator (works on both Linux and FreeBSD,
70 # anyway...).
71 nasm_format = 'elf'
72 format_map = {}
73 for k, v in format_map.items():
74     if string.find(sys.platform, k) != -1:
75         nasm_format = v
76         break
77
78 test.write("wrapper.py",
79 """import os
80 import string
81 import sys
82 open('%s', 'wb').write("wrapper.py\\n")
83 os.system(string.join(sys.argv[1:], " "))
84 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
85
86 test.write('SConstruct', """
87 eee = Environment(tools = ['gcc', 'gnulink', 'nasm'],
88                   ASFLAGS = '-f %(nasm_format)s')
89 fff = eee.Clone(AS = r'%(_python_)s wrapper.py ' + WhereIs('nasm'))
90 eee.Program(target = 'eee', source = ['eee.asm', 'eee_main.c'])
91 fff.Program(target = 'fff', source = ['fff.asm', 'fff_main.c'])
92 """ % locals())
93
94 test.write('eee.asm', 
95 """
96 global name
97 name:
98         db 'eee.asm',0
99 """)
100
101 test.write('fff.asm', 
102 """        
103 global name
104 name:
105         db 'fff.asm',0
106 """)
107
108 test.write('eee_main.c', r"""
109 extern char name[];
110
111 int
112 main(int argc, char *argv[])
113 {
114         argv[argc++] = "--";
115         printf("eee_main.c %s\n", name);
116         exit (0);
117 }
118 """)
119
120 test.write('fff_main.c', r"""
121 #include <stdio.h>
122 #include <stdlib.h>
123
124 extern char name[];
125
126 int
127 main(int argc, char *argv[])
128 {
129         argv[argc++] = "--";
130         printf("fff_main.c %s\n", name);
131         exit (0);
132 }
133 """)
134
135 test.run(arguments = 'eee' + _exe, stderr = None)
136
137 test.run(program = test.workpath('eee'), stdout =  "eee_main.c eee.asm\n")
138
139 test.must_not_exist('wrapper.out')
140
141 test.run(arguments = 'fff' + _exe)
142
143 test.run(program = test.workpath('fff'), stdout =  "fff_main.c fff.asm\n")
144
145 test.must_match('wrapper.out', "wrapper.py\n")
146
147
148
149 test.pass_test()
150
151 # Local Variables:
152 # tab-width:4
153 # indent-tabs-mode:nil
154 # End:
155 # vim: set expandtab tabstop=4 shiftwidth=4: