Whamcloud - gitweb
LU-831 header: struct bit field should be unsigned type
[fs/lustre-release.git] / build / funcs.sh
1 cleanup() {
2
3     true
4 }
5
6 error() {
7     local msg="$1"
8
9     [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&$STDOUT
10
11 }
12
13 fatal() {
14
15     cleanup
16     error "$2"
17     exit $1
18
19 }
20
21 #
22 # in a given directory, find the first rpm matching given requirements
23 #
24 find_rpm() {
25     local dir="$1"
26     local match_type="$2"
27     local match="$3"
28
29     pushd "$dir" > /dev/null || \
30         fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()"
31
32     local file
33     for file in $(ls *.rpm); do
34         if [ ! -f "$file" ]; then
35             continue
36         fi
37         case "$match_type" in
38             provides)
39                 # match is any valid ERE (i.e. given to egrep) match
40                 if rpm -q --provides -p "$file" 2>&$STDOUT | egrep "$match" >&$STDOUT; then
41                     echo "$file"
42                     popd >/dev/null
43                     return 0
44                 fi
45                 ;;
46             *)
47                 popd >/dev/null
48                 fatal 1 "Unknown match type \"$match_type\" given to find_rpm()"
49                 ;;
50         esac
51     done
52
53     popd >/dev/null
54     return 1
55 }
56
57 find_linux_rpms() {
58     local prefix="$1"
59     local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
60
61     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
62     local kernel_rpms=$(find_linux_rpm "$prefix" "$pathtorpms")
63     # call a distro specific hook, if available
64     if type -p find_linux_rpms-$DISTRO; then
65         local rpm
66         if rpm=$(find_linux_rpms-$DISTRO "$prefix" "$wanted_kernel" "$pathtorpms"); then
67             kernel_rpms="$kernel_rpms $rpm"
68         else
69             return 255
70         fi
71     fi
72
73     echo "$kernel_rpms"
74     return 0
75
76 }
77
78 # a noop function which can be overridden by a distro method implementation
79 resolve_arch() {
80     local arch="$1"
81
82     echo "$arch"
83 }
84
85 # XXX this needs to be re-written as a wrapper around find_rpm
86 #     or just gotten rid of.  :-)
87 find_linux_rpm() {
88     local prefix="$1"
89     local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
90
91     local found_rpm=""
92     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
93     local ret=1
94     if [ -d "$pathtorpms" ]; then
95         local rpm
96         for rpm in $(ls ${pathtorpms}/*.$(resolve_arch $TARGET_ARCH $PATCHLESS).rpm); do
97             if rpm -q --provides -p "$rpm" 2>&$STDOUT | grep -q "kernel${prefix} = $wanted_kernel" 2>&$STDOUT; then
98                 found_rpm="$rpm"
99                 ret=0
100                 break
101             fi
102         done
103     else
104         mkdir -p "$pathtorpms"
105     fi
106     # see above "XXX"
107     #     [ -f "$found_rpm" ] && break
108     # done
109     if [ -z "$found_rpm" ]; then
110         # see if there is a distro specific way of getting the RPM
111         if type -p find_linux_rpm-$DISTRO; then
112             if found_rpm=$(find_linux_rpm-$DISTRO "$prefix" "$wanted_kernel" "$pathtorpms"); then
113                 found_rpm="${pathtorpms}/$found_rpm"
114                 ret=0
115             else
116                 ret=${PIPESTATUS[0]}
117             fi
118         fi
119     fi
120
121     echo "$found_rpm"
122     return $ret
123
124 }
125
126 # autodetect used Distro
127 autodetect_distro() {
128
129     local name
130     local version
131
132     if which lsb_release >/dev/null 2>&1; then
133         name="$(lsb_release -s -i)"
134         version="$(lsb_release -s -r)"
135         case "$name" in
136             "EnterpriseEnterpriseServer")
137                 name="oel"
138                 version="${version%%.*}"
139                 ;;
140             "RedHatEnterpriseServer" | "ScientificSL" | "CentOS")
141                 name="rhel"
142                 version="${version%%.*}"
143                 ;;
144             "SUSE LINUX")
145                 name="sles"
146                 ;;
147             *)
148                 fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument."
149                 ;;
150         esac
151     else
152         echo "You really ought to install lsb_release for accurate distro identification"
153         # try some heuristics
154         if [ -f /etc/SuSE-release ]; then
155             name=sles
156             version=$(grep ^VERSION /etc/SuSE-release)
157             version=${version#*= }
158         elif [ -f /etc/redhat-release ]; then
159             #name=$(head -1 /etc/redhat-release)
160             name=rhel
161             version=$(echo "$distroname" |
162                       sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//')
163         fi
164         if [ -z "$name" -o -z "$version" ]; then
165             fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument."
166         fi
167     fi
168
169     echo ${name}${version}
170     return 0
171
172 }
173
174 # autodetect target
175 autodetect_target() {
176     local distro="$1"
177
178     local target=""
179     case ${distro} in
180           oel5) target="2.6-oel5";;
181          rhel5) target="2.6-rhel5";;
182          rhel6) target="2.6-rhel6";;
183         sles10) target="2.6-sles10";;
184         sles11) target="2.6-sles11";;
185             *) fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";;
186     esac
187
188     echo ${target}
189     return 0
190
191 }