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

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

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

Powered by Google App Engine
This is Rietveld 408576698