blob: b24450e4504bac9a98a3cde7a66c95678f1f8971 [file] [log] [blame]
John Stiles4498aa82021-08-16 18:14:52 -04001#!/usr/bin/python
Mike Kleinc0bd9f92019-04-23 12:05:21 -05002#
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
Eric Borena1db7992021-03-25 09:04:43 -04008
Mike Kleinbb413432019-07-26 11:55:40 -05009import argparse
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010import os
John Stiles4498aa82021-08-16 18:14:52 -040011import six
Mike Kleinbb413432019-07-26 11:55:40 -050012import sys
13
John Stiles4498aa82021-08-16 18:14:52 -040014from six import StringIO
15
Eric Borena1db7992021-03-25 09:04:43 -040016
Mike Kleinbb413432019-07-26 11:55:40 -050017parser = argparse.ArgumentParser()
18parser.add_argument('-n', '--dry-run', action='store_true',
19 help='Just check there is nothing to rewrite.')
20parser.add_argument('sources', nargs='*',
21 help='Source files to rewrite, or all if empty.')
22args = parser.parse_args()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023
24roots = [
25 'bench',
26 'dm',
27 'docs',
28 'example',
29 'experimental',
30 'fuzz',
31 'gm',
32 'include',
33 'modules',
34 'platform_tools/android/apps',
35 'samplecode',
36 'src',
37 'tests',
Mike Kleine11e5c12019-04-24 12:46:06 -050038 'third_party/etc1',
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039 'third_party/gif',
40 'tools'
41 ]
42
Mike Klein66ed6a02019-07-25 11:18:06 -050043# Don't count our local Vulkan headers as Skia headers;
44# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
Kevin Lubick87fe9b02019-10-17 15:03:00 -040045# Nor do we care about things in node_modules, used by *Kits.
Florin Malitaa5e8f6b2020-10-16 10:53:27 -040046ignorelist = ['include/third_party/vulkan', 'node_modules']
Mike Klein52337de2019-07-25 09:00:52 -050047
Hal Canary4df3d532019-07-30 13:49:45 -040048assert '/' in [os.sep, os.altsep]
49def fix_path(p):
50 return p.replace(os.sep, os.altsep) if os.altsep else p
51
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052# Map short name -> absolute path for all Skia headers.
53headers = {}
54for root in roots:
55 for path, _, files in os.walk(root):
Kevin Lubick87fe9b02019-10-17 15:03:00 -040056 if not any(snippet in fix_path(path) for snippet in ignorelist):
Mike Klein66ed6a02019-07-25 11:18:06 -050057 for file_name in files:
Mike Kleina121fb62020-02-26 08:25:52 -060058 if file_name.endswith('.h'):
Mike Klein66ed6a02019-07-25 11:18:06 -050059 if file_name in headers:
John Stilesf35853a2021-04-15 14:45:59 -040060 message = ('Header filename is used more than once!\n- ' + path + '/' + file_name +
61 '\n- ' + headers[file_name])
62 assert file_name not in headers, message
Mike Klein66ed6a02019-07-25 11:18:06 -050063 headers[file_name] = os.path.abspath(os.path.join(path, file_name))
Mike Kleinc0bd9f92019-04-23 12:05:21 -050064
Mike Kleinbb413432019-07-26 11:55:40 -050065def to_rewrite():
66 if args.sources:
67 for path in args.sources:
68 yield path
69 else:
70 for root in roots:
71 for path, _, files in os.walk(root):
72 for file_name in files:
73 yield os.path.join(path, file_name)
74
Mike Kleinc0bd9f92019-04-23 12:05:21 -050075# Rewrite any #includes relative to Skia's top-level directory.
Mike Klein140a4762019-08-01 14:24:08 -050076need_rewriting = []
Mike Kleinbb413432019-07-26 11:55:40 -050077for file_path in to_rewrite():
John Stilesd836f842020-09-14 10:21:44 -040078 if ('/generated/' in file_path or
79 'tests/sksl/' in file_path or
80 'third_party/skcms' in file_path):
Mike Kleinbb413432019-07-26 11:55:40 -050081 continue
82 if (file_path.endswith('.h') or
83 file_path.endswith('.c') or
84 file_path.endswith('.m') or
85 file_path.endswith('.mm') or
86 file_path.endswith('.inc') or
Mike Kleinbb413432019-07-26 11:55:40 -050087 file_path.endswith('.cc') or
88 file_path.endswith('.cpp')):
89 # Read the whole file into memory.
90 lines = open(file_path).readlines()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050091
Mike Kleinbb413432019-07-26 11:55:40 -050092 # Write it back out again line by line with substitutions for #includes.
John Stiles4498aa82021-08-16 18:14:52 -040093 output = StringIO() if args.dry_run else open(file_path, 'w')
Mike Kleinc0bd9f92019-04-23 12:05:21 -050094
Mike Kleinbb413432019-07-26 11:55:40 -050095 includes = []
96 for line in lines:
97 parts = line.replace('<', '"').replace('>', '"').split('"')
98 if (len(parts) == 3
99 and '#' in parts[0]
100 and 'include' in parts[0]
101 and os.path.basename(parts[1]) in headers):
Hal Canary4df3d532019-07-30 13:49:45 -0400102 header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
103 includes.append(parts[0] + '"%s"' % header + parts[2])
Mike Kleinbb413432019-07-26 11:55:40 -0500104 else:
105 for inc in sorted(includes):
Hal Canary4df3d532019-07-30 13:49:45 -0400106 output.write(inc.strip('\n') + '\n')
Mike Kleinbb413432019-07-26 11:55:40 -0500107 includes = []
Hal Canary4df3d532019-07-30 13:49:45 -0400108 output.write(line.strip('\n') + '\n')
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500109
Mike Kleinbb413432019-07-26 11:55:40 -0500110 if args.dry_run and output.getvalue() != open(file_path).read():
Mike Klein140a4762019-08-01 14:24:08 -0500111 need_rewriting.append(file_path)
Mike Kleinbb413432019-07-26 11:55:40 -0500112 rc = 1
113 output.close()
Mike Klein140a4762019-08-01 14:24:08 -0500114
115if need_rewriting:
Eric Borena1db7992021-03-25 09:04:43 -0400116 print('Some files need rewritten #includes:')
Mike Klein140a4762019-08-01 14:24:08 -0500117 for path in need_rewriting:
Eric Borena1db7992021-03-25 09:04:43 -0400118 print('\t' + path)
119 print('To do this automatically, run')
120 print('python tools/rewrite_includes.py ' + ' '.join(need_rewriting))
Mike Klein140a4762019-08-01 14:24:08 -0500121 sys.exit(1)