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