build/toolchain/win/setup_toolchain.py
def _LoadToolchainEnv(cpu, toolchain_root, sdk_dir, target_store):
  """Returns a dictionary with environment variables that must be set while
  running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe)."""
  # Check if we are running in the SDK command line environment and use
  # the setup script from the SDK if so. |cpu| should be either
  # 'x86' or 'x64' or 'arm' or 'arm64'.
  assert cpu in ('x86', 'x64', 'arm', 'arm64')
  if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', 1))) and sdk_dir:
    # Load environment from json file.
    env = os.path.normpath(os.path.join(sdk_dir, 'bin/SetEnv.%s.json' % cpu))
    env = json.load(open(env))['env']
    if env['VSINSTALLDIR'] == [["..", "..\\"]]:
      # Old-style paths were relative to the win_sdk\bin directory.
      json_relative_dir = os.path.join(sdk_dir, 'bin')
    else:
      # New-style paths are relative to the toolchain directory.
      json_relative_dir = toolchain_root
    for k in env:
      entries = [os.path.join(*([json_relative_dir] + e)) for e in env[k]]
      # clang-cl wants INCLUDE to be ;-separated even on non-Windows,
      # lld-link wants LIB to be ;-separated even on non-Windows.  Path gets :.
      # The separator for INCLUDE here must match the one used in main() below.
      sep = os.pathsep if k == 'PATH' else ';'
      env[k] = sep.join(entries)
    # PATH is a bit of a special case, it's in addition to the current PATH.
    env['PATH'] = env['PATH'] + os.pathsep + os.environ['PATH']
    # Augment with the current env to pick up TEMP and friends.
    for k in os.environ:
      if k not in env:
        env[k] = os.environ[k]

    varlines = []
    for k in sorted(env.keys()):
      varlines.append('%s=%s' % (str(k), str(env[k])))
    variables = '\n'.join(varlines)

    # Check that the json file contained the same environment as the .cmd file.
    if sys.platform in ('win32', 'cygwin'):
      script = os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.cmd'))
      arg = '/' + cpu
      json_env = _ExtractImportantEnvironment(variables)
      cmd_env = _ExtractImportantEnvironment(_LoadEnvFromBat([script, arg]))
      assert _LowercaseDict(json_env) == _LowercaseDict(cmd_env)
  else:
    if 'GYP_MSVS_OVERRIDE_PATH' not in os.environ:
      os.environ['GYP_MSVS_OVERRIDE_PATH'] = _DetectVisualStudioPath()
    # We only support x64-hosted tools.
    script_path = os.path.normpath(os.path.join(
                                       os.environ['GYP_MSVS_OVERRIDE_PATH'],
                                       'VC/vcvarsall.bat'))
    if not os.path.exists(script_path):
      # vcvarsall.bat for VS 2017 fails if run after running vcvarsall.bat from
      # VS 2013 or VS 2015. Fix this by clearing the vsinstalldir environment
      # variable. Since vcvarsall.bat appends to the INCLUDE, LIB, and LIBPATH
      # environment variables we need to clear those to avoid getting double
      # entries when vcvarsall.bat has been run before gn gen. vcvarsall.bat
      # also adds to PATH, but there is no clean way of clearing that and it
      # doesn't seem to cause problems.
      if 'VSINSTALLDIR' in os.environ:
        del os.environ['VSINSTALLDIR']
        if 'INCLUDE' in os.environ:
          del os.environ['INCLUDE']
        if 'LIB' in os.environ:
          del os.environ['LIB']
        if 'LIBPATH' in os.environ:
          del os.environ['LIBPATH']
      other_path = os.path.normpath(os.path.join(
                                        os.environ['GYP_MSVS_OVERRIDE_PATH'],
                                        'VC/Auxiliary/Build/vcvarsall.bat'))
      if not os.path.exists(other_path):
        raise Exception('%s is missing - make sure VC++ tools are installed.' %
                        script_path)
      script_path = other_path
    cpu_arg = "amd64"
    if (cpu != 'x64'):
      # x64 is default target CPU thus any other CPU requires a target set
      cpu_arg += '_' + cpu
    args = [script_path, cpu_arg, ]
    # Store target must come before any SDK version declaration
    if (target_store):
      args.append('store')
    # Explicitly specifying the SDK version to build with to avoid accidentally
    # building with a new and untested SDK. This should stay in sync with the
    # packaged toolchain in build/vs_toolchain.py.
    args.append('10.0.19041.0')
    variables = _LoadEnvFromBat(args)
  return _ExtractImportantEnvironment(variables)
