1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Copyright (c) 2008 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 | """
|
---|
30 | Binary package creator
|
---|
31 | """
|
---|
32 |
|
---|
33 | import sys
|
---|
34 | import os
|
---|
35 | import subprocess
|
---|
36 | import zlib
|
---|
37 | import shutil
|
---|
38 |
|
---|
39 | SANDBOX = 'pack'
|
---|
40 | LINK = '_link.ld'
|
---|
41 | COMPONENTS = '_components'
|
---|
42 |
|
---|
43 | def usage(prname):
|
---|
44 | "Print usage syntax"
|
---|
45 | print("%s <OBJCOPY> <FORMAT> <ARCH> <ARCH_PATH> [COMPONENTS ...]" % prname)
|
---|
46 |
|
---|
47 | def deflate(data):
|
---|
48 | "Compress using deflate algorithm (without any headers)"
|
---|
49 | return zlib.compress(data, 9)[2:-4]
|
---|
50 |
|
---|
51 | def print_error(msg):
|
---|
52 | "Print a bold error message"
|
---|
53 |
|
---|
54 | sys.stderr.write("\n")
|
---|
55 | sys.stderr.write("######################################################################\n")
|
---|
56 | sys.stderr.write("HelenOS build sanity check error:\n")
|
---|
57 | sys.stderr.write("\n")
|
---|
58 | sys.stderr.write("%s\n" % "\n".join(msg))
|
---|
59 | sys.stderr.write("######################################################################\n")
|
---|
60 | sys.stderr.write("\n")
|
---|
61 |
|
---|
62 | sys.exit(1)
|
---|
63 |
|
---|
64 | def sandbox_enter():
|
---|
65 | "Create a temporal sandbox directory for packing"
|
---|
66 |
|
---|
67 | if (os.path.exists(SANDBOX)):
|
---|
68 | if (os.path.isdir(SANDBOX)):
|
---|
69 | try:
|
---|
70 | shutil.rmtree(SANDBOX)
|
---|
71 | except:
|
---|
72 | print_error(["Unable to cleanup the directory \"%s\"." % SANDBOX])
|
---|
73 | else:
|
---|
74 | print_error(["Please inspect and remove unexpected directory,",
|
---|
75 | "entry \"%s\"." % SANDBOX])
|
---|
76 |
|
---|
77 | try:
|
---|
78 | os.mkdir(SANDBOX)
|
---|
79 | except:
|
---|
80 | print_error(["Unable to create sandbox directory \"%s\"." % SANDBOX])
|
---|
81 |
|
---|
82 | owd = os.getcwd()
|
---|
83 | os.chdir(SANDBOX)
|
---|
84 |
|
---|
85 | return owd
|
---|
86 |
|
---|
87 | def sandbox_leave(owd):
|
---|
88 | "Leave the temporal sandbox directory"
|
---|
89 |
|
---|
90 | os.chdir(owd)
|
---|
91 |
|
---|
92 | def main():
|
---|
93 | if (len(sys.argv) < 5):
|
---|
94 | usage(sys.argv[0])
|
---|
95 | return
|
---|
96 |
|
---|
97 | objcopy = sys.argv[1]
|
---|
98 | format = sys.argv[2]
|
---|
99 | arch = sys.argv[3]
|
---|
100 | arch_path = sys.argv[4]
|
---|
101 |
|
---|
102 | header_ctx = []
|
---|
103 | data_ctx = []
|
---|
104 | link_ctx = []
|
---|
105 | cnt = 0
|
---|
106 | for component in sys.argv[5:]:
|
---|
107 | basename = os.path.basename(component)
|
---|
108 | plainname = os.path.splitext(basename)[0]
|
---|
109 | obj = "%s.co" % plainname
|
---|
110 | symbol = "_binary_%s" % basename.replace(".", "_")
|
---|
111 |
|
---|
112 | print("%s -> %s" % (component, obj))
|
---|
113 |
|
---|
114 | comp_in = open(component, "rb")
|
---|
115 | comp_data = comp_in.read()
|
---|
116 | comp_in.close()
|
---|
117 |
|
---|
118 | comp_deflate = deflate(comp_data)
|
---|
119 |
|
---|
120 | owd = sandbox_enter()
|
---|
121 |
|
---|
122 | try:
|
---|
123 | comp_out = open(basename, "wb")
|
---|
124 | comp_out.write(comp_deflate)
|
---|
125 | comp_out.close()
|
---|
126 |
|
---|
127 | subprocess.call([objcopy,
|
---|
128 | "-I", "binary",
|
---|
129 | "-O", format,
|
---|
130 | "-B", arch,
|
---|
131 | "--rename-section", ".data=.%s_image" % plainname,
|
---|
132 | basename, os.path.join(owd, obj)])
|
---|
133 |
|
---|
134 | finally:
|
---|
135 | sandbox_leave(owd)
|
---|
136 |
|
---|
137 | link_ctx.append("\t\t*(.%s_image);" % plainname)
|
---|
138 |
|
---|
139 | header_rec = "extern int %s_start;\n" % symbol
|
---|
140 | header_rec += "extern int %s_size;\n" % symbol
|
---|
141 | header_ctx.append(header_rec)
|
---|
142 |
|
---|
143 | data_rec = "\t{\n"
|
---|
144 | data_rec += "\t\t.name = \"%s\",\n" % plainname
|
---|
145 | data_rec += "\t\t.start = (void *) &%s_start,\n" % symbol
|
---|
146 | data_rec += "\t\t.size = (size_t) &%s_size,\n" % symbol
|
---|
147 | data_rec += "\t\t.inflated = %d\n" % len(comp_data)
|
---|
148 | data_rec += "\t}"
|
---|
149 | data_ctx.append(data_rec)
|
---|
150 |
|
---|
151 | cnt += 1
|
---|
152 |
|
---|
153 | header = open(os.path.join(arch_path, "include", "%s.h" % COMPONENTS), "w")
|
---|
154 |
|
---|
155 | header.write('/***************************************\n')
|
---|
156 | header.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
|
---|
157 | header.write(' ***************************************/\n\n')
|
---|
158 | header.write("#ifndef BOOT_COMPONENTS_H_\n")
|
---|
159 | header.write("#define BOOT_COMPONENTS_H_\n\n")
|
---|
160 | header.write("#include <typedefs.h>\n\n")
|
---|
161 | header.write("#define COMPONENTS %d\n\n" % cnt)
|
---|
162 | header.write("typedef struct {\n")
|
---|
163 | header.write("\tconst char *name;\n")
|
---|
164 | header.write("\tvoid *start;\n")
|
---|
165 | header.write("\tsize_t size;\n")
|
---|
166 | header.write("\tsize_t inflated;\n")
|
---|
167 | header.write("} component_t;\n\n")
|
---|
168 | header.write("extern component_t components[];\n\n")
|
---|
169 | header.write("\n".join(header_ctx))
|
---|
170 | header.write("\n")
|
---|
171 | header.write("#endif\n")
|
---|
172 |
|
---|
173 | header.close()
|
---|
174 |
|
---|
175 | data = open(os.path.join(arch_path, "src", "%s.c" % COMPONENTS), "w")
|
---|
176 |
|
---|
177 | data.write('/***************************************\n')
|
---|
178 | data.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
|
---|
179 | data.write(' ***************************************/\n\n')
|
---|
180 | data.write("#include <typedefs.h>\n")
|
---|
181 | data.write("#include <arch/%s.h>\n\n" % COMPONENTS)
|
---|
182 | data.write("component_t components[] = {\n")
|
---|
183 | data.write(",\n".join(data_ctx))
|
---|
184 | data.write("\n")
|
---|
185 | data.write("};\n")
|
---|
186 |
|
---|
187 | data.close()
|
---|
188 |
|
---|
189 | link_in = open(os.path.join(arch_path, "%s.in" % LINK), "r")
|
---|
190 | template = link_in.read()
|
---|
191 | link_in.close()
|
---|
192 |
|
---|
193 | link_out = open(os.path.join(arch_path, "%s.comp" % LINK), "w")
|
---|
194 | link_out.write(template.replace("[[COMPONENTS]]", "\n".join(link_ctx)))
|
---|
195 | link_out.close()
|
---|
196 |
|
---|
197 | if __name__ == '__main__':
|
---|
198 | main()
|
---|