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 9from __future__ import print_function 10import StringIO 11import argparse 12import os 13import sys 14 15 16parser = argparse.ArgumentParser() 17parser.add_argument('-n', '--dry-run', action='store_true', 18 help='Just check there is nothing to rewrite.') 19parser.add_argument('sources', nargs='*', 20 help='Source files to rewrite, or all if empty.') 21args = parser.parse_args() 22 23roots = [ 24 'bench', 25 'dm', 26 'docs', 27 'example', 28 'experimental', 29 'fuzz', 30 'gm', 31 'include', 32 'modules', 33 'platform_tools/android/apps', 34 'samplecode', 35 'src', 36 'tests', 37 'third_party/etc1', 38 'third_party/gif', 39 'tools' 40 ] 41 42# Don't count our local Vulkan headers as Skia headers; 43# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them. 44# Nor do we care about things in node_modules, used by *Kits. 45ignorelist = ['include/third_party/vulkan', 'node_modules'] 46 47assert '/' in [os.sep, os.altsep] 48def fix_path(p): 49 return p.replace(os.sep, os.altsep) if os.altsep else p 50 51# Map short name -> absolute path for all Skia headers. 52headers = {} 53for root in roots: 54 for path, _, files in os.walk(root): 55 if not any(snippet in fix_path(path) for snippet in ignorelist): 56 for file_name in files: 57 if file_name.endswith('.h'): 58 if file_name in headers: 59 message = ('Header filename is used more than once!\n- ' + path + '/' + file_name + 60 '\n- ' + headers[file_name]) 61 assert file_name not in headers, message 62 headers[file_name] = os.path.abspath(os.path.join(path, file_name)) 63 64def to_rewrite(): 65 if args.sources: 66 for path in args.sources: 67 yield path 68 else: 69 for root in roots: 70 for path, _, files in os.walk(root): 71 for file_name in files: 72 yield os.path.join(path, file_name) 73 74# Rewrite any #includes relative to Skia's top-level directory. 75need_rewriting = [] 76for file_path in to_rewrite(): 77 if ('/generated/' in file_path or 78 'tests/sksl/' in file_path or 79 'third_party/skcms' in file_path): 80 continue 81 if (file_path.endswith('.h') or 82 file_path.endswith('.c') or 83 file_path.endswith('.m') or 84 file_path.endswith('.mm') or 85 file_path.endswith('.inc') or 86 file_path.endswith('.fp') or 87 file_path.endswith('.cc') or 88 file_path.endswith('.cpp')): 89 # Read the whole file into memory. 90 lines = open(file_path).readlines() 91 92 # Write it back out again line by line with substitutions for #includes. 93 output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb') 94 95 includes = [] 96 for line in lines: 97 parts = line.replace('<', '"').replace('>', '"').split('"') 98 if (len(parts) == 3 99 and '#' in parts[0] 100 and 'include' in parts[0] 101 and os.path.basename(parts[1]) in headers): 102 header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.')) 103 includes.append(parts[0] + '"%s"' % header + parts[2]) 104 else: 105 for inc in sorted(includes): 106 output.write(inc.strip('\n') + '\n') 107 includes = [] 108 output.write(line.strip('\n') + '\n') 109 110 if args.dry_run and output.getvalue() != open(file_path).read(): 111 need_rewriting.append(file_path) 112 rc = 1 113 output.close() 114 115if need_rewriting: 116 print('Some files need rewritten #includes:') 117 for path in need_rewriting: 118 print('\t' + path) 119 print('To do this automatically, run') 120 print('python tools/rewrite_includes.py ' + ' '.join(need_rewriting)) 121 sys.exit(1) 122