xref: /aosp_15_r20/external/skia/tools/rewrite_includes.py (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 sys
12
13from io import StringIO
14
15
16parser = argparse.ArgumentParser()
17parser.add_argument('-n', '--dry-run', action='store_true',
18                    help='Just check there is nothing to rewrite.')
19parser.add_argument('sources', nargs='*',
20                    help='Source files to rewrite, or all if empty.')
21args = parser.parse_args()
22
23roots = [
24  'bench',
25  'dm',
26  'docs',
27  'experimental',
28  'fuzz',
29  'gm',
30  'include',
31  'modules',
32  'platform_tools/android/apps',
33  'samplecode',
34  'src',
35  'tests',
36  'third_party/etc1',
37  'third_party/gif',
38  'tools'
39]
40
41ignorelist = [
42  # Don't count our local Vulkan headers as Skia headers;
43  # we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
44  'include/third_party/vulkan',
45  # Some node_modules/ files (used by CanvasKit et al) have c++ code which we should ignore.
46  'node_modules',
47  'include/third_party/skcms',
48  'src/gpu/vk/vulkanmemoryallocator',
49  # Used by Jetski and Graphite
50  'Surface.h',
51  # Used by Ganesh and Graphite
52  'Device.h',
53
54  # Transitional
55  'tools/window',
56]
57
58assert '/' in [os.sep, os.altsep]
59def fix_path(p):
60  return p.replace(os.sep, os.altsep) if os.altsep else p
61
62# Map short name -> absolute path for all Skia headers.
63headers = {}
64for root in roots:
65  for path, _, files in os.walk(root):
66    if not any(snippet in fix_path(path) for snippet in ignorelist):
67      for file_name in files:
68        if file_name.endswith('.h') and not file_name in ignorelist:
69          if file_name in headers:
70            message = ('Header filename is used more than once!\n- ' + path + '/' + file_name +
71                       '\n- ' + headers[file_name])
72            assert file_name not in headers, message
73          headers[file_name] = os.path.abspath(os.path.join(path, file_name))
74
75def to_rewrite():
76  if args.sources:
77    for path in args.sources:
78      yield path
79  else:
80    for root in roots:
81      for path, _, files in os.walk(root):
82        for file_name in files:
83          yield os.path.join(path, file_name)
84
85# Rewrite any #includes relative to Skia's top-level directory.
86need_rewriting = []
87for file_path in to_rewrite():
88  if ('/generated/' in file_path or
89      'tests/sksl/' in file_path or
90      'third_party/skcms' in file_path or
91      'modules/skcms' in file_path or
92      # transitional
93      'jetski' in file_path or
94      'tools/window' in file_path or
95      file_path.startswith('bazel/rbe') or
96      'example/external_client/' in file_path or
97      # We intentionally list SkUserConfig.h not from the root in this file.
98      file_path == 'include/private/base/SkLoadUserConfig.h'):
99    continue
100  if (file_path.endswith('.h') or
101      file_path.endswith('.c') or
102      file_path.endswith('.m') or
103      file_path.endswith('.mm') or
104      file_path.endswith('.inc') or
105      file_path.endswith('.cc') or
106      file_path.endswith('.cpp')):
107    # Read the whole file into memory.
108    lines = open(file_path).readlines()
109
110    # Write it back out again line by line with substitutions for #includes.
111    output = StringIO() if args.dry_run else open(file_path, 'w')
112
113    includes = []
114    for line in lines:
115      parts = line.replace('<', '"').replace('>', '"').split('"')
116      if (len(parts) == 3
117          and '#' in parts[0]
118          and 'include' in parts[0]
119          and os.path.basename(parts[1]) in headers):
120        header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
121        includes.append(parts[0] + '"%s"' % header + parts[2])
122      else:
123        # deduplicate includes in this block. If a file needs to be included
124        # multiple times, the separate includes should go in different blocks.
125        includes = sorted(list(set(includes)))
126        for inc in includes:
127          output.write(inc.strip('\n') + '\n')
128        includes = []
129        output.write(line.strip('\n') + '\n')
130    # Fix any straggling includes, e.g. in a file that only includes something else.
131    for inc in sorted(includes):
132      output.write(inc.strip('\n') + '\n')
133    if args.dry_run and output.getvalue() != open(file_path).read():
134      need_rewriting.append(file_path)
135      rc = 1
136    output.close()
137
138if need_rewriting:
139  print('Some files need rewritten #includes:')
140  for path in need_rewriting:
141    print('\t' + path)
142  print('To do this automatically, run')
143  print('python3 tools/rewrite_includes.py ' + ' '.join(need_rewriting))
144  sys.exit(1)
145