2f05b46c6f3b1ef38292a9b882339c9bcd1ea821
[scons.git] / src / engine / SCons / Tool / mslink.py
1 """SCons.Tool.mslink
2
3 Tool-specific initialization for the Microsoft linker.
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 Steven Knight
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
38 import SCons.Defaults
39 import SCons.Errors
40 import SCons.Action
41
42 from SCons.Tool.msvc import get_msdev_paths
43
44 def win32TempFileMunge(env, cmd_list, for_signature): 
45     """Given a list of command line arguments, see if it is too
46     long to pass to the win32 command line interpreter.  If so,
47     create a temp file, then pass "@tempfile" as the sole argument
48     to the supplied command (which is the first element of cmd_list).
49     Otherwise, just return [cmd_list]."""
50     cmd = env.subst_list(cmd_list)[0]
51     if for_signature or \
52        (reduce(lambda x, y: x + len(y), cmd, 0) + len(cmd)) <= 2048:
53         return [cmd_list]
54     else:
55         import tempfile
56         # We do a normpath because mktemp() has what appears to be
57         # a bug in Win32 that will use a forward slash as a path
58         # delimiter.  Win32's link mistakes that for a command line
59         # switch and barfs.
60         tmp = os.path.normpath(tempfile.mktemp())
61         args = map(SCons.Util.quote_spaces, cmd[1:])
62         open(tmp, 'w').write(string.join(args, " ") + "\n")
63         return [ [cmd[0], '@' + tmp],
64                  ['del', tmp] ]
65     
66 def win32LinkGenerator(env, target, source, for_signature, **kw):
67     args = [ '$LINK', '$LINKFLAGS', '/OUT:%s' % target[0],
68              '$(', '$_LIBDIRFLAGS', '$)', '$_LIBFLAGS' ]
69     args.extend(map(SCons.Util.to_String, source))
70     return win32TempFileMunge(env, args, for_signature)
71
72 def win32LibGenerator(target, source, env, for_signature, no_import_lib=0):
73     listCmd = [ "$SHLINK", "$SHLINKFLAGS" ]
74
75     for tgt in target:
76         ext = os.path.splitext(str(tgt))[1]
77         if ext == env.subst("$LIBSUFFIX"):
78             # Put it on the command line as an import library.
79             if no_import_lib:
80                 raise SCons.Errors.UserError, "%s: You cannot specify a .lib file as a target of a shared library build if no_import_library is nonzero." % tgt
81             listCmd.append("${WIN32IMPLIBPREFIX}%s" % tgt)
82         else:
83             listCmd.append("${WIN32DLLPREFIX}%s" % tgt)
84
85     listCmd.extend([ '$_LIBDIRFLAGS', '$_LIBFLAGS' ])
86     for src in source:
87         ext = os.path.splitext(str(src))[1]
88         if ext == env.subst("$WIN32DEFSUFFIX"):
89             # Treat this source as a .def file.
90             listCmd.append("${WIN32DEFPREFIX}%s" % src)
91         else:
92             # Just treat it as a generic source file.
93             listCmd.append(str(src))
94     return win32TempFileMunge(env, listCmd, for_signature)
95
96 def win32LibEmitter(target, source, env, no_import_lib=0):
97     dll = None
98     for tgt in target:
99         ext = os.path.splitext(str(tgt))[1]
100         if ext == env.subst("$SHLIBSUFFIX"):
101             dll = tgt
102             break
103     if not dll:
104         raise SCons.Errors.UserError, "A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")
105         
106     if env.has_key("WIN32_INSERT_DEF") and \
107        env["WIN32_INSERT_DEF"] and \
108        not '.def' in map(lambda x: os.path.split(str(x))[1],
109                          source):
110
111         # append a def file to the list of sources
112         source.append("%s%s" % (os.path.splitext(str(dll))[0],
113                                 env.subst("$WIN32DEFSUFFIX")))
114     if not no_import_lib and \
115        not env.subst("$LIBSUFFIX") in \
116        map(lambda x: os.path.split(str(x))[1], target):
117         # Append an import library to the list of targets.
118         target.append("%s%s%s" % (env.subst("$LIBPREFIX"),
119                                   os.path.splitext(str(dll))[0],
120                                   env.subst("$LIBSUFFIX")))
121     return (target, source)
122
123 ShLibAction = SCons.Action.CommandGenerator(win32LibGenerator)
124 LinkAction = SCons.Action.CommandGenerator(win32LinkGenerator)
125
126 def generate(env, platform):
127     """Add Builders and construction variables for ar to an Environment."""
128     env['BUILDERS']['SharedLibrary'] = SCons.Defaults.SharedLibrary
129     env['BUILDERS']['Program'] = SCons.Defaults.Program
130     
131     env['SHLINK']      = '$LINK'
132     env['SHLINKFLAGS'] = '$LINKFLAGS /dll'
133     env['SHLINKCOM']   = ShLibAction
134     env['SHLIBEMITTER']= win32LibEmitter
135     env['LINK']        = 'link'
136     env['LINKFLAGS']   = '/nologo'
137     env['LINKCOM']     = LinkAction
138     env['LIBDIRPREFIX']='/LIBPATH:'
139     env['LIBDIRSUFFIX']=''
140     env['LIBLINKPREFIX']=''
141     env['LIBLINKSUFFIX']='$LIBSUFFIX'
142
143     env['WIN32DEFPREFIX']        = '/def:'
144     env['WIN32DEFSUFFIX']        = '.def'
145     env['WIN32DLLPREFIX']        = '/out:'
146     env['WIN32IMPLIBPREFIX']     = '/implib:'
147     env['WIN32_INSERT_DEF']      = 0
148
149     include_path, lib_path, exe_path = get_msdev_paths()
150     env['ENV']['LIB']            = lib_path
151     env['ENV']['PATH']           = exe_path