ambuild2/util.py
changeset 94 bd331a7a3443
parent 90 1fd8ec354d47
child 105 de952ffb32ee
     1.1 --- a/ambuild2/util.py	Mon Oct 07 20:34:01 2013 -0700
     1.2 +++ b/ambuild2/util.py	Wed Oct 09 20:49:20 2013 -0700
     1.3 @@ -1,7 +1,7 @@
     1.4  # vim: set sts=2 ts=8 sw=2 tw=99 et: 
     1.5  import errno
     1.6 -import os, sys
     1.7  import subprocess
     1.8 +import re, os, sys
     1.9  import multiprocessing
    1.10  try:
    1.11    import __builtin__ as builtins
    1.12 @@ -123,7 +123,8 @@
    1.13      self.new = folder
    1.14  
    1.15    def __enter__(self):
    1.16 -    os.chdir(self.new)
    1.17 +    if self.new:
    1.18 +      os.chdir(self.new)
    1.19  
    1.20    def __exit__(self, type, value, traceback):
    1.21      os.chdir(self.old)
    1.22 @@ -149,7 +150,7 @@
    1.23    out = stdout.decode()
    1.24    err = stderr.decode()
    1.25  
    1.26 -  out = (' '.join([i for i in argv])) + out
    1.27 +  out = (' '.join([i for i in argv])) + '\n' + out
    1.28    return p, out, err
    1.29  
    1.30  def typeof(x):
    1.31 @@ -170,3 +171,40 @@
    1.32    if bytes is str:
    1.33      return s
    1.34    return bytes(s, 'utf8')
    1.35 +
    1.36 +sReadIncludes = 0
    1.37 +sLookForIncludeGuard = 1
    1.38 +sFoundIncludeGuard = 2
    1.39 +sIgnoring = 3
    1.40 +def ParseGCCDeps(text):
    1.41 +  deps = set()
    1.42 +  strip = False
    1.43 +  new_text = ''
    1.44 +
    1.45 +  state = sReadIncludes
    1.46 +  for line in re.split('\n+', text):
    1.47 +    if state == sReadIncludes:
    1.48 +      m = re.match('\.+\s+(.+)\s*$', line)
    1.49 +      if m == None:
    1.50 +        state = sLookForIncludeGuard
    1.51 +      else:
    1.52 +        name = m.groups()[0]
    1.53 +        if os.path.exists(name):
    1.54 +          strip = True
    1.55 +          deps.add(name)
    1.56 +        else:
    1.57 +          state = LookForIncludeGuard
    1.58 +    if state == sLookForIncludeGuard:
    1.59 +      if line.startswith('Multiple include guards may be useful for:'):
    1.60 +        state = sFoundIncludeGuard
    1.61 +        strip = True
    1.62 +      else:
    1.63 +        state = sReadIncludes
    1.64 +        strip = False
    1.65 +    elif state == sFoundIncludeGuard:
    1.66 +      if not line in deps:
    1.67 +        strip = False
    1.68 +        state = sIgnoring
    1.69 +    if not strip and len(line):
    1.70 +      new_text += line + '\n'
    1.71 +  return new_text, deps