makerumpif.sh revision 1.9 1 1.1 pooka #!/bin/sh
2 1.1 pooka #
3 1.9 pooka # $NetBSD: makerumpif.sh,v 1.9 2015/04/23 10:50:00 pooka Exp $
4 1.1 pooka #
5 1.9 pooka # Copyright (c) 2009, 2015 Antti Kantee. All rights reserved.
6 1.1 pooka #
7 1.1 pooka # Redistribution and use in source and binary forms, with or without
8 1.1 pooka # modification, are permitted provided that the following conditions
9 1.1 pooka # are met:
10 1.1 pooka # 1. Redistributions of source code must retain the above copyright
11 1.1 pooka # notice, this list of conditions and the following disclaimer.
12 1.1 pooka # 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pooka # notice, this list of conditions and the following disclaimer in the
14 1.1 pooka # documentation and/or other materials provided with the distribution.
15 1.1 pooka #
16 1.1 pooka # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 pooka # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 pooka # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 pooka # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 pooka # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 pooka # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 pooka # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 pooka # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 pooka # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 pooka # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 pooka #
27 1.1 pooka
28 1.1 pooka #
29 1.1 pooka # This reads a rump component interface description and creates:
30 1.1 pooka # 1: rump private prototypes for internal calls
31 1.1 pooka # 2: public prototypes for calls outside of rump
32 1.1 pooka # 3: public interface implementations which run the rump scheduler
33 1.1 pooka # and call the private interface
34 1.1 pooka #
35 1.1 pooka
36 1.1 pooka usage ()
37 1.1 pooka {
38 1.1 pooka
39 1.1 pooka echo "usage: $0 spec"
40 1.1 pooka exit 1
41 1.1 pooka }
42 1.1 pooka
43 1.1 pooka boom ()
44 1.1 pooka {
45 1.1 pooka
46 1.1 pooka echo $*
47 1.1 pooka exit 1
48 1.1 pooka }
49 1.1 pooka
50 1.6 pooka unset TOPDIR
51 1.6 pooka if [ $# -eq 3 ]; then
52 1.6 pooka if [ $1 = '-R' ]; then
53 1.6 pooka TOPDIR=$2
54 1.6 pooka else
55 1.6 pooka usage
56 1.6 pooka fi
57 1.6 pooka shift; shift
58 1.6 pooka fi
59 1.6 pooka [ $# -ne 1 ] && usage
60 1.1 pooka
61 1.1 pooka MYDIR=`pwd`
62 1.6 pooka if [ -z "${TOPDIR}" ]; then
63 1.6 pooka while [ ! -f Makefile.rump ]; do
64 1.6 pooka [ `pwd` = '/' ] && boom Could not find rump topdir.
65 1.6 pooka cd ..
66 1.6 pooka done
67 1.6 pooka TOPDIR="`pwd`"
68 1.6 pooka fi
69 1.1 pooka cd ${MYDIR}
70 1.1 pooka
71 1.1 pooka sed -e '
72 1.1 pooka :again
73 1.1 pooka /\\$/{
74 1.1 pooka N
75 1.1 pooka s/[ ]*\\\n[ ]*/ /
76 1.1 pooka b again
77 1.1 pooka }
78 1.6 pooka ' ${1} | awk -F\| -v topdir=${TOPDIR} '
79 1.1 pooka function fileheaders(file, srcstr)
80 1.1 pooka {
81 1.9 pooka printf("/*\t$NetBSD: makerumpif.sh,v 1.9 2015/04/23 10:50:00 pooka Exp $\t*/\n\n") > file
82 1.1 pooka printf("/*\n * Automatically generated. DO NOT EDIT.\n") > file
83 1.9 pooka genstr = "$NetBSD: makerumpif.sh,v 1.9 2015/04/23 10:50:00 pooka Exp $"
84 1.1 pooka gsub("\\$", "", genstr)
85 1.1 pooka printf(" * from: %s\n", srcstr) > file
86 1.1 pooka printf(" * by: %s\n", genstr) > file
87 1.1 pooka printf(" */\n") > file
88 1.1 pooka }
89 1.1 pooka
90 1.1 pooka function die(str)
91 1.1 pooka {
92 1.1 pooka
93 1.1 pooka print str
94 1.1 pooka exit(1)
95 1.1 pooka }
96 1.1 pooka
97 1.1 pooka NR == 1 {
98 1.1 pooka sub(";[^\\$]*\\$", "")
99 1.1 pooka sub("\\$", "")
100 1.1 pooka fromstr = $0
101 1.1 pooka next
102 1.1 pooka }
103 1.1 pooka
104 1.1 pooka $1 == "NAME"{myname = $2;next}
105 1.6 pooka $1 == "PUBHDR"{pubhdr = topdir "/" $2;print pubhdr;next}
106 1.6 pooka $1 == "PRIVHDR"{privhdr = topdir "/" $2;print privhdr;next}
107 1.6 pooka $1 == "WRAPPERS"{gencalls = topdir "/" $2;print gencalls;next}
108 1.1 pooka
109 1.1 pooka /^;/{next}
110 1.1 pooka /\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
111 1.1 pooka /^[ \t]*$/{next}
112 1.1 pooka {
113 1.1 pooka if (NF != 3 && NF != 4) {
114 1.1 pooka die("error: unexpected number of fields\n")
115 1.1 pooka }
116 1.9 pooka isweak = 0
117 1.9 pooka iscompat = 0
118 1.1 pooka if (NF == 4) {
119 1.9 pooka if ($4 == "WEAK") {
120 1.1 pooka isweak = 1
121 1.9 pooka } else if (match($4, "COMPAT_")) {
122 1.9 pooka iscompat = 1
123 1.9 pooka compat = $4
124 1.9 pooka } else {
125 1.1 pooka die("error: unexpected fourth field");
126 1.9 pooka }
127 1.1 pooka }
128 1.1 pooka if (!myname)
129 1.1 pooka die("name not specified");
130 1.1 pooka if (!pubhdr)
131 1.1 pooka die("public header not specified");
132 1.1 pooka if (!privhdr)
133 1.1 pooka die("private header not specified");
134 1.1 pooka if (!gencalls)
135 1.1 pooka die("wrapper file not specified");
136 1.1 pooka
137 1.1 pooka if (!once) {
138 1.1 pooka fileheaders(pubhdr, fromstr)
139 1.1 pooka fileheaders(privhdr, fromstr)
140 1.1 pooka fileheaders(gencalls, fromstr)
141 1.1 pooka once = 1
142 1.1 pooka
143 1.1 pooka pubfile = pubhdr
144 1.1 pooka sub(".*/", "", pubfile)
145 1.1 pooka
146 1.1 pooka privfile = privhdr
147 1.1 pooka sub(".*/", "", privfile)
148 1.1 pooka
149 1.1 pooka printf("\n") > pubhdr
150 1.1 pooka printf("\n") > privhdr
151 1.1 pooka
152 1.8 pooka printf("#ifndef _RUMP_PRIF_%s_H_\n", toupper(myname)) > privhdr
153 1.8 pooka printf("#define _RUMP_PRIF_%s_H_\n", toupper(myname)) > privhdr
154 1.8 pooka printf("\n") > privhdr
155 1.8 pooka
156 1.1 pooka printf("\n#include <sys/cdefs.h>\n") > gencalls
157 1.1 pooka printf("#include <sys/systm.h>\n") > gencalls
158 1.1 pooka printf("\n#include <rump/rump.h>\n") > gencalls
159 1.1 pooka printf("#include <rump/%s>\n\n", pubfile) > gencalls
160 1.4 pooka printf("#include \"rump_private.h\"\n", privfile) > gencalls
161 1.1 pooka printf("#include \"%s\"\n\n", privfile) > gencalls
162 1.2 pooka printf("void __dead rump_%s_unavailable(void);\n", \
163 1.1 pooka myname) > gencalls
164 1.1 pooka printf("void __dead\nrump_%s_unavailable(void)\n{\n", \
165 1.1 pooka myname) > gencalls
166 1.1 pooka printf("\n\tpanic(\"%s interface unavailable\");\n}\n", \
167 1.1 pooka myname) > gencalls
168 1.1 pooka }
169 1.1 pooka
170 1.1 pooka funtype = $1
171 1.1 pooka sub("[ \t]*$", "", funtype)
172 1.1 pooka funname = $2
173 1.1 pooka sub("[ \t]*$", "", funname)
174 1.1 pooka funargs = $3
175 1.2 pooka sub("[ \t]*$", "", funargs)
176 1.2 pooka
177 1.3 pooka printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
178 1.3 pooka printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
179 1.7 pooka printf("typedef %s (*rump_%s_fn)(%s);\n", \
180 1.7 pooka funtype, funname, funargs) > privhdr
181 1.1 pooka
182 1.1 pooka if (funtype == "void")
183 1.1 pooka voidret = 1
184 1.1 pooka else
185 1.1 pooka voidret = 0
186 1.1 pooka if (funargs == "void")
187 1.1 pooka voidarg = 1
188 1.1 pooka else
189 1.1 pooka voidarg = 0
190 1.1 pooka
191 1.9 pooka printf("\n") > gencalls
192 1.9 pooka if (iscompat) {
193 1.9 pooka printf("#ifdef %s\n", compat) > gencalls
194 1.9 pooka }
195 1.9 pooka
196 1.9 pooka printf("%s\nrump_pub_%s(", funtype, funname) > gencalls
197 1.1 pooka if (!voidarg) {
198 1.1 pooka narg = split(funargs, argv, ",")
199 1.1 pooka for (i = 1; i <= narg; i++) {
200 1.1 pooka sub(" *", "", argv[i])
201 1.1 pooka if (match(argv[i], "\\*$") != 0)
202 1.1 pooka printf("%sarg%d", argv[i], i) > gencalls
203 1.1 pooka else
204 1.1 pooka printf("%s arg%d", argv[i], i) > gencalls
205 1.1 pooka if (i != narg)
206 1.1 pooka printf(", ") > gencalls
207 1.1 pooka }
208 1.1 pooka } else {
209 1.1 pooka narg = 0
210 1.1 pooka printf("void") > gencalls
211 1.1 pooka }
212 1.1 pooka printf(")\n{\n") > gencalls
213 1.1 pooka
214 1.1 pooka if (!voidret) {
215 1.2 pooka printf("\t%s rv;\n", funtype) > gencalls
216 1.1 pooka }
217 1.4 pooka printf("\n\trump_schedule();\n\t") > gencalls
218 1.1 pooka if (!voidret)
219 1.1 pooka printf("rv = ") > gencalls
220 1.3 pooka printf("rump_%s(", funname) > gencalls
221 1.1 pooka for (i = 1; i <= narg; i++) {
222 1.1 pooka printf("arg%i", i) > gencalls
223 1.1 pooka if (i < narg)
224 1.1 pooka printf(", ") > gencalls
225 1.1 pooka }
226 1.4 pooka printf(");\n\trump_unschedule();\n") > gencalls
227 1.1 pooka if (!voidret)
228 1.1 pooka printf("\n\treturn rv;\n") > gencalls
229 1.1 pooka printf("}\n") > gencalls
230 1.9 pooka if (iscompat) {
231 1.9 pooka printf("#else\n") > gencalls
232 1.9 pooka printf("__strong_alias(rump_pub_%s,rump_%s_unavailable);\n", \
233 1.9 pooka funname, myname) > gencalls
234 1.9 pooka printf("#endif /* %s */\n", compat) > gencalls
235 1.9 pooka }
236 1.1 pooka if (isweak)
237 1.3 pooka printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
238 1.1 pooka funname, myname) > gencalls
239 1.8 pooka }
240 1.8 pooka
241 1.8 pooka END { printf("\n#endif /* _RUMP_PRIF_%s_H_ */\n", toupper(myname)) > privhdr }'
242