2019-04-23 17:05:21 +00:00
|
|
|
#!/usr/bin/python2
|
|
|
|
#
|
|
|
|
# Copyright 2019 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2021-03-25 13:04:43 +00:00
|
|
|
|
|
|
|
from __future__ import print_function
|
2019-07-26 16:55:40 +00:00
|
|
|
import StringIO
|
|
|
|
import argparse
|
2019-04-23 17:05:21 +00:00
|
|
|
import os
|
2019-07-26 16:55:40 +00:00
|
|
|
import sys
|
|
|
|
|
2021-03-25 13:04:43 +00:00
|
|
|
|
2019-07-26 16:55:40 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-n', '--dry-run', action='store_true',
|
|
|
|
help='Just check there is nothing to rewrite.')
|
|
|
|
parser.add_argument('sources', nargs='*',
|
|
|
|
help='Source files to rewrite, or all if empty.')
|
|
|
|
args = parser.parse_args()
|
2019-04-23 17:05:21 +00:00
|
|
|
|
|
|
|
roots = [
|
|
|
|
'bench',
|
|
|
|
'dm',
|
|
|
|
'docs',
|
|
|
|
'example',
|
|
|
|
'experimental',
|
|
|
|
'fuzz',
|
|
|
|
'gm',
|
|
|
|
'include',
|
|
|
|
'modules',
|
|
|
|
'platform_tools/android/apps',
|
|
|
|
'samplecode',
|
|
|
|
'src',
|
|
|
|
'tests',
|
2019-04-24 17:46:06 +00:00
|
|
|
'third_party/etc1',
|
2019-04-23 17:05:21 +00:00
|
|
|
'third_party/gif',
|
|
|
|
'tools'
|
|
|
|
]
|
|
|
|
|
2019-07-25 16:18:06 +00:00
|
|
|
# Don't count our local Vulkan headers as Skia headers;
|
|
|
|
# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
|
2019-10-17 19:03:00 +00:00
|
|
|
# Nor do we care about things in node_modules, used by *Kits.
|
2020-10-16 14:53:27 +00:00
|
|
|
ignorelist = ['include/third_party/vulkan', 'node_modules']
|
2019-07-25 14:00:52 +00:00
|
|
|
|
2019-07-30 17:49:45 +00:00
|
|
|
assert '/' in [os.sep, os.altsep]
|
|
|
|
def fix_path(p):
|
|
|
|
return p.replace(os.sep, os.altsep) if os.altsep else p
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
# Map short name -> absolute path for all Skia headers.
|
|
|
|
headers = {}
|
|
|
|
for root in roots:
|
|
|
|
for path, _, files in os.walk(root):
|
2019-10-17 19:03:00 +00:00
|
|
|
if not any(snippet in fix_path(path) for snippet in ignorelist):
|
2019-07-25 16:18:06 +00:00
|
|
|
for file_name in files:
|
2020-02-26 14:25:52 +00:00
|
|
|
if file_name.endswith('.h'):
|
2019-07-25 16:18:06 +00:00
|
|
|
if file_name in headers:
|
2021-04-15 18:45:59 +00:00
|
|
|
message = ('Header filename is used more than once!\n- ' + path + '/' + file_name +
|
|
|
|
'\n- ' + headers[file_name])
|
|
|
|
assert file_name not in headers, message
|
2019-07-25 16:18:06 +00:00
|
|
|
headers[file_name] = os.path.abspath(os.path.join(path, file_name))
|
2019-04-23 17:05:21 +00:00
|
|
|
|
2019-07-26 16:55:40 +00:00
|
|
|
def to_rewrite():
|
|
|
|
if args.sources:
|
|
|
|
for path in args.sources:
|
|
|
|
yield path
|
|
|
|
else:
|
|
|
|
for root in roots:
|
|
|
|
for path, _, files in os.walk(root):
|
|
|
|
for file_name in files:
|
|
|
|
yield os.path.join(path, file_name)
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
# Rewrite any #includes relative to Skia's top-level directory.
|
2019-08-01 19:24:08 +00:00
|
|
|
need_rewriting = []
|
2019-07-26 16:55:40 +00:00
|
|
|
for file_path in to_rewrite():
|
2020-09-14 14:21:44 +00:00
|
|
|
if ('/generated/' in file_path or
|
|
|
|
'tests/sksl/' in file_path or
|
|
|
|
'third_party/skcms' in file_path):
|
2019-07-26 16:55:40 +00:00
|
|
|
continue
|
|
|
|
if (file_path.endswith('.h') or
|
|
|
|
file_path.endswith('.c') or
|
|
|
|
file_path.endswith('.m') or
|
|
|
|
file_path.endswith('.mm') or
|
|
|
|
file_path.endswith('.inc') or
|
|
|
|
file_path.endswith('.fp') or
|
|
|
|
file_path.endswith('.cc') or
|
|
|
|
file_path.endswith('.cpp')):
|
|
|
|
# Read the whole file into memory.
|
|
|
|
lines = open(file_path).readlines()
|
2019-04-23 17:05:21 +00:00
|
|
|
|
2019-07-26 16:55:40 +00:00
|
|
|
# Write it back out again line by line with substitutions for #includes.
|
2019-07-30 17:49:45 +00:00
|
|
|
output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb')
|
2019-04-23 17:05:21 +00:00
|
|
|
|
2019-07-26 16:55:40 +00:00
|
|
|
includes = []
|
|
|
|
for line in lines:
|
|
|
|
parts = line.replace('<', '"').replace('>', '"').split('"')
|
|
|
|
if (len(parts) == 3
|
|
|
|
and '#' in parts[0]
|
|
|
|
and 'include' in parts[0]
|
|
|
|
and os.path.basename(parts[1]) in headers):
|
2019-07-30 17:49:45 +00:00
|
|
|
header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
|
|
|
|
includes.append(parts[0] + '"%s"' % header + parts[2])
|
2019-07-26 16:55:40 +00:00
|
|
|
else:
|
|
|
|
for inc in sorted(includes):
|
2019-07-30 17:49:45 +00:00
|
|
|
output.write(inc.strip('\n') + '\n')
|
2019-07-26 16:55:40 +00:00
|
|
|
includes = []
|
2019-07-30 17:49:45 +00:00
|
|
|
output.write(line.strip('\n') + '\n')
|
2019-04-23 17:05:21 +00:00
|
|
|
|
2019-07-26 16:55:40 +00:00
|
|
|
if args.dry_run and output.getvalue() != open(file_path).read():
|
2019-08-01 19:24:08 +00:00
|
|
|
need_rewriting.append(file_path)
|
2019-07-26 16:55:40 +00:00
|
|
|
rc = 1
|
|
|
|
output.close()
|
2019-08-01 19:24:08 +00:00
|
|
|
|
|
|
|
if need_rewriting:
|
2021-03-25 13:04:43 +00:00
|
|
|
print('Some files need rewritten #includes:')
|
2019-08-01 19:24:08 +00:00
|
|
|
for path in need_rewriting:
|
2021-03-25 13:04:43 +00:00
|
|
|
print('\t' + path)
|
|
|
|
print('To do this automatically, run')
|
|
|
|
print('python tools/rewrite_includes.py ' + ' '.join(need_rewriting))
|
2019-08-01 19:24:08 +00:00
|
|
|
sys.exit(1)
|