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 |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 11 | import sys |
| 12 | |
Kevin Lubick | 2f57626 | 2022-05-06 08:30:28 -0400 | [diff] [blame] | 13 | from io import StringIO |
John Stiles | 4498aa8 | 2021-08-16 18:14:52 -0400 | [diff] [blame] | 14 | |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 15 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 16 | parser = argparse.ArgumentParser() |
| 17 | parser.add_argument('-n', '--dry-run', action='store_true', |
| 18 | help='Just check there is nothing to rewrite.') |
| 19 | parser.add_argument('sources', nargs='*', |
| 20 | help='Source files to rewrite, or all if empty.') |
| 21 | args = parser.parse_args() |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | |
| 23 | roots = [ |
Kevin Lubick | fed97e8 | 2022-03-25 14:59:33 -0400 | [diff] [blame] | 24 | 'bench', |
| 25 | 'dm', |
| 26 | 'docs', |
Kevin Lubick | fed97e8 | 2022-03-25 14:59:33 -0400 | [diff] [blame] | 27 | 'experimental', |
| 28 | 'fuzz', |
| 29 | 'gm', |
| 30 | 'include', |
| 31 | 'modules', |
| 32 | 'platform_tools/android/apps', |
| 33 | 'samplecode', |
| 34 | 'src', |
| 35 | 'tests', |
| 36 | 'third_party/etc1', |
| 37 | 'third_party/gif', |
| 38 | 'tools' |
| 39 | ] |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | |
Kevin Lubick | fed97e8 | 2022-03-25 14:59:33 -0400 | [diff] [blame] | 41 | ignorelist = [ |
| 42 | # 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. |
| 44 | 'include/third_party/vulkan', |
| 45 | # Some node_modules/ files (used by CanvasKit et al) have c++ code which we should ignore. |
| 46 | 'node_modules', |
Kevin Lubick | 52904d3 | 2022-06-29 15:34:49 -0400 | [diff] [blame] | 47 | 'include/third_party/skcms', |
Kevin Lubick | 26d70e6 | 2023-10-09 12:50:57 -0400 | [diff] [blame] | 48 | 'src/gpu/vk/vulkanmemoryallocator', |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 49 | # Used by Jetski and Graphite |
| 50 | 'Surface.h', |
Kevin Lubick | acdc108 | 2023-06-09 11:05:24 -0400 | [diff] [blame] | 51 | # Used by Ganesh and Graphite |
| 52 | 'Device.h', |
Kevin Lubick | 3b23392 | 2023-03-23 09:26:34 -0400 | [diff] [blame] | 53 | # Temporary shims |
K Lubick | a8e4db1 | 2024-02-15 13:32:24 -0500 | [diff] [blame] | 54 | 'GrBackendDrawableInfo.h', |
Kevin Lubick | 68de6e3 | 2023-10-13 11:19:03 -0400 | [diff] [blame] | 55 | 'SkMultiPictureDocument.h', |
Florin Malita | 28f5db5 | 2023-05-02 10:34:11 -0400 | [diff] [blame] | 56 | # Transitional |
| 57 | 'tools/window', |
Kevin Lubick | fed97e8 | 2022-03-25 14:59:33 -0400 | [diff] [blame] | 58 | ] |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 59 | |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 60 | assert '/' in [os.sep, os.altsep] |
| 61 | def fix_path(p): |
| 62 | return p.replace(os.sep, os.altsep) if os.altsep else p |
| 63 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 64 | # Map short name -> absolute path for all Skia headers. |
| 65 | headers = {} |
| 66 | for root in roots: |
| 67 | for path, _, files in os.walk(root): |
Kevin Lubick | 87fe9b0 | 2019-10-17 15:03:00 -0400 | [diff] [blame] | 68 | if not any(snippet in fix_path(path) for snippet in ignorelist): |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 69 | for file_name in files: |
Kevin Lubick | 10fdc3d | 2023-01-05 15:40:24 -0500 | [diff] [blame] | 70 | if file_name.endswith('.h') and not file_name in ignorelist: |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 71 | if file_name in headers: |
John Stiles | f35853a | 2021-04-15 14:45:59 -0400 | [diff] [blame] | 72 | message = ('Header filename is used more than once!\n- ' + path + '/' + file_name + |
| 73 | '\n- ' + headers[file_name]) |
| 74 | assert file_name not in headers, message |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 75 | headers[file_name] = os.path.abspath(os.path.join(path, file_name)) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 76 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 77 | def to_rewrite(): |
| 78 | if args.sources: |
| 79 | for path in args.sources: |
| 80 | yield path |
| 81 | else: |
| 82 | for root in roots: |
| 83 | for path, _, files in os.walk(root): |
| 84 | for file_name in files: |
| 85 | yield os.path.join(path, file_name) |
| 86 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 87 | # Rewrite any #includes relative to Skia's top-level directory. |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 88 | need_rewriting = [] |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 89 | for file_path in to_rewrite(): |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 90 | if ('/generated/' in file_path or |
| 91 | 'tests/sksl/' in file_path or |
Kevin Lubick | fed97e8 | 2022-03-25 14:59:33 -0400 | [diff] [blame] | 92 | 'third_party/skcms' in file_path or |
Kevin Lubick | 52904d3 | 2022-06-29 15:34:49 -0400 | [diff] [blame] | 93 | 'modules/skcms' in file_path or |
Florin Malita | 28f5db5 | 2023-05-02 10:34:11 -0400 | [diff] [blame] | 94 | # transitional |
| 95 | 'jetski' in file_path or |
| 96 | 'tools/window' in file_path or |
Kevin Lubick | 23f1dcb | 2023-03-23 10:34:20 -0400 | [diff] [blame] | 97 | file_path.startswith('bazel/rbe') or |
Kevin Lubick | 6f4fd97 | 2023-08-29 10:44:24 -0400 | [diff] [blame] | 98 | 'example/external_client/' in file_path or |
Kevin Lubick | 23f1dcb | 2023-03-23 10:34:20 -0400 | [diff] [blame] | 99 | # We intentionally list SkUserConfig.h not from the root in this file. |
| 100 | file_path == 'include/private/base/SkLoadUserConfig.h'): |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 101 | continue |
| 102 | if (file_path.endswith('.h') or |
| 103 | file_path.endswith('.c') or |
| 104 | file_path.endswith('.m') or |
| 105 | file_path.endswith('.mm') or |
| 106 | file_path.endswith('.inc') or |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 107 | file_path.endswith('.cc') or |
| 108 | file_path.endswith('.cpp')): |
| 109 | # Read the whole file into memory. |
| 110 | lines = open(file_path).readlines() |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 111 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 112 | # Write it back out again line by line with substitutions for #includes. |
John Stiles | 4498aa8 | 2021-08-16 18:14:52 -0400 | [diff] [blame] | 113 | output = StringIO() if args.dry_run else open(file_path, 'w') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 114 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 115 | includes = [] |
| 116 | for line in lines: |
| 117 | parts = line.replace('<', '"').replace('>', '"').split('"') |
| 118 | if (len(parts) == 3 |
| 119 | and '#' in parts[0] |
| 120 | and 'include' in parts[0] |
| 121 | and os.path.basename(parts[1]) in headers): |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 122 | header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.')) |
| 123 | includes.append(parts[0] + '"%s"' % header + parts[2]) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 124 | else: |
Kevin Lubick | 18ac33a | 2023-01-12 10:15:07 -0500 | [diff] [blame] | 125 | # deduplicate includes in this block. If a file needs to be included |
| 126 | # multiple times, the separate includes should go in different blocks. |
| 127 | includes = sorted(list(set(includes))) |
| 128 | for inc in includes: |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 129 | output.write(inc.strip('\n') + '\n') |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 130 | includes = [] |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 131 | output.write(line.strip('\n') + '\n') |
Kevin Lubick | dc6cc02 | 2023-01-13 11:24:27 -0500 | [diff] [blame] | 132 | # Fix any straggling includes, e.g. in a file that only includes something else. |
| 133 | for inc in sorted(includes): |
| 134 | output.write(inc.strip('\n') + '\n') |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 135 | if args.dry_run and output.getvalue() != open(file_path).read(): |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 136 | need_rewriting.append(file_path) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 137 | rc = 1 |
| 138 | output.close() |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 139 | |
| 140 | if need_rewriting: |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 141 | print('Some files need rewritten #includes:') |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 142 | for path in need_rewriting: |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 143 | print('\t' + path) |
| 144 | print('To do this automatically, run') |
John Stiles | 9c34b54 | 2023-03-22 17:20:16 -0400 | [diff] [blame] | 145 | print('python3 tools/rewrite_includes.py ' + ' '.join(need_rewriting)) |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 146 | sys.exit(1) |