OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 import argparse | |
7 import os | |
8 import re | |
9 import sys | |
10 | |
11 USAGE = """find_used_resources.py [-h] [-i INPUT] [-o OUTPUT] | |
12 | |
13 Outputs the sorted list of resource ids that are part of unknown pragma warning | |
14 in the given build log. | |
15 | |
16 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 | |
18 script parses out the list of used resource ids. These resource ids show up in | |
19 the build output after building Chrome with gyp variable | |
20 enable_resource_whitelist_generation set to 1. This gyp flag causes the compiler | |
21 to print out a UnknownPragma message every time a resource id is used. E.g.: | |
22 foo.cc:22:0: warning: ignoring #pragma whitelisted_resource_12345 | |
23 [-Wunknown-pragmas] | |
24 | |
25 On Windows, the message is simply a message via __pragma(message(...)). | |
26 | |
27 """ | |
28 | |
29 COMPONENTS_STRINGS_HEADER = 'gen/components/strings/grit/components_strings.h' | |
30 | |
31 # 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. | |
33 ARCH_SPECIFIC_RESOURCES = [ | |
34 'IDS_VERSION_UI_64BIT', | |
35 'IDS_VERSION_UI_32BIT', | |
36 ] | |
37 | |
38 def FindResourceIds(header, resource_names): | |
39 """Returns the numerical resource IDs that correspond to the given resource | |
40 names, as #defined in the given header file." | |
41 """ | |
42 pattern = re.compile( | |
43 r'^#define (%s) _Pragma\S+ (\d+)$' % '|'.join(resource_names)) | |
44 with open(header, 'r') as f: | |
45 res_ids = [ int(pattern.match(line).group(2)) | |
46 for line in f if pattern.match(line) ] | |
47 if len(res_ids) != len(resource_names): | |
48 raise Exception('Find resource id failed: the result is ' + | |
49 ', '.join(str(i) for i in res_ids)) | |
50 return set(res_ids) | |
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 | |
65 | |
66 def Main(): | |
67 parser = argparse.ArgumentParser(usage=USAGE) | |
68 parser.add_argument( | |
69 '-i', '--input', type=argparse.FileType('r'), default=sys.stdin, | |
70 help='The build log to read (default stdin)') | |
71 parser.add_argument( | |
72 '-o', '--output', type=argparse.FileType('w'), default=sys.stdout, | |
73 help='The resource list path to write (default stdout)') | |
74 parser.add_argument('--out-dir', required=True, | |
75 help='The out target directory, for example out/Release') | |
76 | |
77 args = parser.parse_args() | |
78 | |
79 | |
80 used_resources = GetResourceIdsInPragmaWarnings(args.input) | |
81 used_resources |= FindResourceIds( | |
82 os.path.join(args.out_dir, COMPONENTS_STRINGS_HEADER), | |
83 ARCH_SPECIFIC_RESOURCES) | |
84 | |
85 for resource_id in sorted(used_resources): | |
86 args.output.write('%d\n' % resource_id) | |
87 | |
88 if __name__ == '__main__': | |
89 Main() | |
OLD | NEW |