3
Tool-specific initialization for the Microsoft linker.
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()
12
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 The SCons Foundation
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:
22
# The above copyright notice and this permission notice shall be included
23
# in all copies or substantial portions of the Software.
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.
34
__revision__ = "src/engine/SCons/Tool/mslink.py 2013/03/03 09:48:35 garyo"
41
import SCons.Platform.win32
43
import SCons.Tool.msvc
44
import SCons.Tool.msvs
47
from MSCommon import msvc_setup_env_once, msvc_exists
49
def pdbGenerator(env, target, source, for_signature):
51
return ['/PDB:%s' % target[0].attributes.pdb, '/DEBUG']
52
except (AttributeError, IndexError):
55
def _dllTargets(target, source, env, for_signature, paramtp):
57
dll = env.FindIxes(target, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp)
58
if dll: listCmd.append("/out:%s"%dll.get_string(for_signature))
60
implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
61
if implib: listCmd.append("/implib:%s"%implib.get_string(for_signature))
65
def _dllSources(target, source, env, for_signature, paramtp):
68
deffile = env.FindIxes(source, "WINDOWSDEFPREFIX", "WINDOWSDEFSUFFIX")
70
# Check explicitly for a non-None deffile so that the __cmp__
71
# method of the base SCons.Util.Proxy class used for some Node
72
# proxies doesn't try to use a non-existent __dict__ attribute.
73
if deffile and src == deffile:
74
# Treat this source as a .def file.
75
listCmd.append("/def:%s" % src.get_string(for_signature))
77
# Just treat it as a generic source file.
81
def windowsShlinkTargets(target, source, env, for_signature):
82
return _dllTargets(target, source, env, for_signature, 'SHLIB')
84
def windowsShlinkSources(target, source, env, for_signature):
85
return _dllSources(target, source, env, for_signature, 'SHLIB')
87
def _windowsLdmodTargets(target, source, env, for_signature):
88
"""Get targets for loadable modules."""
89
return _dllTargets(target, source, env, for_signature, 'LDMODULE')
91
def _windowsLdmodSources(target, source, env, for_signature):
92
"""Get sources for loadable modules."""
93
return _dllSources(target, source, env, for_signature, 'LDMODULE')
95
def _dllEmitter(target, source, env, paramtp):
96
"""Common implementation of dll emitter."""
97
SCons.Tool.msvc.validate_vars(env)
102
dll = env.FindIxes(target, '%sPREFIX' % paramtp, '%sSUFFIX' % paramtp)
103
no_import_lib = env.get('no_import_lib', 0)
106
raise SCons.Errors.UserError('A shared library should have exactly one target with the suffix: %s' % env.subst('$%sSUFFIX' % paramtp))
108
insert_def = env.subst("$WINDOWS_INSERT_DEF")
109
if not insert_def in ['', '0', 0] and \
110
not env.FindIxes(source, "WINDOWSDEFPREFIX", "WINDOWSDEFSUFFIX"):
112
# append a def file to the list of sources
115
'%sPREFIX' % paramtp, '%sSUFFIX' % paramtp,
116
"WINDOWSDEFPREFIX", "WINDOWSDEFSUFFIX"))
118
version_num, suite = SCons.Tool.msvs.msvs_parse_version(env.get('MSVS_VERSION', '6.0'))
119
if version_num >= 8.0 and \
120
(env.get('WINDOWS_INSERT_MANIFEST', 0) or env.get('WINDOWS_EMBED_MANIFEST', 0)):
121
# MSVC 8 and above automatically generate .manifest files that must be installed
124
'%sPREFIX' % paramtp, '%sSUFFIX' % paramtp,
125
"WINDOWSSHLIBMANIFESTPREFIX", "WINDOWSSHLIBMANIFESTSUFFIX"))
127
if 'PDB' in env and env['PDB']:
128
pdb = env.arg2nodes('$PDB', target=target, source=source)[0]
129
extratargets.append(pdb)
130
target[0].attributes.pdb = pdb
132
if not no_import_lib and \
133
not env.FindIxes(target, "LIBPREFIX", "LIBSUFFIX"):
134
# Append an import library to the list of targets.
137
'%sPREFIX' % paramtp, '%sSUFFIX' % paramtp,
138
"LIBPREFIX", "LIBSUFFIX"))
139
# and .exp file is created if there are exports from a DLL
142
'%sPREFIX' % paramtp, '%sSUFFIX' % paramtp,
143
"WINDOWSEXPPREFIX", "WINDOWSEXPSUFFIX"))
145
return (target+extratargets, source+extrasources)
147
def windowsLibEmitter(target, source, env):
148
return _dllEmitter(target, source, env, 'SHLIB')
150
def ldmodEmitter(target, source, env):
151
"""Emitter for loadable modules.
153
Loadable modules are identical to shared libraries on Windows, but building
154
them is subject to different parameters (LDMODULE*).
156
return _dllEmitter(target, source, env, 'LDMODULE')
158
def prog_emitter(target, source, env):
159
SCons.Tool.msvc.validate_vars(env)
164
exe = env.FindIxes(target, "PROGPREFIX", "PROGSUFFIX")
166
raise SCons.Errors.UserError("An executable should have exactly one target with the suffix: %s" % env.subst("$PROGSUFFIX"))
168
version_num, suite = SCons.Tool.msvs.msvs_parse_version(env.get('MSVS_VERSION', '6.0'))
169
if version_num >= 8.0 and \
170
(env.get('WINDOWS_INSERT_MANIFEST', 0) or env.get('WINDOWS_EMBED_MANIFEST', 0)):
171
# MSVC 8 and above automatically generate .manifest files that have to be installed
174
"PROGPREFIX", "PROGSUFFIX",
175
"WINDOWSPROGMANIFESTPREFIX", "WINDOWSPROGMANIFESTSUFFIX"))
177
if 'PDB' in env and env['PDB']:
178
pdb = env.arg2nodes('$PDB', target=target, source=source)[0]
179
extratargets.append(pdb)
180
target[0].attributes.pdb = pdb
182
if version_num >= 11.0 and env.get('PCH', 0):
183
# MSVC 11 and above need the PCH object file to be added to the link line,
184
# otherwise you get link error LNK2011.
185
pchobj = SCons.Util.splitext(str(env['PCH']))[0] + '.obj'
186
# print "prog_emitter, version %s, appending pchobj %s"%(version_num, pchobj)
187
if pchobj not in extrasources:
188
extrasources.append(pchobj)
190
return (target+extratargets,source+extrasources)
192
def RegServerFunc(target, source, env):
193
if 'register' in env and env['register']:
194
ret = regServerAction([target[0]], [source[0]], env)
196
raise SCons.Errors.UserError("Unable to register %s" % target[0])
198
print "Registered %s sucessfully" % target[0]
202
# These are the actual actions run to embed the manifest.
203
# They are only called from the Check versions below.
204
embedManifestExeAction = SCons.Action.Action('$MTEXECOM')
205
embedManifestDllAction = SCons.Action.Action('$MTSHLIBCOM')
207
def embedManifestDllCheck(target, source, env):
208
"""Function run by embedManifestDllCheckAction to check for existence of manifest
209
and other conditions, and embed the manifest by calling embedManifestDllAction if so."""
210
if env.get('WINDOWS_EMBED_MANIFEST', 0):
211
manifestSrc = target[0].abspath + '.manifest'
212
if os.path.exists(manifestSrc):
213
ret = (embedManifestDllAction) ([target[0]],None,env)
215
raise SCons.Errors.UserError, "Unable to embed manifest into %s" % (target[0])
218
print '(embed: no %s.manifest found; not embedding.)'%str(target[0])
221
def embedManifestExeCheck(target, source, env):
222
"""Function run by embedManifestExeCheckAction to check for existence of manifest
223
and other conditions, and embed the manifest by calling embedManifestExeAction if so."""
224
if env.get('WINDOWS_EMBED_MANIFEST', 0):
225
manifestSrc = target[0].abspath + '.manifest'
226
if os.path.exists(manifestSrc):
227
ret = (embedManifestExeAction) ([target[0]],None,env)
229
raise SCons.Errors.UserError, "Unable to embed manifest into %s" % (target[0])
232
print '(embed: no %s.manifest found; not embedding.)'%str(target[0])
235
embedManifestDllCheckAction = SCons.Action.Action(embedManifestDllCheck, None)
236
embedManifestExeCheckAction = SCons.Action.Action(embedManifestExeCheck, None)
238
regServerAction = SCons.Action.Action("$REGSVRCOM", "$REGSVRCOMSTR")
239
regServerCheck = SCons.Action.Action(RegServerFunc, None)
240
shlibLinkAction = SCons.Action.Action('${TEMPFILE("$SHLINK $SHLINKFLAGS $_SHLINK_TARGETS $_LIBDIRFLAGS $_LIBFLAGS $_PDB $_SHLINK_SOURCES")}', '$SHLINKCOMSTR')
241
compositeShLinkAction = shlibLinkAction + regServerCheck + embedManifestDllCheckAction
242
ldmodLinkAction = SCons.Action.Action('${TEMPFILE("$LDMODULE $LDMODULEFLAGS $_LDMODULE_TARGETS $_LIBDIRFLAGS $_LIBFLAGS $_PDB $_LDMODULE_SOURCES")}', '$LDMODULECOMSTR')
243
compositeLdmodAction = ldmodLinkAction + regServerCheck + embedManifestDllCheckAction
244
exeLinkAction = SCons.Action.Action('${TEMPFILE("$LINK $LINKFLAGS /OUT:$TARGET.windows $_LIBDIRFLAGS $_LIBFLAGS $_PDB $SOURCES.windows")}', '$LINKCOMSTR')
245
compositeLinkAction = exeLinkAction + embedManifestExeCheckAction
248
"""Add Builders and construction variables for ar to an Environment."""
249
SCons.Tool.createSharedLibBuilder(env)
250
SCons.Tool.createProgBuilder(env)
252
env['SHLINK'] = '$LINK'
253
env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS /dll')
254
env['_SHLINK_TARGETS'] = windowsShlinkTargets
255
env['_SHLINK_SOURCES'] = windowsShlinkSources
256
env['SHLINKCOM'] = compositeShLinkAction
257
env.Append(SHLIBEMITTER = [windowsLibEmitter])
259
env['LINKFLAGS'] = SCons.Util.CLVar('/nologo')
260
env['_PDB'] = pdbGenerator
261
env['LINKCOM'] = compositeLinkAction
262
env.Append(PROGEMITTER = [prog_emitter])
263
env['LIBDIRPREFIX']='/LIBPATH:'
264
env['LIBDIRSUFFIX']=''
265
env['LIBLINKPREFIX']=''
266
env['LIBLINKSUFFIX']='$LIBSUFFIX'
268
env['WIN32DEFPREFIX'] = ''
269
env['WIN32DEFSUFFIX'] = '.def'
270
env['WIN32_INSERT_DEF'] = 0
271
env['WINDOWSDEFPREFIX'] = '${WIN32DEFPREFIX}'
272
env['WINDOWSDEFSUFFIX'] = '${WIN32DEFSUFFIX}'
273
env['WINDOWS_INSERT_DEF'] = '${WIN32_INSERT_DEF}'
275
env['WIN32EXPPREFIX'] = ''
276
env['WIN32EXPSUFFIX'] = '.exp'
277
env['WINDOWSEXPPREFIX'] = '${WIN32EXPPREFIX}'
278
env['WINDOWSEXPSUFFIX'] = '${WIN32EXPSUFFIX}'
280
env['WINDOWSSHLIBMANIFESTPREFIX'] = ''
281
env['WINDOWSSHLIBMANIFESTSUFFIX'] = '${SHLIBSUFFIX}.manifest'
282
env['WINDOWSPROGMANIFESTPREFIX'] = ''
283
env['WINDOWSPROGMANIFESTSUFFIX'] = '${PROGSUFFIX}.manifest'
285
env['REGSVRACTION'] = regServerCheck
286
env['REGSVR'] = os.path.join(SCons.Platform.win32.get_system_root(),'System32','regsvr32')
287
env['REGSVRFLAGS'] = '/s '
288
env['REGSVRCOM'] = '$REGSVR $REGSVRFLAGS ${TARGET.windows}'
290
env['WINDOWS_EMBED_MANIFEST'] = 0
292
#env['MTFLAGS'] = ['-hashupdate']
293
env['MTFLAGS'] = SCons.Util.CLVar('/nologo')
294
# Note: use - here to prevent build failure if no manifest produced.
295
# This seems much simpler than a fancy system using a function action to see
296
# if the manifest actually exists before trying to run mt with it.
297
env['MTEXECOM'] = '-$MT $MTFLAGS -manifest ${TARGET}.manifest $_MANIFEST_SOURCES -outputresource:$TARGET;1'
298
env['MTSHLIBCOM'] = '-$MT $MTFLAGS -manifest ${TARGET}.manifest $_MANIFEST_SOURCES -outputresource:$TARGET;2'
299
# Future work garyo 27-Feb-11
300
env['_MANIFEST_SOURCES'] = None # _windowsManifestSources
302
# Set-up ms tools paths
303
msvc_setup_env_once(env)
306
# Loadable modules are on Windows the same as shared libraries, but they
307
# are subject to different build parameters (LDMODULE* variables).
308
# Therefore LDMODULE* variables correspond as much as possible to
309
# SHLINK*/SHLIB* ones.
310
SCons.Tool.createLoadableModuleBuilder(env)
311
env['LDMODULE'] = '$SHLINK'
312
env['LDMODULEPREFIX'] = '$SHLIBPREFIX'
313
env['LDMODULESUFFIX'] = '$SHLIBSUFFIX'
314
env['LDMODULEFLAGS'] = '$SHLINKFLAGS'
315
env['_LDMODULE_TARGETS'] = _windowsLdmodTargets
316
env['_LDMODULE_SOURCES'] = _windowsLdmodSources
317
env['LDMODULEEMITTER'] = [ldmodEmitter]
318
env['LDMODULECOM'] = compositeLdmodAction
325
# indent-tabs-mode:nil
327
# vim: set expandtab tabstop=4 shiftwidth=4: