Add Variables.defualted attribute
[scons.git] / test / Win32 / mingw.py
blobaca8795c7fe6d6ba9e75c796f87bd7f8d9168411
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 This tests the MinGW C/C++ compiler support.
28 This test requires MinGW to be installed.
29 """
31 import os
32 import sys
34 import TestSCons
35 import TestCmd
37 test = TestSCons.TestSCons(match = TestCmd.match_re_dotall)
39 # MinGW is Windows only:
40 if sys.platform != 'win32':
41 msg = "Skipping mingw test on non-Windows platform '%s'\n" % sys.platform
42 test.skip_test(msg)
44 test.write('SConstruct',"""
45 import sys
46 from SCons.Tool.mingw import exists
48 DefaultEnvironment(tools=[])
49 env = Environment()
50 if exists(env):
51 print('mingw exists')
52 sys.exit(0)
53 """)
55 test.run()
56 if 'mingw exists' not in test.stdout():
57 test.skip_test("No MinGW on this system, skipping test.\n")
59 test.subdir('header')
61 # Do the actual testing:
62 test.write('SConstruct',"""
63 DefaultEnvironment(tools=[])
64 env = Environment(tools=['mingw'])
65 assert env['CC'] == 'gcc'
66 env.StaticLibrary('static', 'static.cpp')
67 env.SharedLibrary('shared', 'shared.cpp')
68 env.SharedLibrary('cshared', ['cshared.c', 'cshared.def'], WINDOWS_INSERT_DEF=1)
69 env.Program(
70 'test',
71 ['test.cpp', env.RES('resource.rc', CPPPATH=['header'])],
72 LIBS=['static', 'shared', 'cshared'],
73 LIBPATH=['.'],
75 """)
77 test.write('test.cpp', '''
78 #include <stdio.h>
79 #include <windows.h>
80 #include "resource.h"
82 void shared_func(void);
83 void static_func(void);
84 extern "C" void cshared_func(void);
86 int main(void)
88 printf("%s\\n", "test.cpp");
89 shared_func();
90 static_func();
91 cshared_func();
93 char test[1024];
94 LoadString(GetModuleHandle(NULL), IDS_TEST, test, sizeof(test));
95 printf("%d %s\\n", IDS_TEST, test);
97 return 0;
99 ''')
101 test.write('resource.rc', '''
102 #include "resource.h"
103 #include <resource2.h>
105 STRINGTABLE DISCARDABLE
106 BEGIN
107 IDS_TEST RESOURCE_RC
109 ''')
111 test.write('resource.h', '''
112 #define IDS_TEST 2001
113 ''')
115 test.write('static.cpp', '''
116 #include <stdio.h>
118 void static_func(void)
120 printf("%s\\n", "static.cpp");
122 ''')
124 test.write('shared.cpp', '''
125 #include <stdio.h>
127 void shared_func(void)
129 printf("%s\\n", "shared.cpp");
131 ''')
133 test.write('cshared.c', '''
134 #include <stdio.h>
136 void cshared_func(void)
138 printf("%s\\n", "cshared.c");
140 ''')
142 test.write('cshared.def', '''
143 EXPORTS
144 cshared_func
145 ''')
149 test.write('header/resource2.h', '''
150 #define RESOURCE_RC "resource.rc"
151 ''')
153 # the mingw linker likes to print "Creating library file: libfoo.a" to stderr, but
154 # we'd like for this test to pass once this bug is fixed, so match anything at all
155 # that comes out of stderr:
156 test.run(arguments='test.exe', stderr='.*')
157 # ensure the source def for cshared.def got used, and there wasn't a target def for chshared.dll:
158 test.fail_test('cshared.def' not in test.stdout())
159 test.fail_test('-Wl,--output-def,cshared.def' in test.stdout())
160 # ensure the target def got generated for the shared.dll:
161 test.fail_test(not os.path.exists(test.workpath('cshared.def')))
162 test.fail_test(os.path.exists(test.workpath('shared.def')))
163 test.run(program=test.workpath('test.exe'), stdout='test.cpp\nshared.cpp\nstatic.cpp\ncshared.c\n2001 resource.rc\n')
165 # ensure that modifying the header causes the resource to be rebuilt:
166 test.write('resource.h', '''
167 #define IDS_TEST 2002
168 ''')
169 test.run(arguments='test.exe', stderr='.*')
170 test.run(program=test.workpath('test.exe'), stdout='test.cpp\nshared.cpp\nstatic.cpp\ncshared.c\n2002 resource.rc\n')
172 # Test with specifying the default tool to make sure msvc setting doen't ruin it
173 # for mingw:
174 test.write('SConstruct',"""
175 env=Environment(tools=['default', 'mingw'])
176 assert env['CC'] == 'gcc'
177 env.SharedLibrary('shared', 'shared.cpp')
178 env.Program('test', 'test.cpp', LIBS=['static', 'shared', 'cshared'], LIBPATH=['.'])
179 """)
181 test.write('test.cpp', '''
182 #include <stdio.h>
184 void shared_func(void);
186 int main(void)
188 printf("%s\\n", "test.cpp2");
189 shared_func();
190 return 0;
192 ''')
194 test.write('shared.cpp', '''
195 #include <stdio.h>
197 void shared_func(void)
199 printf("%s\\n", "shared.cpp2");
201 ''')
203 # the mingw linker likes to print "Creating library file: libfoo.a" to stderr, but
204 # we'd like for this test to pass once this bug is fixed, so match anything at all
205 # that comes out of stderr:
206 test.run(arguments='test.exe', stderr='.*')
207 test.run(program=test.workpath('test.exe'), stdout='test.cpp2\nshared.cpp2\n')
209 test.pass_test()
211 # Local Variables:
212 # tab-width:4
213 # indent-tabs-mode:nil
214 # End:
215 # vim: set expandtab tabstop=4 shiftwidth=4: