1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#!/usr/bin/env bash
# libnbd 'run' programs locally script
# Copyright Red Hat
#
# @configure_input@
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#----------------------------------------------------------------------
# With this script you can run libnbd programs without needing to
# install libnbd first. You just have to do for example:
#
# ./run nbdsh
#
# or:
#
# ./run /path/to/my/libnbd/program
#
# or:
#
# ./run python
# >>> import nbd # locally-compiled nbd module
#
# This works for any C program and most non-C bindings.
#
# You can also compile other projects against this uninstalled libnbd
# tree if those projects are using pkgconf/pkg-config:
#
# ../libnbd/run ./configure
# make
#----------------------------------------------------------------------
# Function to intelligently prepend a path to an environment variable.
# See http://stackoverflow.com/a/9631350
prepend()
{
eval $1="$2\${$1:+:\$$1}"
}
# Source and build directories (absolute paths so this works from any
# directory).
s="$(cd @abs_srcdir@ && pwd)"
b="$(cd @abs_builddir@ && pwd)"
# Set the PATH to contain all libnbd binaries.
prepend PATH "$b/copy"
prepend PATH "$b/dump"
prepend PATH "$b/fuse"
prepend PATH "$b/info"
prepend PATH "$b/sh"
prepend PATH "$b/ublk"
export PATH
# Set LD_LIBRARY_PATH and DYLD_LIBRARY_PATH to contain library.
# For use of _DYLD_LIBRARY_PATH see sh/nbdsh.
prepend LD_LIBRARY_PATH "$b/lib/.libs"
prepend DYLD_LIBRARY_PATH "$b/lib/.libs"
prepend _DYLD_LIBRARY_PATH "$b/lib/.libs"
export LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH
export _DYLD_LIBRARY_PATH
# For Python.
export PYTHON="@PYTHON@"
prepend PYTHONPATH "$b/python/.libs"
prepend PYTHONPATH "$b/python"
prepend PYTHONPATH "$s/python"
export PYTHONPATH
# For OCaml.
prepend CAML_LD_LIBRARY_PATH "$b/ocaml"
export CAML_LD_LIBRARY_PATH
# For golang.
export GOLANG="@GOLANG@"
if [ -z "$CGO_CFLAGS" ]; then
CGO_CFLAGS="-I$s/include -I$b"
else
CGO_CFLAGS="$CGO_CFLAGS -I$s/include -I$b"
fi
export CGO_CFLAGS
if [ -z "$CGO_LDFLAGS" ]; then
CGO_LDFLAGS="-L$b/lib/.libs"
else
CGO_LDFLAGS="$CGO_LDFLAGS -L$b/lib/.libs"
fi
export CGO_LDFLAGS
if [ -n "@GOLANG_MAJOR_VERSION@" ] &&
( ( [ "@GOLANG_MAJOR_VERSION@" -eq 1 ] &&
[ "@GOLANG_MINOR_VERSION@" -ge 21 ] ) ||
[ "@GOLANG_MAJOR_VERSION@" -ge 2 ] ); then
# cgocheck=1 "implements reasonably cheap dynamic checks"
export GODEBUG=cgocheck=1,invalidptr=1
# cgocheck2 implements complete pointer checking.
export GOEXPERIMENT=cgocheck2
else
# golang <= 1.20 used this instead:
export GODEBUG=cgocheck=2,invalidptr=1
fi
# On failure, crash (giving us a useful core dump) instead of hiding that.
export GOTRACEBACK=crash
# Allow dependent packages to be compiled against local libnbd.
prepend PKG_CONFIG_PATH "$b/lib/local"
export PKG_CONFIG_PATH
prepend OCAMLPATH "$b/ocaml"
export OCAMLPATH
# Do we have libtool? If we have it then we can use it to make
# running valgrind simpler. However don't depend on it.
if libtool --help >/dev/null 2>&1; then
libtool="libtool --mode=execute"
fi
# Use valgrind?
#
# If we're running a binary then apply valgrind directly to it.
#
# If it's a shell script then we set $VG and the script must run any
# valgrinded programs by using $VG explicitly (note this variable is
# not set if not valgrinding).
#
# Unfortunately the obvious thing of running ‘file $1’ will not work
# because of libtool, so we have to base this off the file extension.
if [ "x$LIBNBD_VALGRIND" = "x1" ]; then
_VG="valgrind --vgdb=no --leak-check=full --show-leak-kinds=all --error-exitcode=119 --suppressions=$b/valgrind/suppressions --trace-children=no --run-libc-freeres=no --num-callers=100"
case "$1" in
*.sh)
VG="$libtool $_VG"; export VG ;;
*)
valgrind="$_VG" ;;
esac
# Don't invoke malloc debugging when we are valgrinding because
# it duplicates work done by valgrind and might even hide issues.
#
# Originally this was a workaround for:
# https://sourceware.org/bugzilla/show_bug.cgi?id=28256
unset GLIBC_TUNABLES
fi
# Avoid GNOME keyring stupidity
export GNOME_KEYRING_CONTROL=
export GNOME_KEYRING_PID=
# Run the program.
exec $libtool $valgrind "$@"
|