blob: 6ebd4600f1d8965c8eec51b212772e30001b1011 [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.
Kevin Lubick87fe9b02019-10-17 15:03:00 -040041# Nor do we care about things in node_modules, used by *Kits.
Florin Malitaa5e8f6b2020-10-16 10:53:27 -040042ignorelist = ['include/third_party/vulkan', 'node_modules']
Mike Klein52337de2019-07-25 09:00:52 -050043
Hal Canary4df3d532019-07-30 13:49:45 -040044assert '/' in [os.sep, os.altsep]
45def fix_path(p):
46 return p.replace(os.sep, os.altsep) if os.altsep else p
47
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048# Map short name -> absolute path for all Skia headers.
49headers = {}
50for root in roots:
51 for path, _, files in os.walk(root):
Kevin Lubick87fe9b02019-10-17 15:03:00 -040052 if not any(snippet in fix_path(path) for snippet in ignorelist):
Mike Klein66ed6a02019-07-25 11:18:06 -050053 for file_name in files:
Mike Kleina121fb62020-02-26 08:25:52 -060054 if file_name.endswith('.h'):
Mike Klein66ed6a02019-07-25 11:18:06 -050055 if file_name in headers:
56 print path, file_name, headers[file_name]
57 assert file_name not in headers
58 headers[file_name] = os.path.abspath(os.path.join(path, file_name))
Mike Kleinc0bd9f92019-04-23 12:05:21 -050059
Mike Kleinbb413432019-07-26 11:55:40 -050060def to_rewrite():
61 if args.sources:
62 for path in args.sources:
63 yield path
64 else:
65 for root in roots:
66 for path, _, files in os.walk(root):
67 for file_name in files:
68 yield os.path.join(path, file_name)
69
Mike Kleinc0bd9f92019-04-23 12:05:21 -050070# Rewrite any #includes relative to Skia's top-level directory.
Mike Klein140a4762019-08-01 14:24:08 -050071need_rewriting = []
Mike Kleinbb413432019-07-26 11:55:40 -050072for file_path in to_rewrite():
John Stilesd836f842020-09-14 10:21:44 -040073 if ('/generated/' in file_path or
74 'tests/sksl/' in file_path or
75 'third_party/skcms' in file_path):
Mike Kleinbb413432019-07-26 11:55:40 -050076 continue
77 if (file_path.endswith('.h') or
78 file_path.endswith('.c') or
79 file_path.endswith('.m') or
80 file_path.endswith('.mm') or
81 file_path.endswith('.inc') or
82 file_path.endswith('.fp') or
83 file_path.endswith('.cc') or
84 file_path.endswith('.cpp')):
85 # Read the whole file into memory.
86 lines = open(file_path).readlines()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050087
Mike Kleinbb413432019-07-26 11:55:40 -050088 # Write it back out again line by line with substitutions for #includes.
Hal Canary4df3d532019-07-30 13:49:45 -040089 output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb')
Mike Kleinc0bd9f92019-04-23 12:05:21 -050090
Mike Kleinbb413432019-07-26 11:55:40 -050091 includes = []
92 for line in lines:
93 parts = line.replace('<', '"').replace('>', '"').split('"')
94 if (len(parts) == 3
95 and '#' in parts[0]
96 and 'include' in parts[0]
97 and os.path.basename(parts[1]) in headers):
Hal Canary4df3d532019-07-30 13:49:45 -040098 header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
99 includes.append(parts[0] + '"%s"' % header + parts[2])
Mike Kleinbb413432019-07-26 11:55:40 -0500100 else:
101 for inc in sorted(includes):
Hal Canary4df3d532019-07-30 13:49:45 -0400102 output.write(inc.strip('\n') + '\n')
Mike Kleinbb413432019-07-26 11:55:40 -0500103 includes = []
Hal Canary4df3d532019-07-30 13:49:45 -0400104 output.write(line.strip('\n') + '\n')
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500105
Mike Kleinbb413432019-07-26 11:55:40 -0500106 if args.dry_run and output.getvalue() != open(file_path).read():
Mike Klein140a4762019-08-01 14:24:08 -0500107 need_rewriting.append(file_path)
Mike Kleinbb413432019-07-26 11:55:40 -0500108 rc = 1
109 output.close()
Mike Klein140a4762019-08-01 14:24:08 -0500110
111if need_rewriting:
112 print 'Some files need rewritten #includes:'
113 for path in need_rewriting:
114 print '\t' + path
115 print 'To do this automatically, run'
116 print 'python tools/rewrite_includes.py ' + ' '.join(need_rewriting)
117 sys.exit(1)