• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
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
8
9import argparse
10import os
11import six
12import sys
13
14from six import StringIO
15
16
17parser = argparse.ArgumentParser()
18parser.add_argument('-n', '--dry-run', action='store_true',
19                    help='Just check there is nothing to rewrite.')
20parser.add_argument('sources', nargs='*',
21                    help='Source files to rewrite, or all if empty.')
22args = parser.parse_args()
23
24roots = [
25    'bench',
26    'dm',
27    'docs',
28    'example',
29    'experimental',
30    'fuzz',
31    'gm',
32    'include',
33    'modules',
34    'platform_tools/android/apps',
35    'samplecode',
36    'src',
37    'tests',
38    'third_party/etc1',
39    'third_party/gif',
40    'tools'
41  ]
42
43# Don't count our local Vulkan headers as Skia headers;
44# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
45# Nor do we care about things in node_modules, used by *Kits.
46ignorelist = ['include/third_party/vulkan', 'node_modules']
47
48assert '/' in [os.sep, os.altsep]
49def fix_path(p):
50  return p.replace(os.sep, os.altsep) if os.altsep else p
51
52# Map short name -> absolute path for all Skia headers.
53headers = {}
54for root in roots:
55  for path, _, files in os.walk(root):
56    if not any(snippet in fix_path(path) for snippet in ignorelist):
57      for file_name in files:
58        if file_name.endswith('.h'):
59          if file_name in headers:
60            message = ('Header filename is used more than once!\n- ' + path + '/' + file_name +
61                       '\n- ' + headers[file_name])
62            assert file_name not in headers, message
63          headers[file_name] = os.path.abspath(os.path.join(path, file_name))
64
65def to_rewrite():
66  if args.sources:
67    for path in args.sources:
68      yield path
69  else:
70    for root in roots:
71      for path, _, files in os.walk(root):
72        for file_name in files:
73          yield os.path.join(path, file_name)
74
75# Rewrite any #includes relative to Skia's top-level directory.
76need_rewriting = []
77for file_path in to_rewrite():
78  if ('/generated/' in file_path or
79      'tests/sksl/' in file_path or
80      'third_party/skcms' in file_path):
81    continue
82  if (file_path.endswith('.h') or
83      file_path.endswith('.c') or
84      file_path.endswith('.m') or
85      file_path.endswith('.mm') or
86      file_path.endswith('.inc') or
87      file_path.endswith('.cc') or
88      file_path.endswith('.cpp')):
89    # Read the whole file into memory.
90    lines = open(file_path).readlines()
91
92    # Write it back out again line by line with substitutions for #includes.
93    output = StringIO() if args.dry_run else open(file_path, 'w')
94
95    includes = []
96    for line in lines:
97      parts = line.replace('<', '"').replace('>', '"').split('"')
98      if (len(parts) == 3
99          and '#' in parts[0]
100          and 'include' in parts[0]
101          and os.path.basename(parts[1]) in headers):
102        header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
103        includes.append(parts[0] + '"%s"' % header + parts[2])
104      else:
105        for inc in sorted(includes):
106          output.write(inc.strip('\n') + '\n')
107        includes = []
108        output.write(line.strip('\n') + '\n')
109
110    if args.dry_run and output.getvalue() != open(file_path).read():
111      need_rewriting.append(file_path)
112      rc = 1
113    output.close()
114
115if need_rewriting:
116  print('Some files need rewritten #includes:')
117  for path in need_rewriting:
118    print('\t' + path)
119  print('To do this automatically, run')
120  print('python tools/rewrite_includes.py ' + ' '.join(need_rewriting))
121  sys.exit(1)
122