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