Eliminate unnecessary WIN32/Win32/win32 references in tests, too.
[scons.git] / test / gnutools.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 Testing the gnu tool chain, i.e. the tools 'gcc', 'g++' and 'gnulink'.
29 """
30
31 import TestSCons
32 import string
33 import sys
34 python = TestSCons.python
35 _exe = TestSCons._exe
36 _dll = TestSCons._dll
37 dll_ = TestSCons.dll_
38 test = TestSCons.TestSCons()
39
40 test.subdir('gnutools')
41
42 test.write(['gnutools','mygcc.py'], """
43 import getopt
44 import sys
45 cmd_opts, args = getopt.getopt(sys.argv[1:], 'f:s:co:', [])
46 output = None
47 opt_string = ''
48 for opt, arg in cmd_opts:
49     if opt == '-o': output = open(arg, 'wb')
50     else: opt_string = opt_string + ' ' + opt + arg
51 output.write('gcc ' + opt_string + '\\n')
52 for a in args:
53     contents = open(a, 'rb').read()
54     output.write(contents)
55 output.close()
56 sys.exit(0)
57 """)
58
59 test.write(['gnutools','myg++.py'], """
60 import getopt
61 import sys
62 cmd_opts, args = getopt.getopt(sys.argv[1:], 'f:s:co:', [])
63 output = None
64 opt_string = ''
65 for opt, arg in cmd_opts:
66     if opt == '-o': output = open(arg, 'wb')
67     else: opt_string = opt_string + ' ' + opt + arg
68 output.write('g++ ' + opt_string + '\\n')
69 for a in args:
70     contents = open(a, 'rb').read()
71     output.write(contents)
72 output.close()
73 sys.exit(0)
74 """)
75
76 test.subdir('work1')
77
78 test.write(['work1', 'cfile1.c'],"""
79 /* c file 1 */
80 """)
81
82 test.write(['work1', 'cfile2.c'],"""
83 /* c file 2 */
84 """)
85
86 test.write(['work1', 'cppfile1.cpp'],"""
87 /* cpp file 1 */
88 """)
89
90 test.write(['work1', 'cppfile2.cpp'],"""
91 /* cpp file 2 */
92 """)
93
94 test.write(['work1', 'SConstruct'],"""
95 env = Environment(tools=['gcc','g++','gnulink'],
96                   CC=r'%s %s',
97                   CXX=r'%s %s',
98                   OBJSUFFIX='.o',
99                   SHOBJSUFFIX='.os')
100 env.Program('c-only', Split('cfile1.c cfile2.c'))
101 env.Program('cpp-only', Split('cppfile1.cpp cppfile2.cpp'))
102 env.Program('c-and-cpp', Split('cfile1.c cppfile1.cpp'))
103
104 env.SharedLibrary('c-only', Split('cfile1.c cfile2.c'))
105 env.SharedLibrary('cpp-only', Split('cppfile1.cpp cppfile2.cpp'))
106 env.SharedLibrary('c-and-cpp', Split('cfile1.c cppfile1.cpp'))
107 """ % (python, test.workpath('gnutools','mygcc.py'),
108        python, test.workpath('gnutools','myg++.py')))
109
110 test.run(chdir='work1')
111
112 def testObject(test, obj, command, flags):
113     contents = test.read(test.workpath('work1', obj))
114     line1 = string.split(contents,'\n')[0]
115     items = string.split(line1, ' ')
116     cmd = ''
117     for i in items:
118         if i != '':
119             if cmd:
120                 cmd = cmd + ' ' + i
121             else:
122                 cmd = i
123     res = ((flags and (cmd == command + ' ' + flags)) or
124            (not flags and (cmd == command)))
125     if not res: print "'"+obj+command+flags+"'"+"!='"+str(line1)+"'"
126     return res
127
128 if sys.platform == 'cygwin':
129     fpic = ''
130 else:
131     fpic = ' -fPIC'
132
133 test.fail_test(not testObject(test, 'cfile1.o', 'gcc', '-c') or
134                not testObject(test, 'cfile2.o', 'gcc', '-c') or
135                not testObject(test, 'cppfile1.o', 'g++', '-c') or
136                not testObject(test, 'cppfile2.o', 'g++', '-c') or
137                not testObject(test, 'cfile1.os', 'gcc', '-c' + fpic) or
138                not testObject(test, 'cfile2.os', 'gcc', '-c' + fpic) or
139                not testObject(test, 'cppfile1.os', 'g++', '-c' + fpic) or
140                not testObject(test, 'cppfile2.os', 'g++', '-c' + fpic) or
141                not testObject(test, 'c-only' + _exe, 'gcc', '') or
142                not testObject(test, 'cpp-only' + _exe, 'g++', '') or
143                not testObject(test, 'c-and-cpp' + _exe, 'g++', '') or
144                not testObject(test, dll_ + 'c-only' + _dll, 'gcc', '-shared') or
145                not testObject(test, dll_ + 'cpp-only' + _dll, 'g++', '-shared') or
146                not testObject(test, dll_ + 'c-and-cpp' + _dll, 'g++', '-shared'))
147
148 test.pass_test()