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 | |
| 8 | import os |
| 9 | |
| 10 | roots = [ |
| 11 | 'bench', |
| 12 | 'dm', |
| 13 | 'docs', |
| 14 | 'example', |
| 15 | 'experimental', |
| 16 | 'fuzz', |
| 17 | 'gm', |
| 18 | 'include', |
| 19 | 'modules', |
| 20 | 'platform_tools/android/apps', |
| 21 | 'samplecode', |
| 22 | 'src', |
| 23 | 'tests', |
Mike Klein | e11e5c1 | 2019-04-24 12:46:06 -0500 | [diff] [blame] | 24 | 'third_party/etc1', |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | 'third_party/gif', |
| 26 | 'tools' |
| 27 | ] |
| 28 | |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame^] | 29 | # Don't want to always force our local Vulkan headers. |
| 30 | angle_bracket_whitelist = ['vulkan/'] |
| 31 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | # Map short name -> absolute path for all Skia headers. |
| 33 | headers = {} |
| 34 | for root in roots: |
| 35 | for path, _, files in os.walk(root): |
| 36 | for file_name in files: |
| 37 | if file_name.endswith('.h'): |
| 38 | if file_name in headers: |
| 39 | print path, file_name, headers[file_name] |
| 40 | assert file_name not in headers |
| 41 | headers[file_name] = os.path.abspath(os.path.join(path, file_name)) |
| 42 | |
| 43 | # Rewrite any #includes relative to Skia's top-level directory. |
| 44 | for root in roots: |
| 45 | for path, _, files in os.walk(root): |
| 46 | if 'generated' in path: |
| 47 | continue |
| 48 | for file_name in files: |
| 49 | if (file_name.endswith('.h') or |
| 50 | file_name.endswith('.c') or |
| 51 | file_name.endswith('.m') or |
| 52 | file_name.endswith('.mm') or |
| 53 | file_name.endswith('.inc') or |
| 54 | file_name.endswith('.fp') or |
| 55 | file_name.endswith('.cc') or |
| 56 | file_name.endswith('.cpp')): |
| 57 | # Read the whole file into memory. |
| 58 | file_path = os.path.join(path, file_name) |
| 59 | lines = open(file_path).readlines() |
| 60 | |
| 61 | # Write it back out again line by line with substitutions for #includes. |
| 62 | with open(file_path, 'w') as output: |
| 63 | includes = [] |
| 64 | |
| 65 | for line in lines: |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame^] | 66 | rewritten = line |
| 67 | if not any(token in line for token in angle_bracket_whitelist): |
| 68 | rewritten = rewritten.replace('<', '"').replace('>', '"') |
| 69 | parts = rewritten.split('"') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 70 | if (len(parts) == 3 |
| 71 | and '#' in parts[0] |
| 72 | and 'include' in parts[0] |
| 73 | and os.path.basename(parts[1]) in headers): |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 74 | header = headers[os.path.basename(parts[1])] |
Mike Klein | ad44dd5 | 2019-05-14 14:01:39 -0500 | [diff] [blame] | 75 | includes.append(parts[0] + |
| 76 | '"%s"' % os.path.relpath(header, '.') + |
| 77 | parts[2]) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 78 | else: |
| 79 | for inc in sorted(includes): |
Mike Klein | ad44dd5 | 2019-05-14 14:01:39 -0500 | [diff] [blame] | 80 | print >>output, inc.strip('\n') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 81 | includes = [] |
| 82 | print >>output, line.strip('\n') |
| 83 | |