| 1 |
"""engine.SCons.Tool.mstoolkit.py |
| 2 |
|
| 3 |
Tool-specific initialization for Microsoft Visual C/C++ Toolkit Commandline |
| 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 |
# Based on http://www.scons.org/cgi-bin/wiki/MicrosoftPlatform |
| 12 |
|
| 13 |
# |
| 14 |
# Copyright (c) 2004 John Connors |
| 15 |
# |
| 16 |
# Permission is hereby granted, free of charge, to any person obtaining |
| 17 |
# a copy of this software and associated documentation files (the |
| 18 |
# "Software"), to deal in the Software without restriction, including |
| 19 |
# without limitation the rights to use, copy, modify, merge, publish, |
| 20 |
# distribute, sublicense, and/or sell copies of the Software, and to |
| 21 |
# permit persons to whom the Software is furnished to do so, subject to |
| 22 |
# the following conditions: |
| 23 |
# |
| 24 |
# The above copyright notice and this permission notice shall be included |
| 25 |
# in all copies or substantial portions of the Software. |
| 26 |
# |
| 27 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 28 |
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 29 |
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 31 |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 32 |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 33 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 |
# |
| 35 |
|
| 36 |
|
| 37 |
import os.path |
| 38 |
import re |
| 39 |
import string |
| 40 |
import types |
| 41 |
|
| 42 |
import SCons.Action |
| 43 |
import SCons.Builder |
| 44 |
import SCons.Errors |
| 45 |
import SCons.Platform.win32 |
| 46 |
import SCons.Tool |
| 47 |
import SCons.Util |
| 48 |
import SCons.Warnings |
| 49 |
|
| 50 |
CSuffixes = ['.c', '.C'] |
| 51 |
CXXSuffixes = ['.cc', '.cpp', '.cxx', '.c++', '.C++'] |
| 52 |
|
| 53 |
def get_msvctoolkit_paths(): |
| 54 |
"""Return a 4-tuple of (INCLUDE, LIB, PATH, TOOLKIT) as the values of those |
| 55 |
three environment variables that should be set in order to execute |
| 56 |
the MSVC .NET tools properly, if the information wasn't available |
| 57 |
from the registry.""" |
| 58 |
|
| 59 |
MSToolkitDir = None |
| 60 |
paths = {} |
| 61 |
exe_path = '' |
| 62 |
lib_path = '' |
| 63 |
include_path = '' |
| 64 |
|
| 65 |
# First, we get the shell folder for this user: |
| 66 |
if not SCons.Util.can_read_reg: |
| 67 |
raise SCons.Errors.InternalError("No Windows registry module was found") |
| 68 |
|
| 69 |
# look for toolkit |
| 70 |
if 'VCToolkitInstallDir' in os.environ: |
| 71 |
MSToolkitDir = os.path.normpath(os.environ['VCToolkitInstallDir']) |
| 72 |
elif 'VCToolsInstallDir' in os.environ: |
| 73 |
MSToolkitDir = os.path.normpath(os.environ['VCToolsInstallDir']) |
| 74 |
else: |
| 75 |
raise SCons.Errors.InternalError("Microsoft Visual C++ Toolkit directory was not found in the `VCToolkitInstallDir` or `VCToolsInstallDir` environment variables.") |
| 76 |
|
| 77 |
# look for platform sdk |
| 78 |
if 'MSSdk' in os.environ: |
| 79 |
PlatformSDKDir = os.path.normpath(os.environ['MSSdk']) |
| 80 |
else: |
| 81 |
try: |
| 82 |
PlatformSDKDir = SCons.Util.RegGetValue(SCons.Util.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\MicrosoftSDK\Directories\Install Dir')[0] |
| 83 |
PlatformSDKDir = str(PlatformSDKDir) |
| 84 |
except SCons.Util.RegError: |
| 85 |
try: |
| 86 |
PlatformSDKDir = SCons.Util.RegGetValue(SCons.Util.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\MicrosoftSDK\InstalledSDKs\8F9E5EF3-A9A5-491B-A889-C58EFFECE8B3\Install Dir')[0] |
| 87 |
PlatformSDKDir = str(PlatformSDKDir) |
| 88 |
except SCons.Util.RegError: |
| 89 |
raise SCons.Errors.InternalError("The Platform SDK directory was not found in the registry or in the `MSSdk` environment variable.") |
| 90 |
|
| 91 |
include_path = r'%s\include;%s\include' % (PlatformSDKDir, MSToolkitDir) |
| 92 |
lib_path = r'%s\lib;%s\lib' % (PlatformSDKDir, MSToolkitDir) |
| 93 |
exe_path = r'%s\bin;%s\bin\win95;%s\bin' % (MSToolkitDir, PlatformSDKDir, PlatformSDKDir) |
| 94 |
return (include_path, lib_path, exe_path, PlatformSDKDir) |
| 95 |
|
| 96 |
def validate_vars(env): |
| 97 |
"""Validate the PDB, PCH, and PCHSTOP construction variables.""" |
| 98 |
if 'PCH' in env and env['PCH']: |
| 99 |
if not 'PCHSTOP' in env: |
| 100 |
raise SCons.Errors.UserError("The PCHSTOP construction must be defined if PCH is defined.") |
| 101 |
if not SCons.Util.is_String(env['PCHSTOP']): |
| 102 |
raise SCons.Errors.UserError("The PCHSTOP construction variable must be a string: %r" % env['PCHSTOP']) |
| 103 |
|
| 104 |
def pch_emitter(target, source, env): |
| 105 |
"""Sets up the PDB dependencies for a pch file, and adds the object |
| 106 |
file target.""" |
| 107 |
|
| 108 |
validate_vars(env) |
| 109 |
|
| 110 |
pch = None |
| 111 |
obj = None |
| 112 |
|
| 113 |
for t in target: |
| 114 |
if SCons.Util.splitext(str(t))[1] == '.pch': |
| 115 |
pch = t |
| 116 |
if SCons.Util.splitext(str(t))[1] == '.obj': |
| 117 |
obj = t |
| 118 |
|
| 119 |
if not obj: |
| 120 |
obj = SCons.Util.splitext(str(pch))[0]+'.obj' |
| 121 |
|
| 122 |
target = [pch, obj] # pch must be first, and obj second for the PCHCOM to work |
| 123 |
|
| 124 |
if 'PDB' in env and env['PDB']: |
| 125 |
env.SideEffect(env['PDB'], target) |
| 126 |
env.Precious(env['PDB']) |
| 127 |
|
| 128 |
return (target, source) |
| 129 |
|
| 130 |
def object_emitter(target, source, env, parent_emitter): |
| 131 |
"""Sets up the PDB and PCH dependencies for an object file.""" |
| 132 |
|
| 133 |
validate_vars(env) |
| 134 |
|
| 135 |
parent_emitter(target, source, env) |
| 136 |
|
| 137 |
if 'PDB' in env and env['PDB']: |
| 138 |
env.SideEffect(env['PDB'], target) |
| 139 |
env.Precious(env['PDB']) |
| 140 |
|
| 141 |
if 'PCH' in env and env['PCH']: |
| 142 |
env.Depends(target, env['PCH']) |
| 143 |
|
| 144 |
return (target, source) |
| 145 |
|
| 146 |
def static_object_emitter(target, source, env): |
| 147 |
return object_emitter(target, source, env, |
| 148 |
SCons.Defaults.StaticObjectEmitter) |
| 149 |
|
| 150 |
def shared_object_emitter(target, source, env): |
| 151 |
return object_emitter(target, source, env, |
| 152 |
SCons.Defaults.SharedObjectEmitter) |
| 153 |
|
| 154 |
pch_builder = SCons.Builder.Builder(action='$PCHCOM', suffix='.pch', emitter=pch_emitter) |
| 155 |
res_builder = SCons.Builder.Builder(action='$RCCOM', suffix='.res') |
| 156 |
|
| 157 |
def pdbGenerator(env, target, source, for_signature): |
| 158 |
if target and 'PDB' in env and env['PDB']: |
| 159 |
return ['/PDB:%s'%target[0].File(env['PDB']).get_string(for_signature), |
| 160 |
'/DEBUG'] |
| 161 |
|
| 162 |
def win32ShlinkTargets(target, source, env, for_signature): |
| 163 |
listCmd = [] |
| 164 |
dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX') |
| 165 |
if dll: listCmd.append("/out:%s"%dll.get_string(for_signature)) |
| 166 |
|
| 167 |
implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX') |
| 168 |
if implib: listCmd.append("/implib:%s"%implib.get_string(for_signature)) |
| 169 |
|
| 170 |
return listCmd |
| 171 |
|
| 172 |
def win32ShlinkSources(target, source, env, for_signature): |
| 173 |
listCmd = [] |
| 174 |
|
| 175 |
deffile = env.FindIxes(source, "WIN32DEFPREFIX", "WIN32DEFSUFFIX") |
| 176 |
for src in source: |
| 177 |
if deffile is not None and src == deffile: |
| 178 |
# Treat this source as a .def file. |
| 179 |
listCmd.append("/def:%s" % src.get_string(for_signature)) |
| 180 |
else: |
| 181 |
# Just treat it as a generic source file. |
| 182 |
listCmd.append(src) |
| 183 |
return listCmd |
| 184 |
|
| 185 |
def win32LibEmitter(target, source, env): |
| 186 |
# SCons.Tool.msvc.validate_vars(env) |
| 187 |
|
| 188 |
dll = env.FindIxes(target, "SHLIBPREFIX", "SHLIBSUFFIX") |
| 189 |
no_import_lib = env.get('no_import_lib', 0) |
| 190 |
|
| 191 |
if not dll: |
| 192 |
raise SCons.Errors.UserError("A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")) |
| 193 |
|
| 194 |
if env.get("WIN32_INSERT_DEF", 0) and \ |
| 195 |
not env.FindIxes(source, "WIN32DEFPREFIX", "WIN32DEFSUFFIX"): |
| 196 |
|
| 197 |
# append a def file to the list of sources |
| 198 |
source.append(env.ReplaceIxes(dll, |
| 199 |
"SHLIBPREFIX", "SHLIBSUFFIX", |
| 200 |
"WIN32DEFPREFIX", "WIN32DEFSUFFIX")) |
| 201 |
|
| 202 |
if 'PDB' in env and env['PDB']: |
| 203 |
env.SideEffect(env['PDB'], target) |
| 204 |
env.Precious(env['PDB']) |
| 205 |
|
| 206 |
if not no_import_lib and \ |
| 207 |
not env.FindIxes(target, "LIBPREFIX", "LIBSUFFIX"): |
| 208 |
# Append an import library to the list of targets. |
| 209 |
target.append(env.ReplaceIxes(dll, |
| 210 |
"SHLIBPREFIX", "SHLIBSUFFIX", |
| 211 |
"LIBPREFIX", "LIBSUFFIX")) |
| 212 |
# and .exp file is created if there are exports from a DLL |
| 213 |
target.append(env.ReplaceIxes(dll, |
| 214 |
"SHLIBPREFIX", "SHLIBSUFFIX", |
| 215 |
"WIN32EXPPREFIX", "WIN32EXPSUFFIX")) |
| 216 |
|
| 217 |
return (target, source) |
| 218 |
|
| 219 |
def prog_emitter(target, source, env): |
| 220 |
#SCons.Tool.msvc.validate_vars(env) |
| 221 |
|
| 222 |
if 'PDB' in env and env['PDB']: |
| 223 |
env.SideEffect(env['PDB'], target) |
| 224 |
env.Precious(env['PDB']) |
| 225 |
|
| 226 |
return (target,source) |
| 227 |
|
| 228 |
def RegServerFunc(target, source, env): |
| 229 |
if 'register' in env and env['register']: |
| 230 |
ret = regServerAction([target[0]], [source[0]], env) |
| 231 |
if ret: |
| 232 |
raise SCons.Errors.UserError("Unable to register %s" % target[0]) |
| 233 |
else: |
| 234 |
print("Registered %s successfully" % target[0]) |
| 235 |
return ret |
| 236 |
return 0 |
| 237 |
|
| 238 |
regServerAction = SCons.Action.Action("$REGSVRCOM") |
| 239 |
regServerCheck = SCons.Action.Action(RegServerFunc, None) |
| 240 |
shlibLinkAction = SCons.Action.Action('${TEMPFILE("$SHLINK $SHLINKFLAGS $_SHLINK_TARGETS $( $_LIBDIRFLAGS $) $_LIBFLAGS $_PDB $_SHLINK_SOURCES")}') |
| 241 |
compositeLinkAction = shlibLinkAction + regServerCheck |
| 242 |
|
| 243 |
def generate(env): |
| 244 |
"""Add Builders and construction variables for MSVC++ to an Environment.""" |
| 245 |
static_obj, shared_obj = SCons.Tool.createObjBuilders(env) |
| 246 |
|
| 247 |
for suffix in CSuffixes: |
| 248 |
static_obj.add_action(suffix, SCons.Defaults.CAction) |
| 249 |
shared_obj.add_action(suffix, SCons.Defaults.ShCAction) |
| 250 |
|
| 251 |
for suffix in CXXSuffixes: |
| 252 |
static_obj.add_action(suffix, SCons.Defaults.CXXAction) |
| 253 |
shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction) |
| 254 |
|
| 255 |
SCons.Tool.createStaticLibBuilder(env) |
| 256 |
SCons.Tool.createSharedLibBuilder(env) |
| 257 |
SCons.Tool.createProgBuilder(env) |
| 258 |
|
| 259 |
env['CCPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Zi /Fd%s"%File(PDB)) or ""}']) |
| 260 |
env['CCPCHFLAGS'] = SCons.Util.CLVar(['${(PCH and "/Yu%s /Fp%s"%(PCHSTOP or "",File(PCH))) or ""}']) |
| 261 |
env['CCCOMFLAGS'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Fo$TARGET $CCPCHFLAGS $CCPDBFLAGS' |
| 262 |
env['CC'] = 'cl' |
| 263 |
env['CCFLAGS'] = SCons.Util.CLVar('/nologo') |
| 264 |
env['CCCOM'] = '$CC $CCFLAGS $CCCOMFLAGS' |
| 265 |
env['SHCC'] = '$CC' |
| 266 |
env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS') |
| 267 |
env['SHCCCOM'] = '$SHCC $SHCCFLAGS $CCCOMFLAGS' |
| 268 |
env['CXX'] = '$CC' |
| 269 |
env['CXXFLAGS'] = SCons.Util.CLVar('$CCFLAGS $( /TP $)') |
| 270 |
env['CXXCOM'] = '$CXX $CXXFLAGS $CCCOMFLAGS' |
| 271 |
env['SHCXX'] = '$CXX' |
| 272 |
env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS') |
| 273 |
env['SHCXXCOM'] = '$SHCXX $SHCXXFLAGS $CCCOMFLAGS' |
| 274 |
env['CPPDEFPREFIX'] = '/D' |
| 275 |
env['CPPDEFSUFFIX'] = '' |
| 276 |
env['INCPREFIX'] = '/I' |
| 277 |
env['INCSUFFIX'] = '' |
| 278 |
env['OBJEMITTER'] = static_object_emitter |
| 279 |
env['SHOBJEMITTER'] = shared_object_emitter |
| 280 |
env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1 |
| 281 |
|
| 282 |
env['RC'] = 'rc' |
| 283 |
env['RCFLAGS'] = SCons.Util.CLVar('') |
| 284 |
env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS $RCFLAGS /fo$TARGET $SOURCES' |
| 285 |
CScan = env.get_scanner('.c') |
| 286 |
if CScan: |
| 287 |
CScan.add_skey('.rc') |
| 288 |
env['BUILDERS']['RES'] = res_builder |
| 289 |
|
| 290 |
|
| 291 |
include_path, lib_path, exe_path, sdk_path = "", "", "", "" |
| 292 |
targ_arc = env.get('TARGET_ARCH', 'x86') |
| 293 |
|
| 294 |
if "None" == env.get('MSVC_USE_SCRIPT', '!'): |
| 295 |
for x in ['INCLUDE', 'LIB', 'PATH', 'CL', '_CL_', 'LINK', '_LINK_', 'ML']: env['ENV'][x] = "" |
| 296 |
if not env.WhereIs('cl', os.environ['PATH']): |
| 297 |
raise SCons.Errors.InternalError("CL not found in %s" % os.environ['PATH']) |
| 298 |
include_path = os.environ['INCLUDE'] |
| 299 |
lib_path = os.environ['LIB'] |
| 300 |
exe_path = os.environ['PATH'] |
| 301 |
sdk_path = env.WhereIs('windows.h', include_path, '.h') |
| 302 |
if not sdk_path: |
| 303 |
raise SCons.Errors.InternalError("windows.h not found in %s" % include_path) |
| 304 |
sdk_path = os.path.normpath(sdk_path + "\..\..") |
| 305 |
sdk_path_LINK = env.WhereIs('link', exe_path) |
| 306 |
sdk_path_AR = env.WhereIs('lib', exe_path) |
| 307 |
else: |
| 308 |
include_path, lib_path, exe_path, sdk_path = get_msvctoolkit_paths() |
| 309 |
if float(env['MSVS_VERSION']) < 7.0: # Override SConstruct default |
| 310 |
env['MSVS_VERSION'] = '7.1' |
| 311 |
sdk_path_LINK = env.WhereIs('link', exe_path) |
| 312 |
sdk_path_AR = sdk_path + '\\bin\\Win64\\lib.exe' |
| 313 |
|
| 314 |
env.PrependENVPath('INCLUDE', include_path) |
| 315 |
env.PrependENVPath('LIB', lib_path) |
| 316 |
env.PrependENVPath('PATH', exe_path) |
| 317 |
# 'LIBPATH' = ? |
| 318 |
|
| 319 |
env['ENV']['CPU'] = (targ_arc.upper(), 'i386')['x86' in targ_arc.lower()] # AMD64/ARM64 or i386 |
| 320 |
env['ENV']['TARGETOS'] = 'BOTH' |
| 321 |
env['ENV']['APPVER'] = '4.0' |
| 322 |
env['ENV']['MSSDK'] = sdk_path |
| 323 |
env['ENV']['BkOffice'] = sdk_path |
| 324 |
env['ENV']['Basemake'] = sdk_path + "\\Include\\BKOffice.Mak" |
| 325 |
env['ENV']['INETSDK'] = sdk_path |
| 326 |
env['ENV']['MSSDK'] = sdk_path |
| 327 |
env['ENV']['MSTOOLS'] = sdk_path |
| 328 |
|
| 329 |
env['CFILESUFFIX'] = '.c' |
| 330 |
env['CXXFILESUFFIX'] = '.cc' |
| 331 |
|
| 332 |
env['PCHCOM'] = '$CXX $CXXFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Fo${TARGETS[1]} /Yc$PCHSTOP /Fp${TARGETS[0]} $CCPDBFLAGS' |
| 333 |
env['BUILDERS']['PCH'] = pch_builder |
| 334 |
|
| 335 |
# VC 2003 Toolkit does not have lib.exe but we can use link.exe |
| 336 |
if not sdk_path_AR or not env.File(sdk_path_AR).exists(): |
| 337 |
env['AR'] = '"' + sdk_path_LINK + '"' |
| 338 |
env['ARFLAGS'] = '/LIB ' + env['ARFLAGS'] |
| 339 |
else: |
| 340 |
env['AR'] = '"' + sdk_path_AR + '"' |
| 341 |
env['ARFLAGS'] = SCons.Util.CLVar('/nologo') |
| 342 |
env['ARCOM'] = "${TEMPFILE('$AR $ARFLAGS /OUT:$TARGET $SOURCES')}" |
| 343 |
|
| 344 |
if 'AMD64' in targ_arc.upper(): |
| 345 |
env['AS'] = 'ml64' |
| 346 |
if 'ARM64' in targ_arc.upper(): |
| 347 |
env['AS'] = 'armasm64' |
| 348 |
|
| 349 |
env['SHLINK'] = '$LINK' |
| 350 |
env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS /dll') |
| 351 |
env['_SHLINK_TARGETS'] = win32ShlinkTargets |
| 352 |
env['_SHLINK_SOURCES'] = win32ShlinkSources |
| 353 |
env['SHLINKCOM'] = compositeLinkAction |
| 354 |
env['SHLIBEMITTER']= win32LibEmitter |
| 355 |
env['LINK'] = '"' + sdk_path_LINK + '"' |
| 356 |
env['LINKFLAGS'] = SCons.Util.CLVar('/nologo') |
| 357 |
env['_PDB'] = pdbGenerator |
| 358 |
env['LINKCOM'] = '${TEMPFILE("$LINK $LINKFLAGS /OUT:$TARGET $( $_LIBDIRFLAGS $) $_LIBFLAGS $_PDB $SOURCES")}' |
| 359 |
env['PROGEMITTER'] = prog_emitter |
| 360 |
env['LIBDIRPREFIX']='/LIBPATH:' |
| 361 |
env['LIBDIRSUFFIX']='' |
| 362 |
env['LIBLINKPREFIX']='' |
| 363 |
env['LIBLINKSUFFIX']='$LIBSUFFIX' |
| 364 |
|
| 365 |
env['WIN32DEFPREFIX'] = '' |
| 366 |
env['WIN32DEFSUFFIX'] = '.def' |
| 367 |
env['WIN32_INSERT_DEF'] = 0 |
| 368 |
|
| 369 |
env['WIN32EXPPREFIX'] = '' |
| 370 |
env['WIN32EXPSUFFIX'] = '.exp' |
| 371 |
|
| 372 |
env['REGSVRACTION'] = regServerCheck |
| 373 |
env['REGSVR'] = os.path.join(SCons.Platform.win32.get_system_root(),'System32','regsvr32') |
| 374 |
env['REGSVRFLAGS'] = '/s ' |
| 375 |
env['REGSVRCOM'] = '$REGSVR $REGSVRFLAGS $TARGET' |
| 376 |
|
| 377 |
|
| 378 |
def exists(env): |
| 379 |
return env.Detect('cl') |