crossmingw.py revision a6e642be5c5ef2d772ffd621205a9fb71454f069
1"""SCons.Tool.gcc
2
3Tool-specific initialization for MinGW (http://www.mingw.org/)
4
5There normally shouldn't be any need to import this module directly.
6It will usually be imported through the generic SCons.Tool.Tool()
7selection method.
8
9See also http://www.scons.org/wiki/CrossCompilingMingw
10"""
11
12#
13# Copyright (c) 2001, 2002, 2003, 2004 The SCons Foundation
14#
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:
22#
23# The above copyright notice and this permission notice shall be included
24# in all copies or substantial portions of the Software.
25#
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.
33#
34
35import os
36import os.path
37import string
38
39import SCons.Action
40import SCons.Builder
41import SCons.Tool
42import SCons.Util
43
44# This is what we search for to find mingw:
45prefixes32 = SCons.Util.Split("""
46    mingw32-
47    mingw32msvc-
48    i386-mingw32-
49    i486-mingw32-
50    i586-mingw32-
51    i686-mingw32-
52    i386-mingw32msvc-
53    i486-mingw32msvc-
54    i586-mingw32msvc-
55    i686-mingw32msvc-
56    i686-pc-mingw32-
57    i686-w64-mingw32-
58""")
59prefixes64 = SCons.Util.Split("""
60    amd64-mingw32-
61    amd64-mingw32msvc-
62    amd64-pc-mingw32-
63    x86_64-w64-mingw32-
64""")
65
66def find(env):
67    if env['machine'] == 'x86_64':
68        prefixes = prefixes64
69    else:
70        prefixes = prefixes32
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'):
74            return prefix
75
76    return ''
77
78def shlib_generator(target, source, env, for_signature):
79    cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS'])
80
81    dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
82    if dll: cmd.extend(['-o', dll])
83
84    cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
85
86    implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
87    if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
88
89    def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
90    if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
91
92    return [cmd]
93
94def shlib_emitter(target, source, env):
95    dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
96    no_import_lib = env.get('no_import_lib', 0)
97
98    if not dll:
99        raise SCons.Errors.UserError, "A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")
100
101    if not no_import_lib and \
102       not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
103
104        # Append an import library to the list of targets.
105        target.append(env.ReplaceIxes(dll,
106                                      'SHLIBPREFIX', 'SHLIBSUFFIX',
107                                      'LIBPREFIX', 'LIBSUFFIX'))
108
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'))
119
120    return (target, source)
121
122
123shlib_action = SCons.Action.Action(shlib_generator, '$SHLINKCOMSTR', generator=1)
124
125res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
126
127res_builder = SCons.Builder.Builder(action=res_action, suffix='.o',
128                                    source_scanner=SCons.Tool.SourceFileScanner)
129SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
130
131def generate(env):
132    mingw_prefix = find(env)
133
134    if mingw_prefix:
135        dir = os.path.dirname(env.WhereIs(mingw_prefix + 'gcc') or SCons.Util.WhereIs(mingw_prefix + 'gcc'))
136
137        # The mingw bin directory must be added to the path:
138        path = env['ENV'].get('PATH', [])
139        if not path:
140            path = []
141        if SCons.Util.is_String(path):
142            path = string.split(path, os.pathsep)
143
144        env['ENV']['PATH'] = string.join([dir] + path, os.pathsep)
145
146    # Most of mingw is the same as gcc and friends...
147    gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas']
148    for tool in gnu_tools:
149        SCons.Tool.Tool(tool)(env)
150
151    #... but a few things differ:
152    env['CC'] = mingw_prefix + 'gcc'
153    env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
154    env['CXX'] = mingw_prefix + 'g++'
155    env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
156    env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
157    env['SHLINKCOM']   = shlib_action
158    env.Append(SHLIBEMITTER = [shlib_emitter])
159    env['LINK'] = mingw_prefix + 'g++'
160    env['AR'] = mingw_prefix + 'ar'
161    env['RANLIB'] = mingw_prefix + 'ranlib'
162    env['LINK'] = mingw_prefix + 'g++'
163    env['AS'] = mingw_prefix + 'as'
164    env['WIN32DEFPREFIX']        = ''
165    env['WIN32DEFSUFFIX']        = '.def'
166    env['SHOBJSUFFIX'] = '.o'
167    env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
168
169    env['RC'] = mingw_prefix + 'windres'
170    env['RCFLAGS'] = SCons.Util.CLVar('')
171    env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS ${INCPREFIX}${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET'
172    env['BUILDERS']['RES'] = res_builder
173
174    # Some setting from the platform also have to be overridden:
175    env['OBJPREFIX']      = ''
176    env['OBJSUFFIX']      = '.o'
177    env['SHOBJPREFIX']    = '$OBJPREFIX'
178    env['SHOBJSUFFIX']    = '$OBJSUFFIX'
179    env['PROGPREFIX']     = ''
180    env['PROGSUFFIX']     = '.exe'
181    env['LIBPREFIX']      = 'lib'
182    env['LIBSUFFIX']      = '.a'
183    env['SHLIBPREFIX']    = ''
184    env['SHLIBSUFFIX']    = '.dll'
185    env['LIBPREFIXES']    = [ 'lib', '' ]
186    env['LIBSUFFIXES']    = [ '.a', '.lib' ]
187
188    # MinGW port of gdb does not handle well dwarf debug info which is the
189    # default in recent gcc versions
190    env.AppendUnique(CCFLAGS = ['-gstabs'])
191
192    env.AppendUnique(CPPDEFINES = [('__MSVCRT_VERSION__', '0x0700')])
193    #env.AppendUnique(LIBS = ['iberty'])
194    env.AppendUnique(SHLINKFLAGS = ['-Wl,--enable-stdcall-fixup'])
195    #env.AppendUnique(SHLINKFLAGS = ['-Wl,--kill-at'])
196
197def exists(env):
198    return find(env)
199