Returns a dictionary with environment variables that must be set while
  running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe).
Returns a dictionary with environment variables that must be set while
running binaries from the toolchain .
Returns a dictionary with environment variables that must be set while running binaries from the toolchain .
def _LoadToolchainEnv(cpu, toolchain_root, sdk_dir, target_store):
  assert cpu in ('x86', 'x64', 'arm', 'arm64')
  if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', 1))) and sdk_dir:
    env = os.path.normpath(os.path.join(sdk_dir, 'bin/SetEnv.%s.json' % cpu))
    env = json.load(open(env))['env']
    if env['VSINSTALLDIR'] == [["..", "..\\"]]:
      json_relative_dir = os.path.join(sdk_dir, 'bin')
    else:
      json_relative_dir = toolchain_root
    for k in env:
      entries = [os.path.join(*([json_relative_dir] + e)) for e in env[k]]
      sep = os.pathsep if k == 'PATH' else ';'
      env[k] = sep.join(entries)
    env['PATH'] = env['PATH'] + os.pathsep + os.environ['PATH']
    for k in os.environ:
      if k not in env:
        env[k] = os.environ[k]
    varlines = []
    for k in sorted(env.keys()):
      varlines.append('%s=%s' % (str(k), str(env[k])))
    variables = '\n'.join(varlines)
    if sys.platform in ('win32', 'cygwin'):
      script = os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.cmd'))
      arg = '/' + cpu
      json_env = _ExtractImportantEnvironment(variables)
      cmd_env = _ExtractImportantEnvironment(_LoadEnvFromBat([script, arg]))
      assert _LowercaseDict(json_env) == _LowercaseDict(cmd_env)
  else:
    if 'GYP_MSVS_OVERRIDE_PATH' not in os.environ:
      os.environ['GYP_MSVS_OVERRIDE_PATH'] = _DetectVisualStudioPath()
    script_path = os.path.normpath(os.path.join(
                                       os.environ['GYP_MSVS_OVERRIDE_PATH'],
                                       'VC/vcvarsall.bat'))
    if not os.path.exists(script_path):
      if 'VSINSTALLDIR' in os.environ:
        del os.environ['VSINSTALLDIR']
        if 'INCLUDE' in os.environ:
          del os.environ['INCLUDE']
        if 'LIB' in os.environ:
          del os.environ['LIB']
        if 'LIBPATH' in os.environ:
          del os.environ['LIBPATH']
      other_path = os.path.normpath(os.path.join(
                                        os.environ['GYP_MSVS_OVERRIDE_PATH'],
                                        'VC/Auxiliary/Build/vcvarsall.bat'))
      if not os.path.exists(other_path):
        raise Exception('%s is missing - make sure VC++ tools are installed.' %
                        script_path)
      script_path = other_path
    cpu_arg = "amd64"
    if (cpu != 'x64'):
      cpu_arg += '_' + cpu
    args = [script_path, cpu_arg, ]
    if (target_store):
      args.append('store')
    args.append('10.0.19041.0')
    variables = _LoadEnvFromBat(args)
  return _ExtractImportantEnvironment(variables)
