blob: 5402471316a1e531a6ed8f25568027ec0cb44fa1 [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
Mike Klein52337de2019-07-25 09:00:52 -050029# Don't want to always force our local Vulkan headers.
30angle_bracket_whitelist = ['vulkan/']
31
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032# Map short name -> absolute path for all Skia headers.
33headers = {}
34for 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.
44for 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 Klein52337de2019-07-25 09:00:52 -050066 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 Kleinc0bd9f92019-04-23 12:05:21 -050070 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 Kleinc0bd9f92019-04-23 12:05:21 -050074 header = headers[os.path.basename(parts[1])]
Mike Kleinad44dd52019-05-14 14:01:39 -050075 includes.append(parts[0] +
76 '"%s"' % os.path.relpath(header, '.') +
77 parts[2])
Mike Kleinc0bd9f92019-04-23 12:05:21 -050078 else:
79 for inc in sorted(includes):
Mike Kleinad44dd52019-05-14 14:01:39 -050080 print >>output, inc.strip('\n')
Mike Kleinc0bd9f92019-04-23 12:05:21 -050081 includes = []
82 print >>output, line.strip('\n')
83