blob: 6210b8a0c60d52e14b6bd4a6631cdca6d18d8a10 [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
Eric Borena1db7992021-03-25 09:04:43 -04008
9from __future__ import print_function
Mike Kleinbb413432019-07-26 11:55:40 -050010import StringIO
11import argparse
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012import os
Mike Kleinbb413432019-07-26 11:55:40 -050013import sys
14
Eric Borena1db7992021-03-25 09:04:43 -040015
Mike Kleinbb413432019-07-26 11:55:40 -050016parser = argparse.ArgumentParser()
17parser.add_argument('-n', '--dry-run', action='store_true',
18 help='Just check there is nothing to rewrite.')
19parser.add_argument('sources', nargs='*',
20 help='Source files to rewrite, or all if empty.')
21args = parser.parse_args()
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022
23roots = [
24 'bench',
25 'dm',
26 'docs',
27 'example',
28 'experimental',
29 'fuzz',
30 'gm',
31 'include',
32 'modules',
33 'platform_tools/android/apps',
34 'samplecode',
35 'src',
36 'tests',
Mike Kleine11e5c12019-04-24 12:46:06 -050037 'third_party/etc1',
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038 'third_party/gif',
39 'tools'
40 ]
41
Mike Klein66ed6a02019-07-25 11:18:06 -050042# Don't count our local Vulkan headers as Skia headers;
43# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
Kevin Lubick87fe9b02019-10-17 15:03:00 -040044# Nor do we care about things in node_modules, used by *Kits.
Florin Malitaa5e8f6b2020-10-16 10:53:27 -040045ignorelist = ['include/third_party/vulkan', 'node_modules']
Mike Klein52337de2019-07-25 09:00:52 -050046
Hal Canary4df3d532019-07-30 13:49:45 -040047assert '/' in [os.sep, os.altsep]
48def fix_path(p):
49 return p.replace(os.sep, os.altsep) if os.altsep else p
50
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051# Map short name -> absolute path for all Skia headers.
52headers = {}
53for root in roots:
54 for path, _, files in os.walk(root):
Kevin Lubick87fe9b02019-10-17 15:03:00 -040055 if not any(snippet in fix_path(path) for snippet in ignorelist):
Mike Klein66ed6a02019-07-25 11:18:06 -050056 for file_name in files:
Mike Kleina121fb62020-02-26 08:25:52 -060057 if file_name.endswith('.h'):
Mike Klein66ed6a02019-07-25 11:18:06 -050058 if file_name in headers:
John Stilesf35853a2021-04-15 14:45:59 -040059 message = ('Header filename is used more than once!\n- ' + path + '/' + file_name +
60 '\n- ' + headers[file_name])
61 assert file_name not in headers, message
Mike Klein66ed6a02019-07-25 11:18:06 -050062 headers[file_name] = os.path.abspath(os.path.join(path, file_name))
Mike Kleinc0bd9f92019-04-23 12:05:21 -050063
Mike Kleinbb413432019-07-26 11:55:40 -050064def to_rewrite():
65 if args.sources:
66 for path in args.sources:
67 yield path
68 else:
69 for root in roots:
70 for path, _, files in os.walk(root):
71 for file_name in files:
72 yield os.path.join(path, file_name)
73
Mike Kleinc0bd9f92019-04-23 12:05:21 -050074# Rewrite any #includes relative to Skia's top-level directory.
Mike Klein140a4762019-08-01 14:24:08 -050075need_rewriting = []
Mike Kleinbb413432019-07-26 11:55:40 -050076for file_path in to_rewrite():
John Stilesd836f842020-09-14 10:21:44 -040077 if ('/generated/' in file_path or
78 'tests/sksl/' in file_path or
79 'third_party/skcms' in file_path):
Mike Kleinbb413432019-07-26 11:55:40 -050080 continue
81 if (file_path.endswith('.h') or
82 file_path.endswith('.c') or
83 file_path.endswith('.m') or
84 file_path.endswith('.mm') or
85 file_path.endswith('.inc') or
86 file_path.endswith('.fp') or
87 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.
Hal Canary4df3d532019-07-30 13:49:45 -040093 output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb')
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)