parameters: (filename, vars=None)
return_statement: return vars
docstring: Parse a Makefile-style file.
  
      A dictionary containing name/value pairs is returned.  If an
      optional dictionary is passed in as the second argument, it is
      used instead of a new dictionary.
docstring_summary: Parse a Makefile-style file.
docstring_tokens: Parse a Makefile - style file .
function: def _parse_makefile(filename, vars=None):
      """Parse a Makefile-style file.
  
      A dictionary containing name/value pairs is returned.  If an
      optional dictionary is passed in as the second argument, it is
      used instead of a new dictionary.
      """
      # Regexes needed for parsing Makefile (and similar syntaxes,
      # like old-style Setup files).
      _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
      _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
      _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  
      if vars is None:
          vars = {}
      done = {}
      notdone = {}
  
      with codecs.open(filename, encoding='utf-8', errors="surrogateescape") as f:
          lines = f.readlines()
  
      for line in lines:
          if line.startswith('#') or line.strip() == '':
              continue
          m = _variable_rx.match(line)
          if m:
              n, v = m.group(1, 2)
              v = v.strip()
              # `$$' is a literal `$' in make
              tmpv = v.replace('$$', '')
  
              if "$" in tmpv:
                  notdone[n] = v
              else:
                  try:
                      v = int(v)
                  except ValueError:
                      # insert literal `$'
                      done[n] = v.replace('$$', '$')
                  else:
                      done[n] = v
  
      # do variable interpolation here
      variables = list(notdone.keys())
  
      # Variables with a 'PY_' prefix in the makefile. These need to
      # be made available without that prefix through sysconfig.
      # Special care is needed to ensure that variable expansion works, even
      # if the expansion uses the name without a prefix.
      renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
  
      while len(variables) > 0:
          for name in tuple(variables):
              value = notdone[name]
              m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
              if m is not None:
                  n = m.group(1)
                  found = True
                  if n in done:
                      item = str(done[n])
                  elif n in notdone:
                      # get it on a subsequent round
                      found = False
                  elif n in os.environ:
                      # do it like make: fall back to environment
                      item = os.environ[n]
  
                  elif n in renamed_variables:
                      if (name.startswith('PY_') and
                          name[3:] in renamed_variables):
                          item = ""
  
                      elif 'PY_' + n in notdone:
                          found = False
  
                      else:
                          item = str(done['PY_' + n])
  
                  else:
                      done[n] = item = ""
  
                  if found:
                      after = value[m.end():]
                      value = value[:m.start()] + item + after
                      if "$" in after:
                          notdone[name] = value
                      else:
                          try:
                              value = int(value)
                          except ValueError:
                              done[name] = value.strip()
                          else:
                              done[name] = value
                          variables.remove(name)
  
                          if (name.startswith('PY_') and
                              name[3:] in renamed_variables):
  
                              name = name[3:]
                              if name not in done:
                                  done[name] = value
  
              else:
                  # bogus variable reference (e.g. "prefix=$/opt/python");
                  # just drop it since we can't deal
                  done[name] = value
                  variables.remove(name)
  
      # strip spurious spaces
      for k, v in done.items():
          if isinstance(v, str):
              done[k] = v.strip()
  
      # save the results in the global dictionary
      vars.update(done)
      return vars
function_tokens: def _parse_makefile ( filename , vars = None ) : # Regexes needed for parsing Makefile (and similar syntaxes, # like old-style Setup files). _variable_rx = re . compile ( "([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)" ) _findvar1_rx = re . compile ( r"\$\(([A-Za-z][A-Za-z0-9_]*)\)" ) _findvar2_rx = re . compile ( r"\${([A-Za-z][A-Za-z0-9_]*)}" ) if vars is None : vars = { } done = { } notdone = { } with codecs . open ( filename , encoding = 'utf-8' , errors = "surrogateescape" ) as f : lines = f . readlines ( ) for line in lines : if line . startswith ( '#' ) or line . strip ( ) == '' : continue m = _variable_rx . match ( line ) if m : n , v = m . group ( 1 , 2 ) v = v . strip ( ) # `$$' is a literal `$' in make tmpv = v . replace ( '$$' , '' ) if "$" in tmpv : notdone [ n ] = v else : try : v = int ( v ) except ValueError : # insert literal `$' done [ n ] = v . replace ( '$$' , '$' ) else : done [ n ] = v # do variable interpolation here variables = list ( notdone . keys ( ) ) # Variables with a 'PY_' prefix in the makefile. These need to # be made available without that prefix through sysconfig. # Special care is needed to ensure that variable expansion works, even # if the expansion uses the name without a prefix. renamed_variables = ( 'CFLAGS' , 'LDFLAGS' , 'CPPFLAGS' ) while len ( variables ) > 0 : for name in tuple ( variables ) : value = notdone [ name ] m = _findvar1_rx . search ( value ) or _findvar2_rx . search ( value ) if m is not None : n = m . group ( 1 ) found = True if n in done : item = str ( done [ n ] ) elif n in notdone : # get it on a subsequent round found = False elif n in os . environ : # do it like make: fall back to environment item = os . environ [ n ] elif n in renamed_variables : if ( name . startswith ( 'PY_' ) and name [ 3 : ] in renamed_variables ) : item = "" elif 'PY_' + n in notdone : found = False else : item = str ( done [ 'PY_' + n ] ) else : done [ n ] = item = "" if found : after = value [ m . end ( ) : ] value = value [ : m . start ( ) ] + item + after if "$" in after : notdone [ name ] = value else : try : value = int ( value ) except ValueError : done [ name ] = value . strip ( ) else : done [ name ] = value variables . remove ( name ) if ( name . startswith ( 'PY_' ) and name [ 3 : ] in renamed_variables ) : name = name [ 3 : ] if name not in done : done [ name ] = value else : # bogus variable reference (e.g. "prefix=$/opt/python"); # just drop it since we can't deal done [ name ] = value variables . remove ( name ) # strip spurious spaces for k , v in done . items ( ) : if isinstance ( v , str ) : done [ k ] = v . strip ( ) # save the results in the global dictionary vars . update ( done ) return vars