Rev 3027 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3027 | Rev 4055 | ||
---|---|---|---|
1 | #!/usr/bin/env python |
1 | #!/usr/bin/env python |
2 | # |
2 | # |
3 | # Copyright (c) 2008 Martin Decky |
3 | # Copyright (c) 2008 Martin Decky |
4 | # All rights reserved. |
4 | # All rights reserved. |
5 | # |
5 | # |
6 | # Redistribution and use in source and binary forms, with or without |
6 | # Redistribution and use in source and binary forms, with or without |
7 | # modification, are permitted provided that the following conditions |
7 | # modification, are permitted provided that the following conditions |
8 | # are met: |
8 | # are met: |
9 | # |
9 | # |
10 | # - Redistributions of source code must retain the above copyright |
10 | # - Redistributions of source code must retain the above copyright |
11 | # notice, this list of conditions and the following disclaimer. |
11 | # notice, this list of conditions and the following disclaimer. |
12 | # - Redistributions in binary form must reproduce the above copyright |
12 | # - Redistributions in binary form must reproduce the above copyright |
13 | # notice, this list of conditions and the following disclaimer in the |
13 | # notice, this list of conditions and the following disclaimer in the |
14 | # documentation and/or other materials provided with the distribution. |
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 |
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. |
16 | # derived from this software without specific prior written permission. |
17 | # |
17 | # |
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | # |
28 | # |
29 | """ |
29 | """ |
30 | Binary package creator |
30 | Binary package creator |
31 | """ |
31 | """ |
32 | 32 | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import subprocess |
35 | import subprocess |
36 | 36 | ||
37 | def usage(prname): |
37 | def usage(prname): |
38 | "Print usage syntax" |
38 | "Print usage syntax" |
39 | print prname + " <OBJCOPY> <FORMAT> <ARCH> <ALIGNMENT> <UINTPTR>" |
39 | print prname + " <OBJCOPY> <FORMAT> <ARCH> <ALIGNMENT> <UINTPTR>" |
40 | 40 | ||
41 | def main(): |
41 | def main(): |
42 | if (len(sys.argv) < 6): |
42 | if (len(sys.argv) < 6): |
43 | usage(sys.argv[0]) |
43 | usage(sys.argv[0]) |
44 | return |
44 | return |
45 | 45 | ||
46 | if (not sys.argv[4].isdigit()): |
46 | if (not sys.argv[4].isdigit()): |
47 | print "<ALIGNMENT> must be a number" |
47 | print "<ALIGNMENT> must be a number" |
48 | return |
48 | return |
49 | 49 | ||
50 | objcopy = sys.argv[1] |
50 | objcopy = sys.argv[1] |
51 | format = sys.argv[2] |
51 | format = sys.argv[2] |
52 | arch = sys.argv[3] |
52 | arch = sys.argv[3] |
53 | align = int(sys.argv[4], 0) |
53 | align = int(sys.argv[4], 0) |
54 | uintptr = sys.argv[5] |
54 | uintptr = sys.argv[5] |
55 | 55 | ||
56 | workdir = os.getcwd() |
56 | workdir = os.getcwd() |
57 | 57 | ||
58 | header = file("_components.h", "w") |
58 | header = file("_components.h", "w") |
59 | data = file("_components.c", "w") |
59 | data = file("_components.c", "w") |
60 | 60 | ||
61 | header.write("#ifndef ___COMPONENTS_H__\n") |
61 | header.write("#ifndef ___COMPONENTS_H__\n") |
62 | header.write("#define ___COMPONENTS_H__\n\n") |
62 | header.write("#define ___COMPONENTS_H__\n\n") |
63 | 63 | ||
64 | data.write("#include \"_components.h\"\n\n") |
64 | data.write("#include \"_components.h\"\n\n") |
65 | data.write("void init_components(component_t *components)\n") |
65 | data.write("void init_components(component_t *components)\n") |
66 | data.write("{\n") |
66 | data.write("{\n") |
67 | 67 | ||
68 | cnt = 0 |
68 | cnt = 0 |
69 | link = "" |
69 | link = "" |
70 | for task in sys.argv[6:]: |
70 | for task in sys.argv[6:]: |
71 | basename = os.path.basename(task) |
71 | basename = os.path.basename(task) |
72 | plainname = os.path.splitext(basename)[0] |
72 | plainname = os.path.splitext(basename)[0] |
73 | path = os.path.dirname(task) |
73 | path = os.path.dirname(task) |
74 | object = plainname + ".o" |
74 | object = plainname + ".o" |
75 | symbol = "_binary_" + basename.replace(".", "_") |
75 | symbol = "_binary_" + basename.replace(".", "_") |
76 | macro = plainname.upper() |
76 | macro = plainname.upper() |
77 | 77 | ||
78 | print task + " -> " + object |
78 | print task + " -> " + object |
79 | 79 | ||
80 | if (align > 1): |
80 | if (align > 1): |
81 | link += "\t\t. = ALIGN(" + ("%d" % align) + ");\n" |
81 | link += "\t\t. = ALIGN(" + ("%d" % align) + ");\n" |
82 | 82 | ||
83 | link += "\t\t*(." + plainname + "_image);\n" |
83 | link += "\t\t*(." + plainname + "_image);\n" |
84 | 84 | ||
85 | header.write("extern int " + symbol + "_start;\n") |
85 | header.write("extern int " + symbol + "_start;\n") |
86 | header.write("extern int " + symbol + "_end;\n\n") |
86 | header.write("extern int " + symbol + "_end;\n\n") |
87 | header.write("#define " + macro + "_START ((void *) &" + symbol + "_start)\n") |
87 | header.write("#define " + macro + "_START ((void *) &" + symbol + "_start)\n") |
88 | header.write("#define " + macro + "_END ((void *) &" + symbol + "_end)\n") |
88 | header.write("#define " + macro + "_END ((void *) &" + symbol + "_end)\n") |
89 | header.write("#define " + macro + "_SIZE ((" + uintptr + ") " + macro + "_END - (" + uintptr + ") " + macro + "_START)\n\n") |
89 | header.write("#define " + macro + "_SIZE ((" + uintptr + ") " + macro + "_END - (" + uintptr + ") " + macro + "_START)\n\n") |
90 | 90 | ||
91 | data.write("\tcomponents[" + ("%d" % cnt) + "].name = \"" + plainname + "\";\n") |
91 | data.write("\tcomponents[" + ("%d" % cnt) + "].name = \"" + plainname + "\";\n") |
92 | data.write("\tcomponents[" + ("%d" % cnt) + "].start = " + macro + "_START;\n") |
92 | data.write("\tcomponents[" + ("%d" % cnt) + "].start = " + macro + "_START;\n") |
93 | data.write("\tcomponents[" + ("%d" % cnt) + "].end = " + macro + "_END;\n") |
93 | data.write("\tcomponents[" + ("%d" % cnt) + "].end = " + macro + "_END;\n") |
94 | data.write("\tcomponents[" + ("%d" % cnt) + "].size = " + macro + "_SIZE;\n\n") |
94 | data.write("\tcomponents[" + ("%d" % cnt) + "].size = " + macro + "_SIZE;\n\n") |
95 | 95 | ||
96 | os.chdir(path) |
96 | os.chdir(path) |
97 | subprocess.call([objcopy, |
97 | subprocess.call([objcopy, |
98 | "-I", "binary", |
98 | "-I", "binary", |
99 | "-O", format, |
99 | "-O", format, |
100 | "-B", arch, |
100 | "-B", arch, |
101 | "--rename-section", ".data=." + plainname + "_image", |
101 | "--rename-section", ".data=." + plainname + "_image", |
102 | basename, os.path.join(workdir, object)]) |
102 | basename, os.path.join(workdir, object)]) |
103 | os.chdir(workdir) |
103 | os.chdir(workdir) |
104 | 104 | ||
105 | cnt += 1 |
105 | cnt += 1 |
106 | 106 | ||
107 | header.write("#define COMPONENTS " + ("%d" % cnt) + "\n\n") |
107 | header.write("#define COMPONENTS " + ("%d" % cnt) + "\n\n") |
108 | header.write("typedef struct {\n") |
108 | header.write("typedef struct {\n") |
109 | header.write("\tchar *name;\n\n") |
109 | header.write("\tchar *name;\n\n") |
110 | header.write("\tvoid *start;\n") |
110 | header.write("\tvoid *start;\n") |
111 | header.write("\tvoid *end;\n") |
111 | header.write("\tvoid *end;\n") |
112 | header.write("\t" + uintptr + " size;\n") |
112 | header.write("\t" + uintptr + " size;\n") |
113 | header.write("} component_t;\n\n") |
113 | header.write("} component_t;\n\n") |
114 | header.write("extern void init_components(component_t *components);\n\n") |
114 | header.write("extern void init_components(component_t *components);\n\n") |
115 | header.write("#endif\n") |
115 | header.write("#endif\n") |
116 | 116 | ||
117 | data.write("}\n") |
117 | data.write("}\n") |
118 | 118 | ||
119 | header.close() |
119 | header.close() |
120 | data.close() |
120 | data.close() |
121 | 121 | ||
122 | linkname = "_link.ld" |
122 | linkname = "_link.ld" |
123 | link_in = file(linkname + ".in", "r") |
123 | link_in = file(linkname + ".in", "r") |
124 | template = link_in.read(os.path.getsize(linkname + ".in")) |
124 | template = link_in.read(os.path.getsize(linkname + ".in")) |
125 | link_in.close() |
125 | link_in.close() |
126 | 126 | ||
127 | link_out = file(linkname, "w") |
127 | link_out = file(linkname, "w") |
128 | link_out.write(template.replace("[[COMPONENTS]]", link)) |
128 | link_out.write(template.replace("[[COMPONENTS]]", link)) |
129 | link_out.close() |
129 | link_out.close() |
130 | 130 | ||
131 | if __name__ == '__main__': |
131 | if __name__ == '__main__': |
132 | main() |
132 | main() |
133 | 133 |