Skip to content

Commit

Permalink
Make Visual Studio version selection more smart
Browse files Browse the repository at this point in the history
This CL enhances the Visual Studio version selection by considering a
prioritized support list, env variable and the ones installed in system.

BUG=956482

Change-Id: Ic147480f2780bb2bb38e489364893695c7ca2150
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1595840
Commit-Queue: Yang Gu <yang.gu@intel.com>
Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
Reviewed-by: Robert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659830}
  • Loading branch information
gyagp authored and Commit Bot committed May 15, 2019
1 parent 2956187 commit c4a16c7
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions build/vs_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import print_function

import collections
import glob
import json
import os
Expand All @@ -22,9 +23,11 @@
script_dir = os.path.dirname(os.path.realpath(__file__))
json_data_file = os.path.join(script_dir, 'win_toolchain.json')


# Use MSVS2017 as the default toolchain.
CURRENT_DEFAULT_TOOLCHAIN_VERSION = '2017'
# VS versions are listed in descending order of priority (highest first).
MSVS_VERSIONS = collections.OrderedDict([
('2017', '15.0'),
('2019', '16.0'),
])


def SetEnvironmentAndGetRuntimeDllDirs():
Expand Down Expand Up @@ -129,9 +132,47 @@ def _RegistryGetValue(key, value):


def GetVisualStudioVersion():
"""Return GYP_MSVS_VERSION of Visual Studio.
"""Return best available version of Visual Studio.
"""
return os.environ.get('GYP_MSVS_VERSION', CURRENT_DEFAULT_TOOLCHAIN_VERSION)

env_version = os.environ.get('GYP_MSVS_VERSION')
supported_versions = MSVS_VERSIONS.keys()

# VS installed in depot_tools for Googlers
if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1'))):
if env_version:
return env_version
else:
return supported_versions[0]

# VS installed in system for external developers
supported_versions_str = ', '.join('{} ({})'.format(v,k)
for k,v in MSVS_VERSIONS.items())
available_versions = []
for version in supported_versions:
for path in (
os.environ.get('vs%s_install' % version),
os.path.expandvars('%ProgramFiles(x86)%' +
'/Microsoft Visual Studio/%s' % version)):
if path and os.path.exists(path):
available_versions.append(version)
break

if not available_versions:
raise Exception('No supported Visual Studio can be found.'
' Supported versions are: %s.' % supported_versions_str)

if env_version:
if env_version not in supported_versions:
raise Exception('Visual Studio version %s (from GYP_MSVS_VERSION)'
' is not supported. Supported versions are: %s.'
% (env_version, supported_versions_str))
if env_version not in available_versions:
raise Exception('Visual Studio version %s (from GYP_MSVS_VERSION)'
' is not available.' % env_version)
return env_version

return available_versions[0]


def DetectVisualStudioPath():
Expand All @@ -141,14 +182,6 @@ def DetectVisualStudioPath():
# Note that this code is used from
# build/toolchain/win/setup_toolchain.py as well.
version_as_year = GetVisualStudioVersion()
year_to_version = {
'2017': '15.0',
'2019': '16.0',
}
if version_as_year not in year_to_version:
raise Exception(('Visual Studio version %s (from GYP_MSVS_VERSION)'
' not supported. Supported versions are: %s') % (
version_as_year, ', '.join(year_to_version.keys())))

# The VC++ >=2017 install location needs to be located using COM instead of
# the registry. For details see:
Expand All @@ -171,8 +204,8 @@ def DetectVisualStudioPath():
if path and os.path.exists(path):
return path

raise Exception(('Visual Studio Version %s (from GYP_MSVS_VERSION)'
' not found.') % (version_as_year))
raise Exception('Visual Studio Version %s (from GYP_MSVS_VERSION)'
' not found.' % version_as_year)


def _CopyRuntimeImpl(target, source, verbose=True):
Expand Down Expand Up @@ -267,7 +300,7 @@ def FindVCComponentRoot(component):
version number part changes frequently so the highest version number found is
used.
"""
assert GetVisualStudioVersion() in ['2017', '2019']

SetEnvironmentAndGetRuntimeDllDirs()
assert ('GYP_MSVS_OVERRIDE_PATH' in os.environ)
vc_component_msvc_root = os.path.join(os.environ['GYP_MSVS_OVERRIDE_PATH'],
Expand Down

0 comments on commit c4a16c7

Please sign in to comment.