Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: tools/resources/find_used_resources.py

Issue 2271453003: Revert of Enable whitelist generation for official builds. (patchset #2 id:20001 of https://coderev… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2837
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/grit/repack.gni ('k') | tools/resources/generate_resource_whitelist.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2016 The Chromium Authors. All rights reserved. 2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import argparse 6 import argparse
7 import os 7 import os
8 import re 8 import re
9 import sys 9 import sys
10 10
11 USAGE = """generate_resource_whitelist.py [-h] [-i INPUT] [-o OUTPUT] 11 USAGE = """find_used_resources.py [-h] [-i INPUT] [-o OUTPUT]
12 12
13 INPUT specifies a file containing existing resource IDs that should be 13 Outputs the sorted list of resource ids that are part of unknown pragma warning
14 whitelisted, where each line of INPUT contains a single resource ID. 14 in the given build log.
15
16 Creates a resource whitelist by collecting existing resource IDs that are part
17 of unknown pragma warnings and adds additional arch specfic resource IDs.
18 15
19 This script is used to find the resources that are actually compiled in Chrome 16 This script is used to find the resources that are actually compiled in Chrome
20 in order to only include the needed strings/images in Chrome PAK files. 17 in order to only include the needed strings/images in Chrome PAK files. The
21 These resource IDs show up in the build output after building Chrome with 18 script parses out the list of used resource ids. These resource ids show up in
22 gn variable enable_resource_whitelist_generation set to true. 19 the build output after building Chrome with gyp variable
23 This causes the compiler to print out an UnknownPragma message every time a 20 enable_resource_whitelist_generation set to 1. This gyp flag causes the compiler
24 resource ID is used. 21 to print out a UnknownPragma message every time a resource id is used. E.g.:
25 22 foo.cc:22:0: warning: ignoring #pragma whitelisted_resource_12345
26 E.g. foo.cc:22:0: warning: ignoring #pragma whitelisted_resource_12345
27 [-Wunknown-pragmas] 23 [-Wunknown-pragmas]
28 24
29 On Windows, the message is simply a message via __pragma(message(...)). 25 On Windows, the message is simply a message via __pragma(message(...)).
30 26
31 """ 27 """
32 28
33 COMPONENTS_STRINGS_HEADER = 'gen/components/strings/grit/components_strings.h' 29 COMPONENTS_STRINGS_HEADER = 'gen/components/strings/grit/components_strings.h'
34 30
35 # We don't want the resources are different between 32-bit and 64-bit build, 31 # We don't want the resources are different between 32-bit and 64-bit build,
36 # added arch related resources even they are not used. 32 # added arch related resources even they are not used.
37 ARCH_SPECIFIC_RESOURCES = [ 33 ARCH_SPECIFIC_RESOURCES = [
38 'IDS_VERSION_UI_64BIT', 34 'IDS_VERSION_UI_64BIT',
39 'IDS_VERSION_UI_32BIT', 35 'IDS_VERSION_UI_32BIT',
40 ] 36 ]
41 37
42 38 def FindResourceIds(header, resource_names):
43 def _FindResourceIds(header, resource_names):
44 """Returns the numerical resource IDs that correspond to the given resource 39 """Returns the numerical resource IDs that correspond to the given resource
45 names, as #defined in the given header file." 40 names, as #defined in the given header file."
46 """ 41 """
47 pattern = re.compile( 42 pattern = re.compile(
48 r'^#define (%s) _Pragma\S+ (\d+)$' % '|'.join(resource_names)) 43 r'^#define (%s) _Pragma\S+ (\d+)$' % '|'.join(resource_names))
49 with open(header, 'r') as f: 44 with open(header, 'r') as f:
50 res_ids = [ int(pattern.match(line).group(2)) 45 res_ids = [ int(pattern.match(line).group(2))
51 for line in f if pattern.match(line) ] 46 for line in f if pattern.match(line) ]
52 if len(res_ids) != len(resource_names): 47 if len(res_ids) != len(resource_names):
53 raise Exception('Find resource id failed: the result is ' + 48 raise Exception('Find resource id failed: the result is ' +
54 ', '.join(str(i) for i in res_ids)) 49 ', '.join(str(i) for i in res_ids))
55 return set(res_ids) 50 return set(res_ids)
56 51
52 def GetResourceIdsInPragmaWarnings(input):
53 """Returns set of resource ids that are inside unknown pragma warnings
54 for the given input.
55 """
56 used_resources = set()
57 unknown_pragma_warning_pattern = re.compile(
58 'whitelisted_resource_(?P<resource_id>[0-9]+)')
59 for ln in input:
60 match = unknown_pragma_warning_pattern.search(ln)
61 if match:
62 resource_id = int(match.group('resource_id'))
63 used_resources.add(resource_id)
64 return used_resources
57 65
58 def main(): 66 def Main():
59 parser = argparse.ArgumentParser(usage=USAGE) 67 parser = argparse.ArgumentParser(usage=USAGE)
60 parser.add_argument( 68 parser.add_argument(
61 '-i', '--input', type=argparse.FileType('r'), default=sys.stdin, 69 '-i', '--input', type=argparse.FileType('r'), default=sys.stdin,
62 help='A resource whitelist where each line contains one resource ID') 70 help='The build log to read (default stdin)')
63 parser.add_argument( 71 parser.add_argument(
64 '-o', '--output', type=argparse.FileType('w'), default=sys.stdout, 72 '-o', '--output', type=argparse.FileType('w'), default=sys.stdout,
65 help='The resource list path to write (default stdout)') 73 help='The resource list path to write (default stdout)')
66 parser.add_argument( 74 parser.add_argument('--out-dir', required=True,
67 '--out-dir', required=True,
68 help='The out target directory, for example out/Release') 75 help='The out target directory, for example out/Release')
69 76
70 args = parser.parse_args() 77 args = parser.parse_args()
71 78
72 used_resources = set() 79
73 used_resources.update([int(resource_id) for resource_id in args.input]) 80 used_resources = GetResourceIdsInPragmaWarnings(args.input)
74 used_resources |= _FindResourceIds( 81 used_resources |= FindResourceIds(
75 os.path.join(args.out_dir, COMPONENTS_STRINGS_HEADER), 82 os.path.join(args.out_dir, COMPONENTS_STRINGS_HEADER),
76 ARCH_SPECIFIC_RESOURCES) 83 ARCH_SPECIFIC_RESOURCES)
77 84
78 for resource_id in sorted(used_resources): 85 for resource_id in sorted(used_resources):
79 args.output.write('%d\n' % resource_id) 86 args.output.write('%d\n' % resource_id)
80 87
81 if __name__ == '__main__': 88 if __name__ == '__main__':
82 main() 89 Main()
OLDNEW
« no previous file with comments | « tools/grit/repack.gni ('k') | tools/resources/generate_resource_whitelist.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698