Re: [RFC/PATCH] python: search parent lib directory for libnotmuch.so

Subject: Re: [RFC/PATCH] python: search parent lib directory for libnotmuch.so

Date: Tue, 09 Apr 2013 09:57:05 -0500

To: Justus Winter, notmuch@notmuchmail.org

Cc: Sebastian Spaeth

From: Jed Brown


Justus Winter <4winter@informatik.uni-hamburg.de> writes:
>
> May I ask why you cannot use LD_LIBRARY_PATH? I too install libnotmuch
> to a non-standard location as unprivileged user and to make this
> library available I add its path to LD_LIBRARY_PATH. 

See libdir_in_ldconfig testing in configure: we make a significant
effort to set RPATH appropriately when installing to a location that is
not already searched (perhaps via LD_LIBRARY_PATH).  This currently does
not apply to the Python bindings, so while you can install without
LD_LIBRARY_PATH and still run the notmuch executable fine, you must set
LD_LIBRARY_PATH to use the Python bindings.  That is the inconsistency I
wanted to fix here.

>> This is sort of a hack, but I don't know a less intrusive way to get
>> libnotmuch.so from somewhere dlopen(3) doesn't already search.
>> 
>> The absolute path version won't do the right thing in case of 'setup.py
>> develop', otherwise we could use it in all cases.  It may still make
>> sense to make the absolute path version take precedence.
>
> Well, if something like this is necessary and wanted (opinions
> anyone?) at least let's not hardcode the assumption about the
> directory layout but just walk up the tree until we find notmuch.so.3
> or lib/notmuch.so.3. This way the bindings will find the correct
> library even when they are included directly from within the source
> tree.

I actually wrote the more permissive version below, then decided I
preferred the stricter behavior because there was less chance of
accidentally finding a stale libnotmuch.so.3.  Note that in the source
tree, notmuch-shared already has RPATH pointing to the install location
so it's not valid without install.  The strict version of my patch has
similar behavior in that Python bindings are only valid when installed.
If you want to run them from the source tree, you'd have to add
/path/to/notmuch/lib to LD_LIBRARY_PATH.

diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py
index c7632c3..2fd383f 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -24,7 +24,16 @@ from ctypes import CDLL, Structure, POINTER
 try:
     nmlib = CDLL("libnotmuch.so.3")
 except:
-    raise ImportError("Could not find shared 'notmuch' library.")
+    import os.path
+    path = os.path.abspath(__file__)
+    while True:
+        path = os.path.dirname(path)
+        try:
+            nmlib = CDLL(os.path.join(path, 'libnotmuch.so.3'))
+            break
+        except:
+            if path == '/':
+                raise ImportError("Could not find shared 'notmuch' library.")
 
 from .compat import Python3StringMixIn, encode_utf8 as _str
 

> Otoh, adding such behavior might be 'surprising' and lead to many
> problems down the road like spurious bug reports just because the
> magic library finder locates a rogue libnotmuch.so.3 somewhere.
>
>> An alternative would be to find libnotmuch.so using the notmuch
>> executable.
>
> How would you do that? fork'ing and exec'ing is not an option (and I'm
> not sure what one could achieve by exec'ing it, but how else would you
> talk to the dynamic linker?) , and poking around in binaries to get
> their rpath isn't either in my opinion. And you would have to locate
> the 'right' binary in the first place?

I don't like the indirection either, but the binary is compiled with
knowledge of prefix/RPATH, so if we wanted a single canonical location
to specify this information, I would make it the binary.

If you don't want to trust Python install directory hierarchy, we could
have 'setup.py install' write some info about RPATH.

Thread: