Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | |
| 3 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import binascii |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 8 | import optparse |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 9 | import sys |
| 10 | |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 11 | parser = optparse.OptionParser() |
| 12 | parser.add_option("--mac", |
| 13 | help="generate assembly file for Mac/iOS (default: False)", |
| 14 | action="store_true", default=False) |
| 15 | parser.set_usage("""make_data_assembly icu_data [assembly_file] [--mac] |
| 16 | icu_data: ICU data file to generate assembly from. |
| 17 | assembly_file: Output file converted from icu_data file.""") |
| 18 | (options, args) = parser.parse_args() |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 19 | |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 20 | if len(args) < 1: |
| 21 | parser.error("ICU data file is not given.") |
| 22 | |
| 23 | input_file = args[0] |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 24 | n = input_file.find(".dat") |
| 25 | if n == -1: |
| 26 | sys.exit("%s is not an ICU .dat file." % input_file) |
| 27 | |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 28 | if len(args) < 2: |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 29 | output_file = input_file[0:n] + "_dat.S" |
| 30 | else: |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 31 | output_file = args[1] |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 32 | |
| 33 | if input_file.find("l.dat") == -1: |
| 34 | if input_file.find("b.dat") == -1: |
| 35 | sys.exit("%s has no endianness marker." % input_file) |
| 36 | else: |
| 37 | step = 1 |
| 38 | else: |
| 39 | step = -1 |
| 40 | |
| 41 | input_data = open(input_file, 'rb').read() |
| 42 | n = input_data.find("icudt") |
| 43 | if n == -1: |
| 44 | sys.exit("Cannot find a version number in %s." % input_file) |
| 45 | |
| 46 | version_number = input_data[n + 5:n + 7] |
| 47 | |
| 48 | output = open(output_file, 'w') |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 49 | |
| 50 | if options.mac: |
| 51 | output.write(".globl _icudt%s_dat\n" |
| 52 | "#ifdef U_HIDE_DATA_SYMBOL\n" |
| 53 | "\t.private_extern _icudt%s_dat\n" |
| 54 | "#endif\n" |
| 55 | "\t.data\n" |
| 56 | "\t.const\n" |
| 57 | "\t.align 4\n" |
| 58 | "_icudt%s_dat:\n" %tuple([version_number] * 3)) |
| 59 | else: |
| 60 | output.write(".globl icudt%s_dat\n" |
| 61 | "\t.section .note.GNU-stack,\"\",%%progbits\n" |
| 62 | "\t.section .rodata\n" |
| 63 | "\t.balign 16\n" |
| 64 | "#ifdef U_HIDE_DATA_SYMBOL\n" |
| 65 | "\t.hidden icudt%s_dat\n" |
| 66 | "#endif\n" |
| 67 | "\t.type icudt%s_dat,%%object\n" |
| 68 | "icudt%s_dat:\n" % tuple([version_number] * 4)) |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 69 | |
| 70 | split = [binascii.hexlify(input_data[i:i + 4][::step]).upper().lstrip('0') |
| 71 | for i in range(0, len(input_data), 4)] |
| 72 | |
| 73 | for i in range(len(split)): |
| 74 | if (len(split[i]) == 0): |
| 75 | value = '0' |
| 76 | elif (len(split[i]) == 1): |
| 77 | if not any((c in '123456789') for c in split[i]): |
| 78 | value = '0x0' + split[i] |
| 79 | else: |
| 80 | value = split[i] |
| 81 | elif (len(split[i]) % 2 == 1): |
| 82 | value = '0x0' + split[i] |
| 83 | else: |
| 84 | value = '0x' + split[i] |
| 85 | |
| 86 | if (i % 32 == 0): |
| 87 | output.write("\n.long ") |
| 88 | else: |
| 89 | output.write(",") |
| 90 | output.write(value) |
| 91 | |
| 92 | output.write("\n") |
| 93 | output.close() |
| 94 | print "Generated " + output_file |