source: mainline/tools/autogen.py@ d776329b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d776329b was 147a066, checked in by Jakub Jermar <jakub@…>, 9 years ago

Generate dependency rules between autogenerated headers automatically

  • Property mode set to 100755
File size: 5.2 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2014 Jakub Jermar
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
30import sys
31import yaml
32import re
33
34def usage():
35 print("%s - Automated structure and offsets generator" % sys.argv[0])
36 print("%s file.ag depend|probe|generate struct.ag" % sys.argv[0])
37 sys.exit()
38
39def depend(struct):
40 deps = ""
41 for include in struct['includes']:
42 if 'depends' in include.keys():
43 deps = deps + include['depends'] + "\n"
44 return deps.strip()
45
46def generate_includes(struct):
47 code = ""
48 for include in struct['includes']:
49 if 'guard' in include.keys():
50 code = code + "#ifdef %s\n" % include['guard']
51 if 'negative-guard' in include.keys():
52 code = code + "#ifndef %s\n" % include['negative-guard']
53 code = code + "#include %s\n" % include['include']
54 if 'guard' in include.keys():
55 code = code + "#endif\n"
56 if 'negative-guard' in include.keys():
57 code = code + "#endif\n"
58 return code.strip()
59
60def generate_struct(struct):
61 packed = ""
62 if ('packed' in struct.keys() and struct['packed']):
63 packed = "__attribute__ ((packed)) "
64 code = "typedef struct %s {\n" % struct['name']
65 for i in range(len(struct['members'])):
66 member = struct['members'][i]
67 if 'elements' in member.keys():
68 code = code + "\t%s %s[%d];\n" % (member['type'], member['name'], member['elements'])
69 else:
70 code = code + "\t%s %s;\n" % (member['type'], member['name'])
71 code = code + "} %s%s_t;" % (packed, struct['name'])
72 return code
73
74def generate_probes(struct):
75 code = ""
76 for i in range(len(struct['members'])):
77 member = struct['members'][i]
78 code = code + ("\temit_constant(%s_OFFSET_%s, offsetof(%s_t, %s));\n" %
79 (struct['name'].upper(), member['name'].upper(), struct['name'],
80 member['name']))
81 code = code + ("\temit_constant(%s_SIZE_%s, sizeof(((%s_t *) 0)->%s));\n" %
82 (struct['name'].upper(), member['name'].upper(), struct['name'],
83 member['name']))
84 if 'elements' in member.keys():
85 code = code + ("\temit_constant(%s_%s_ITEM_SIZE, sizeof(%s));\n" %
86 (struct['name'].upper(), member['name'].upper(), member['type']))
87
88 return code
89
90def probe(struct):
91 name = struct['name']
92 typename = struct['name'] + "_t"
93
94 code = """
95%s
96
97#define str(s) #s
98#define emit_constant(n, v) \
99 asm volatile ("EMITTED_CONSTANT " str(n) \" = %%0\" :: \"i\" (v))
100#define offsetof(t, m) ((size_t) &(((t *) 0)->m))
101
102%s
103
104extern int main(int, char *[]);
105
106int main(int argc, char *argv[])
107{
108%s
109 emit_constant(%s_SIZE, sizeof(%s));
110 return 0;
111}
112 """ % (generate_includes(struct), generate_struct(struct),
113 generate_probes(struct), name.upper(), typename)
114
115 return code
116
117def generate_defines(pairs):
118 code = ""
119 for pair in pairs:
120 code = code + "#define %s %s\n" % (pair[0], pair[1])
121 return code.strip()
122
123def generate(struct, lines):
124 code = """
125/*****************************************************************************
126 * AUTO-GENERATED FILE, DO NOT EDIT!!!
127 * Generated by: tools/autogen.py
128 * Generated from: %s
129 *****************************************************************************/
130
131#ifndef AUTOGEN_%s_H
132#define AUTOGEN_%s_H
133
134#ifndef __ASM__
135%s
136#endif
137
138%s
139
140#ifndef __ASM__
141%s
142#endif
143
144#endif
145 """ % (sys.argv[2], struct['name'].upper(), struct['name'].upper(),
146 generate_includes(struct), generate_defines(lines),
147 generate_struct(struct))
148
149 return code
150
151def filter_pairs(lines):
152 pattern = re.compile("^\tEMITTED_CONSTANT ([A-Z_][A-Z0-9_]*) = (\$|#)?([0-9]+)$");
153 pairs = []
154 for line in lines:
155 res = pattern.match(line)
156 if res == None:
157 continue
158 pairs = pairs + [res.group(1, 3)]
159 return pairs
160
161
162def run():
163 if len(sys.argv) != 3:
164 usage()
165
166 with open(sys.argv[2], "rb") as fp:
167 struct = yaml.load(fp)
168
169 if sys.argv[1] == "depend":
170 deps = depend(struct)
171 print(deps)
172 elif sys.argv[1] == "probe":
173 code = probe(struct)
174 print(code)
175 elif sys.argv[1] == "generate":
176 lines = sys.stdin.readlines()
177 pairs = filter_pairs(lines)
178 code = generate(struct, pairs)
179 print(code)
180 else:
181 usage()
182
183run()
Note: See TracBrowser for help on using the repository browser.