mapnik/scons/scons-local-4.1.0/SCons/Tool/MSCommon/common.py

354 lines
12 KiB
Python
Raw Normal View History

2017-11-16 10:59:55 +00:00
"""
Common helper functions for working with the Microsoft tool chain.
"""
2015-10-01 13:25:41 +00:00
#
2021-03-05 10:18:26 +00:00
# __COPYRIGHT__
2015-10-01 13:25:41 +00:00
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
2021-03-05 10:18:26 +00:00
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
2015-10-01 13:25:41 +00:00
import copy
2021-03-05 10:18:26 +00:00
import json
2015-10-01 13:25:41 +00:00
import os
import re
2021-03-05 10:18:26 +00:00
import subprocess
import sys
2015-10-01 13:25:41 +00:00
import SCons.Util
2021-03-05 10:18:26 +00:00
# SCONS_MSCOMMON_DEBUG is internal-use so undocumented:
# set to '-' to print to console, else set to filename to log to
2017-11-16 10:59:55 +00:00
LOGFILE = os.environ.get('SCONS_MSCOMMON_DEBUG')
if LOGFILE == '-':
def debug(message):
print(message)
elif LOGFILE:
2021-03-05 10:18:26 +00:00
import logging
modulelist = (
# root module and parent/root module
'MSCommon', 'Tool',
# python library and below: correct iff scons does not have a lib folder
'lib',
# scons modules
'SCons', 'test', 'scons'
)
def get_relative_filename(filename, module_list):
if not filename:
return filename
for module in module_list:
try:
ind = filename.rindex(module)
return filename[ind:]
except ValueError:
pass
return filename
class _Debug_Filter(logging.Filter):
# custom filter for module relative filename
def filter(self, record):
relfilename = get_relative_filename(record.pathname, modulelist)
relfilename = relfilename.replace('\\', '/')
record.relfilename = relfilename
return True
logging.basicConfig(
# This looks like:
# 00109ms:MSCommon/vc.py:find_vc_pdir#447:
format=(
'%(relativeCreated)05dms'
':%(relfilename)s'
':%(funcName)s'
'#%(lineno)s'
':%(message)s: '
),
filename=LOGFILE,
level=logging.DEBUG)
logger = logging.getLogger(name=__name__)
logger.addFilter(_Debug_Filter())
debug = logger.debug
2015-10-01 13:25:41 +00:00
else:
2021-03-05 10:18:26 +00:00
def debug(x): return None
# SCONS_CACHE_MSVC_CONFIG is public, and is documented.
CONFIG_CACHE = os.environ.get('SCONS_CACHE_MSVC_CONFIG')
if CONFIG_CACHE in ('1', 'true', 'True'):
CONFIG_CACHE = os.path.join(os.path.expanduser('~'), '.scons_msvc_cache')
def read_script_env_cache():
""" fetch cached msvc env vars if requested, else return empty dict """
envcache = {}
if CONFIG_CACHE:
try:
with open(CONFIG_CACHE, 'r') as f:
envcache = json.load(f)
except FileNotFoundError:
# don't fail if no cache file, just proceed without it
pass
return envcache
def write_script_env_cache(cache):
""" write out cache of msvc env vars if requested """
if CONFIG_CACHE:
try:
with open(CONFIG_CACHE, 'w') as f:
json.dump(cache, f, indent=2)
except TypeError:
# data can't serialize to json, don't leave partial file
os.remove(CONFIG_CACHE)
except IOError:
# can't write the file, just skip
pass
2015-10-01 13:25:41 +00:00
_is_win64 = None
2021-03-05 10:18:26 +00:00
2015-10-01 13:25:41 +00:00
def is_win64():
"""Return true if running on windows 64 bits.
Works whether python itself runs in 64 bits or 32 bits."""
# Unfortunately, python does not provide a useful way to determine
# if the underlying Windows OS is 32-bit or 64-bit. Worse, whether
# the Python itself is 32-bit or 64-bit affects what it returns,
# so nothing in sys.* or os.* help.
# Apparently the best solution is to use env vars that Windows
# sets. If PROCESSOR_ARCHITECTURE is not x86, then the python
# process is running in 64 bit mode (on a 64-bit OS, 64-bit
# hardware, obviously).
# If this python is 32-bit but the OS is 64, Windows will set
# ProgramW6432 and PROCESSOR_ARCHITEW6432 to non-null.
# (Checking for HKLM\Software\Wow6432Node in the registry doesn't
# work, because some 32-bit installers create it.)
global _is_win64
if _is_win64 is None:
# I structured these tests to make it easy to add new ones or
# add exceptions in the future, because this is a bit fragile.
_is_win64 = False
2017-11-16 10:59:55 +00:00
if os.environ.get('PROCESSOR_ARCHITECTURE', 'x86') != 'x86':
2015-10-01 13:25:41 +00:00
_is_win64 = True
if os.environ.get('PROCESSOR_ARCHITEW6432'):
_is_win64 = True
if os.environ.get('ProgramW6432'):
_is_win64 = True
return _is_win64
2015-11-26 10:09:02 +00:00
def read_reg(value, hkroot=SCons.Util.HKEY_LOCAL_MACHINE):
return SCons.Util.RegGetValue(hkroot, value)[0]
2015-10-01 13:25:41 +00:00
2021-03-05 10:18:26 +00:00
2015-10-01 13:25:41 +00:00
def has_reg(value):
"""Return True if the given key exists in HKEY_LOCAL_MACHINE, False
otherwise."""
try:
SCons.Util.RegOpenKeyEx(SCons.Util.HKEY_LOCAL_MACHINE, value)
ret = True
2016-08-05 09:16:06 +00:00
except SCons.Util.WinError:
2015-10-01 13:25:41 +00:00
ret = False
return ret
# Functions for fetching environment variable settings from batch files.
2021-03-05 10:18:26 +00:00
2015-10-01 13:25:41 +00:00
def normalize_env(env, keys, force=False):
"""Given a dictionary representing a shell environment, add the variables
from os.environ needed for the processing of .bat files; the keys are
controlled by the keys argument.
It also makes sure the environment values are correctly encoded.
If force=True, then all of the key values that exist are copied
into the returned dictionary. If force=false, values are only
copied if the key does not already exist in the copied dictionary.
Note: the environment is copied."""
normenv = {}
if env:
2021-03-05 10:18:26 +00:00
for k, v in env.items():
normenv[k] = copy.deepcopy(v)
2015-10-01 13:25:41 +00:00
for k in keys:
2021-03-05 10:18:26 +00:00
if k in os.environ and (force or k not in normenv):
2017-11-16 10:59:55 +00:00
normenv[k] = os.environ[k]
2015-10-01 13:25:41 +00:00
2021-03-05 10:18:26 +00:00
# add some things to PATH to prevent problems:
# Shouldn't be necessary to add system32, since the default environment
# should include it, but keep this here to be safe (needed for reg.exe)
sys32_dir = os.path.join(
os.environ.get("SystemRoot", os.environ.get("windir", r"C:\Windows")), "System32"
)
if sys32_dir not in normenv["PATH"]:
normenv["PATH"] = normenv["PATH"] + os.pathsep + sys32_dir
2015-10-01 13:25:41 +00:00
2017-11-16 10:59:55 +00:00
# Without Wbem in PATH, vcvarsall.bat has a "'wmic' is not recognized"
# error starting with Visual Studio 2017, although the script still
# seems to work anyway.
sys32_wbem_dir = os.path.join(sys32_dir, 'Wbem')
if sys32_wbem_dir not in normenv['PATH']:
normenv['PATH'] = normenv['PATH'] + os.pathsep + sys32_wbem_dir
2021-03-05 10:18:26 +00:00
# Without Powershell in PATH, an internal call to a telemetry
# function (starting with a VS2019 update) can fail
# Note can also set VSCMD_SKIP_SENDTELEMETRY to avoid this.
sys32_ps_dir = os.path.join(sys32_dir, r'WindowsPowerShell\v1.0')
if sys32_ps_dir not in normenv['PATH']:
normenv['PATH'] = normenv['PATH'] + os.pathsep + sys32_ps_dir
2015-10-01 13:25:41 +00:00
2021-03-05 10:18:26 +00:00
debug("PATH: %s" % normenv['PATH'])
2015-10-01 13:25:41 +00:00
return normenv
2021-03-05 10:18:26 +00:00
def get_output(vcbat, args=None, env=None):
2015-10-01 13:25:41 +00:00
"""Parse the output of given bat file, with given args."""
if env is None:
# Create a blank environment, for use in launching the tools
env = SCons.Environment.Environment(tools=[])
2021-03-05 10:18:26 +00:00
# TODO: Hard-coded list of the variables that (may) need to be
# imported from os.environ[] for the chain of development batch
# files to execute correctly. One call to vcvars*.bat may
# end up running a dozen or more scripts, changes not only with
# each release but with what is installed at the time. We think
# in modern installations most are set along the way and don't
# need to be picked from the env, but include these for safety's sake.
# Any VSCMD variables definitely are picked from the env and
# control execution in interesting ways.
# Note these really should be unified - either controlled by vs.py,
# or synced with the the common_tools_var # settings in vs.py.
2017-11-16 10:59:55 +00:00
vs_vc_vars = [
2021-03-05 10:18:26 +00:00
'COMSPEC', # path to "shell"
'VS160COMNTOOLS', # path to common tools for given version
'VS150COMNTOOLS',
2017-11-16 10:59:55 +00:00
'VS140COMNTOOLS',
'VS120COMNTOOLS',
2015-10-01 13:25:41 +00:00
'VS110COMNTOOLS',
'VS100COMNTOOLS',
'VS90COMNTOOLS',
'VS80COMNTOOLS',
'VS71COMNTOOLS',
'VS70COMNTOOLS',
'VS60COMNTOOLS',
2021-03-05 10:18:26 +00:00
'VSCMD_DEBUG', # enable logging and other debug aids
'VSCMD_SKIP_SENDTELEMETRY',
2015-10-01 13:25:41 +00:00
]
2017-11-16 10:59:55 +00:00
env['ENV'] = normalize_env(env['ENV'], vs_vc_vars, force=False)
2015-10-01 13:25:41 +00:00
if args:
debug("Calling '%s %s'" % (vcbat, args))
popen = SCons.Action._subproc(env,
2017-11-16 10:59:55 +00:00
'"%s" %s & set' % (vcbat, args),
stdin='devnull',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
2015-10-01 13:25:41 +00:00
else:
debug("Calling '%s'" % vcbat)
popen = SCons.Action._subproc(env,
2017-11-16 10:59:55 +00:00
'"%s" & set' % vcbat,
stdin='devnull',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
2015-10-01 13:25:41 +00:00
# Use the .stdout and .stderr attributes directly because the
# .communicate() method uses the threading module on Windows
# and won't work under Pythons not built with threading.
2021-03-05 10:18:26 +00:00
with popen.stdout:
stdout = popen.stdout.read()
with popen.stderr:
stderr = popen.stderr.read()
2015-10-01 13:25:41 +00:00
2016-08-05 09:16:06 +00:00
# Extra debug logic, uncomment if necessary
2021-03-05 10:18:26 +00:00
# debug('stdout:%s' % stdout)
# debug('stderr:%s' % stderr)
# Ongoing problems getting non-corrupted text led to this
# changing to "oem" from "mbcs" - the scripts run presumably
# attached to a console, so some particular rules apply.
# Unfortunately, "oem" not defined in Python 3.5, so get another way
if sys.version_info.major == 3 and sys.version_info.minor < 6:
from ctypes import windll
2015-10-01 13:25:41 +00:00
2021-03-05 10:18:26 +00:00
OEM = "cp{}".format(windll.kernel32.GetConsoleOutputCP())
else:
OEM = "oem"
2015-10-01 13:25:41 +00:00
if stderr:
# TODO: find something better to do with stderr;
# this at least prevents errors from getting swallowed.
2021-03-05 10:18:26 +00:00
sys.stderr.write(stderr.decode(OEM))
2015-10-01 13:25:41 +00:00
if popen.wait() != 0:
2021-03-05 10:18:26 +00:00
raise IOError(stderr.decode(OEM))
return stdout.decode(OEM)
KEEPLIST = (
"INCLUDE",
"LIB",
"LIBPATH",
"PATH",
"VSCMD_ARG_app_plat",
"VCINSTALLDIR", # needed by clang -VS 2017 and newer
"VCToolsInstallDir", # needed by clang - VS 2015 and older
)
2015-10-01 13:25:41 +00:00
2021-03-05 10:18:26 +00:00
def parse_output(output, keep=KEEPLIST):
2017-11-16 10:59:55 +00:00
"""
Parse output from running visual c++/studios vcvarsall.bat and running set
To capture the values listed in keep
"""
2015-10-01 13:25:41 +00:00
# dkeep is a dict associating key: path_list, where key is one item from
2021-03-05 10:18:26 +00:00
# keep, and path_list the associated list of paths
2015-10-01 13:25:41 +00:00
dkeep = dict([(i, []) for i in keep])
# rdk will keep the regex to match the .bat file output line starts
rdk = {}
for i in keep:
rdk[i] = re.compile('%s=(.*)' % i, re.I)
def add_env(rmatch, key, dkeep=dkeep):
2017-11-16 10:59:55 +00:00
path_list = rmatch.group(1).split(os.pathsep)
for path in path_list:
2015-10-01 13:25:41 +00:00
# Do not add empty paths (when a var ends with ;)
2017-11-16 10:59:55 +00:00
if path:
2015-10-01 13:25:41 +00:00
# XXX: For some reason, VC98 .bat file adds "" around the PATH
# values, and it screws up the environment later, so we strip
# it.
2017-11-16 10:59:55 +00:00
path = path.strip('"')
dkeep[key].append(str(path))
2015-10-01 13:25:41 +00:00
for line in output.splitlines():
2017-11-16 10:59:55 +00:00
for k, value in rdk.items():
match = value.match(line)
if match:
add_env(match, k)
2015-10-01 13:25:41 +00:00
return dkeep
# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: