blob: 2d9a26c9a7bfce2f89858b35b1bde7d5b47cd14d [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.
42ignorelist = ['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:
54 if file_name.endswith('.h'):
55 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():
skia-autorollfe16a332019-08-20 19:44:54 +000073 if 'generated' in file_path or 'third_party/skcms' in file_path:
Mike Kleinbb413432019-07-26 11:55:40 -050074 continue
75 if (file_path.endswith('.h') or
76 file_path.endswith('.c') or
77 file_path.endswith('.m') or
78 file_path.endswith('.mm') or
79 file_path.endswith('.inc') or
80 file_path.endswith('.fp') or
81 file_path.endswith('.cc') or
82 file_path.endswith('.cpp')):
83 # Read the whole file into memory.
84 lines = open(file_path).readlines()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050085
Mike Kleinbb413432019-07-26 11:55:40 -050086 # Write it back out again line by line with substitutions for #includes.
Hal Canary4df3d532019-07-30 13:49:45 -040087 output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb')
Mike Kleinc0bd9f92019-04-23 12:05:21 -050088
Mike Kleinbb413432019-07-26 11:55:40 -050089 includes = []
90 for line in lines:
91 parts = line.replace('<', '"').replace('>', '"').split('"')
92 if (len(parts) == 3
93 and '#' in parts[0]
94 and 'include' in parts[0]
95 and os.path.basename(parts[1]) in headers):
Hal Canary4df3d532019-07-30 13:49:45 -040096 header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
97 includes.append(parts[0] + '"%s"' % header + parts[2])
Mike Kleinbb413432019-07-26 11:55:40 -050098 else:
99 for inc in sorted(includes):
Hal Canary4df3d532019-07-30 13:49:45 -0400100 output.write(inc.strip('\n') + '\n')
Mike Kleinbb413432019-07-26 11:55:40 -0500101 includes = []
Hal Canary4df3d532019-07-30 13:49:45 -0400102 output.write(line.strip('\n') + '\n')
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500103
Mike Kleinbb413432019-07-26 11:55:40 -0500104 if args.dry_run and output.getvalue() != open(file_path).read():
Mike Klein140a4762019-08-01 14:24:08 -0500105 need_rewriting.append(file_path)
Mike Kleinbb413432019-07-26 11:55:40 -0500106 rc = 1
107 output.close()
Mike Klein140a4762019-08-01 14:24:08 -0500108
109if need_rewriting:
110 print 'Some files need rewritten #includes:'
111 for path in need_rewriting:
112 print '\t' + path
113 print 'To do this automatically, run'
114 print 'python tools/rewrite_includes.py ' + ' '.join(need_rewriting)
115 sys.exit(1)