OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import argparse |
| 7 import os |
| 8 import re |
| 9 import sys |
| 10 |
| 11 USAGE = """generate_resource_whitelist.py [-h] [-i INPUT] [-o OUTPUT] |
| 12 |
| 13 INPUT specifies a file containing existing resource IDs that should be |
| 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. |
| 18 |
| 19 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. |
| 21 These resource IDs show up in the build output after building Chrome with |
| 22 gn variable enable_resource_whitelist_generation set to true. |
| 23 This causes the compiler to print out an UnknownPragma message every time a |
| 24 resource ID is used. |
| 25 |
| 26 E.g. foo.cc:22:0: warning: ignoring #pragma whitelisted_resource_12345 |
| 27 [-Wunknown-pragmas] |
| 28 |
| 29 On Windows, the message is simply a message via __pragma(message(...)). |
| 30 |
| 31 """ |
| 32 |
| 33 COMPONENTS_STRINGS_HEADER = 'gen/components/strings/grit/components_strings.h' |
| 34 |
| 35 # 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. |
| 37 ARCH_SPECIFIC_RESOURCES = [ |
| 38 'IDS_VERSION_UI_64BIT', |
| 39 'IDS_VERSION_UI_32BIT', |
| 40 ] |
| 41 |
| 42 |
| 43 def _FindResourceIds(header, resource_names): |
| 44 """Returns the numerical resource IDs that correspond to the given resource |
| 45 names, as #defined in the given header file." |
| 46 """ |
| 47 pattern = re.compile( |
| 48 r'^#define (%s) _Pragma\S+ (\d+)$' % '|'.join(resource_names)) |
| 49 with open(header, 'r') as f: |
| 50 res_ids = [ int(pattern.match(line).group(2)) |
| 51 for line in f if pattern.match(line) ] |
| 52 if len(res_ids) != len(resource_names): |
| 53 raise Exception('Find resource id failed: the result is ' + |
| 54 ', '.join(str(i) for i in res_ids)) |
| 55 return set(res_ids) |
| 56 |
| 57 |
| 58 def main(): |
| 59 parser = argparse.ArgumentParser(usage=USAGE) |
| 60 parser.add_argument( |
| 61 '-i', '--input', type=argparse.FileType('r'), default=sys.stdin, |
| 62 help='A resource whitelist where each line contains one resource ID') |
| 63 parser.add_argument( |
| 64 '-o', '--output', type=argparse.FileType('w'), default=sys.stdout, |
| 65 help='The resource list path to write (default stdout)') |
| 66 parser.add_argument( |
| 67 '--out-dir', required=True, |
| 68 help='The out target directory, for example out/Release') |
| 69 |
| 70 args = parser.parse_args() |
| 71 |
| 72 used_resources = set() |
| 73 used_resources.update([int(resource_id) for resource_id in args.input]) |
| 74 used_resources |= _FindResourceIds( |
| 75 os.path.join(args.out_dir, COMPONENTS_STRINGS_HEADER), |
| 76 ARCH_SPECIFIC_RESOURCES) |
| 77 |
| 78 for resource_id in sorted(used_resources): |
| 79 args.output.write('%d\n' % resource_id) |
| 80 |
| 81 if __name__ == '__main__': |
| 82 main() |
OLD | NEW |