Merge commit 'origin/gallium-master-merge'
[mesa.git] / scons / crossmingw.py
1 """SCons.Tool.gcc
2
3 Tool-specific initialization for MinGW (http://www.mingw.org/)
4
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()
7 selection method.
8
9 See 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
35 import os
36 import os.path
37 import string
38
39 import SCons.Action
40 import SCons.Builder
41 import SCons.Tool
42 import SCons.Util
43
44 # This is what we search for to find mingw:
45 prefixes = 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 """)
57
58 def find(env):
59 for prefix in prefixes:
60 # First search in the SCons path and then the OS path:
61 if env.WhereIs(prefix + 'gcc') or SCons.Util.WhereIs(prefix + 'gcc'):
62 return prefix
63
64 return ''
65
66 def shlib_generator(target, source, env, for_signature):
67 cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS'])
68
69 dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
70 if dll: cmd.extend(['-o', dll])
71
72 cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
73
74 implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
75 if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
76
77 def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
78 if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
79
80 return [cmd]
81
82 def shlib_emitter(target, source, env):
83 dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
84 no_import_lib = env.get('no_import_lib', 0)
85
86 if not dll:
87 raise SCons.Errors.UserError, "A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")
88
89 if not no_import_lib and \
90 not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
91
92 # Append an import library to the list of targets.
93 target.append(env.ReplaceIxes(dll,
94 'SHLIBPREFIX', 'SHLIBSUFFIX',
95 'LIBPREFIX', 'LIBSUFFIX'))
96
97 # Append a def file target if there isn't already a def file target
98 # or a def file source. There is no option to disable def file
99 # target emitting, because I can't figure out why someone would ever
100 # want to turn it off.
101 def_source = env.FindIxes(source, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
102 def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
103 if not def_source and not def_target:
104 target.append(env.ReplaceIxes(dll,
105 'SHLIBPREFIX', 'SHLIBSUFFIX',
106 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX'))
107
108 return (target, source)
109
110
111 shlib_action = SCons.Action.Action(shlib_generator, generator=1)
112
113 res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
114
115 res_builder = SCons.Builder.Builder(action=res_action, suffix='.o',
116 source_scanner=SCons.Tool.SourceFileScanner)
117 SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
118
119 def generate(env):
120 mingw_prefix = find(env)
121
122 if mingw_prefix:
123 dir = os.path.dirname(env.WhereIs(mingw_prefix + 'gcc') or SCons.Util.WhereIs(mingw_prefix + 'gcc'))
124
125 # The mingw bin directory must be added to the path:
126 path = env['ENV'].get('PATH', [])
127 if not path:
128 path = []
129 if SCons.Util.is_String(path):
130 path = string.split(path, os.pathsep)
131
132 env['ENV']['PATH'] = string.join([dir] + path, os.pathsep)
133
134 # Most of mingw is the same as gcc and friends...
135 gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas']
136 for tool in gnu_tools:
137 SCons.Tool.Tool(tool)(env)
138
139 #... but a few things differ:
140 env['CC'] = mingw_prefix + 'gcc'
141 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
142 env['CXX'] = mingw_prefix + 'g++'
143 env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
144 env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
145 env['SHLINKCOM'] = shlib_action
146 env.Append(SHLIBEMITTER = [shlib_emitter])
147 env['LINK'] = mingw_prefix + 'g++'
148 env['AR'] = mingw_prefix + 'ar'
149 env['RANLIB'] = mingw_prefix + 'ranlib'
150 env['LINK'] = mingw_prefix + 'g++'
151 env['AS'] = mingw_prefix + 'as'
152 env['WIN32DEFPREFIX'] = ''
153 env['WIN32DEFSUFFIX'] = '.def'
154 env['SHOBJSUFFIX'] = '.o'
155 env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
156
157 env['RC'] = mingw_prefix + 'windres'
158 env['RCFLAGS'] = SCons.Util.CLVar('')
159 env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS ${INCPREFIX}${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET'
160 env['BUILDERS']['RES'] = res_builder
161
162 # Some setting from the platform also have to be overridden:
163 env['OBJPREFIX'] = ''
164 env['OBJSUFFIX'] = '.o'
165 env['SHOBJPREFIX'] = '$OBJPREFIX'
166 env['SHOBJSUFFIX'] = '$OBJSUFFIX'
167 env['PROGPREFIX'] = ''
168 env['PROGSUFFIX'] = '.exe'
169 env['LIBPREFIX'] = 'lib'
170 env['LIBSUFFIX'] = '.a'
171 env['SHLIBPREFIX'] = ''
172 env['SHLIBSUFFIX'] = '.dll'
173 env['LIBPREFIXES'] = [ 'lib', '' ]
174 env['LIBSUFFIXES'] = [ '.a', '.lib' ]
175
176 # MinGW port of gdb does not handle well dwarf debug info which is the
177 # default in recent gcc versions
178 env.AppendUnique(CFLAGS = ['-gstabs'])
179
180 env.AppendUnique(LIBS = ['iberty'])
181 env.AppendUnique(SHLINKFLAGS = ['-Wl,--enable-stdcall-fixup'])
182 #env.AppendUnique(SHLINKFLAGS = ['-Wl,--kill-at'])
183
184 def exists(env):
185 return find(env)