654849882a991c34dd8cfb029f3dcec0d13fa354
[scons.git] / test / F77.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('myg77.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[:4] != '#g77':
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('myg77.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[:4] != '#g77':
109         outfile.write(l)
110 sys.exit(0)
111 """)
112
113 test.write('SConstruct', """
114 env = Environment(LINK = r'%s mylink.py',
115                   F77 = r'%s myg77.py')
116 env.Program(target = 'test1', source = 'test1.f')
117 env.Program(target = 'test2', source = 'test2.for')
118 env.Program(target = 'test3', source = 'test3.FOR')
119 env.Program(target = 'test4', source = 'test4.F')
120 env.Program(target = 'test5', source = 'test5.fpp')
121 env.Program(target = 'test6', source = 'test6.FPP')
122 """ % (python, python))
123
124 test.write('test1.f', r"""This is a .f file.
125 #g77
126 #link
127 """)
128
129 test.write('test2.for', r"""This is a .for file.
130 #g77
131 #link
132 """)
133
134 test.write('test3.FOR', r"""This is a .FOR file.
135 #g77
136 #link
137 """)
138
139 test.write('test4.F', r"""This is a .F file.
140 #g77
141 #link
142 """)
143
144 test.write('test5.fpp', r"""This is a .fpp file.
145 #g77
146 #link
147 """)
148
149 test.write('test6.FPP', r"""This is a .FPP file.
150 #g77
151 #link
152 """)
153
154 test.run(arguments = '.', stderr = None)
155
156 test.fail_test(test.read('test1' + _exe) != "This is a .f file.\n")
157
158 test.fail_test(test.read('test2' + _exe) != "This is a .for file.\n")
159
160 test.fail_test(test.read('test3' + _exe) != "This is a .FOR file.\n")
161
162 test.fail_test(test.read('test4' + _exe) != "This is a .F file.\n")
163
164 test.fail_test(test.read('test5' + _exe) != "This is a .fpp file.\n")
165
166 test.fail_test(test.read('test6' + _exe) != "This is a .FPP file.\n")
167
168
169
170 g77 = test.where_is('g77')
171
172 if g77:
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 foo = Environment(LIBS = 'g2c')
184 f77 = foo.Dictionary('F77')
185 bar = foo.Copy(F77 = r'%s wrapper.py ' + f77)
186 foo.Program(target = 'foo', source = 'foo.f')
187 bar.Program(target = 'bar', source = 'bar.f')
188 """ % python)
189
190     test.write('foo.f', r"""
191       PROGRAM FOO
192       PRINT *,'foo.f'
193       STOP
194       END
195 """)
196
197     test.write('bar.f', r"""
198       PROGRAM BAR
199       PRINT *,'bar.f'
200       STOP
201       END
202 """)
203
204
205     test.run(arguments = 'foo' + _exe, stderr = None)
206
207     test.run(program = test.workpath('foo'), stdout =  " foo.f\n")
208
209     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
210
211     test.run(arguments = 'bar' + _exe)
212
213     test.run(program = test.workpath('bar'), stdout =  " bar.f\n")
214
215     test.fail_test(test.read('wrapper.out') != "wrapper.py\n")
216
217 test.pass_test()