find_headers.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2016 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 collections
  8. import json
  9. import os
  10. import subprocess
  11. import sys
  12. # Finds all public sources in include directories then write them to skia.h.
  13. # Also write skia.h.deps, which Ninja uses to track dependencies. It's the
  14. # very same mechanism Ninja uses to know which .h files affect which .cpp files.
  15. gn = sys.argv[1]
  16. absolute_source = sys.argv[2]
  17. skia_h = sys.argv[3]
  18. include_dirs = sys.argv[4:]
  19. absolute_source = os.path.normpath(absolute_source)
  20. include_dirs = [os.path.join(os.path.normpath(include_dir), '')
  21. for include_dir in include_dirs]
  22. include_dirs.sort(key=len, reverse=True)
  23. gn_desc_cmd = [gn, 'desc', '.', '--root=%s' % absolute_source, '--format=json',
  24. '*']
  25. desc_json_txt = ''
  26. try:
  27. desc_json_txt = subprocess.check_output(gn_desc_cmd)
  28. except subprocess.CalledProcessError as e:
  29. print (e.output)
  30. raise
  31. desc_json = {}
  32. try:
  33. desc_json = json.loads(desc_json_txt)
  34. except ValueError:
  35. print (desc_json_txt)
  36. raise
  37. sources = set()
  38. for target in desc_json.values():
  39. # We'll use `public` headers if they're listed, or pull them from `sources`
  40. # if not. GN sneaks in a default "public": "*" into the JSON if you don't
  41. # set one explicitly.
  42. search_list = target.get('public')
  43. if search_list == '*':
  44. search_list = target.get('sources', [])
  45. for name in search_list:
  46. sources.add(os.path.join(absolute_source, os.path.normpath(name[2:])))
  47. Header = collections.namedtuple('Header', ['absolute', 'include'])
  48. headers = {}
  49. for source in sources:
  50. source_as_include = [os.path.relpath(source, absolute_source)
  51. for include_dir in include_dirs
  52. if source.startswith(include_dir)]
  53. if not source_as_include:
  54. continue
  55. statinfo = os.stat(source)
  56. key = str(statinfo.st_ino) + ':' + str(statinfo.st_dev)
  57. # On Windows os.stat st_ino is 0 until 3.3.4 and st_dev is 0 until 3.4.0.
  58. if key == '0:0':
  59. key = source
  60. include_path = source_as_include[0]
  61. if key not in headers or len(include_path) < len(headers[key].include):
  62. headers[key] = Header(source, include_path)
  63. headers = headers.values()
  64. sorted(headers, key=lambda x: x.include)
  65. with open(skia_h, 'w') as f:
  66. f.write('// skia.h generated by GN.\n')
  67. f.write('#ifndef skia_h_DEFINED\n')
  68. f.write('#define skia_h_DEFINED\n')
  69. for header in headers:
  70. f.write('#include "' + header.include + '"\n')
  71. f.write('#endif//skia_h_DEFINED\n')
  72. with open(skia_h + '.deps', 'w') as f:
  73. f.write(skia_h + ':')
  74. for header in headers:
  75. f.write(' ' + header.absolute)
  76. f.write(' build.ninja.d')
  77. f.write('\n')
  78. # Temporary: during development this file wrote skia.h.d, not skia.h.deps,
  79. # and I think we have some bad versions of those files laying around.
  80. if os.path.exists(skia_h + '.d'):
  81. os.remove(skia_h + '.d')