blob: 6ca00e46178f8bac44744669d606c5898da9f6e3 [file] [log] [blame]
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001#!/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
8import os
9
10roots = [
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 Kleine11e5c12019-04-24 12:46:06 -050024 'third_party/etc1',
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025 'third_party/gif',
26 'tools'
27 ]
28
29# Map short name -> absolute path for all Skia headers.
30headers = {}
31for 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.
41for 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 Kleinad44dd52019-05-14 14:01:39 -050070 includes.append(parts[0] +
71 '"%s"' % os.path.relpath(header, '.') +
72 parts[2])
Mike Kleinc0bd9f92019-04-23 12:05:21 -050073 else:
74 for inc in sorted(includes):
Mike Kleinad44dd52019-05-14 14:01:39 -050075 print >>output, inc.strip('\n')
Mike Kleinc0bd9f92019-04-23 12:05:21 -050076 includes = []
77 print >>output, line.strip('\n')
78