Skip to content

Commit

Permalink
vs toolchain: Move toolchain env computation into its own function.
Browse files Browse the repository at this point in the history
Also replace a few ';'s with os.path.pathsep and a few \s with os.path.sep.
No behavior change on Windows; the motivation is that this might make it
possible to do this environment computation on non-Windows hosts too some day.

BUG=495204

Review URL: https://codereview.chromium.org/1697023002

Cr-Commit-Position: refs/heads/master@{#375476}
  • Loading branch information
nico authored and Commit bot committed Feb 15, 2016
1 parent 00c72f4 commit 44a40f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
34 changes: 20 additions & 14 deletions build/toolchain/win/setup_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ def _SetupScript(target_cpu, sdk_dir):
'amd64_x86' if target_cpu == 'x86' else 'amd64']


def _LoadToolchainEnv(cpu, win_sdk_path):
"""Returns a dictionary with environment variables that must be set while
running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe)."""
args = _SetupScript(cpu, win_sdk_path)
args.extend(('&&', 'set'))
popen = subprocess.Popen(
args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
variables, _ = popen.communicate()
if popen.returncode != 0:
raise Exception('"%s" failed with error %d' % (args, popen.returncode))
return _ExtractImportantEnvironment(variables)


def _FormatAsEnvironmentBlock(envvar_dict):
"""Format as an 'environment block' directly suitable for CreateProcess.
Briefly this is a list of key=value\0, terminated by an additional \0. See
Expand Down Expand Up @@ -134,15 +147,8 @@ def main():

for cpu in cpus:
# Extract environment variables for subprocesses.
args = _SetupScript(cpu, win_sdk_path)
args.extend(('&&', 'set'))
popen = subprocess.Popen(
args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
variables, _ = popen.communicate()
if popen.returncode != 0:
raise Exception('"%s" failed with error %d' % (args, popen.returncode))
env = _ExtractImportantEnvironment(variables)
env['PATH'] = runtime_dirs + ';' + env['PATH']
env = _LoadToolchainEnv(cpu, win_sdk_path)
env['PATH'] = runtime_dirs + os.path.pathsep + env['PATH']

if cpu == target_cpu:
for path in env['PATH'].split(os.pathsep):
Expand All @@ -157,11 +163,11 @@ def main():
# version is used.

if win_sdk_path:
additional_includes = ('{sdk_dir}\\Include\\10.0.10586.0\\shared;' +
'{sdk_dir}\\Include\\10.0.10586.0\\um;' +
'{sdk_dir}\\Include\\10.0.10586.0\\winrt;').format(
sdk_dir=win_sdk_path)
env['INCLUDE'] = additional_includes + env['INCLUDE']
additional_includes = [
os.path.join(win_sdk_path, 'Include', '10.0.10586.0', p)
for p in ['shared', 'um', 'winrt']]
additional_includes = os.path.pathsep.join(additional_includes)
env['INCLUDE'] = additional_includes + os.path.pathsep + env['INCLUDE']
env_block = _FormatAsEnvironmentBlock(env)
with open('environment.' + cpu, 'wb') as f:
f.write(env_block)
Expand Down
6 changes: 3 additions & 3 deletions build/vs_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def SetEnvironmentAndGetRuntimeDllDirs():
os.environ['WINDOWSSDKDIR'] = win_sdk
os.environ['WDK_DIR'] = wdk
# Include the VS runtime in the PATH in case it's not machine-installed.
runtime_path = ';'.join(vs_runtime_dll_dirs)
os.environ['PATH'] = runtime_path + ';' + os.environ['PATH']
runtime_path = os.path.pathsep.join(vs_runtime_dll_dirs)
os.environ['PATH'] = runtime_path + os.path.pathsep + os.environ['PATH']
elif sys.platform == 'win32' and not depot_tools_win_toolchain:
if not 'GYP_MSVS_OVERRIDE_PATH' in os.environ:
os.environ['GYP_MSVS_OVERRIDE_PATH'] = DetectVisualStudioPath()
Expand Down Expand Up @@ -350,7 +350,7 @@ def GetToolchainDir():
os.environ['WINDOWSSDKDIR'],
GetVisualStudioVersion(),
os.environ.get('WDK_DIR', ''),
';'.join(runtime_dll_dirs or ['None']))
os.path.pathsep.join(runtime_dll_dirs or ['None']))


def main():
Expand Down

0 comments on commit 44a40f8

Please sign in to comment.