Home | History | Annotate | Line # | Download | only in librump
makerumpif.sh revision 1.5.18.1
      1       1.1  pooka #!/bin/sh
      2       1.1  pooka #
      3  1.5.18.1    tls #	$NetBSD: makerumpif.sh,v 1.5.18.1 2013/02/25 00:30:08 tls Exp $
      4       1.1  pooka #
      5       1.1  pooka # Copyright (c) 2009 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.5.18.1    tls unset TOPDIR
     51  1.5.18.1    tls if [ $# -eq 3 ]; then
     52  1.5.18.1    tls 	if [ $1 = '-R' ]; then
     53  1.5.18.1    tls 		TOPDIR=$2
     54  1.5.18.1    tls 	else
     55  1.5.18.1    tls 		usage
     56  1.5.18.1    tls 	fi
     57  1.5.18.1    tls 	shift; shift
     58  1.5.18.1    tls fi
     59  1.5.18.1    tls [ $# -ne 1 ] && usage
     60       1.1  pooka 
     61       1.1  pooka MYDIR=`pwd`
     62  1.5.18.1    tls if [ -z "${TOPDIR}" ]; then
     63  1.5.18.1    tls 	while [ ! -f Makefile.rump  ]; do
     64  1.5.18.1    tls 		[ `pwd` = '/' ] && boom Could not find rump topdir.
     65  1.5.18.1    tls 		cd ..
     66  1.5.18.1    tls 	done
     67  1.5.18.1    tls 	TOPDIR="`pwd`"
     68  1.5.18.1    tls 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.5.18.1    tls ' ${1} | awk -F\| -v topdir=${TOPDIR} '
     79       1.1  pooka function fileheaders(file, srcstr)
     80       1.1  pooka {
     81  1.5.18.1    tls 	printf("/*\t$NetBSD: makerumpif.sh,v 1.5.18.1 2013/02/25 00:30:08 tls Exp $\t*/\n\n") > file
     82       1.1  pooka 	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
     83  1.5.18.1    tls 	genstr = "$NetBSD: makerumpif.sh,v 1.5.18.1 2013/02/25 00:30:08 tls 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.5.18.1    tls $1 == "PUBHDR"{pubhdr = topdir "/" $2;print pubhdr;next}
    106  1.5.18.1    tls $1 == "PRIVHDR"{privhdr = topdir "/" $2;print privhdr;next}
    107  1.5.18.1    tls $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.1  pooka 	if (NF == 4) {
    117       1.1  pooka 		if ($4 == "WEAK")
    118       1.1  pooka 			isweak = 1
    119       1.1  pooka 		else
    120       1.1  pooka 			die("error: unexpected fourth field");
    121       1.1  pooka 	} else {
    122       1.1  pooka 		isweak = 0
    123       1.1  pooka 	}
    124       1.1  pooka 	if (!myname)
    125       1.1  pooka 		die("name not specified");
    126       1.1  pooka 	if (!pubhdr)
    127       1.1  pooka 		die("public header not specified");
    128       1.1  pooka 	if (!privhdr)
    129       1.1  pooka 		die("private header not specified");
    130       1.1  pooka 	if (!gencalls)
    131       1.1  pooka 		die("wrapper file not specified");
    132       1.1  pooka 
    133       1.1  pooka 	if (!once) {
    134       1.1  pooka 		fileheaders(pubhdr, fromstr)
    135       1.1  pooka 		fileheaders(privhdr, fromstr)
    136       1.1  pooka 		fileheaders(gencalls, fromstr)
    137       1.1  pooka 		once = 1
    138       1.1  pooka 
    139       1.1  pooka 		pubfile = pubhdr
    140       1.1  pooka 		sub(".*/", "", pubfile)
    141       1.1  pooka 
    142       1.1  pooka 		privfile = privhdr
    143       1.1  pooka 		sub(".*/", "", privfile)
    144       1.1  pooka 
    145       1.1  pooka 		printf("\n") > pubhdr
    146       1.1  pooka 		printf("\n") > privhdr
    147       1.1  pooka 
    148       1.1  pooka 		printf("\n#include <sys/cdefs.h>\n") > gencalls
    149       1.1  pooka 		printf("#include <sys/systm.h>\n") > gencalls
    150       1.1  pooka 		printf("\n#include <rump/rump.h>\n") > gencalls
    151       1.1  pooka 		printf("#include <rump/%s>\n\n", pubfile) > gencalls
    152       1.4  pooka 		printf("#include \"rump_private.h\"\n", privfile) > gencalls
    153       1.1  pooka 		printf("#include \"%s\"\n\n", privfile) > gencalls
    154       1.2  pooka 		printf("void __dead rump_%s_unavailable(void);\n",	\
    155       1.1  pooka 		    myname) > gencalls
    156       1.1  pooka 		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
    157       1.1  pooka 		    myname) > gencalls
    158       1.1  pooka 		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
    159       1.1  pooka 		    myname) > gencalls
    160       1.1  pooka 	}
    161       1.1  pooka 
    162       1.1  pooka 	funtype = $1
    163       1.1  pooka 	sub("[ \t]*$", "", funtype)
    164       1.1  pooka 	funname = $2
    165       1.1  pooka 	sub("[ \t]*$", "", funname)
    166       1.1  pooka 	funargs = $3
    167       1.2  pooka 	sub("[ \t]*$", "", funargs)
    168       1.2  pooka 
    169       1.3  pooka 	printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
    170       1.3  pooka 	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
    171       1.1  pooka 
    172       1.1  pooka 	if (funtype == "void")
    173       1.1  pooka 		voidret = 1
    174       1.1  pooka 	else
    175       1.1  pooka 		voidret = 0
    176       1.1  pooka 	if (funargs == "void")
    177       1.1  pooka 		voidarg = 1
    178       1.1  pooka 	else
    179       1.1  pooka 		voidarg = 0
    180       1.1  pooka 
    181       1.3  pooka 	printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls
    182       1.1  pooka 	if (!voidarg) {
    183       1.1  pooka 		narg = split(funargs, argv, ",")
    184       1.1  pooka 		for (i = 1; i <= narg; i++) {
    185       1.1  pooka 			sub(" *", "", argv[i])
    186       1.1  pooka 			if (match(argv[i], "\\*$") != 0)
    187       1.1  pooka 				printf("%sarg%d", argv[i], i) > gencalls
    188       1.1  pooka 			else
    189       1.1  pooka 				printf("%s arg%d", argv[i], i) > gencalls
    190       1.1  pooka 			if (i != narg)
    191       1.1  pooka 				printf(", ") > gencalls
    192       1.1  pooka 		}
    193       1.1  pooka 	} else {
    194       1.1  pooka 		narg = 0
    195       1.1  pooka 		printf("void") > gencalls
    196       1.1  pooka 	}
    197       1.1  pooka 	printf(")\n{\n") > gencalls
    198       1.1  pooka 
    199       1.1  pooka 	if (!voidret) {
    200       1.2  pooka 		printf("\t%s rv;\n", funtype) > gencalls
    201       1.1  pooka 	}
    202       1.4  pooka 	printf("\n\trump_schedule();\n\t") > gencalls
    203       1.1  pooka 	if (!voidret)
    204       1.1  pooka 		printf("rv = ") > gencalls
    205       1.3  pooka 	printf("rump_%s(", funname) > gencalls
    206       1.1  pooka 	for (i = 1; i <= narg; i++) {
    207       1.1  pooka 		printf("arg%i", i) > gencalls
    208       1.1  pooka 		if (i < narg)
    209       1.1  pooka 			printf(", ") > gencalls
    210       1.1  pooka 	}
    211       1.4  pooka 	printf(");\n\trump_unschedule();\n") > gencalls
    212       1.1  pooka 	if (!voidret)
    213       1.1  pooka 		printf("\n\treturn rv;\n") > gencalls
    214       1.1  pooka 	printf("}\n") > gencalls
    215       1.1  pooka 	if (isweak)
    216       1.3  pooka 		printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
    217       1.1  pooka 		    funname, myname) > gencalls
    218       1.1  pooka }'
    219