parameters: (env, version)
return_statement: return d
docstring: Find and execute appropriate batch script to set up build env.
  
      The MSVC build environment depends heavily on having the shell
      environment set.  SCons does not inherit that, and does not count
      on that being set up correctly anyway, so it tries to find the right
      MSVC batch script, or the right arguments to the generic batch script
      vcvarsall.bat, and run that, so we have a valid environment to build in.
      There are dragons here: the batch scripts don't fail (see comments
      elsewhere), they just leave you with a bad setup, so try hard to
      get it right.
docstring_summary: Find and execute appropriate batch script to set up build env.
docstring_tokens: Find and execute appropriate batch script to set up build env .
function: def msvc_find_valid_batch_script(env, version):
      """Find and execute appropriate batch script to set up build env.
  
      The MSVC build environment depends heavily on having the shell
      environment set.  SCons does not inherit that, and does not count
      on that being set up correctly anyway, so it tries to find the right
      MSVC batch script, or the right arguments to the generic batch script
      vcvarsall.bat, and run that, so we have a valid environment to build in.
      There are dragons here: the batch scripts don't fail (see comments
      elsewhere), they just leave you with a bad setup, so try hard to
      get it right.
      """
  
      # Find the host, target, and if present the requested target:
      platforms = get_host_target(env)
      debug("host_platform %s, target_platform %s req_target_platform %s" % platforms)
      host_platform, target_platform, req_target_platform = platforms
  
      # Most combinations of host + target are straightforward.
      # While all MSVC / Visual Studio tools are pysically 32-bit, they
      # make it look like there are 64-bit tools if the host is 64-bit,
      # so you can invoke the environment batch script to set up to build,
      # say, amd64 host -> x86 target. Express versions are an exception:
      # they always look 32-bit, so the batch scripts with 64-bit
      # host parts are absent. We try to fix that up in a couple of ways.
      # One is here: we make a table of "targets" to try, with the extra
      # targets being tags that tell us to try a different "host" instead
      # of the deduced host.
      try_target_archs = [target_platform]
      if req_target_platform in ('amd64', 'x86_64'):
          try_target_archs.append('x86_amd64')
      elif req_target_platform in ('x86',):
          try_target_archs.append('x86_x86')
      elif req_target_platform in ('arm',):
          try_target_archs.append('x86_arm')
      elif req_target_platform in ('arm64',):
          try_target_archs.append('x86_arm64')
      elif not req_target_platform:
          if target_platform in ('amd64', 'x86_64'):
              try_target_archs.append('x86_amd64')
              # If the user hasn't specifically requested a TARGET_ARCH,
              # and the TARGET_ARCH is amd64 then also try 32 bits
              # if there are no viable 64 bit tools installed
              try_target_archs.append('x86')
  
      debug("host_platform: %s, try_target_archs: %s"%(host_platform, try_target_archs))
  
      d = None
      for tp in try_target_archs:
          # Set to current arch.
          env['TARGET_ARCH'] = tp
  
          debug("trying target_platform:%s" % tp)
          host_target = (host_platform, tp)
          if not is_host_target_supported(host_target, version):
              warn_msg = "host, target = %s not supported for MSVC version %s" % \
                  (host_target, version)
              SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
          arg = _HOST_TARGET_ARCH_TO_BAT_ARCH[host_target]
  
          # Try to locate a batch file for this host/target platform combo
          try:
              (vc_script, use_arg, sdk_script) = find_batch_file(env, version, host_platform, tp)
              debug('vc_script:%s sdk_script:%s'%(vc_script,sdk_script))
          except VisualCException as e:
              msg = str(e)
              debug('Caught exception while looking for batch file (%s)' % msg)
              warn_msg = "VC version %s not installed.  " + \
                         "C/C++ compilers are most likely not set correctly.\n" + \
                         " Installed versions are: %s"
              warn_msg = warn_msg % (version, get_installed_vcs(env))
              SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
              continue
  
          # Try to use the located batch file for this host/target platform combo
          debug('use_script 2 %s, args:%s' % (repr(vc_script), arg))
          found = None
          if vc_script:
              if not use_arg:
                  arg = ''  # bat file will supply platform type
              # Get just version numbers
              maj, min = msvc_version_to_maj_min(version)
              # VS2015+
              if maj >= 14:
                  if env.get('MSVC_UWP_APP') == '1':
                      # Initialize environment variables with store/UWP paths
                      arg = (arg + ' store').lstrip()
  
              try:
                  d = script_env(vc_script, args=arg)
                  found = vc_script
              except BatchFileExecutionError as e:
                  debug('use_script 3: failed running VC script %s: %s: Error:%s'%(repr(vc_script),arg,e))
                  vc_script=None
                  continue
          if not vc_script and sdk_script:
              debug('use_script 4: trying sdk script: %s' % sdk_script)
              try:
                  d = script_env(sdk_script)
                  found = sdk_script
              except BatchFileExecutionError as e:
                  debug('use_script 5: failed running SDK script %s: Error:%s'%(repr(sdk_script), e))
                  continue
          elif not vc_script and not sdk_script:
              debug('use_script 6: Neither VC script nor SDK script found')
              continue
  
          debug("Found a working script/target: %s/%s"%(repr(found),arg))
          break # We've found a working target_platform, so stop looking
  
      # If we cannot find a viable installed compiler, reset the TARGET_ARCH
      # To it's initial value
      if not d:
          env['TARGET_ARCH']=req_target_platform
  
      return d
