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