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