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 | |
| 29 | # Map short name -> absolute path for all Skia headers. |
| 30 | headers = {} |
| 31 | for root in roots: |
| 32 | for path, _, files in os.walk(root): |
| 33 | for file_name in files: |
| 34 | if file_name.endswith('.h'): |
| 35 | if file_name in headers: |
| 36 | print path, file_name, headers[file_name] |
| 37 | assert file_name not in headers |
| 38 | headers[file_name] = os.path.abspath(os.path.join(path, file_name)) |
| 39 | |
| 40 | # Rewrite any #includes relative to Skia's top-level directory. |
| 41 | for root in roots: |
| 42 | for path, _, files in os.walk(root): |
| 43 | if 'generated' in path: |
| 44 | continue |
| 45 | for file_name in files: |
| 46 | if (file_name.endswith('.h') or |
| 47 | file_name.endswith('.c') or |
| 48 | file_name.endswith('.m') or |
| 49 | file_name.endswith('.mm') or |
| 50 | file_name.endswith('.inc') or |
| 51 | file_name.endswith('.fp') or |
| 52 | file_name.endswith('.cc') or |
| 53 | file_name.endswith('.cpp')): |
| 54 | # Read the whole file into memory. |
| 55 | file_path = os.path.join(path, file_name) |
| 56 | lines = open(file_path).readlines() |
| 57 | |
| 58 | # Write it back out again line by line with substitutions for #includes. |
| 59 | with open(file_path, 'w') as output: |
| 60 | includes = [] |
| 61 | |
| 62 | for line in lines: |
| 63 | parts = line.split('"') |
| 64 | if (len(parts) == 3 |
| 65 | and '#' in parts[0] |
| 66 | and 'include' in parts[0] |
| 67 | and os.path.basename(parts[1]) in headers): |
| 68 | |
| 69 | header = headers[os.path.basename(parts[1])] |
Mike Klein | ad44dd5 | 2019-05-14 14:01:39 -0500 | [diff] [blame] | 70 | includes.append(parts[0] + |
| 71 | '"%s"' % os.path.relpath(header, '.') + |
| 72 | parts[2]) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 73 | else: |
| 74 | for inc in sorted(includes): |
Mike Klein | ad44dd5 | 2019-05-14 14:01:39 -0500 | [diff] [blame] | 75 | print >>output, inc.strip('\n') |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 76 | includes = [] |
| 77 | print >>output, line.strip('\n') |
| 78 | |