Home | History | Annotate | Line # | Download | only in rump_wmd
rump_wmd.sh revision 1.4.8.2
      1  1.4.8.2  tls #!/bin/sh
      2  1.4.8.2  tls #
      3  1.4.8.2  tls #	$NetBSD: rump_wmd.sh,v 1.4.8.2 2014/08/20 00:05:03 tls Exp $
      4  1.4.8.2  tls #
      5  1.4.8.2  tls # Copyright (c) 2014 Antti Kantee <pooka (at] iki.fi>
      6  1.4.8.2  tls #
      7  1.4.8.2  tls # Redistribution and use in source and binary forms, with or without
      8  1.4.8.2  tls # modification, are permitted provided that the following conditions
      9  1.4.8.2  tls # are met:
     10  1.4.8.2  tls # 1. Redistributions of source code must retain the above copyright
     11  1.4.8.2  tls #    notice, this list of conditions and the following disclaimer.
     12  1.4.8.2  tls # 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.8.2  tls #    notice, this list of conditions and the following disclaimer in the
     14  1.4.8.2  tls #    documentation and/or other materials provided with the distribution.
     15  1.4.8.2  tls #
     16  1.4.8.2  tls # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  1.4.8.2  tls # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.4.8.2  tls # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  1.4.8.2  tls # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.4.8.2  tls # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.4.8.2  tls # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.4.8.2  tls # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.4.8.2  tls # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.4.8.2  tls # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.4.8.2  tls # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.4.8.2  tls # SUCH DAMAGE.
     27  1.4.8.2  tls #
     28  1.4.8.2  tls 
     29  1.4.8.2  tls : ${CC:=cc}
     30  1.4.8.2  tls DEBUGLEVEL=0
     31  1.4.8.2  tls LIBDIR=/usr/lib
     32  1.4.8.2  tls 
     33  1.4.8.2  tls die ()
     34  1.4.8.2  tls {
     35  1.4.8.2  tls 
     36  1.4.8.2  tls 	echo $* >&2
     37  1.4.8.2  tls 	exit 1
     38  1.4.8.2  tls }
     39  1.4.8.2  tls 
     40  1.4.8.2  tls usage ()
     41  1.4.8.2  tls {
     42  1.4.8.2  tls 
     43  1.4.8.2  tls 	die "Usage: $0 [-hv] [-L libdir] -lrump_component [...]"
     44  1.4.8.2  tls }
     45  1.4.8.2  tls 
     46  1.4.8.2  tls unset FIRSTLIB
     47  1.4.8.2  tls while getopts 'hl:L:v' opt; do
     48  1.4.8.2  tls 	case "${opt}" in
     49  1.4.8.2  tls 	l)
     50  1.4.8.2  tls 		: ${FIRSTLIB:=${OPTIND}}
     51  1.4.8.2  tls 		;;
     52  1.4.8.2  tls 	L)
     53  1.4.8.2  tls 		[ -z "${FIRSTLIB}" ] || usage
     54  1.4.8.2  tls 		LIBDIR=${OPTARG}
     55  1.4.8.2  tls 		;;
     56  1.4.8.2  tls 	v)
     57  1.4.8.2  tls 		[ -z "${FIRSTLIB}" ] || usage
     58  1.4.8.2  tls 		DEBUGLEVEL=$((DEBUGLEVEL + 1))
     59  1.4.8.2  tls 		;;
     60  1.4.8.2  tls 	-h|*)
     61  1.4.8.2  tls 		usage
     62  1.4.8.2  tls 		;;
     63  1.4.8.2  tls 	esac
     64  1.4.8.2  tls done
     65  1.4.8.2  tls [ -z "${FIRSTLIB}" ] && usage
     66  1.4.8.2  tls shift $((${FIRSTLIB} - 2))
     67  1.4.8.2  tls [ $# -eq 0 ] && usage
     68  1.4.8.2  tls 
     69  1.4.8.2  tls debug ()
     70  1.4.8.2  tls {
     71  1.4.8.2  tls 
     72  1.4.8.2  tls 	[ ${DEBUGLEVEL} -ge ${1} ] && \
     73  1.4.8.2  tls 	    { lvl=$1; shift; echo DEBUG${lvl}: $* >&2; }
     74  1.4.8.2  tls }
     75  1.4.8.2  tls 
     76  1.4.8.2  tls # filters from list
     77  1.4.8.2  tls filter ()
     78  1.4.8.2  tls {
     79  1.4.8.2  tls 
     80  1.4.8.2  tls 	filtee=$1
     81  1.4.8.2  tls 	vname=$2
     82  1.4.8.2  tls 	tmplist=''
     83  1.4.8.2  tls 	found=false
     84  1.4.8.2  tls 	for x in $(eval echo \${${vname}}); do
     85  1.4.8.2  tls 		if [ "${filtee}" = "${x}" ]; then
     86  1.4.8.2  tls 			found=true
     87  1.4.8.2  tls 		else
     88  1.4.8.2  tls 			tmplist="${tmplist} ${x}"
     89  1.4.8.2  tls 		fi
     90  1.4.8.2  tls 	done
     91  1.4.8.2  tls 	${found} || die \"${1}\" not found in \$${2}
     92  1.4.8.2  tls 
     93  1.4.8.2  tls 	eval ${vname}="\${tmplist}"
     94  1.4.8.2  tls }
     95  1.4.8.2  tls 
     96  1.4.8.2  tls SEEDPROG='int rump_init(void); int main() { rump_init(); }'
     97  1.4.8.2  tls CCPARAMS='-Wl,--no-as-needed -o /dev/null -x c -'
     98  1.4.8.2  tls 
     99  1.4.8.2  tls # sanity-check
    100  1.4.8.2  tls for lib in $* ; do
    101  1.4.8.2  tls 	[ "${lib#-l}" = "${lib}" -o -z "${lib#-l}" ] \
    102  1.4.8.2  tls 	    && die Param \"${lib}\" is not of format -llib
    103  1.4.8.2  tls done
    104  1.4.8.2  tls 
    105  1.4.8.2  tls # starting set and available components
    106  1.4.8.2  tls WANTEDCOMP="$*"
    107  1.4.8.2  tls RUMPBASE='-lrump -lrumpuser'
    108  1.4.8.2  tls CURCOMP="${WANTEDCOMP}"
    109  1.4.8.2  tls NEEDEDCOMP=''
    110  1.4.8.2  tls ALLCOMP=$(ls ${LIBDIR} 2>/dev/null \
    111  1.4.8.2  tls     | sed -n '/^librump.*.so$/{s/lib/-l/;s/\.so//p;}')
    112  1.4.8.2  tls [ -z "${ALLCOMP}" ] && die No rump kernel components in \"${LIBDIR}\"
    113  1.4.8.2  tls 
    114  1.4.8.2  tls # filter out ones we'll definitely not use
    115  1.4.8.2  tls for f in ${CURCOMP} -lrumphijack -lrumpclient; do
    116  1.4.8.2  tls 	filter ${f} ALLCOMP
    117  1.4.8.2  tls done
    118  1.4.8.2  tls 
    119  1.4.8.2  tls # put the factions first so that they'll be tried first.
    120  1.4.8.2  tls # this is an optimization to minimize link attempts.
    121  1.4.8.2  tls FACTIONS='-lrumpvfs -lrumpnet -lrumpdev'
    122  1.4.8.2  tls for f in ${FACTIONS}; do
    123  1.4.8.2  tls 	filter ${f} ALLCOMP
    124  1.4.8.2  tls done
    125  1.4.8.2  tls ALLCOMP="${FACTIONS} ${ALLCOMP}"
    126  1.4.8.2  tls 
    127  1.4.8.2  tls debug 0 Searching component combinations.  This may take a while ...
    128  1.4.8.2  tls while :; do
    129  1.4.8.2  tls 	debug 1 Current components: ${CURCOMP}
    130  1.4.8.2  tls 
    131  1.4.8.2  tls 	IFS=' '
    132  1.4.8.2  tls 	out=$(echo ${SEEDPROG} \
    133  1.4.8.2  tls 	    | ${CC} -L${LIBDIR} ${CCPARAMS} ${CURCOMP} ${RUMPBASE} 2>&1)
    134  1.4.8.2  tls 	[ -z "${out}" ] && break
    135  1.4.8.2  tls 	undef=$(echo ${out} \
    136  1.4.8.2  tls 	    | sed -n '/undefined reference to/{s/.*`//;s/.$//p;q;}')
    137  1.4.8.2  tls 	[ -z "${undef}" ] && break
    138  1.4.8.2  tls 
    139  1.4.8.2  tls 	debug 1 Trying to find ${undef}
    140  1.4.8.2  tls 	for attempt in ${ALLCOMP}; do
    141  1.4.8.2  tls 		debug 2 Component attempt: ${attempt}
    142  1.4.8.2  tls 		unset IFS
    143  1.4.8.2  tls 		nowundef=$(echo ${SEEDPROG}				\
    144  1.4.8.2  tls 		    | ${CC} -L${LIBDIR} ${CCPARAMS}			\
    145  1.4.8.2  tls 		      ${attempt} ${CURCOMP} ${RUMPBASE} 2>&1		\
    146  1.4.8.2  tls 		    | sed -n '/undefined reference to/{s/.*`//;s/.$//p;}')
    147  1.4.8.2  tls 		for one in ${nowundef}; do
    148  1.4.8.2  tls 			[ "${one}" = "${undef}" ] && continue 2
    149  1.4.8.2  tls 		done
    150  1.4.8.2  tls 		CURCOMP="${attempt} ${CURCOMP}"
    151  1.4.8.2  tls 		filter ${attempt} ALLCOMP
    152  1.4.8.2  tls 		debug 1 Found ${undef} from ${attempt}
    153  1.4.8.2  tls 		continue 2
    154  1.4.8.2  tls 	done
    155  1.4.8.2  tls 	die Internal error: unreachable statement
    156  1.4.8.2  tls done
    157  1.4.8.2  tls 
    158  1.4.8.2  tls [ ! -z "${out}" ] && { echo 'ERROR:' >&2 ; echo ${out} >&2 ; exit 1 ; }
    159  1.4.8.2  tls debug 0 Found a set
    160  1.4.8.2  tls echo ${CURCOMP}
    161  1.4.8.2  tls exit 0
    162