1 #!/usr/bin/env python 2 # 3 # Copyright (C) 2011-2012 The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 import os 19 import sys 20 21 try: 22 import hashlib 23 sha1 = hashlib.sha1 24 except ImportError, e: 25 import sha 26 sha1 = sha.sha 27 28 def compute_sha1(h, path): 29 f = open(path, 'rb') 30 while True: 31 buf = f.read(1024) 32 h.update(buf) 33 if len(buf) < 1024: 34 break 35 f.close() 36 37 """The result is a list of pair of file path and its SHA-1 digest""" 38 def compute_sha1_list(path_list): 39 result = [] 40 for path in path_list: 41 h = sha1() 42 compute_sha1(h, path) 43 result.append((path, h.digest())) 44 return result 45 46 """For each path like /xxx/libfoo.so, generate a symbol named libfoo_so_SHA1""" 47 def get_symbol_name(path): 48 return os.path.basename(path).replace('.', '_') + '_SHA1'; 49 50 """Print out header for assembly file.""" 51 def print_asm_header(symbols): 52 sys.stdout.write(""" 53 /*\ 54 * The mid-2007 version of gcc that ships with Macs requires a\n\ 55 * comma on the .section line, but the rest of the world thinks\n\ 56 * that's a syntax error. It also wants globals to be explicitly\n\ 57 * prefixed with \"_\" as opposed to modern gccs that do the\n\ 58 * prefixing for you.\n\ 59 */\n\ 60 """) 61 for sym in symbols: 62 sys.stdout.write(""" 63 #ifdef __APPLE_CC__ 64 .globl _%s\n\ 65 #else\n\ 66 .globl %s\n\ 67 #endif\ 68 """ % (sym, sym)) 69 sys.stdout.write(""" 70 #ifdef __APPLE_CC__ 71 .section .rodata,\n\ 72 #else\n\ 73 .section .rodata\n\ 74 #endif\ 75 """ ) 76 77 def print_asm_data(data, size): 78 col = 0 79 sys.stdout.write(".align 8\n") 80 for i in xrange(size): 81 c = data[i] 82 if col == 0: 83 sys.stdout.write(".byte ") 84 elif col % 4 == 0: 85 sys.stdout.write(", ") 86 else: 87 sys.stdout.write(",") 88 sys.stdout.write("0x%02x" % ord(c)) 89 col += 1 90 if col == 8: 91 sys.stdout.write("\n") 92 col = 0 93 if col != 0: 94 sys.stdout.write("\n") 95 96 def print_asm_symbol_data(sym, h): 97 sys.stdout.write(""" 98 #ifdef __APPLE_CC__ 99 _%s:\n\ 100 #else\n\ 101 %s:\n\ 102 #endif\n\ 103 """ % (sym, sym)) 104 print_asm_data(h, 20) 105 106 def print_asm(x): 107 symbols = [get_symbol_name(item[0]) for item in x] 108 print_asm_header(symbols) 109 for (symbol, y) in zip(symbols, x): 110 print_asm_symbol_data(symbol, y[1]) 111 112 def main(): 113 if len(sys.argv) < 2: 114 print 'USAGE:', sys.argv[0], '[OUTPUT] [INPUTs]' 115 sys.exit(1) 116 117 result = compute_sha1_list(sys.argv[1:]) 118 print_asm(result) 119 120 if __name__ == '__main__': 121 main() 122