Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 1 | #!/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 Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 8 | import StringIO |
| 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 | |
| 13 | parser = argparse.ArgumentParser() |
| 14 | parser.add_argument('-n', '--dry-run', action='store_true', |
| 15 | help='Just check there is nothing to rewrite.') |
| 16 | parser.add_argument('sources', nargs='*', |
| 17 | help='Source files to rewrite, or all if empty.') |
| 18 | args = parser.parse_args() |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | |
| 20 | roots = [ |
| 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 Klein | e11e5c1 | 2019-04-24 12:46:06 -0500 | [diff] [blame] | 34 | 'third_party/etc1', |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | 'third_party/gif', |
| 36 | 'tools' |
| 37 | ] |
| 38 | |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 39 | # 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. |
| 41 | blacklist = ['include/third_party/vulkan'] |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 42 | |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 43 | assert '/' in [os.sep, os.altsep] |
| 44 | def fix_path(p): |
| 45 | return p.replace(os.sep, os.altsep) if os.altsep else p |
| 46 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 47 | # Map short name -> absolute path for all Skia headers. |
| 48 | headers = {} |
| 49 | for root in roots: |
| 50 | for path, _, files in os.walk(root): |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 51 | if not any(snippet in fix_path(path) for snippet in blacklist): |
Mike Klein | 66ed6a0 | 2019-07-25 11:18:06 -0500 | [diff] [blame] | 52 | for file_name in files: |
| 53 | if file_name.endswith('.h'): |
| 54 | if file_name in headers: |
| 55 | print path, file_name, headers[file_name] |
| 56 | assert file_name not in headers |
| 57 | headers[file_name] = os.path.abspath(os.path.join(path, file_name)) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 58 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 59 | def to_rewrite(): |
| 60 | if args.sources: |
| 61 | for path in args.sources: |
| 62 | yield path |
| 63 | else: |
| 64 | for root in roots: |
| 65 | for path, _, files in os.walk(root): |
| 66 | for file_name in files: |
| 67 | yield os.path.join(path, file_name) |
| 68 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 69 | # Rewrite any #includes relative to Skia's top-level directory. |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 70 | need_rewriting = [] |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 71 | for file_path in to_rewrite(): |
skia-autoroll | fe16a33 | 2019-08-20 19:44:54 +0000 | [diff] [blame] | 72 | if 'generated' in file_path or 'third_party/skcms' in file_path: |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 73 | continue |
| 74 | if (file_path.endswith('.h') or |
| 75 | file_path.endswith('.c') or |
| 76 | file_path.endswith('.m') or |
| 77 | file_path.endswith('.mm') or |
| 78 | file_path.endswith('.inc') or |
| 79 | file_path.endswith('.fp') or |
| 80 | file_path.endswith('.cc') or |
| 81 | file_path.endswith('.cpp')): |
| 82 | # Read the whole file into memory. |
| 83 | lines = open(file_path).readlines() |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 84 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 85 | # Write it back out again line by line with substitutions for #includes. |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 86 | output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 87 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 88 | includes = [] |
| 89 | for line in lines: |
| 90 | parts = line.replace('<', '"').replace('>', '"').split('"') |
| 91 | if (len(parts) == 3 |
| 92 | and '#' in parts[0] |
| 93 | and 'include' in parts[0] |
| 94 | and os.path.basename(parts[1]) in headers): |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 95 | header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.')) |
| 96 | includes.append(parts[0] + '"%s"' % header + parts[2]) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 97 | else: |
| 98 | for inc in sorted(includes): |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 99 | output.write(inc.strip('\n') + '\n') |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 100 | includes = [] |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 101 | output.write(line.strip('\n') + '\n') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 102 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 103 | if args.dry_run and output.getvalue() != open(file_path).read(): |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 104 | need_rewriting.append(file_path) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 105 | rc = 1 |
| 106 | output.close() |
Mike Klein | 140a476 | 2019-08-01 14:24:08 -0500 | [diff] [blame] | 107 | |
| 108 | if need_rewriting: |
| 109 | print 'Some files need rewritten #includes:' |
| 110 | for path in need_rewriting: |
| 111 | print '\t' + path |
| 112 | print 'To do this automatically, run' |
| 113 | print 'python tools/rewrite_includes.py ' + ' '.join(need_rewriting) |
| 114 | sys.exit(1) |