1#!/usr/bin/python 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 9import argparse 10import os 11import sys 12 13from io import StringIO 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 'experimental', 28 'fuzz', 29 'gm', 30 'include', 31 'modules', 32 'platform_tools/android/apps', 33 'samplecode', 34 'src', 35 'tests', 36 'third_party/etc1', 37 'third_party/gif', 38 'tools' 39] 40 41ignorelist = [ 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 'include/third_party/vulkan', 45 # Some node_modules/ files (used by CanvasKit et al) have c++ code which we should ignore. 46 'node_modules', 47 'include/third_party/skcms', 48 'src/gpu/vk/vulkanmemoryallocator', 49] 50 51assert '/' in [os.sep, os.altsep] 52def fix_path(p): 53 return p.replace(os.sep, os.altsep) if os.altsep else p 54 55# Map short name -> absolute path for all Skia headers. Duplicate short names are allowed, the 56# stored value will hold a list of absolute paths. If a short name is found in source code and the 57# absolute path is ambiguous, an error message is reported. 58headers = {} 59for root in roots: 60 for path, _, files in os.walk(root): 61 if not any(snippet in fix_path(path) for snippet in ignorelist): 62 for file_name in files: 63 if file_name.endswith('.h') and not file_name in ignorelist: 64 full_name = os.path.abspath(os.path.join(path, file_name)) 65 include_path = fix_path(os.path.relpath(full_name, '.')) 66 headers.setdefault(file_name, []).append(include_path) 67 68def to_rewrite(): 69 if args.sources: 70 for path in args.sources: 71 yield path 72 else: 73 for root in roots: 74 for path, _, files in os.walk(root): 75 for file_name in files: 76 yield os.path.join(path, file_name) 77 78# Rewrite any #includes relative to Skia's top-level directory. 79need_rewriting = [] 80rewrite_is_automatic = True 81for file_path in to_rewrite(): 82 if ('/generated/' in file_path or 83 'tests/sksl/' in file_path or 84 'third_party/skcms' in file_path or 85 'modules/skcms' in file_path or 86 # transitional 87 'jetski' in file_path or 88 'tools/window' in file_path or 89 file_path.startswith('bazel/rbe') or 90 'example/external_client/' in file_path or 91 # We intentionally list SkUserConfig.h not from the root in this file. 92 file_path == 'include/private/base/SkLoadUserConfig.h'): 93 continue 94 if (file_path.endswith('.h') or 95 file_path.endswith('.c') or 96 file_path.endswith('.m') or 97 file_path.endswith('.mm') or 98 file_path.endswith('.inc') or 99 file_path.endswith('.cc') or 100 file_path.endswith('.cpp')): 101 # Read the whole file into memory. 102 lines = open(file_path).readlines() 103 104 # Write it back out again line by line with substitutions for #includes. 105 output = StringIO() if args.dry_run else open(file_path, 'w') 106 107 includes = [] 108 for line in lines: 109 parts = line.replace('<', '"').replace('>', '"').split('"') 110 if (len(parts) == 3 111 and '#' in parts[0] 112 and 'include' in parts[0] 113 and os.path.basename(parts[1]) in headers): 114 include_paths = headers[os.path.basename(parts[1])] 115 if (len(include_paths) == 1): 116 header = include_paths[0] 117 else: 118 # This either represents a valid include path in include_paths, or it preserves the 119 # original ambiguous include that has to be manually fixed. 120 header = parts[1] 121 if not (parts[1] in include_paths): 122 need_rewriting.append(file_path) 123 rewrite_is_automatic = False 124 print('File has an ambiguous relative include: ' + file_path) 125 print('Replace "' + parts[1] + '" with one of:') 126 for opt in include_paths: 127 print('\t' + opt) 128 129 includes.append(parts[0] + '"%s"' % header + parts[2]) 130 else: 131 # deduplicate includes in this block. If a file needs to be included 132 # multiple times, the separate includes should go in different blocks. 133 includes = sorted(list(set(includes))) 134 for inc in includes: 135 output.write(inc.strip('\n') + '\n') 136 includes = [] 137 output.write(line.strip('\n') + '\n') 138 # Fix any straggling includes, e.g. in a file that only includes something else. 139 for inc in sorted(includes): 140 output.write(inc.strip('\n') + '\n') 141 if args.dry_run and output.getvalue() != open(file_path).read(): 142 need_rewriting.append(file_path) 143 output.close() 144 145if need_rewriting: 146 print('Some files need rewritten #includes:') 147 for path in need_rewriting: 148 print('\t' + path) 149 150 if rewrite_is_automatic: 151 print('To do this automatically, run') 152 print('python3 tools/rewrite_includes.py ' + ' '.join(need_rewriting)) 153 else: 154 print('Automatic rewritting of includes not possible.') 155 sys.exit(1) 156