Refactor to use real Nodes for command-line attributes and eliminate PathList. ...
[scons.git] / src / 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__
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__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
35
36 import os.path
37 import os
38 import SCons.Tool
39 import SCons.Util
40 import string
41
42 # This is what we search for to find mingw:
43 key_program = 'mingw32-gcc'
44
45 def find(env):
46     # First search in the SCons path and then the OS path:
47     return env.WhereIs(key_program) or SCons.Util.WhereIs(key_program)
48
49 def shlib_generator(target, source, env, for_signature):
50     cmd = ['$SHLINK', '$SHLINKFLAGS'] 
51
52     dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
53     if dll: cmd.extend(['-o', dll])
54
55     cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
56
57     implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
58     if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
59
60     def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
61     if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
62
63     return [cmd]
64
65 def shlib_emitter(target, source, env):
66     dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
67     no_import_lib = env.get('no_import_lib', 0)
68
69     if not dll:
70         raise SCons.Errors.UserError, "A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")
71     
72     if not no_import_lib and \
73        not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
74
75         # Append an import library to the list of targets.
76         target.append(env.ReplaceIxes(dll,  
77                                       'SHLIBPREFIX', 'SHLIBSUFFIX',
78                                       'LIBPREFIX', 'LIBSUFFIX'))
79
80     # Append a def file target if there isn't already a def file target
81     # or a def file source. There is no option to disable def file
82     # target emitting, because I can't figure out why someone would ever
83     # want to turn it off.
84     def_source = env.FindIxes(source, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
85     def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
86     if not def_source and not def_target:
87         target.append(env.ReplaceIxes(dll,  
88                                       'SHLIBPREFIX', 'SHLIBSUFFIX',
89                                       'WIN32DEFPREFIX', 'WIN32DEFSUFFIX'))
90     
91     return (target, source)
92                          
93
94 shlib_action = SCons.Action.CommandGenerator(shlib_generator)
95
96 res_builder = SCons.Builder.Builder(action='$RCCOM', suffix='.o')
97
98 def generate(env):
99     mingw = find(env)
100     if mingw:
101         dir = os.path.dirname(mingw)
102
103         # The mingw bin directory must be added to the path:
104         path = env['ENV'].get('PATH', [])
105         if not path: 
106             path = []
107         if SCons.Util.is_String(path):
108             path = string.split(path, os.pathsep)
109
110         env['ENV']['PATH'] = string.join([dir] + path, os.pathsep)
111         
112
113     # Most of mingw is the same as gcc and friends...
114     gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas']
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'] = '$CCFLAGS'
121     env['CXX'] = 'g++'
122     env['SHCXXFLAGS'] = '$CXXFLAGS'
123     env['SHLINKFLAGS'] = '$LINKFLAGS -shared'
124     env['SHLINKCOM']   = shlib_action
125     env['SHLIBEMITTER']= shlib_emitter
126     env['LINK'] = 'g++'
127     env['AS'] = 'as'
128     env['WIN32DEFPREFIX']        = ''
129     env['WIN32DEFSUFFIX']        = '.def'
130     env['SHOBJSUFFIX'] = '.o'
131     env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
132
133     env['RC'] = 'windres'
134     env['RCFLAGS'] = ''
135     env['RCINCFLAGS'] = '$( ${_concat(RCINCPREFIX, CPPPATH, RCINCSUFFIX, locals(), globals(), RDirs)} $)'
136     env['RCINCPREFIX'] = '--include-dir '
137     env['RCINCSUFFIX'] = ''
138     env['RCCOM'] = '$RC $RCINCFLAGS $RCFLAGS -i $SOURCE -o $TARGET'
139     CScan = env.get_scanner('.c')
140     if CScan:
141         CScan.add_skey('.rc')
142     env['BUILDERS']['RES'] = res_builder
143     
144     # Some setting from the platform also have to be overridden:
145     env['OBJSUFFIX'] = '.o'
146     env['LIBPREFIX'] = 'lib'
147     env['LIBSUFFIX'] = '.a'
148
149 def exists(env):
150     return find(env)