blob: d0ee8f883bb9aacb9093d9253475cf83d455c5a3 [file] [log] [blame]
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001#!/usr/bin/python2
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
Mike Kleinbb413432019-07-26 11:55:40 -05008import StringIO
9import argparse
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010import os
Mike Kleinbb413432019-07-26 11:55:40 -050011import sys
12
13parser = argparse.ArgumentParser()
14parser.add_argument('-n', '--dry-run', action='store_true',
15 help='Just check there is nothing to rewrite.')
16parser.add_argument('sources', nargs='*',
17 help='Source files to rewrite, or all if empty.')
18args = parser.parse_args()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019
20roots = [
21 'bench',
22 'dm',
23 'docs',
24 'example',
25 'experimental',
26 'fuzz',
27 'gm',
28 'include',
29 'modules',
30 'platform_tools/android/apps',
31 'samplecode',
32 'src',
33 'tests',
Mike Kleine11e5c12019-04-24 12:46:06 -050034 'third_party/etc1',
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035 'third_party/gif',
36 'tools'
37 ]
38
Mike Klein66ed6a02019-07-25 11:18:06 -050039# Don't count our local Vulkan headers as Skia headers;
40# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
41blacklist = ['include/third_party/vulkan']
Mike Klein52337de2019-07-25 09:00:52 -050042
Hal Canary4df3d532019-07-30 13:49:45 -040043assert '/' in [os.sep, os.altsep]
44def fix_path(p):
45 return p.replace(os.sep, os.altsep) if os.altsep else p
46
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047# Map short name -> absolute path for all Skia headers.
48headers = {}
49for root in roots:
50 for path, _, files in os.walk(root):
Hal Canary4df3d532019-07-30 13:49:45 -040051 if not any(snippet in fix_path(path) for snippet in blacklist):
Mike Klein66ed6a02019-07-25 11:18:06 -050052 for file_name in files:
53 if file_name.endswith('.h'):
54 if file_name in headers:
55 print path, file_name, headers[file_name]
56 assert file_name not in headers
57 headers[file_name] = os.path.abspath(os.path.join(path, file_name))
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058
Mike Kleinbb413432019-07-26 11:55:40 -050059def to_rewrite():
60 if args.sources:
61 for path in args.sources:
62 yield path
63 else:
64 for root in roots:
65 for path, _, files in os.walk(root):
66 for file_name in files:
67 yield os.path.join(path, file_name)
68
Mike Kleinc0bd9f92019-04-23 12:05:21 -050069# Rewrite any #includes relative to Skia's top-level directory.
Mike Klein140a4762019-08-01 14:24:08 -050070need_rewriting = []
Mike Kleinbb413432019-07-26 11:55:40 -050071for file_path in to_rewrite():
skia-autorollfe16a332019-08-20 19:44:54 +000072 if 'generated' in file_path or 'third_party/skcms' in file_path:
Mike Kleinbb413432019-07-26 11:55:40 -050073 continue
74 if (file_path.endswith('.h') or
75 file_path.endswith('.c') or
76 file_path.endswith('.m') or
77 file_path.endswith('.mm') or
78 file_path.endswith('.inc') or
79 file_path.endswith('.fp') or
80 file_path.endswith('.cc') or
81 file_path.endswith('.cpp')):
82 # Read the whole file into memory.
83 lines = open(file_path).readlines()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050084
Mike Kleinbb413432019-07-26 11:55:40 -050085 # Write it back out again line by line with substitutions for #includes.
Hal Canary4df3d532019-07-30 13:49:45 -040086 output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb')
Mike Kleinc0bd9f92019-04-23 12:05:21 -050087
Mike Kleinbb413432019-07-26 11:55:40 -050088 includes = []
89 for line in lines:
90 parts = line.replace('<', '"').replace('>', '"').split('"')
91 if (len(parts) == 3
92 and '#' in parts[0]
93 and 'include' in parts[0]
94 and os.path.basename(parts[1]) in headers):
Hal Canary4df3d532019-07-30 13:49:45 -040095 header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
96 includes.append(parts[0] + '"%s"' % header + parts[2])
Mike Kleinbb413432019-07-26 11:55:40 -050097 else:
98 for inc in sorted(includes):
Hal Canary4df3d532019-07-30 13:49:45 -040099 output.write(inc.strip('\n') + '\n')
Mike Kleinbb413432019-07-26 11:55:40 -0500100 includes = []
Hal Canary4df3d532019-07-30 13:49:45 -0400101 output.write(line.strip('\n') + '\n')
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500102
Mike Kleinbb413432019-07-26 11:55:40 -0500103 if args.dry_run and output.getvalue() != open(file_path).read():
Mike Klein140a4762019-08-01 14:24:08 -0500104 need_rewriting.append(file_path)
Mike Kleinbb413432019-07-26 11:55:40 -0500105 rc = 1
106 output.close()
Mike Klein140a4762019-08-01 14:24:08 -0500107
108if need_rewriting:
109 print 'Some files need rewritten #includes:'
110 for path in need_rewriting:
111 print '\t' + path
112 print 'To do this automatically, run'
113 print 'python tools/rewrite_includes.py ' + ' '.join(need_rewriting)
114 sys.exit(1)