source: mainline/tools/mkarray.py@ 5f53428a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5f53428a was 9ce911d, checked in by Martin Decky <martin@…>, 8 years ago

improve binary data packer

  • optional deflate compression
  • performance improvements
  • generate an assembly source, a header file and a C source with metadata (all there files are stored in an uncompressed ZIP archive first to workaround the inability of GNU Make to express a dependency rule with multiple targets)
  • Property mode set to 100755
File size: 5.1 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2011 Martin Decky
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29"""
30Binary inline data packer
31"""
32
33import sys
34import os
35import zlib
36import zipfile
37import binascii
38
39def usage(prname):
40 "Print usage syntax"
41 print("%s [--deflate] <DESTINATION> <LABEL> <AS_PROLOG> [SOURCE ...]" % prname)
42
43def arg_check():
44 if (len(sys.argv) < 4):
45 usage(sys.argv[0])
46 sys.exit()
47
48def deflate(data):
49 "Compress using deflate algorithm (without any headers)"
50 return zlib.compress(data, 9)[2:-4]
51
52def chunks(string, length):
53 "Produce string chunks"
54 for start in range(0, len(string), length):
55 yield string[start:start + length]
56
57def main():
58 arg_check()
59
60 if sys.argv[1] == "--deflate":
61 sys.argv.pop(1)
62 arg_check()
63 compress = True
64 else:
65 compress = False
66
67 dest = sys.argv[1]
68 label = sys.argv[2]
69 as_prolog = sys.argv[3]
70
71 header_ctx = []
72 desc_ctx = []
73 size_ctx = []
74 data_ctx = []
75
76 src_cnt = 0
77
78 for src in sys.argv[4:]:
79 basename = os.path.basename(src)
80 plainname = os.path.splitext(basename)[0]
81 symbol = basename.replace(".", "_")
82
83 print("%s -> %s" % (src, symbol))
84
85 src_in = open(src, "rb")
86 src_data = src_in.read()
87 src_in.close()
88
89 length = len(src_data)
90
91 if compress:
92 src_data = deflate(src_data)
93
94 if sys.version_info < (3,):
95 src_data = bytearray(src_data)
96
97 length_out = len(src_data)
98
99 header_ctx.append("extern uint8_t %s[];" % symbol)
100 header_ctx.append("extern size_t %s_size;" % symbol)
101
102 data_ctx.append(".globl %s" % symbol)
103 data_ctx.append(".balign 8")
104 data_ctx.append(".size %s, %u" % (symbol, length_out))
105 data_ctx.append("%s:" % symbol)
106 data_ctx.append("\t.byte 0x" + ', 0x'.join(chunks(binascii.b2a_hex(src_data), 2)) + "\n")
107
108 desc_field = []
109 desc_field.append("\t{")
110 desc_field.append("\t\t.name = \"%s\"," % plainname)
111 desc_field.append("\t\t.addr = (void *) %s," % symbol)
112 desc_field.append("\t\t.size = %u," % length_out)
113 desc_field.append("\t\t.inflated = %u" % length)
114 desc_field.append("\t}")
115
116 desc_ctx.append("\n".join(desc_field))
117
118 size_ctx.append("size_t %s_size = %u;" % (symbol, length_out))
119
120 src_cnt += 1
121
122 archive = zipfile.ZipFile("%s.zip" % dest, "w", zipfile.ZIP_STORED)
123
124 data = ''
125 data += '/***************************************\n'
126 data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
127 data += ' * Generated by: tools/mkarray.py *\n'
128 data += ' ***************************************/\n\n'
129 data += "#ifndef %sS_H_\n" % label.upper()
130 data += "#define %sS_H_\n\n" % label.upper()
131 data += "#include <stddef.h>\n"
132 data += "#include <stdint.h>\n\n"
133 data += "#define %sS %u\n\n" % (label.upper(), src_cnt)
134 data += "typedef struct {\n"
135 data += "\tconst char *name;\n"
136 data += "\tvoid *addr;\n"
137 data += "\tsize_t size;\n"
138 data += "\tsize_t inflated;\n"
139 data += "} %s_t;\n\n" % label
140 data += "extern %s_t %ss[];\n\n" % (label, label)
141 data += "\n".join(header_ctx)
142 data += "\n\n"
143 data += "#endif\n"
144 archive.writestr("%s.h" % dest, data)
145
146 data = ''
147 data += '/***************************************\n'
148 data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
149 data += ' * Generated by: tools/mkarray.py *\n'
150 data += ' ***************************************/\n\n'
151 data += as_prolog
152 data += '.data\n\n'
153 data += "\n".join(data_ctx)
154 data += "\n"
155 archive.writestr("%s.s" % dest, data)
156
157 data = ''
158 data += '/***************************************\n'
159 data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
160 data += ' * Generated by: tools/mkarray.py *\n'
161 data += ' ***************************************/\n\n'
162 data += "#include \"%s.h\"\n\n" % dest
163 data += "%s_t %ss[] = {\n" % (label, label)
164 data += ",\n".join(desc_ctx)
165 data += "\n"
166 data += "};\n\n"
167 data += "\n".join(size_ctx)
168 data += "\n"
169 archive.writestr("%s_desc.c" % dest, data)
170
171 archive.close()
172
173if __name__ == '__main__':
174 main()
Note: See TracBrowser for help on using the repository browser.