Add some documentation to the SCons-version-switching hack
[senf.git] / tools / scons-1.2.0 / engine / SCons / Tool / mingw.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 """
10
11 #
12 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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__ = "src/engine/SCons/Tool/mingw.py 3842 2008/12/20 22:59:52 scons"
35
36 import os
37 import os.path
38 import string
39
40 import SCons.Action
41 import SCons.Builder
42 import SCons.Defaults
43 import SCons.Tool
44 import SCons.Util
45
46 # This is what we search for to find mingw:
47 key_program = 'mingw32-gcc'
48
49 def find(env):
50     # First search in the SCons path and then the OS path:
51     return env.WhereIs(key_program) or SCons.Util.WhereIs(key_program)
52
53 def shlib_generator(target, source, env, for_signature):
54     cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS']) 
55
56     dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
57     if dll: cmd.extend(['-o', dll])
58
59     cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
60
61     implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
62     if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
63
64     def_target = env.FindIxes(target, 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
65     if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
66
67     return [cmd]
68
69 def shlib_emitter(target, source, env):
70     dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
71     no_import_lib = env.get('no_import_lib', 0)
72
73     if not dll:
74         raise SCons.Errors.UserError, "A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")
75     
76     if not no_import_lib and \
77        not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
78
79         # Append an import library to the list of targets.
80         target.append(env.ReplaceIxes(dll,  
81                                       'SHLIBPREFIX', 'SHLIBSUFFIX',
82                                       'LIBPREFIX', 'LIBSUFFIX'))
83
84     # Append a def file target if there isn't already a def file target
85     # or a def file source. There is no option to disable def file
86     # target emitting, because I can't figure out why someone would ever
87     # want to turn it off.
88     def_source = env.FindIxes(source, 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
89     def_target = env.FindIxes(target, 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
90     if not def_source and not def_target:
91         target.append(env.ReplaceIxes(dll,  
92                                       'SHLIBPREFIX', 'SHLIBSUFFIX',
93                                       'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX'))
94     
95     return (target, source)
96                          
97
98 shlib_action = SCons.Action.Action(shlib_generator, generator=1)
99
100 res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
101
102 res_builder = SCons.Builder.Builder(action=res_action, suffix='.o',
103                                     source_scanner=SCons.Tool.SourceFileScanner)
104 SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
105
106 def generate(env):
107     mingw = find(env)
108     if mingw:
109         dir = os.path.dirname(mingw)
110         env.PrependENVPath('PATH', dir )
111         
112
113     # Most of mingw is the same as gcc and friends...
114     gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas', 'm4']
115     for tool in gnu_tools:
116         SCons.Tool.Tool(tool)(env)
117
118     #... but a few things differ:
119     env['CC'] = 'gcc'
120     env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
121     env['CXX'] = 'g++'
122     env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
123     env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
124     env['SHLINKCOM']   = shlib_action
125     env['LDMODULECOM'] = shlib_action
126     env.Append(SHLIBEMITTER = [shlib_emitter])
127     env['AS'] = 'as'
128
129     env['WIN32DEFPREFIX']        = ''
130     env['WIN32DEFSUFFIX']        = '.def'
131     env['WINDOWSDEFPREFIX']      = '${WIN32DEFPREFIX}'
132     env['WINDOWSDEFSUFFIX']      = '${WIN32DEFSUFFIX}'
133
134     env['SHOBJSUFFIX'] = '.o'
135     env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
136
137     env['RC'] = 'windres'
138     env['RCFLAGS'] = SCons.Util.CLVar('')
139     env['RCINCFLAGS'] = '$( ${_concat(RCINCPREFIX, CPPPATH, RCINCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
140     env['RCINCPREFIX'] = '--include-dir '
141     env['RCINCSUFFIX'] = ''
142     env['RCCOM'] = '$RC $_CPPDEFFLAGS $RCINCFLAGS ${RCINCPREFIX} ${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET'
143     env['BUILDERS']['RES'] = res_builder
144     
145     # Some setting from the platform also have to be overridden:
146     env['OBJSUFFIX'] = '.o'
147     env['LIBPREFIX'] = 'lib'
148     env['LIBSUFFIX'] = '.a'
149
150 def exists(env):
151     return find(env)