| 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 |
|
| 9 |
import argparse |
| 10 |
import os |
| 11 |
import sys |
| 12 |
|
| 13 |
from io import StringIO |
| 14 |
|
| 15 |
|
| 16 |
parser = argparse.ArgumentParser() |
| 17 |
parser.add_argument('-n', '--dry-run', action='store_true', |
| 18 |
help='Just check there is nothing to rewrite.') |
| 19 |
parser.add_argument('sources', nargs='*', |
| 20 |
help='Source files to rewrite, or all if empty.') |
| 21 |
args = parser.parse_args() |
| 22 |
|
| 23 |
roots = [ |
| 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 |
|
| 41 |
ignorelist = [ |
| 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 |
] |
| 50 |
|
| 51 |
assert '/' in [os.sep, os.altsep] |
| 52 |
def fix_path(p): |
| 53 |
return p.replace(os.sep, os.altsep) if os.altsep else p |
| 54 |
|
| 55 |
# Map short name -> absolute path for all Skia headers. Duplicate short names are allowed, the |
| 56 |
# stored value will hold a list of absolute paths. If a short name is found in source code and the |
| 57 |
# absolute path is ambiguous, an error message is reported. |
| 58 |
headers = {} |
| 59 |
for root in roots: |
| 60 |
for path, _, files in os.walk(root): |
| 61 |
if not any(snippet in fix_path(path) for snippet in ignorelist): |
| 62 |
for file_name in files: |
| 63 |
if file_name.endswith('.h') and not file_name in ignorelist: |
| 64 |
full_name = os.path.abspath(os.path.join(path, file_name)) |
| 65 |
include_path = fix_path(os.path.relpath(full_name, '.')) |
| 66 |
headers.setdefault(file_name, []).append(include_path) |
| 67 |
|
| 68 |
def to_rewrite(): |
| 69 |
if args.sources: |
| 70 |
for path in args.sources: |
| 71 |
yield path |
| 72 |
else: |
| 73 |
for root in roots: |
| 74 |
for path, _, files in os.walk(root): |
| 75 |
for file_name in files: |
| 76 |
yield os.path.join(path, file_name) |
| 77 |
|
| 78 |
# Rewrite any #includes relative to Skia's top-level directory. |
| 79 |
need_rewriting = [] |
| 80 |
rewrite_is_automatic = True |
| 81 |
for file_path in to_rewrite(): |
| 82 |
if ('/generated/' in file_path or |
| 83 |
'tests/sksl/' in file_path or |
| 84 |
'third_party/skcms' in file_path or |
| 85 |
'modules/skcms' in file_path or |
| 86 |
'tools/window' in file_path or |
| 87 |
file_path.startswith('bazel/rbe') or |
| 88 |
'example/external_client/' in file_path or |
| 89 |
# We intentionally list SkUserConfig.h not from the root in this file. |
| 90 |
file_path == 'include/private/base/SkLoadUserConfig.h'): |
| 91 |
continue |
| 92 |
if (file_path.endswith('.h') or |
| 93 |
file_path.endswith('.c') or |
| 94 |
file_path.endswith('.m') or |
| 95 |
file_path.endswith('.mm') or |
| 96 |
file_path.endswith('.inc') or |
| 97 |
file_path.endswith('.cc') or |
| 98 |
file_path.endswith('.cpp')): |
| 99 |
# Read the whole file into memory. |
| 100 |
lines = open(file_path).readlines() |
| 101 |
|
| 102 |
# Write it back out again line by line with substitutions for #includes. |
| 103 |
output = StringIO() if args.dry_run else open(file_path, 'w') |
| 104 |
|
| 105 |
includes = [] |
| 106 |
for line in lines: |
| 107 |
parts = line.replace('<', '"').replace('>', '"').split('"') |
| 108 |
if (len(parts) == 3 |
| 109 |
and '#' in parts[0] |
| 110 |
and 'include' in parts[0] |
| 111 |
and os.path.basename(parts[1]) in headers): |
| 112 |
include_paths = headers[os.path.basename(parts[1])] |
| 113 |
if (len(include_paths) == 1): |
| 114 |
header = include_paths[0] |
| 115 |
else: |
| 116 |
# This either represents a valid include path in include_paths, or it preserves the |
| 117 |
# original ambiguous include that has to be manually fixed. |
| 118 |
header = parts[1] |
| 119 |
if not (parts[1] in include_paths): |
| 120 |
need_rewriting.append(file_path) |
| 121 |
rewrite_is_automatic = False |
| 122 |
print('File has an ambiguous relative include: ' + file_path) |
| 123 |
print('Replace "' + parts[1] + '" with one of:') |
| 124 |
for opt in include_paths: |
| 125 |
print('\t' + opt) |
| 126 |
|
| 127 |
includes.append(parts[0] + '"%s"' % header + parts[2]) |
| 128 |
else: |
| 129 |
# deduplicate includes in this block. If a file needs to be included |
| 130 |
# multiple times, the separate includes should go in different blocks. |
| 131 |
includes = sorted(list(set(includes))) |
| 132 |
for inc in includes: |
| 133 |
output.write(inc.strip('\n') + '\n') |
| 134 |
includes = [] |
| 135 |
output.write(line.strip('\n') + '\n') |
| 136 |
# Fix any straggling includes, e.g. in a file that only includes something else. |
| 137 |
for inc in sorted(includes): |
| 138 |
output.write(inc.strip('\n') + '\n') |
| 139 |
if args.dry_run and output.getvalue() != open(file_path).read(): |
| 140 |
need_rewriting.append(file_path) |
| 141 |
output.close() |
| 142 |
|
| 143 |
if need_rewriting: |
| 144 |
print('Some files need rewritten #includes:') |
| 145 |
for path in need_rewriting: |
| 146 |
print('\t' + path) |
| 147 |
|
| 148 |
if rewrite_is_automatic: |
| 149 |
print('To do this automatically, run') |
| 150 |
print('python3 tools/rewrite_includes.py ' + ' '.join(need_rewriting)) |
| 151 |
else: |
| 152 |
print('Automatic rewritting of includes not possible.') |
| 153 |
sys.exit(1) |