John Stiles | 4498aa8 | 2021-08-16 18:14:52 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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 Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 8 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 9 | import argparse |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | import os |
John Stiles | 4498aa8 | 2021-08-16 18:14:52 -0400 | [diff] [blame] | 11 | import six |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 12 | import sys |
| 13 | |
John Stiles | 4498aa8 | 2021-08-16 18:14:52 -0400 | [diff] [blame] | 14 | from six import StringIO |
| 15 | |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 16 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 17 | parser = argparse.ArgumentParser() |
| 18 | parser.add_argument('-n', '--dry-run', action='store_true', |
| 19 | help='Just check there is nothing to rewrite.') |
| 20 | parser.add_argument('sources', nargs='*', |
| 21 | help='Source files to rewrite, or all if empty.') |
| 22 | args = parser.parse_args() |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | |
| 24 | roots = [ |
| 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 Klein | e11e5c1 | 2019-04-24 12:46:06 -0500 | [diff] [blame] | 38 | 'third_party/etc1', |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 39 | 'third_party/gif', |
| 40 | 'tools' |
| 41 | ] |
| 42 | |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 43 | # 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 Lubick | 87fe9b0 | 2019-10-17 15:03:00 -0400 | [diff] [blame] | 45 | # Nor do we care about things in node_modules, used by *Kits. |
Florin Malita | a5e8f6b | 2020-10-16 10:53:27 -0400 | [diff] [blame] | 46 | ignorelist = ['include/third_party/vulkan', 'node_modules'] |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 47 | |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 48 | assert '/' in [os.sep, os.altsep] |
| 49 | def fix_path(p): |
| 50 | return p.replace(os.sep, os.altsep) if os.altsep else p |
| 51 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 52 | # Map short name -> absolute path for all Skia headers. |
| 53 | headers = {} |
| 54 | for root in roots: |
| 55 | for path, _, files in os.walk(root): |
Kevin Lubick | 87fe9b0 | 2019-10-17 15:03:00 -0400 | [diff] [blame] | 56 | if not any(snippet in fix_path(path) for snippet in ignorelist): |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 57 | for file_name in files: |
Mike Klein | a121fb6 | 2020-02-26 08:25:52 -0600 | [diff] [blame] | 58 | if file_name.endswith('.h'): |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 59 | if file_name in headers: |
John Stiles | f35853a | 2021-04-15 14:45:59 -0400 | [diff] [blame] | 60 | 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 Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 63 | headers[file_name] = os.path.abspath(os.path.join(path, file_name)) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 64 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 65 | def 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 75 | # Rewrite any #includes relative to Skia's top-level directory. |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 76 | need_rewriting = [] |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 77 | for file_path in to_rewrite(): |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 78 | if ('/generated/' in file_path or |
| 79 | 'tests/sksl/' in file_path or |
| 80 | 'third_party/skcms' in file_path): |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 81 | 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 Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 91 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 92 | # Write it back out again line by line with substitutions for #includes. |
John Stiles | 4498aa8 | 2021-08-16 18:14:52 -0400 | [diff] [blame] | 93 | output = StringIO() if args.dry_run else open(file_path, 'w') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 94 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 95 | 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 Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 102 | header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.')) |
| 103 | includes.append(parts[0] + '"%s"' % header + parts[2]) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 104 | else: |
| 105 | for inc in sorted(includes): |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 106 | output.write(inc.strip('\n') + '\n') |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 107 | includes = [] |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 108 | output.write(line.strip('\n') + '\n') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 109 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 110 | if args.dry_run and output.getvalue() != open(file_path).read(): |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 111 | need_rewriting.append(file_path) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 112 | rc = 1 |
| 113 | output.close() |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 114 | |
| 115 | if need_rewriting: |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 116 | print('Some files need rewritten #includes:') |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 117 | for path in need_rewriting: |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 118 | print('\t' + path) |
| 119 | print('To do this automatically, run') |
| 120 | print('python tools/rewrite_includes.py ' + ' '.join(need_rewriting)) |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 121 | sys.exit(1) |