1#!/usr/bin/env python 2# Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights 3# reserved. Use of this source code is governed by a BSD-style license 4# that can be found in the LICENSE file. 5""" 6A simple utility function to merge pack resource files into a single resource file. 7""" 8 9from __future__ import absolute_import 10from __future__ import print_function 11from date_util import * 12from file_util import * 13import os 14import re 15import string 16import sys 17 18 19def MakeFileSegment(input, all_names): 20 result = """ 21 22// --------------------------------------------------------------------------- 23// From $FILE$: 24""" 25 26 filename = os.path.split(input)[1] 27 result = result.replace('$FILE$', filename) 28 29 contents = read_file(input) 30 31 # Format for Windows builds with resource whitelisting enabled [1]: 32 # #define IDR_RESOURCE_NAME (::ui::WhitelistedResource<12345>(), 12345) 33 # Format for other builds: 34 # #define IDR_RESOURCE_NAME 12345 35 # [1] See https://crbug.com/684788#c18 36 37 regex = '#define\s([A-Za-z0-9_]{1,})\s' 38 if contents.find('ui::WhitelistedResource') > 0: 39 regex += '.*<' 40 regex += '([0-9]{1,})' 41 42 # identify the defines in the file 43 p = re.compile(regex) 44 list = p.findall(contents) 45 for name, id in list: 46 # If the same define exists in multiple files add a suffix. 47 if name in all_names: 48 all_names[name] += 1 49 name += '_%d' % all_names[name] 50 else: 51 all_names[name] = 1 52 53 result += "\n#define %s %s" % (name, id) 54 55 return result 56 57 58def MakeFile(output, input): 59 # header string 60 result = \ 61"""// Copyright (c) $YEAR$ Marshall A. Greenblatt. All rights reserved. 62// 63// Redistribution and use in source and binary forms, with or without 64// modification, are permitted provided that the following conditions are 65// met: 66// 67// * Redistributions of source code must retain the above copyright 68// notice, this list of conditions and the following disclaimer. 69// * Redistributions in binary form must reproduce the above 70// copyright notice, this list of conditions and the following disclaimer 71// in the documentation and/or other materials provided with the 72// distribution. 73// * Neither the name of Google Inc. nor the name Chromium Embedded 74// Framework nor the names of its contributors may be used to endorse 75// or promote products derived from this software without specific prior 76// written permission. 77// 78// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 79// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 80// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 81// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 82// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 83// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 84// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 85// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 86// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 87// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 88// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 89// 90// --------------------------------------------------------------------------- 91// 92// This file is generated by the make_pack_header.py tool. 93// 94 95#ifndef $GUARD$ 96#define $GUARD$ 97#pragma once""" 98 99 # sort the input files by name 100 input = sorted(input, key=lambda path: os.path.split(path)[1]) 101 102 all_names = {} 103 104 # generate the file segments 105 for file in input: 106 result += MakeFileSegment(file, all_names) 107 108 # footer string 109 result += \ 110""" 111 112#endif // $GUARD$ 113""" 114 115 # add the copyright year 116 result = result.replace('$YEAR$', get_year()) 117 # add the guard string 118 filename = os.path.split(output)[1] 119 guard = 'CEF_INCLUDE_' + filename.replace('.', '_').upper() + '_' 120 result = result.replace('$GUARD$', guard) 121 122 write_file_if_changed(output, result) 123 124 125def main(argv): 126 if len(argv) < 3: 127 print(("Usage:\n %s <output_filename> <input_file1> [input_file2] ... " % 128 argv[0])) 129 sys.exit(-1) 130 MakeFile(argv[1], argv[2:]) 131 132 133if '__main__' == __name__: 134 main(sys.argv) 135