remove_bundled_libraries.py (3288B)
1 #!/usr/bin/env python3 2 # Copyright 2013 The Chromium Authors 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """ 7 Removes bundled libraries to make sure they are not used. 8 9 See README for more details. 10 """ 11 12 import optparse 13 import os.path 14 import sys 15 16 17 def DoMain(argv): 18 my_dirname = os.path.abspath(os.path.dirname(__file__)) 19 source_tree_root = os.path.abspath( 20 os.path.join(my_dirname, '..', '..', '..')) 21 22 if os.path.join(source_tree_root, 'build', 'linux', 'unbundle') != my_dirname: 23 print('Sanity check failed: please run this script from ' 24 'build/linux/unbundle directory.') 25 return 1 26 27 parser = optparse.OptionParser() 28 parser.add_option('--do-remove', action='store_true') 29 30 options, args = parser.parse_args(argv) 31 32 exclusion_used = {} 33 for exclusion in args: 34 exclusion_used[exclusion] = False 35 36 for root, dirs, files in os.walk(source_tree_root, topdown=False): 37 # Only look at paths which contain a "third_party" component 38 # (note that e.g. third_party.png doesn't count). 39 root_relpath = os.path.relpath(root, source_tree_root) 40 if 'third_party' not in root_relpath.split(os.sep): 41 continue 42 43 for f in files: 44 path = os.path.join(root, f) 45 relpath = os.path.relpath(path, source_tree_root) 46 47 excluded = False 48 for exclusion in args: 49 # Require precise exclusions. Find the right-most third_party 50 # in the relative path, and if there is more than one ignore 51 # the exclusion if it's completely contained within the part 52 # before right-most third_party path component. 53 split = relpath.rsplit(os.sep + 'third_party' + os.sep, 1) 54 if len(split) > 1 and split[0].startswith(exclusion): 55 continue 56 57 if relpath.startswith(exclusion): 58 # Multiple exclusions can match the same path. Go through all of them 59 # and mark each one as used. 60 exclusion_used[exclusion] = True 61 excluded = True 62 if excluded: 63 continue 64 65 # Deleting gyp files almost always leads to gyp failures. 66 # These files come from Chromium project, and can be replaced if needed. 67 if f.endswith('.gyp') or f.endswith('.gypi'): 68 continue 69 70 # Same about GN files. 71 if f.endswith('.gn') or f.endswith('.gni'): 72 continue 73 74 # Deleting .isolate files leads to gyp failures. They are usually 75 # not used by a distro build anyway. 76 # See http://www.chromium.org/developers/testing/isolated-testing 77 # for more info. 78 if f.endswith('.isolate'): 79 continue 80 81 if options.do_remove: 82 # Delete the file - best way to ensure it's not used during build. 83 os.remove(path) 84 else: 85 # By default just print paths that would be removed. 86 print(path) 87 88 exit_code = 0 89 90 # Fail if exclusion list contains stale entries - this helps keep it 91 # up to date. 92 for exclusion, used in exclusion_used.items(): 93 if not used: 94 print('%s does not exist' % exclusion) 95 exit_code = 1 96 97 if not options.do_remove: 98 print('To actually remove files printed above, please pass ' 99 '--do-remove flag.') 100 101 return exit_code 102 103 104 if __name__ == '__main__': 105 sys.exit(DoMain(sys.argv[1:]))