Merge branch 'master' into jbrill-msvc-detect
[scons.git] / test / gnutools.py
blobeb0d8476132e6530a131ec5daf77fea8d1c64dcc
1 #!/usr/bin/env python
3 # __COPYRIGHT__
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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
24 """
25 Testing the gnu tool chain, i.e. the tools 'gcc', 'g++' and 'gnulink'.
26 """
28 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
30 import TestSCons
31 import sys
33 _python_ = TestSCons._python_
34 _exe = TestSCons._exe
36 def dll(s):
37 return TestSCons.dll_ + s + TestSCons._dll
39 test = TestSCons.TestSCons()
41 test.subdir('gnutools')
43 test.write(['gnutools','mygcc.py'], """
44 import getopt
45 import sys
46 try:
47 cmd_opts, args = getopt.getopt(sys.argv[1:], 'f:s:co:', [])
48 except getopt.GetoptError:
49 # we may be called with --version, just quit if so
50 sys.exit(0)
51 out = None
52 opt_string = ''
53 for opt, arg in cmd_opts:
54 if opt == '-o': out = arg
55 else: opt_string = opt_string + ' ' + opt + arg
56 with open(out, 'w') as ofp:
57 ofp.write('gcc ' + opt_string + '\\n')
58 for a in args:
59 with open(a, 'r') as ifp:
60 ofp.write(ifp.read())
61 sys.exit(0)
62 """)
64 test.write(['gnutools','myg++.py'], """
65 import getopt
66 import sys
67 try:
68 cmd_opts, args = getopt.getopt(sys.argv[1:], 'f:s:co:', [])
69 except getopt.GetoptError:
70 # we may be called with --version, just quit if so
71 sys.exit(0)
72 out = None
73 opt_string = ''
74 for opt, arg in cmd_opts:
75 if opt == '-o': out = arg
76 else: opt_string = opt_string + ' ' + opt + arg
77 with open(out, 'w') as ofp:
78 ofp.write('g++ ' + opt_string + '\\n')
79 for a in args:
80 with open(a, 'r') as ifp:
81 ofp.write(ifp.read())
82 sys.exit(0)
83 """)
85 test.subdir('work1')
87 test.write(['work1', 'cfile1.c'],"""
88 /* c file 1 */
89 """)
91 test.write(['work1', 'cfile2.c'],"""
92 /* c file 2 */
93 """)
95 test.write(['work1', 'cppfile1.cpp'],"""
96 /* cpp file 1 */
97 """)
99 test.write(['work1', 'cppfile2.cpp'],"""
100 /* cpp file 2 */
101 """)
103 mygcc_py = test.workpath('gnutools','mygcc.py')
104 mygxx_py = test.workpath('gnutools','myg++.py')
106 test.write(['work1', 'SConstruct'],"""
107 env = Environment(tools=['gcc','g++','gnulink'],
108 CC=r'%(_python_)s %(mygcc_py)s',
109 CXX=r'%(_python_)s %(mygxx_py)s',
110 OBJSUFFIX='.o',
111 SHOBJSUFFIX='.os')
112 env.Program('c-only', Split('cfile1.c cfile2.c'))
113 env.Program('cpp-only', Split('cppfile1.cpp cppfile2.cpp'))
114 env.Program('c-and-cpp', Split('cfile1.c cppfile1.cpp'))
116 env.SharedLibrary('c-only', Split('cfile1.c cfile2.c'))
117 env.SharedLibrary('cpp-only', Split('cppfile1.cpp cppfile2.cpp'))
118 env.SharedLibrary('c-and-cpp', Split('cfile1.c cppfile1.cpp'))
119 """ % locals())
121 test.run(chdir='work1')
123 def testObject(test, obj, expect):
124 contents = test.read(test.workpath('work1', obj))
125 line1 = contents.split(b'\n')[0]
126 actual = b' '.join(line1.split())
127 if not expect == actual:
128 print("%s: %s != %s\n" % (obj, repr(expect), repr(actual)))
129 test.fail_test()
131 if sys.platform in ('win32', 'cygwin'):
132 c_fpic = b''
133 else:
134 c_fpic = b' -fPIC'
136 testObject(test, 'cfile1.o', b'gcc -c')
137 testObject(test, 'cfile2.o', b'gcc -c')
138 testObject(test, 'cppfile1.o', b'g++ -c')
139 testObject(test, 'cppfile2.o', b'g++ -c')
140 testObject(test, 'cfile1.os', b'gcc -c' + c_fpic)
141 testObject(test, 'cfile2.os', b'gcc -c' + c_fpic)
142 testObject(test, 'cppfile1.os', b'g++ -c' + c_fpic)
143 testObject(test, 'cppfile2.os', b'g++ -c' + c_fpic)
144 testObject(test, 'c-only' + _exe, b'gcc')
145 testObject(test, 'cpp-only' + _exe, b'g++')
146 testObject(test, 'c-and-cpp' + _exe, b'g++')
147 testObject(test, dll('c-only'), b'gcc -shared')
148 testObject(test, dll('cpp-only'), b'g++ -shared')
149 testObject(test, dll('c-and-cpp'), b'g++ -shared')
151 test.pass_test()
153 # Local Variables:
154 # tab-width:4
155 # indent-tabs-mode:nil
156 # End:
157 # vim: set expandtab tabstop=4 shiftwidth=4: