Debian Codegraph @

SCons/Tools/crossmingw.py

raw
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 """
10
11 #
12 # Copyright (c) 2001, 2002, 2003, 2004 The SCons Foundation
13 #
14 # Permission is hereby granted, free of charge, to any person obtaining
15 # a copy of this software and associated documentation files (the
16 # "Software"), to deal in the Software without restriction, including
17 # without limitation the rights to use, copy, modify, merge, publish,
18 # distribute, sublicense, and/or sell copies of the Software, and to
19 # permit persons to whom the Software is furnished to do so, subject to
20 # the following conditions:
21 #
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
26 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
27 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #
33
34 __revision__ = "/home/scons/scons/branch.0/branch.96/baseline/src/engine/SCons/Tool/mingw.py 0.96.90.D001 2005/02/15 20:11:37 knight"
35
36 import os
37 import os.path
38 import string
39
40 import SCons.Action
41 import SCons.Builder
42 import SCons.Tool
43 import SCons.Util
44
45 # This is what we search for to find mingw:
46 prefixes32 = SCons.Util.Split("""
47 mingw32-
48 mingw32msvc-
49 i386-mingw32-
50 i486-mingw32-
51 i586-mingw32-
52 i686-mingw32-
53 i386-mingw32msvc-
54 i486-mingw32msvc-
55 i586-mingw32msvc-
56 i686-mingw32msvc-
57 i386-pc-mingw32-
58 i486-pc-mingw32-
59 i586-pc-mingw32-
60 i686-pc-mingw32-
61 i386-w64-mingw32-
62 i486-w64-mingw32-
63 i586-w64-mingw32-
64 i686-w64-mingw32-
65 """)
66
67 prefixes64 = SCons.Util.Split("""
68 x86_64-w64-mingw32-
69 """)
70
71 def find(env):
72 # Explicitly specified toolchain to build Windows executables
73 # takes predominance.
74 prefix = SCons.Script.ARGUMENTS.get('XGCC_W32_PREFIX', None)
75 prefixes = prefixes32
76 if env['TARGET_ARCH'] == 'amd64':
77 prefixes = prefixes64
78
79 if prefix:
80 prefixes.insert(0, prefix)
81 for prefix in prefixes:
82 # First search in the SCons path and then the OS path:
83 if env.WhereIs(prefix + 'gcc') or SCons.Util.WhereIs(prefix + 'gcc'):
84 return prefix
85
86 return ''
87
88 def shlib_generator(target, source, env, for_signature):
89 cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS'])
90
91 dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
92 if dll: cmd.extend(['-o', dll])
93
94 cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
95
96 implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
97 if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
98
99 def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
100 if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
101
102 return [cmd]
103
104 def shlib_emitter(target, source, env):
105 dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
106 no_import_lib = env.get('no_import_lib', 0)
107
108 if not dll:
109 raise SCons.Errors.UserError("A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX"))
110
111 if not no_import_lib and \
112 not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
113
114 # Append an import library to the list of targets.
115 target.append(env.ReplaceIxes(dll,
116 'SHLIBPREFIX', 'SHLIBSUFFIX',
117 'LIBPREFIX', 'LIBSUFFIX'))
118
119 # Append a def file target if there isn't already a def file target
120 # or a def file source. There is no option to disable def file
121 # target emitting, because I can't figure out why someone would ever
122 # want to turn it off.
123 def_source = env.FindIxes(source, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
124 def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
125 if not def_source and not def_target:
126 target.append(env.ReplaceIxes(dll,
127 'SHLIBPREFIX', 'SHLIBSUFFIX',
128 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX'))
129
130 return (target, source)
131
132
133 shlib_action = SCons.Action.Action(shlib_generator, generator=1)
134
135 res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
136
137 res_builder = SCons.Builder.Builder(action=res_action, suffix='.o',
138 source_scanner=SCons.Tool.SourceFileScanner)
139 SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
140
141 def generate(env):
142 mingw_prefix = find(env)
143
144 if mingw_prefix:
145 dir = os.path.dirname(env.WhereIs(mingw_prefix + 'gcc') or SCons.Util.WhereIs(mingw_prefix + 'gcc'))
146
147 # The mingw bin directory must be added to the path:
148 path = env['ENV'].get('PATH', [])
149 if not path:
150 path = []
151 if SCons.Util.is_String(path):
152 path = str.split(path, os.pathsep)
153
154 env['ENV']['PATH'] = str.join(os.pathsep, [dir] + path)
155
156 # Most of mingw is the same as gcc and friends...
157 gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas']
158 for tool in gnu_tools:
159 SCons.Tool.Tool(tool)(env)
160
161 #... but a few things differ:
162 env['CC'] = mingw_prefix + 'gcc'
163 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
164 env['CXX'] = mingw_prefix + 'g++'
165 env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
166 env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
167 env['SHLINKCOM'] = shlib_action
168 env.Append(SHLIBEMITTER = [shlib_emitter])
169 env['LINK'] = mingw_prefix + 'g++'
170 env['AS'] = mingw_prefix + 'as'
171 env['AR'] = mingw_prefix + 'ar'
172 env['RANLIB'] = mingw_prefix + 'ranlib'
173 env['WIN32DEFPREFIX'] = ''
174 env['WIN32DEFSUFFIX'] = '.def'
175 env['SHOBJSUFFIX'] = '.o'
176 env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
177
178 env['RC'] = mingw_prefix + 'windres'
179 env['RCFLAGS'] = SCons.Util.CLVar('')
180 env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS ${INCPREFIX}${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET'
181 env['BUILDERS']['RES'] = res_builder
182
183 # Some setting from the platform also have to be overridden:
184 env['OBJPREFIX'] = ''
185 env['OBJSUFFIX'] = '.o'
186 env['LIBPREFIX'] = 'lib'
187 env['LIBSUFFIX'] = '.a'
188 env['SHOBJPREFIX'] = '$OBJPREFIX'
189 env['SHOBJSUFFIX'] = '$OBJSUFFIX'
190 env['PROGPREFIX'] = ''
191 env['PROGSUFFIX'] = '.exe'
192 env['SHLIBPREFIX'] = ''
193 env['SHLIBSUFFIX'] = '.dll'
194 env['LIBPREFIXES'] = [ '$LIBPREFIX' ]
195 env['LIBSUFFIXES'] = [ '$LIBSUFFIX' ]
196
197 def exists(env):
198 return find(env)