@@ -24,7 +24,7 @@ class OpenLDAP2:
24
24
extra_compile_args = []
25
25
extra_link_args = []
26
26
extra_objects = []
27
- libs = [ 'ldap' , 'lber' ]
27
+ libs = None
28
28
defines = []
29
29
extra_files = []
30
30
@@ -38,6 +38,49 @@ class OpenLDAP2:
38
38
if cfg .has_option ('_ldap' , name ):
39
39
setattr (LDAP_CLASS , name , cfg .get ('_ldap' , name ).split ())
40
40
41
+ if LDAP_CLASS .libs is None :
42
+ # libldap(_r) not selected by setup.cfg, turn to auto-detection
43
+ from ctypes import cdll , c_void_p , c_char_p , c_int , byref , Structure
44
+ import ctypes .util
45
+
46
+ NULL = c_void_p ()
47
+ LDAP_OPT_API_FEATURE_INFO = 0x15
48
+ class ldap_apifeature_info (Structure ):
49
+ _fields_ = [
50
+ ("ldapaif_info_version" , c_int ),
51
+ ("ldapaif_name" , c_char_p ),
52
+ ("ldapaif_version" , c_int )
53
+ ]
54
+
55
+ for name in ('ldap' , 'ldap_r' ):
56
+ ldapname = ctypes .util .find_library (name )
57
+ if not ldapname :
58
+ continue
59
+
60
+ try :
61
+ libldap = cdll .LoadLibrary (ldapname )
62
+ except OSError :
63
+ # Cannot load this version of libldap
64
+ continue
65
+
66
+ info = ldap_apifeature_info (1 , b"X_OPENLDAP_THREAD_SAFE" )
67
+
68
+ if libldap .ldap_get_option (NULL , LDAP_OPT_API_FEATURE_INFO , byref (info )):
69
+ # Does not support THREADSAFE
70
+ continue
71
+
72
+ if info .ldapaif_version != 1 :
73
+ # Does not support THREADSAFE
74
+ continue
75
+
76
+ LDAP_CLASS .libs = [name , 'lber' ]
77
+ if 'HAVE_LIBLDAP_R' not in LDAP_CLASS .defines :
78
+ LDAP_CLASS .defines .append ('HAVE_LIBLDAP_R' )
79
+ break
80
+ else :
81
+ # At least try
82
+ LDAP_CLASS .libs = ['ldap' , 'lber' ]
83
+
41
84
for i in range (len (LDAP_CLASS .defines )):
42
85
LDAP_CLASS .defines [i ]= ((LDAP_CLASS .defines [i ],None ))
43
86
@@ -132,7 +175,6 @@ class OpenLDAP2:
132
175
extra_objects = LDAP_CLASS .extra_objects ,
133
176
runtime_library_dirs = (not sys .platform .startswith ("win" ))* LDAP_CLASS .library_dirs ,
134
177
define_macros = LDAP_CLASS .defines + \
135
- ('ldap_r' in LDAP_CLASS .libs or 'oldap_r' in LDAP_CLASS .libs )* [('HAVE_LIBLDAP_R' ,None )] + \
136
178
('sasl' in LDAP_CLASS .libs or 'sasl2' in LDAP_CLASS .libs or 'libsasl' in LDAP_CLASS .libs )* [('HAVE_SASL' ,None )] + \
137
179
('ssl' in LDAP_CLASS .libs and 'crypto' in LDAP_CLASS .libs )* [('HAVE_TLS' ,None )] + \
138
180
[
0 commit comments