def _LoadToolchainEnv ( cpu , toolchain_root , sdk_dir , target_store ) : assert cpu in ( 'x86' , 'x64' , 'arm' , 'arm64' ) if bool ( int ( os . environ . get ( 'DEPOT_TOOLS_WIN_TOOLCHAIN' , 1 ) ) ) and sdk_dir : env = os . path . normpath ( os . path . join ( sdk_dir , 'bin/SetEnv.%s.json' % cpu ) ) env = json . load ( open ( env ) ) [ 'env' ] if env [ 'VSINSTALLDIR' ] == [ [ ".." , "..\\" ] ] : json_relative_dir = os . path . join ( sdk_dir , 'bin' ) else : json_relative_dir = toolchain_root for k in env : entries = [ os . path . join ( * ( [ json_relative_dir ] + e ) ) for e in env [ k ] ] sep = os . pathsep if k == 'PATH' else ';' env [ k ] = sep . join ( entries ) env [ 'PATH' ] = env [ 'PATH' ] + os . pathsep + os . environ [ 'PATH' ] for k in os . environ : if k not in env : env [ k ] = os . environ [ k ] varlines = [ ] for k in sorted ( env . keys ( ) ) : varlines . append ( '%s=%s' % ( str ( k ) , str ( env [ k ] ) ) ) variables = '\n' . join ( varlines ) if sys . platform in ( 'win32' , 'cygwin' ) : script = os . path . normpath ( os . path . join ( sdk_dir , 'Bin/SetEnv.cmd' ) ) arg = '/' + cpu json_env = _ExtractImportantEnvironment ( variables ) cmd_env = _ExtractImportantEnvironment ( _LoadEnvFromBat ( [ script , arg ] ) ) assert _LowercaseDict ( json_env ) == _LowercaseDict ( cmd_env ) else : if 'GYP_MSVS_OVERRIDE_PATH' not in os . environ : os . environ [ 'GYP_MSVS_OVERRIDE_PATH' ] = _DetectVisualStudioPath ( ) script_path = os . path . normpath ( os . path . join ( os . environ [ 'GYP_MSVS_OVERRIDE_PATH' ] , 'VC/vcvarsall.bat' ) ) if not os . path . exists ( script_path ) : if 'VSINSTALLDIR' in os . environ : del os . environ [ 'VSINSTALLDIR' ] if 'INCLUDE' in os . environ : del os . environ [ 'INCLUDE' ] if 'LIB' in os . environ : del os . environ [ 'LIB' ] if 'LIBPATH' in os . environ : del os . environ [ 'LIBPATH' ] other_path = os . path . normpath ( os . path . join ( os . environ [ 'GYP_MSVS_OVERRIDE_PATH' ] , 'VC/Auxiliary/Build/vcvarsall.bat' ) ) if not os . path . exists ( other_path ) : raise Exception ( '%s is missing - make sure VC++ tools are installed.' % script_path ) script_path = other_path cpu_arg = "amd64" if ( cpu != 'x64' ) : cpu_arg += '_' + cpu args = [ script_path , cpu_arg , ] if ( target_store ) : args . append ( 'store' ) args . append ( '10.0.19041.0' ) variables = _LoadEnvFromBat ( args ) return _ExtractImportantEnvironment ( variables )
Returns a dictionary with environment variables that must be set while
running binaries from the toolchain (e.g.
Returns a dictionary with environment variables that must be set while running binaries from the toolchain ( e . g .
"""Returns a dictionary with environment variables that must be set while
  running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe).""" # Check if we are running in the SDK command line environment and use # the setup script from the SDK if so. |cpu| should be either # 'x86' or 'x64' or 'arm' or 'arm64'. # Load environment from json file. # Old-style paths were relative to the win_sdk\bin directory. # New-style paths are relative to the toolchain directory. # clang-cl wants INCLUDE to be ;-separated even on non-Windows, # lld-link wants LIB to be ;-separated even on non-Windows.  Path gets :. # The separator for INCLUDE here must match the one used in main() below. # PATH is a bit of a special case, it's in addition to the current PATH. # Augment with the current env to pick up TEMP and friends. # Check that the json file contained the same environment as the .cmd file. # We only support x64-hosted tools. # vcvarsall.bat for VS 2017 fails if run after running vcvarsall.bat from # VS 2013 or VS 2015. Fix this by clearing the vsinstalldir environment # variable. Since vcvarsall.bat appends to the INCLUDE, LIB, and LIBPATH # environment variables we need to clear those to avoid getting double # entries when vcvarsall.bat has been run before gn gen. vcvarsall.bat # also adds to PATH, but there is no clean way of clearing that and it # doesn't seem to cause problems. # x64 is default target CPU thus any other CPU requires a target set # Store target must come before any SDK version declaration # Explicitly specifying the SDK version to build with to avoid accidentally # building with a new and untested SDK. This should stay in sync with the # packaged toolchain in build/vs_toolchain.py.
cpu
toolchain_root
sdk_dir
target_store