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