OILS / testdata / osh-runtime / functions.sh View on Github | oilshell.org

251 lines, 213 significant
1# /usr/share/abuild/functions.sh
2
3sysconfdir=/etc
4program=${0##*/}
5
6arch_to_hostspec() {
7 case "$1" in
8 aarch64) echo "aarch64-alpine-linux-musl" ;;
9 armhf) echo "armv6-alpine-linux-muslgnueabihf" ;;
10 armv7) echo "armv7-alpine-linux-musleabihf" ;;
11 ppc) echo "powerpc-alpine-linux-musl" ;;
12 ppc64) echo "powerpc64-alpine-linux-musl" ;;
13 ppc64le) echo "powerpc64le-alpine-linux-musl" ;;
14 s390x) echo "s390x-alpine-linux-musl" ;;
15 x86) echo "i586-alpine-linux-musl" ;;
16 x86_64) echo "x86_64-alpine-linux-musl" ;;
17 *) echo "unknown" ;;
18 esac
19}
20
21hostspec_to_arch() {
22 case "$1" in
23 aarch64*-*-*-*) echo "aarch64" ;;
24 arm*-*-*-*eabi) echo "armel" ;;
25 armv6*-*-*-*eabihf) echo "armhf" ;;
26 armv7*-*-*-*eabihf) echo "armv7" ;;
27 i[0-9]86-*-*-*) echo "x86" ;;
28 powerpc-*-*-*) echo "ppc" ;;
29 powerpc64-*-*-*) echo "ppc64" ;;
30 powerpc64le-*-*-*) echo "ppc64le" ;;
31 s390x-*-*-*) echo "s390x" ;;
32 x86_64-*-*-*) echo "x86_64" ;;
33 *) echo "unknown" ;;
34 esac
35}
36
37hostspec_to_libc() {
38 case "$1" in
39 *-*-*-uclibc*) echo "uclibc" ;;
40 *-*-*-musl*) echo "musl" ;;
41 *-*-*-gnu*) echo "glibc" ;;
42 *) echo "unknown" ;;
43 esac
44}
45
46readconfig() {
47 local _APORTSDIR _BUILDDIR _PKGDEST _SRCPKGDEST _REPODEST _SRCDEST
48 local _CARCH _CHOST _CTARGET _CPPFLAGS _CFLAGS _CXXFLAGS _LDFLAGS
49 local _JOBS _MAKEFLAGS _PACKAGER _USE_COLORS
50 local gitbase=
51 [ -n "${APORTSDIR+x}" ] && _APORTSDIR=$APORTSDIR
52 [ -n "${BUILDDIR+x}" ] && _BUILDDIR=$BUILDDIR
53 [ -n "${PKGDEST+x}" ] && _PKGDEST=$PKGDEST
54 [ -n "${SRCPKGDEST+x}" ] && _SRCPKGDEST=$SRCPKGDEST
55 [ -n "${REPODEST+x}" ] && _REPODEST=$REPODEST
56 [ -n "${SRCDEST+x}" ] && _SRCDEST=$SRCDEST
57 [ -n "${CARCH+x}" ] && _CARCH=$CARCH
58 [ -n "${CHOST+x}" ] && _CHOST=$CHOST
59 [ -n "${CTARGET+x}" ] && _CTARGET=$CTARGET
60 [ -n "${CPPFLAGS+x}" ] && _CPPFLAGS=$CPPFLAGS
61 [ -n "${CFLAGS+x}" ] && _CFLAGS=$CFLAGS
62 [ -n "${CXXFLAGS+x}" ] && _CXXFLAGS=$CXXFLAGS
63 [ -n "${LDFLAGS+x}" ] && _LDFLAGS=$LDFLAGS
64 [ -n "${JOBS+x}" ] && _JOBS=$JOBS
65 [ -n "${MAKEFLAGS+x}" ] && _MAKEFLAGS=$MAKEFLAGS
66 [ -n "${PACKAGER+x}" ] && _PACKAGER=$PACKAGER
67 [ -n "${USE_COLORS+x}" ] && _USE_COLORS="$USE_COLORS"
68 : ${ABUILD_CONF:=$sysconfdir/abuild.conf}
69 : ${ABUILD_USERDIR:=$HOME/.abuild}
70 : ${ABUILD_USERCONF:=$ABUILD_USERDIR/abuild.conf}
71 [ -f "$ABUILD_CONF" ] && . "$ABUILD_CONF"
72 [ -f "$ABUILD_USERCONF" ] && . "$ABUILD_USERCONF"
73 APORTSDIR=${_APORTSDIR-$APORTSDIR}
74 gitbase=$(git rev-parse --show-toplevel 2>/dev/null) || true # already is -P
75 if [ -d "$APORTSDIR" ]; then
76 APORTSDIR=$(cd "$APORTSDIR"; pwd -P)
77 elif [ -z "$APORTSDIR" ] && [ -d "$HOME/aports" ]; then
78 APORTSDIR=$(cd "$HOME/aports"; pwd -P)
79 else
80 if [ -n "$gitbase" ]; then
81 case $(git config remote.origin.url) in
82 */aports) APORTSDIR=$gitbase ;;
83 *) APORTSDIR= ;;
84 esac
85 else
86 APORTSDIR=
87 fi
88 fi
89 # source any .abuild file at the aports root, but only if we are currently in aports tree
90 [ -n "$APORTSDIR" ] && [ -f "$APORTSDIR/.abuild" ] && [ "$APORTSDIR" = "$gitbase" ] && . "$APORTSDIR/.abuild"
91 BUILDDIR=${_BUILDDIR-$BUILDDIR}
92 PKGDEST=${_PKGDEST-$PKGDEST}
93 SRCPKGDEST=${_SRCPKGDEST-$SRCPKGDEST}
94 REPODEST=${_REPODEST-$REPODEST}
95 SRCDEST=${_SRCDEST-$SRCDEST}
96 CARCH=${_CARCH-$CARCH}
97 CHOST=${_CHOST-$CHOST}
98 CTARGET=${_CTARGET-$CTARGET}
99 CPPFLAGS=${_CPPFLAGS-$CPPFLAGS}
100 CFLAGS=${_CFLAGS-$CFLAGS}
101 CXXFLAGS=${_CXXFLAGS-$CXXFLAGS}
102 LDFLAGS=${_LDFLAGS-$LDFLAGS}
103 JOBS=${_JOBS-$JOBS}
104 MAKEFLAGS=${_MAKEFLAGS-$MAKEFLAGS}
105 PACKAGER=${_PACKAGER-$PACKAGER}
106 USE_COLORS=${_USE_COLORS-$USE_COLORS}
107
108 [ -z "$CBUILD" ] && CBUILD="$(gcc -dumpmachine)"
109 [ -z "$CHOST" ] && CHOST="$CBUILD"
110 [ -z "$CTARGET" ] && CTARGET="$CHOST"
111 [ "$(arch_to_hostspec $CBUILD)" != "unknown" ] && CBUILD="$(arch_to_hostspec $CBUILD)"
112 [ "$(arch_to_hostspec $CHOST)" != "unknown" ] && CHOST="$(arch_to_hostspec $CHOST)"
113 [ "$(arch_to_hostspec $CTARGET)" != "unknown" ] && CTARGET="$(arch_to_hostspec $CTARGET)"
114
115 [ -z "$CARCH" ] && CARCH="$(hostspec_to_arch $CHOST)"
116 [ -z "$CLIBC" ] && CLIBC="$(hostspec_to_libc $CHOST)"
117 [ -z "$CBUILD_ARCH" ] && CBUILD_ARCH="$(hostspec_to_arch $CBUILD)"
118 [ -z "$CTARGET_ARCH" ] && CTARGET_ARCH="$(hostspec_to_arch $CTARGET)"
119 [ -z "$CTARGET_LIBC" ] && CTARGET_LIBC="$(hostspec_to_libc $CTARGET)"
120
121 if [ "$CHOST" != "$CTARGET" ]; then
122 # setup environment for creating cross compiler
123 [ -z "$CBUILDROOT" ] && export CBUILDROOT="$HOME/sysroot-$CTARGET_ARCH/"
124 elif [ "$CBUILD" != "$CHOST" ]; then
125 # setup build root
126 [ -z "$CBUILDROOT" ] && export CBUILDROOT="$HOME/sysroot-$CTARGET_ARCH/"
127 # prepare pkg-config for cross building
128 [ -z "$PKG_CONFIG_PATH" ] && export PKG_CONFIG_PATH="${CBUILDROOT}/usr/lib/pkgconfig/"
129 [ -z "$PKG_CONFIG_SYSROOT_DIR" ] && export PKG_CONFIG_SYSROOT_DIR="${CBUILDROOT}"
130 # libtool bug workaround for extra rpaths
131 [ -z "$lt_cv_sys_lib_dlsearch_path_spec" ] && \
132 export lt_cv_sys_lib_dlsearch_path_spec="${CBUILDROOT}/lib ${CBUILDROOT}/usr/lib /usr/lib /lib /usr/local/lib"
133 # setup cross-compiler
134 if [ -z "$CROSS_COMPILE" ]; then
135 export CROSS_COMPILE="${CHOST}-"
136 export HOSTCC="$CC"
137 export HOSTCXX="$CXX"
138 export HOSTLD="$LD"
139 export HOSTCPPFLAGS="$CPPFLAGS"
140 export HOSTCXXFLAGS="$CXXFLAGS"
141 export HOSTCFLAGS="$CFLAGS"
142 export HOSTLDFLAGS="$LDFLAGS"
143 export CC=${CROSS_COMPILE}gcc
144 export CXX=${CROSS_COMPILE}g++
145 export LD=${CROSS_COMPILE}ld
146 export CPPFLAGS="--sysroot=${CBUILDROOT} $CPPFLAGS"
147 export CXXFLAGS="--sysroot=${CBUILDROOT} $CXXFLAGS"
148 export CFLAGS="--sysroot=${CBUILDROOT} $CFLAGS"
149 export LDFLAGS="--sysroot=${CBUILDROOT} $LDFLAGS"
150 fi
151 fi
152}
153readconfig
154
155# expects $1 to be a package directory in the aports tree ('foo' or 'main/foo')
156# outputs APKBUILD's path if successful
157aports_buildscript() {
158 [ -n "$APORTSDIR" ] || return 1
159 if [ "${1#*/}" != "$1" ]; then
160 ( cd "$APORTSDIR/$1" && [ -f APKBUILD ] && echo "$PWD/APKBUILD" )
161 else
162 ( cd "$APORTSDIR"/*/"$1" && [ -f APKBUILD ] && echo "$PWD/APKBUILD" )
163 fi
164}
165
166# expects $1 to be a file, or a directory containing an APKBUILD, or a package directory in the aports tree
167# outputs APKBUILD's path if successful (doesn't verify that it's a valid APKBUILD)
168any_buildscript() {
169 if [ -f "$1" ]; then
170 echo "$1"
171 elif [ -d "$1" ]; then
172 [ -f "$1/APKBUILD" ] || return 1
173 echo "$1/APKBUILD"
174 else
175 aports_buildscript "$1" || return 1
176 fi
177}
178
179# output functions
180msg() {
181 [ -n "$quiet" ] && return 0
182 local prompt="$GREEN>>>${NORMAL}"
183 printf "${prompt} %s\n" "$1" >&2
184}
185
186msg2() {
187 [ -n "$quiet" ] && return 0
188 # ">>> %s"
189 printf " %s\n" "$1" >&2
190}
191
192warning() {
193 local prompt="${YELLOW}>>> WARNING:${NORMAL}"
194 printf "${prompt} %s\n" "$1" >&2
195}
196
197warning2() {
198 # ">>> WARNING: %s\n"
199 printf " %s\n" "$1" >&2
200}
201
202error() {
203 local prompt="${RED}>>> ERROR:${NORMAL}"
204 printf "${prompt} %s\n" "$1" >&2
205}
206
207error2() {
208 # ">>> ERROR:
209 printf " %s\n" "$1" >&2
210}
211
212set_xterm_title() {
213 if [ "$TERM" = xterm ] && [ -n "$USE_COLORS" ]; then
214 printf "\033]0;$1\007" >&2
215 fi
216}
217
218disable_colors() {
219 NORMAL=""
220 STRONG=""
221 RED=""
222 GREEN=""
223 YELLOW=""
224 BLUE=""
225}
226
227enable_colors() {
228 NORMAL="\033[1;0m"
229 STRONG="\033[1;1m"
230 RED="\033[1;31m"
231 GREEN="\033[1;32m"
232 YELLOW="\033[1;33m"
233 BLUE="\033[1;34m"
234}
235
236if [ -n "$USE_COLORS" ] && [ -t 1 ]; then
237 enable_colors
238else
239 disable_colors
240fi
241
242# caller may override
243cleanup() {
244 return 0
245}
246
247die() {
248 error "$@"
249 cleanup
250 exit 1
251}