Richard W.M. Jones
2015-Feb-13 10:16 UTC
[Libguestfs] [PATCH] ./run: Use 'prepend' function to build paths.
Add a bash function 'prepend' for intelligently prepending elements to paths. eg: prepend PYTHONPATH "/foo" would set PYTHONPATH to "/foo" or "/foo:<previous-contents-of-PYTHONPATH>" Tested by: (1) Building and testing libguestfs twice: first without libguestfs installed as a system library, and then with it installed. (2) Examining the output of './run printenv' by hand and comparing environment variables to the expected values. --- run.in | 103 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/run.in b/run.in index bf7ea1b..46b47c3 100755 --- a/run.in +++ b/run.in @@ -43,6 +43,13 @@ if [ "$1" = "--test" ]; then shift fi +# 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)" @@ -74,33 +81,55 @@ fi # Set the PATH to contain all the libguestfs binaries. There are a # lot of binaries, so a lot of path entries. -PATH="$b/align:$b/builder:$b/cat:$b/customize:$b/df:$b/diff:$b/edit:$b/erlang:$b/fish:$b/format:$b/fuse:$b/inspector:$b/make-fs:$b/p2v:$b/rescue:$b/resize:$b/sparsify:$b/sysprep:$b/test-tool:$b/tools:$b/v2v:$PATH" +prepend PATH "$b/align" +prepend PATH "$b/builder" +prepend PATH "$b/cat" +prepend PATH "$b/customize" +prepend PATH "$b/df" +prepend PATH "$b/diff" +prepend PATH "$b/edit" +prepend PATH "$b/erlang" +prepend PATH "$b/fish" +prepend PATH "$b/format" +prepend PATH "$b/fuse" +prepend PATH "$b/inspector" +prepend PATH "$b/make-fs" +prepend PATH "$b/p2v" +prepend PATH "$b/rescue" +prepend PATH "$b/resize" +prepend PATH "$b/sparsify" +prepend PATH "$b/sysprep" +prepend PATH "$b/test-tool" +prepend PATH "$b/tools" +prepend PATH "$b/v2v" export PATH # Set LD_LIBRARY_PATH and DYLD_LIBRARY_PATH to contain library. -build_libs_path="$b/src/.libs:$b/java/.libs:$b/gobject/.libs" -export LD_LIBRARY_PATH="$build_libs_path${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH" -export DYLD_LIBRARY_PATH="$build_libs_path${DYLD_LIBRARY_PATH:+:}$DYLD_LIBRARY_PATH" +prepend LD_LIBRARY_PATH "$b/gobject/.libs" +prepend LD_LIBRARY_PATH "$b/java/.libs" +prepend LD_LIBRARY_PATH "$b/src/.libs" +prepend DYLD_LIBRARY_PATH "$b/gobject/.libs" +prepend DYLD_LIBRARY_PATH "$b/java/.libs" +prepend DYLD_LIBRARY_PATH "$b/src/.libs" +export LD_LIBRARY_PATH +export DYLD_LIBRARY_PATH # Make virt-builder use the local website copy to avoid hitting # the network all the time. if [ -z "$XDG_CONFIG_DIRS" ]; then - XDG_CONFIG_DIRS="$b/builder/test-website" + prepend XDG_CONFIG_DIRS "$b/builder/test-website" export XDG_CONFIG_DIRS fi # virt-p2v-make-* data directory. if [ -z "$VIRT_P2V_DATA_DIR" ]; then - VIRT_P2V_DATA_DIR="$b/p2v" + prepend VIRT_P2V_DATA_DIR "$b/p2v" export VIRT_P2V_DATA_DIR fi # For Perl. -if [ -z "$PERL5LIB" ]; then - PERL5LIB="$b/perl/blib/lib:$b/perl/blib/arch" -else - PERL5LIB="$b/perl/blib/lib:$b/perl/blib/arch:$PERL5LIB" -fi +prepend PERL5LIB "$b/perl/blib/arch" +prepend PERL5LIB "$b/perl/blib/lib" export PERL5LIB # Enable Perl valgrinding. @@ -112,52 +141,38 @@ export PERL_DESTRUCT_LEVEL=2 # For Python. export PYTHON=@PYTHON@ -if [ -z "$PYTHONPATH" ]; then - PYTHONPATH="$s/python:$b/python:$b/python/.libs" -else - PYTHONPATH="$s/python:$b/python:$b/python/.libs:$PYTHONPATH" -fi +prepend PYTHONPATH "$b/python/.libs" +prepend PYTHONPATH "$b/python" +prepend PYTHONPATH "$s/python" export PYTHONPATH # For Ruby. export RUBY=@RUBY@ export RAKE=@RAKE@ -if [ -z "$RUBYLIB" ]; then - RUBYLIB="$b/ruby/lib:$b/ruby/ext/guestfs" -else - RUBYLIB="$b/ruby/lib:$b/ruby/ext/guestfs:$RUBYLIB" -fi +prepend RUBYLIB "$b/ruby/ext/guestfs" +prepend RUBYLIB "$b/ruby/lib" export RUBYLIB -export LD_LIBRARY_PATH="$b/ruby/ext/guestfs${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH" -export DYLD_LIBRARY_PATH="$b/ruby/ext/guestfs${DYLD_LIBRARY_PATH:+:}$DYLD_LIBRARY_PATH" +prepend LD_LIBRARY_PATH "$b/ruby/ext/guestfs" +prepend DYLD_LIBRARY_PATH "$b/ruby/ext/guestfs" # For OCaml. -if [ -z "$CAML_LD_LIBRARY_PATH" ]; then - CAML_LD_LIBRARY_PATH="$b/ocaml" -else - CAML_LD_LIBRARY_PATH="$b/ocaml:$CAML_LD_LIBRARY_PATH" -fi +prepend CAML_LD_LIBRARY_PATH "$b/ocaml" export CAML_LD_LIBRARY_PATH # For Java. export JAVA_EXE=@JAVA_EXE@ -if [ -z "$CLASSPATH" ]; then - CLASSPATH="$b/java:$b/java/t:$b/java/libguestfs-@VERSION@.jar" -else - CLASSPATH="$b/java:$b/java/t:$b/java/libguestfs-@VERSION@.jar:$CLASSPATH" -fi +prepend CLASSPATH "$b/java/libguestfs-@VERSION@.jar" +prepend CLASSPATH "$b/java/t" +prepend CLASSPATH "$b/java" export CLASSPATH # For Erlang (note $PATH is adjusted above to include erl-guestfs). -if [ -z "$ERL_LIBS" ]; then - ERL_LIBS="$b/erlang" -else - ERL_LIBS="$b/erlang:$ERL_LIBS" -fi +prepend ERL_LIBS "$b/erlang" export ERL_LIBS # For Lua. export LUA=@LUA@ +# Can't use 'prepend' here because Lua paths use semicolons. if [ -z "$LUA_CPATH" ]; then LUA_CPATH="$b/lua/?.so" else @@ -167,11 +182,7 @@ export LUA_CPATH # For golang. export GOLANG=@GOLANG@ -if [ -z "$GOPATH" ]; then - GOPATH="$b/golang" -else - GOPATH="$b/golang:$GOPATH" -fi +prepend GOPATH "$b/golang" export GOPATH if [ -z "$CGO_CFLAGS" ]; then CGO_CFLAGS="-I$s/src" @@ -188,11 +199,7 @@ export CGO_LDFLAGS # For GObject, Javascript and friends. export GJS=@GJS@ -if [ -z "$GI_TYPELIB_PATH" ]; then - GI_TYPELIB_PATH="$b/gobject" -else - GI_TYPELIB_PATH="$b/gobject:$GI_TYPELIB_PATH" -fi +prepend GI_TYPELIB_PATH "$b/gobject" export GI_TYPELIB_PATH # Be friendly to valgrind (https://live.gnome.org/Valgrind) export G_SLICE=always-malloc -- 2.1.0
Pino Toscano
2015-Feb-13 10:35 UTC
Re: [Libguestfs] [PATCH] ./run: Use 'prepend' function to build paths.
On Friday 13 February 2015 10:16:34 Richard W.M. Jones wrote:> Add a bash function 'prepend' for intelligently prepending elements to > paths. eg: > > prepend PYTHONPATH "/foo" > > would set PYTHONPATH to "/foo" or "/foo:<previous-contents-of-PYTHONPATH>" > > Tested by: > > (1) Building and testing libguestfs twice: first without libguestfs > installed as a system library, and then with it installed. > > (2) Examining the output of './run printenv' by hand and comparing > environment variables to the expected values. > ---Nice idea, thanks for it.> # Set LD_LIBRARY_PATH and DYLD_LIBRARY_PATH to contain library. > -build_libs_path="$b/src/.libs:$b/java/.libs:$b/gobject/.libs" > -export LD_LIBRARY_PATH="$build_libs_path${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH" > -export DYLD_LIBRARY_PATH="$build_libs_path${DYLD_LIBRARY_PATH:+:}$DYLD_LIBRARY_PATH" > +prepend LD_LIBRARY_PATH "$b/gobject/.libs" > +prepend LD_LIBRARY_PATH "$b/java/.libs" > +prepend LD_LIBRARY_PATH "$b/src/.libs" > +prepend DYLD_LIBRARY_PATH "$b/gobject/.libs" > +prepend DYLD_LIBRARY_PATH "$b/java/.libs" > +prepend DYLD_LIBRARY_PATH "$b/src/.libs" > +export LD_LIBRARY_PATH > +export DYLD_LIBRARY_PATHI'd still leave the build_libs_path variable, so we don't duplicate its content twice.> # virt-p2v-make-* data directory. > if [ -z "$VIRT_P2V_DATA_DIR" ]; then > - VIRT_P2V_DATA_DIR="$b/p2v" > + prepend VIRT_P2V_DATA_DIR "$b/p2v" > export VIRT_P2V_DATA_DIR > fiIs prepend needed here? After all, it is just a single directory.> # For Lua. > export LUA=@LUA@ > +# Can't use 'prepend' here because Lua paths use semicolons. > if [ -z "$LUA_CPATH" ]; then > LUA_CPATH="$b/lua/?.so" > elseI guess using a second helper like: prepend_semicolon() { eval $1="$2\${$1:+;\$$1}" } could help? -- Pino Toscano
Richard W.M. Jones
2015-Feb-13 10:42 UTC
Re: [Libguestfs] [PATCH] ./run: Use 'prepend' function to build paths.
On Fri, Feb 13, 2015 at 11:35:15AM +0100, Pino Toscano wrote:> On Friday 13 February 2015 10:16:34 Richard W.M. Jones wrote: > > Add a bash function 'prepend' for intelligently prepending elements to > > paths. eg: > > > > prepend PYTHONPATH "/foo" > > > > would set PYTHONPATH to "/foo" or "/foo:<previous-contents-of-PYTHONPATH>" > > > > Tested by: > > > > (1) Building and testing libguestfs twice: first without libguestfs > > installed as a system library, and then with it installed. > > > > (2) Examining the output of './run printenv' by hand and comparing > > environment variables to the expected values. > > --- > > Nice idea, thanks for it. > > > # Set LD_LIBRARY_PATH and DYLD_LIBRARY_PATH to contain library. > > -build_libs_path="$b/src/.libs:$b/java/.libs:$b/gobject/.libs" > > -export LD_LIBRARY_PATH="$build_libs_path${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH" > > -export DYLD_LIBRARY_PATH="$build_libs_path${DYLD_LIBRARY_PATH:+:}$DYLD_LIBRARY_PATH" > > +prepend LD_LIBRARY_PATH "$b/gobject/.libs" > > +prepend LD_LIBRARY_PATH "$b/java/.libs" > > +prepend LD_LIBRARY_PATH "$b/src/.libs" > > +prepend DYLD_LIBRARY_PATH "$b/gobject/.libs" > > +prepend DYLD_LIBRARY_PATH "$b/java/.libs" > > +prepend DYLD_LIBRARY_PATH "$b/src/.libs" > > +export LD_LIBRARY_PATH > > +export DYLD_LIBRARY_PATH > > I'd still leave the build_libs_path variable, so we don't duplicate > its content twice.I considered that, but think this way is clearer, even if there's some minimal duplication.> > # virt-p2v-make-* data directory. > > if [ -z "$VIRT_P2V_DATA_DIR" ]; then > > - VIRT_P2V_DATA_DIR="$b/p2v" > > + prepend VIRT_P2V_DATA_DIR "$b/p2v" > > export VIRT_P2V_DATA_DIR > > fi > > Is prepend needed here? After all, it is just a single directory.No it's not -- I removed this hunk.> > # For Lua. > > export LUA=@LUA@ > > +# Can't use 'prepend' here because Lua paths use semicolons. > > if [ -z "$LUA_CPATH" ]; then > > LUA_CPATH="$b/lua/?.so" > > else > > I guess using a second helper like: > > prepend_semicolon() > { > eval $1="$2\${$1:+;\$$1}" > } > > could help?Actually what's needed is a more general function which could use a generic separator and also handle appending, thus coping with: if [ -z "$CGO_CFLAGS" ]; then CGO_CFLAGS="-I$s/src" else CGO_CFLAGS="$CGO_CFLAGS -I$s/src" fi Anyway, I have pushed this for now, without the P2V data dir hunk. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Maybe Matching Threads
- [PATCH] run.in: Quote contents of @VAR@ substitutions
- Re: [PATCH] ./run: Use 'prepend' function to build paths.
- [PATCH 1/2] run: Set DYLD_LIBRARY_PATH along with LD_LIBRARY_PATH
- [PATCH libnbd] Add outline framework for Go language bindings (golang).
- [PATCH libnbd v2 2/3] Add outline framework for Go language bindings (golang).