3 # Copyright (c) 2008 Martin Decky
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
30 Binary package creator
41 COMPONENTS
= '_components'
45 print("%s <OBJCOPY> <FORMAT> <ARCH> <ARCH_PATH> [COMPONENTS ...]" % prname
)
48 "Compress using deflate algorithm (without any headers)"
49 return zlib
.compress(data
, 9)[2:-4]
52 "Print a bold error message"
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")
65 "Create a temporal sandbox directory for packing"
67 if (os
.path
.exists(SANDBOX
)):
68 if (os
.path
.isdir(SANDBOX
)):
70 shutil
.rmtree(SANDBOX
)
72 print_error(["Unable to cleanup the directory \"%s\"." % SANDBOX
])
74 print_error(["Please inspect and remove unexpected directory,",
75 "entry \"%s\"." % SANDBOX
])
80 print_error(["Unable to create sandbox directory \"%s\"." % SANDBOX
])
87 def sandbox_leave(owd
):
88 "Leave the temporal sandbox directory"
93 if (len(sys
.argv
) < 5):
100 arch_path
= sys
.argv
[4]
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(".", "_")
112 print("%s -> %s" % (component
, obj
))
114 comp_in
= open(component
, "rb")
115 comp_data
= comp_in
.read()
118 comp_deflate
= deflate(comp_data
)
120 owd
= sandbox_enter()
123 comp_out
= open(basename
, "wb")
124 comp_out
.write(comp_deflate
)
127 subprocess
.call([objcopy
,
131 "--rename-section", ".data=.%s_image" % plainname
,
132 basename
, os
.path
.join(owd
, obj
)])
137 link_ctx
.append("\t\t*(.%s_image);" % plainname
)
139 header_rec
= "extern int %s_start;\n" % symbol
140 header_rec
+= "extern int %s_size;\n" % symbol
141 header_ctx
.append(header_rec
)
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
)
149 data_ctx
.append(data_rec
)
153 header
= open(os
.path
.join(arch_path
, "include", "%s.h" % COMPONENTS
), "w")
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
))
171 header
.write("#endif\n")
175 data
= open(os
.path
.join(arch_path
, "src", "%s.c" % COMPONENTS
), "w")
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
))
189 link_in
= open(os
.path
.join(arch_path
, "%s.in" % LINK
), "r")
190 template
= link_in
.read()
193 link_out
= open(os
.path
.join(arch_path
, "%s.comp" % LINK
), "w")
194 link_out
.write(template
.replace("[[COMPONENTS]]", "\n".join(link_ctx
)))
197 if __name__
== '__main__':