1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#!/usr/bin/env python
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)# Copyright 2014 The Chromium Authors. All rights reserved.
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)# found in the LICENSE file.
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import re
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import sys
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)usage = """find_used_resources.py
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
120529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochPrints out (to stdout) the sorted list of resource ids that are part of unknown
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)pragma warning in the given build log (via stdin).
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)This script is used to find the resources that are actually compiled in Chrome
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)in order to only include the needed strings/images in Chrome PAK files. The
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)script parses out the list of used resource ids. These resource ids show up in
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)the build output after building Chrome with gyp variable
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)enable_resource_whitelist_generation set to 1. This gyp flag causes the compiler
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)to print out a UnknownPragma message every time a resource id is used. E.g.:
21c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochfoo.cc:22:0: warning: ignoring #pragma whitelisted_resource_12345
22c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch[-Wunknown-pragmas]
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
24a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochOn Windows, the message is simply a message via __pragma(message(...)).
25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)"""
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def GetResourceIdsInPragmaWarnings(input):
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  """Returns sorted set of resource ids that are inside unknown pragma warnings
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     for the given input.
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  """
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  used_resources = set()
34a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  unknown_pragma_warning_pattern = re.compile(
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      'whitelisted_resource_(?P<resource_id>[0-9]+)')
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for ln in input:
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    match = unknown_pragma_warning_pattern.search(ln)
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if match:
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      resource_id = int(match.group('resource_id'))
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      used_resources.add(resource_id)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return sorted(used_resources)
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def Main():
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if len(sys.argv) != 1:
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    sys.stderr.write(usage)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    sys.exit(1)
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  else:
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    used_resources = GetResourceIdsInPragmaWarnings(sys.stdin)
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    for resource_id in used_resources:
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      sys.stdout.write('%d\n' % resource_id)
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)if __name__ == '__main__':
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Main()
54