source: mainline/tools/mkarray.py@ a35b458

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

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