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 7import re 8import sys 9 10usage = """find_used_resources.py 11 12Prints out (to stdout) the sorted list of resource ids that are part of unknown 13pragma warning in the given build log (via stdin). 14 15This script is used to find the resources that are actually compiled in Chrome 16in order to only include the needed strings/images in Chrome PAK files. The 17script parses out the list of used resource ids. These resource ids show up in 18the build output after building Chrome with gyp variable 19enable_resource_whitelist_generation set to 1. This gyp flag causes the compiler 20to print out a UnknownPragma message every time a resource id is used. E.g.: 21foo.cc:22:0: warning: ignoring #pragma whitelisted_resource_12345 22[-Wunknown-pragmas] 23 24On Windows, the message is simply a message via __pragma(message(...)). 25 26""" 27 28 29def GetResourceIdsInPragmaWarnings(input): 30 """Returns sorted set of resource ids that are inside unknown pragma warnings 31 for the given input. 32 """ 33 used_resources = set() 34 unknown_pragma_warning_pattern = re.compile( 35 'whitelisted_resource_(?P<resource_id>[0-9]+)') 36 for ln in input: 37 match = unknown_pragma_warning_pattern.search(ln) 38 if match: 39 resource_id = int(match.group('resource_id')) 40 used_resources.add(resource_id) 41 return sorted(used_resources) 42 43def Main(): 44 if len(sys.argv) != 1: 45 sys.stderr.write(usage) 46 sys.exit(1) 47 else: 48 used_resources = GetResourceIdsInPragmaWarnings(sys.stdin) 49 for resource_id in used_resources: 50 sys.stdout.write('%d\n' % resource_id) 51 52if __name__ == '__main__': 53 Main() 54