function_tokens: def msvc_find_valid_batch_script ( env , version ) : # Find the host, target, and if present the requested target: platforms = get_host_target ( env ) debug ( "host_platform %s, target_platform %s req_target_platform %s" % platforms ) host_platform , target_platform , req_target_platform = platforms # Most combinations of host + target are straightforward. # While all MSVC / Visual Studio tools are pysically 32-bit, they # make it look like there are 64-bit tools if the host is 64-bit, # so you can invoke the environment batch script to set up to build, # say, amd64 host -> x86 target. Express versions are an exception: # they always look 32-bit, so the batch scripts with 64-bit # host parts are absent. We try to fix that up in a couple of ways. # One is here: we make a table of "targets" to try, with the extra # targets being tags that tell us to try a different "host" instead # of the deduced host. try_target_archs = [ target_platform ] if req_target_platform in ( 'amd64' , 'x86_64' ) : try_target_archs . append ( 'x86_amd64' ) elif req_target_platform in ( 'x86' , ) : try_target_archs . append ( 'x86_x86' ) elif req_target_platform in ( 'arm' , ) : try_target_archs . append ( 'x86_arm' ) elif req_target_platform in ( 'arm64' , ) : try_target_archs . append ( 'x86_arm64' ) elif not req_target_platform : if target_platform in ( 'amd64' , 'x86_64' ) : try_target_archs . append ( 'x86_amd64' ) # If the user hasn't specifically requested a TARGET_ARCH, # and the TARGET_ARCH is amd64 then also try 32 bits # if there are no viable 64 bit tools installed try_target_archs . append ( 'x86' ) debug ( "host_platform: %s, try_target_archs: %s" % ( host_platform , try_target_archs ) ) d = None for tp in try_target_archs : # Set to current arch. env [ 'TARGET_ARCH' ] = tp debug ( "trying target_platform:%s" % tp ) host_target = ( host_platform , tp ) if not is_host_target_supported ( host_target , version ) : warn_msg = "host, target = %s not supported for MSVC version %s" % ( host_target , version ) SCons . Warnings . warn ( SCons . Warnings . VisualCMissingWarning , warn_msg ) arg = _HOST_TARGET_ARCH_TO_BAT_ARCH [ host_target ] # Try to locate a batch file for this host/target platform combo try : ( vc_script , use_arg , sdk_script ) = find_batch_file ( env , version , host_platform , tp ) debug ( 'vc_script:%s sdk_script:%s' % ( vc_script , sdk_script ) ) except VisualCException as e : msg = str ( e ) debug ( 'Caught exception while looking for batch file (%s)' % msg ) warn_msg = "VC version %s not installed.  " + "C/C++ compilers are most likely not set correctly.\n" + " Installed versions are: %s" warn_msg = warn_msg % ( version , get_installed_vcs ( env ) ) SCons . Warnings . warn ( SCons . Warnings . VisualCMissingWarning , warn_msg ) continue # Try to use the located batch file for this host/target platform combo debug ( 'use_script 2 %s, args:%s' % ( repr ( vc_script ) , arg ) ) found = None if vc_script : if not use_arg : arg = '' # bat file will supply platform type # Get just version numbers maj , min = msvc_version_to_maj_min ( version ) # VS2015+ if maj >= 14 : if env . get ( 'MSVC_UWP_APP' ) == '1' : # Initialize environment variables with store/UWP paths arg = ( arg + ' store' ) . lstrip ( ) try : d = script_env ( vc_script , args = arg ) found = vc_script except BatchFileExecutionError as e : debug ( 'use_script 3: failed running VC script %s: %s: Error:%s' % ( repr ( vc_script ) , arg , e ) ) vc_script = None continue if not vc_script and sdk_script : debug ( 'use_script 4: trying sdk script: %s' % sdk_script ) try : d = script_env ( sdk_script ) found = sdk_script except BatchFileExecutionError as e : debug ( 'use_script 5: failed running SDK script %s: Error:%s' % ( repr ( sdk_script ) , e ) ) continue elif not vc_script and not sdk_script : debug ( 'use_script 6: Neither VC script nor SDK script found' ) continue debug ( "Found a working script/target: %s/%s" % ( repr ( found ) , arg ) ) break # We've found a working target_platform, so stop looking # If we cannot find a viable installed compiler, reset the TARGET_ARCH # To it's initial value if not d : env [ 'TARGET_ARCH' ] = req_target_platform return d