rewrite_includes.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/python2
  2. #
  3. # Copyright 2019 Google Inc.
  4. #
  5. # Use of this source code is governed by a BSD-style license that can be
  6. # found in the LICENSE file.
  7. import os
  8. roots = [
  9. 'bench',
  10. 'dm',
  11. 'docs',
  12. 'example',
  13. 'experimental',
  14. 'fuzz',
  15. 'gm',
  16. 'include',
  17. 'modules',
  18. 'platform_tools/android/apps',
  19. 'samplecode',
  20. 'src',
  21. 'tests',
  22. 'third_party/etc1',
  23. 'third_party/gif',
  24. 'tools'
  25. ]
  26. # Map short name -> absolute path for all Skia headers.
  27. headers = {}
  28. for root in roots:
  29. for path, _, files in os.walk(root):
  30. for file_name in files:
  31. if file_name.endswith('.h'):
  32. if file_name in headers:
  33. print path, file_name, headers[file_name]
  34. assert file_name not in headers
  35. headers[file_name] = os.path.abspath(os.path.join(path, file_name))
  36. # Rewrite any #includes relative to Skia's top-level directory.
  37. for root in roots:
  38. for path, _, files in os.walk(root):
  39. if 'generated' in path:
  40. continue
  41. for file_name in files:
  42. if (file_name.endswith('.h') or
  43. file_name.endswith('.c') or
  44. file_name.endswith('.m') or
  45. file_name.endswith('.mm') or
  46. file_name.endswith('.inc') or
  47. file_name.endswith('.fp') or
  48. file_name.endswith('.cc') or
  49. file_name.endswith('.cpp')):
  50. # Read the whole file into memory.
  51. file_path = os.path.join(path, file_name)
  52. lines = open(file_path).readlines()
  53. # Write it back out again line by line with substitutions for #includes.
  54. with open(file_path, 'w') as output:
  55. includes = []
  56. for line in lines:
  57. parts = line.split('"')
  58. if (len(parts) == 3
  59. and '#' in parts[0]
  60. and 'include' in parts[0]
  61. and os.path.basename(parts[1]) in headers):
  62. header = headers[os.path.basename(parts[1])]
  63. includes.append(parts[0] +
  64. '"%s"' % os.path.relpath(header, '.') +
  65. parts[2])
  66. else:
  67. for inc in sorted(includes):
  68. print >>output, inc.strip('\n')
  69. includes = []
  70. print >>output, line.strip('\n')