Home | History | Annotate | Line # | Download | only in librump
makerumpif.sh revision 1.4.6.1
      1      1.1  pooka #!/bin/sh
      2      1.1  pooka #
      3  1.4.6.1  rmind #	$NetBSD: makerumpif.sh,v 1.4.6.1 2011/03/05 20:56:14 rmind 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.1  pooka [ $# != 1 ] && usage
     51      1.1  pooka 
     52      1.1  pooka MYDIR=`pwd`
     53      1.1  pooka while [ ! -f Makefile.rump  ]; do
     54      1.1  pooka 	[ `pwd` = '/' ] && boom Could not find rump topdir.
     55      1.1  pooka 	cd ..
     56      1.1  pooka done
     57      1.1  pooka RUMPTOP="`pwd`"
     58      1.1  pooka cd ${MYDIR}
     59      1.1  pooka 
     60      1.1  pooka sed -e '
     61      1.1  pooka :again
     62      1.1  pooka 	/\\$/{
     63      1.1  pooka 		N
     64      1.1  pooka 		s/[ 	]*\\\n[ 	]*/ /
     65      1.1  pooka 		b again
     66      1.1  pooka 	}
     67      1.1  pooka ' ${1} | awk -F\| -v rumptop=${RUMPTOP} '
     68      1.1  pooka function fileheaders(file, srcstr)
     69      1.1  pooka {
     70  1.4.6.1  rmind 	printf("/*\t$NetBSD: makerumpif.sh,v 1.4.6.1 2011/03/05 20:56:14 rmind Exp $\t*/\n\n") > file
     71      1.1  pooka 	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
     72  1.4.6.1  rmind 	genstr = "$NetBSD: makerumpif.sh,v 1.4.6.1 2011/03/05 20:56:14 rmind Exp $"
     73      1.1  pooka 	gsub("\\$", "", genstr)
     74      1.1  pooka 	printf(" * from: %s\n", srcstr) > file
     75      1.1  pooka 	printf(" * by:   %s\n", genstr) > file
     76      1.1  pooka 	printf(" */\n") > file
     77      1.1  pooka }
     78      1.1  pooka 
     79      1.1  pooka function die(str)
     80      1.1  pooka {
     81      1.1  pooka 
     82      1.1  pooka 	print str
     83      1.1  pooka 	exit(1)
     84      1.1  pooka }
     85      1.1  pooka 
     86      1.1  pooka NR == 1 {
     87      1.1  pooka 	sub(";[^\\$]*\\$", "")
     88      1.1  pooka 	sub("\\$", "")
     89      1.1  pooka 	fromstr = $0
     90      1.1  pooka 	next
     91      1.1  pooka }
     92      1.1  pooka 
     93      1.1  pooka $1 == "NAME"{myname = $2;next}
     94  1.4.6.1  rmind $1 == "PUBHDR"{pubhdr = rumptop "/" $2;print pubhdr;next}
     95  1.4.6.1  rmind $1 == "PRIVHDR"{privhdr = rumptop "/" $2;print privhdr;next}
     96  1.4.6.1  rmind $1 == "WRAPPERS"{gencalls = rumptop "/" $2;print gencalls;next}
     97      1.1  pooka 
     98      1.1  pooka /^;/{next}
     99      1.1  pooka /\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
    100      1.1  pooka /^[ \t]*$/{next}
    101      1.1  pooka {
    102      1.1  pooka 	if (NF != 3 && NF != 4) {
    103      1.1  pooka 		die("error: unexpected number of fields\n")
    104      1.1  pooka 	}
    105      1.1  pooka 	if (NF == 4) {
    106      1.1  pooka 		if ($4 == "WEAK")
    107      1.1  pooka 			isweak = 1
    108      1.1  pooka 		else
    109      1.1  pooka 			die("error: unexpected fourth field");
    110      1.1  pooka 	} else {
    111      1.1  pooka 		isweak = 0
    112      1.1  pooka 	}
    113      1.1  pooka 	if (!myname)
    114      1.1  pooka 		die("name not specified");
    115      1.1  pooka 	if (!pubhdr)
    116      1.1  pooka 		die("public header not specified");
    117      1.1  pooka 	if (!privhdr)
    118      1.1  pooka 		die("private header not specified");
    119      1.1  pooka 	if (!gencalls)
    120      1.1  pooka 		die("wrapper file not specified");
    121      1.1  pooka 
    122      1.1  pooka 	if (!once) {
    123      1.1  pooka 		fileheaders(pubhdr, fromstr)
    124      1.1  pooka 		fileheaders(privhdr, fromstr)
    125      1.1  pooka 		fileheaders(gencalls, fromstr)
    126      1.1  pooka 		once = 1
    127      1.1  pooka 
    128      1.1  pooka 		pubfile = pubhdr
    129      1.1  pooka 		sub(".*/", "", pubfile)
    130      1.1  pooka 
    131      1.1  pooka 		privfile = privhdr
    132      1.1  pooka 		sub(".*/", "", privfile)
    133      1.1  pooka 
    134      1.1  pooka 		printf("\n") > pubhdr
    135      1.1  pooka 		printf("\n") > privhdr
    136      1.1  pooka 
    137      1.1  pooka 		printf("\n#include <sys/cdefs.h>\n") > gencalls
    138      1.1  pooka 		printf("#include <sys/systm.h>\n") > gencalls
    139      1.1  pooka 		printf("\n#include <rump/rump.h>\n") > gencalls
    140      1.1  pooka 		printf("#include <rump/%s>\n\n", pubfile) > gencalls
    141      1.4  pooka 		printf("#include \"rump_private.h\"\n", privfile) > gencalls
    142      1.1  pooka 		printf("#include \"%s\"\n\n", privfile) > gencalls
    143      1.2  pooka 		printf("void __dead rump_%s_unavailable(void);\n",	\
    144      1.1  pooka 		    myname) > gencalls
    145      1.1  pooka 		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
    146      1.1  pooka 		    myname) > gencalls
    147      1.1  pooka 		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
    148      1.1  pooka 		    myname) > gencalls
    149      1.1  pooka 	}
    150      1.1  pooka 
    151      1.1  pooka 	funtype = $1
    152      1.1  pooka 	sub("[ \t]*$", "", funtype)
    153      1.1  pooka 	funname = $2
    154      1.1  pooka 	sub("[ \t]*$", "", funname)
    155      1.1  pooka 	funargs = $3
    156      1.2  pooka 	sub("[ \t]*$", "", funargs)
    157      1.2  pooka 
    158      1.3  pooka 	printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
    159      1.3  pooka 	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
    160      1.1  pooka 
    161      1.1  pooka 	if (funtype == "void")
    162      1.1  pooka 		voidret = 1
    163      1.1  pooka 	else
    164      1.1  pooka 		voidret = 0
    165      1.1  pooka 	if (funargs == "void")
    166      1.1  pooka 		voidarg = 1
    167      1.1  pooka 	else
    168      1.1  pooka 		voidarg = 0
    169      1.1  pooka 
    170      1.3  pooka 	printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls
    171      1.1  pooka 	if (!voidarg) {
    172      1.1  pooka 		narg = split(funargs, argv, ",")
    173      1.1  pooka 		for (i = 1; i <= narg; i++) {
    174      1.1  pooka 			sub(" *", "", argv[i])
    175      1.1  pooka 			if (match(argv[i], "\\*$") != 0)
    176      1.1  pooka 				printf("%sarg%d", argv[i], i) > gencalls
    177      1.1  pooka 			else
    178      1.1  pooka 				printf("%s arg%d", argv[i], i) > gencalls
    179      1.1  pooka 			if (i != narg)
    180      1.1  pooka 				printf(", ") > gencalls
    181      1.1  pooka 		}
    182      1.1  pooka 	} else {
    183      1.1  pooka 		narg = 0
    184      1.1  pooka 		printf("void") > gencalls
    185      1.1  pooka 	}
    186      1.1  pooka 	printf(")\n{\n") > gencalls
    187      1.1  pooka 
    188      1.1  pooka 	if (!voidret) {
    189      1.2  pooka 		printf("\t%s rv;\n", funtype) > gencalls
    190      1.1  pooka 	}
    191      1.4  pooka 	printf("\n\trump_schedule();\n\t") > gencalls
    192      1.1  pooka 	if (!voidret)
    193      1.1  pooka 		printf("rv = ") > gencalls
    194      1.3  pooka 	printf("rump_%s(", funname) > gencalls
    195      1.1  pooka 	for (i = 1; i <= narg; i++) {
    196      1.1  pooka 		printf("arg%i", i) > gencalls
    197      1.1  pooka 		if (i < narg)
    198      1.1  pooka 			printf(", ") > gencalls
    199      1.1  pooka 	}
    200      1.4  pooka 	printf(");\n\trump_unschedule();\n") > gencalls
    201      1.1  pooka 	if (!voidret)
    202      1.1  pooka 		printf("\n\treturn rv;\n") > gencalls
    203      1.1  pooka 	printf("}\n") > gencalls
    204      1.1  pooka 	if (isweak)
    205      1.3  pooka 		printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
    206      1.1  pooka 		    funname, myname) > gencalls
    207      1.1  pooka }'
    208