source: mainline/tools/mkarray.py@ 94c05b89

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

use the .incbin directive instead of marshalling the data manually
(this is the fastest and also the most resource-conservative method)

  • Property mode set to 100755
File size: 5.4 KB
RevLine 
[67b05ff]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"""
[9ce911d]30Binary inline data packer
[67b05ff]31"""
32
33import sys
34import os
[9ce911d]35import zlib
36import zipfile
37import binascii
[67b05ff]38
39def usage(prname):
40 "Print usage syntax"
[22299ed]41 print("%s [--deflate] <DESTINATION> <LABEL> <AS_PROLOG> <SECTION> [SOURCE ...]" % prname)
[67b05ff]42
[9ce911d]43def arg_check():
[22299ed]44 if (len(sys.argv) < 5):
[67b05ff]45 usage(sys.argv[0])
[9ce911d]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
[67b05ff]66
67 dest = sys.argv[1]
68 label = sys.argv[2]
[2628642]69 as_prolog = sys.argv[3]
[22299ed]70 section = sys.argv[4]
[67b05ff]71
72 header_ctx = []
[9ce911d]73 desc_ctx = []
74 size_ctx = []
[67b05ff]75 data_ctx = []
[2628642]76
[9ce911d]77 src_cnt = 0
78
[94c05b89]79 archive = zipfile.ZipFile("%s.zip" % dest, "w", zipfile.ZIP_STORED)
80
[22299ed]81 for src in sys.argv[5:]:
[67b05ff]82 basename = os.path.basename(src)
[9ce911d]83 plainname = os.path.splitext(basename)[0]
[67b05ff]84 symbol = basename.replace(".", "_")
85
86 print("%s -> %s" % (src, symbol))
87
88 src_in = open(src, "rb")
89 src_data = src_in.read()
90 src_in.close()
91
[9ce911d]92 length = len(src_data)
[67b05ff]93
[9ce911d]94 if compress:
95 src_data = deflate(src_data)
[94c05b89]96 src_fname = os.path.basename("%s.deflate" % src)
97 archive.writestr(src_fname, src_data)
98 else:
99 src_fname = src
[67b05ff]100
[9ce911d]101 if sys.version_info < (3,):
102 src_data = bytearray(src_data)
[67b05ff]103
[9ce911d]104 length_out = len(src_data)
[2628642]105
106 header_ctx.append("extern uint8_t %s[];" % symbol)
[9ce911d]107 header_ctx.append("extern size_t %s_size;" % symbol)
[c6f08726]108
[2628642]109 data_ctx.append(".globl %s" % symbol)
110 data_ctx.append(".balign 8")
[9ce911d]111 data_ctx.append(".size %s, %u" % (symbol, length_out))
[2628642]112 data_ctx.append("%s:" % symbol)
[94c05b89]113 data_ctx.append("\t.incbin \"%s\"\n" % src_fname)
[9ce911d]114
115 desc_field = []
116 desc_field.append("\t{")
117 desc_field.append("\t\t.name = \"%s\"," % plainname)
118 desc_field.append("\t\t.addr = (void *) %s," % symbol)
119 desc_field.append("\t\t.size = %u," % length_out)
[94c05b89]120 desc_field.append("\t\t.inflated = %u," % length)
121
122 if compress:
123 desc_field.append("\t\t.compressed = true")
124 else:
125 desc_field.append("\t\t.compressed = false")
126
[9ce911d]127 desc_field.append("\t}")
128
129 desc_ctx.append("\n".join(desc_field))
130
131 size_ctx.append("size_t %s_size = %u;" % (symbol, length_out))
132
133 src_cnt += 1
[67b05ff]134
[9ce911d]135 data = ''
136 data += '/***************************************\n'
137 data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
138 data += ' * Generated by: tools/mkarray.py *\n'
139 data += ' ***************************************/\n\n'
140 data += "#ifndef %sS_H_\n" % label.upper()
141 data += "#define %sS_H_\n\n" % label.upper()
142 data += "#include <stddef.h>\n"
[94c05b89]143 data += "#include <stdint.h>\n"
144 data += "#include <stdbool.h>\n\n"
[9ce911d]145 data += "#define %sS %u\n\n" % (label.upper(), src_cnt)
146 data += "typedef struct {\n"
147 data += "\tconst char *name;\n"
148 data += "\tvoid *addr;\n"
149 data += "\tsize_t size;\n"
150 data += "\tsize_t inflated;\n"
[94c05b89]151 data += "\tbool compressed;\n"
[9ce911d]152 data += "} %s_t;\n\n" % label
153 data += "extern %s_t %ss[];\n\n" % (label, label)
154 data += "\n".join(header_ctx)
155 data += "\n\n"
156 data += "#endif\n"
157 archive.writestr("%s.h" % dest, data)
[67b05ff]158
[9ce911d]159 data = ''
160 data += '/***************************************\n'
161 data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
162 data += ' * Generated by: tools/mkarray.py *\n'
163 data += ' ***************************************/\n\n'
164 data += as_prolog
[22299ed]165 data += "%s\n\n" % section
[9ce911d]166 data += "\n".join(data_ctx)
167 data += "\n"
168 archive.writestr("%s.s" % dest, data)
[67b05ff]169
[9ce911d]170 data = ''
171 data += '/***************************************\n'
172 data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
173 data += ' * Generated by: tools/mkarray.py *\n'
174 data += ' ***************************************/\n\n'
175 data += "#include \"%s.h\"\n\n" % dest
176 data += "%s_t %ss[] = {\n" % (label, label)
177 data += ",\n".join(desc_ctx)
178 data += "\n"
179 data += "};\n\n"
180 data += "\n".join(size_ctx)
181 data += "\n"
182 archive.writestr("%s_desc.c" % dest, data)
[67b05ff]183
[9ce911d]184 archive.close()
[67b05ff]185
186if __name__ == '__main__':
187 main()
Note: See TracBrowser for help on using the repository browser.