3 Tool-specific initialization for MinGW (http://www.mingw.org/)
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
9 See also http://www.scons.org/wiki/CrossCompilingMingw
13 # Copyright (c) 2001, 2002, 2003, 2004 The SCons Foundation
15 # Permission is hereby granted, free of charge, to any person obtaining
16 # a copy of this software and associated documentation files (the
17 # "Software"), to deal in the Software without restriction, including
18 # without limitation the rights to use, copy, modify, merge, publish,
19 # distribute, sublicense, and/or sell copies of the Software, and to
20 # permit persons to whom the Software is furnished to do so, subject to
21 # the following conditions:
23 # The above copyright notice and this permission notice shall be included
24 # in all copies or substantial portions of the Software.
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
27 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
28 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 # This is what we search for to find mingw:
45 prefixes32
= SCons
.Util
.Split("""
59 prefixes64
= SCons
.Util
.Split("""
67 if env
['machine'] == 'x86_64':
71 for prefix
in prefixes
:
72 # First search in the SCons path and then the OS path:
73 if env
.WhereIs(prefix
+ 'gcc') or SCons
.Util
.WhereIs(prefix
+ 'gcc'):
78 def shlib_generator(target
, source
, env
, for_signature
):
79 cmd
= SCons
.Util
.CLVar(['$SHLINK', '$SHLINKFLAGS'])
81 dll
= env
.FindIxes(target
, 'SHLIBPREFIX', 'SHLIBSUFFIX')
82 if dll
: cmd
.extend(['-o', dll
])
84 cmd
.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
86 implib
= env
.FindIxes(target
, 'LIBPREFIX', 'LIBSUFFIX')
87 if implib
: cmd
.append('-Wl,--out-implib,'+implib
.get_string(for_signature
))
89 def_target
= env
.FindIxes(target
, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
90 if def_target
: cmd
.append('-Wl,--output-def,'+def_target
.get_string(for_signature
))
94 def shlib_emitter(target
, source
, env
):
95 dll
= env
.FindIxes(target
, 'SHLIBPREFIX', 'SHLIBSUFFIX')
96 no_import_lib
= env
.get('no_import_lib', 0)
99 raise SCons
.Errors
.UserError
, "A shared library should have exactly one target with the suffix: %s" % env
.subst("$SHLIBSUFFIX")
101 if not no_import_lib
and \
102 not env
.FindIxes(target
, 'LIBPREFIX', 'LIBSUFFIX'):
104 # Append an import library to the list of targets.
105 target
.append(env
.ReplaceIxes(dll
,
106 'SHLIBPREFIX', 'SHLIBSUFFIX',
107 'LIBPREFIX', 'LIBSUFFIX'))
109 # Append a def file target if there isn't already a def file target
110 # or a def file source. There is no option to disable def file
111 # target emitting, because I can't figure out why someone would ever
112 # want to turn it off.
113 def_source
= env
.FindIxes(source
, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
114 def_target
= env
.FindIxes(target
, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
115 if not def_source
and not def_target
:
116 target
.append(env
.ReplaceIxes(dll
,
117 'SHLIBPREFIX', 'SHLIBSUFFIX',
118 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX'))
120 return (target
, source
)
123 shlib_action
= SCons
.Action
.Action(shlib_generator
, '$SHLINKCOMSTR', generator
=1)
125 res_action
= SCons
.Action
.Action('$RCCOM', '$RCCOMSTR')
127 res_builder
= SCons
.Builder
.Builder(action
=res_action
, suffix
='.o',
128 source_scanner
=SCons
.Tool
.SourceFileScanner
)
129 SCons
.Tool
.SourceFileScanner
.add_scanner('.rc', SCons
.Defaults
.CScan
)
133 def compile_without_gstabs(env
, sources
, c_file
):
134 '''This is a hack used to compile some source files without the
137 It seems that some versions of mingw32's gcc (4.4.2 at least) die
138 when compiling large files with the -gstabs option. -gstabs is
139 related to debug symbols and can be omitted from the effected
142 This function compiles the given c_file without -gstabs, removes
143 the c_file from the sources list, then appends the new .o file to
144 sources. Then return the new sources list.
147 # Modify CCFLAGS to not have -gstabs option:
149 flags
= str(env2
['CCFLAGS'])
150 flags
= flags
.replace("-gstabs", "")
151 env2
['CCFLAGS'] = SCons
.Util
.CLVar(flags
)
153 # Build the special-case files:
154 obj_file
= env2
.SharedObject(c_file
)
156 # Replace ".cpp" or ".c" with ".o"
157 o_file
= c_file
.replace(".cpp", ".o")
158 o_file
= o_file
.replace(".c", ".o")
160 # Replace the .c files with the specially-compiled .o file
161 sources
.remove(c_file
)
162 sources
.append(o_file
)
168 mingw_prefix
= find(env
)
171 dir = os
.path
.dirname(env
.WhereIs(mingw_prefix
+ 'gcc') or SCons
.Util
.WhereIs(mingw_prefix
+ 'gcc'))
173 # The mingw bin directory must be added to the path:
174 path
= env
['ENV'].get('PATH', [])
177 if SCons
.Util
.is_String(path
):
178 path
= string
.split(path
, os
.pathsep
)
180 env
['ENV']['PATH'] = string
.join([dir] + path
, os
.pathsep
)
182 # Most of mingw is the same as gcc and friends...
183 gnu_tools
= ['gcc', 'g++', 'gnulink', 'ar', 'gas']
184 for tool
in gnu_tools
:
185 SCons
.Tool
.Tool(tool
)(env
)
187 #... but a few things differ:
188 env
['CC'] = mingw_prefix
+ 'gcc'
189 env
['SHCCFLAGS'] = SCons
.Util
.CLVar('$CCFLAGS')
190 env
['CXX'] = mingw_prefix
+ 'g++'
191 env
['SHCXXFLAGS'] = SCons
.Util
.CLVar('$CXXFLAGS')
192 env
['SHLINKFLAGS'] = SCons
.Util
.CLVar('$LINKFLAGS -shared')
193 env
['SHLINKCOM'] = shlib_action
194 env
.Append(SHLIBEMITTER
= [shlib_emitter
])
195 env
['LINK'] = mingw_prefix
+ 'g++'
196 env
['AR'] = mingw_prefix
+ 'ar'
197 env
['RANLIB'] = mingw_prefix
+ 'ranlib'
198 env
['LINK'] = mingw_prefix
+ 'g++'
199 env
['AS'] = mingw_prefix
+ 'as'
200 env
['WIN32DEFPREFIX'] = ''
201 env
['WIN32DEFSUFFIX'] = '.def'
202 env
['SHOBJSUFFIX'] = '.o'
203 env
['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
205 env
['RC'] = mingw_prefix
+ 'windres'
206 env
['RCFLAGS'] = SCons
.Util
.CLVar('')
207 env
['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS ${INCPREFIX}${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET'
208 env
['BUILDERS']['RES'] = res_builder
210 # Some setting from the platform also have to be overridden:
211 env
['OBJPREFIX'] = ''
212 env
['OBJSUFFIX'] = '.o'
213 env
['SHOBJPREFIX'] = '$OBJPREFIX'
214 env
['SHOBJSUFFIX'] = '$OBJSUFFIX'
215 env
['PROGPREFIX'] = ''
216 env
['PROGSUFFIX'] = '.exe'
217 env
['LIBPREFIX'] = 'lib'
218 env
['LIBSUFFIX'] = '.a'
219 env
['SHLIBPREFIX'] = ''
220 env
['SHLIBSUFFIX'] = '.dll'
221 env
['LIBPREFIXES'] = [ 'lib', '' ]
222 env
['LIBSUFFIXES'] = [ '.a', '.lib' ]
224 # MinGW port of gdb does not handle well dwarf debug info which is the
225 # default in recent gcc versions
226 env
.AppendUnique(CCFLAGS
= ['-gstabs'])
228 env
.AppendUnique(CPPDEFINES
= [('__MSVCRT_VERSION__', '0x0700')])
229 #env.AppendUnique(LIBS = ['iberty'])
230 env
.AppendUnique(SHLINKFLAGS
= ['-Wl,--enable-stdcall-fixup'])
231 #env.AppendUnique(SHLINKFLAGS = ['-Wl,--kill-at'])
233 # Avoid depending on gcc runtime DLLs
234 env
.AppendUnique(LINKFLAGS
= ['-static-libgcc'])
236 env
.AddMethod(compile_without_gstabs
, 'compile_without_gstabs')