3535from ctypes import cdll
3636from collections import namedtuple
3737
38- libwolfssl_path = ""
39-
40-
4138def local_path (path ):
4239 """ Return path relative to the root of this project
4340 """
@@ -224,7 +221,11 @@ def build_wolfssl(ref, debug=False):
224221
225222def make_optional_func_list (libwolfssl_path , funcs ):
226223 sys .stderr .write ("\n libwolfssl Path: %s\n " % libwolfssl_path )
227- if libwolfssl_path .endswith (".so" ):
224+ if not libwolfssl_path or not os .path .exists (libwolfssl_path ):
225+ sys .stderr .write ("WARNING: libwolfssl not found, skipping optional "
226+ "function detection\n " )
227+ return []
228+ if libwolfssl_path .endswith (".so" ) or libwolfssl_path .endswith (".dylib" ):
228229 libwolfssl = cdll .LoadLibrary (libwolfssl_path )
229230 defined = []
230231 for func in funcs :
@@ -244,16 +245,13 @@ def make_optional_func_list(libwolfssl_path, funcs):
244245 return defined
245246
246247
247- def get_libwolfssl ():
248- libwolfssl_path = os .path .join (wolfssl_lib_path (), "libwolfssl.a" )
249- if not os .path .exists (libwolfssl_path ):
250- libwolfssl_path = os .path .join (wolfssl_lib_path (), "libwolfssl.so" )
251- if not os .path .exists (libwolfssl_path ):
252- return 0
253- else :
254- return 1
255- else :
256- return 1
248+ def get_libwolfssl_path ():
249+ lib_dir = wolfssl_lib_path ()
250+ for ext in (".so" , ".dylib" , ".a" ):
251+ path = os .path .join (lib_dir , "libwolfssl" + ext )
252+ if os .path .exists (path ):
253+ return path
254+ return None
257255
258256
259257def generate_libwolfssl ():
@@ -282,6 +280,7 @@ def generate_libwolfssl():
282280 raise RuntimeError ("wolfSSL needs to be compiled with "
283281 "--enable-opensslextra" )
284282 featureDetection = 1
283+ libwolfssl_path = get_libwolfssl_path ()
285284 sys .stderr .write ("\n DEBUG: Found <wolfssl/options.h>, attempting native "
286285 "feature detection\n " )
287286
@@ -290,9 +289,10 @@ def generate_libwolfssl():
290289 featureDetection = 0
291290 sys .stderr .write ("\n DEBUG: Skipping native feature detection, build not "
292291 "using USE_LOCAL_WOLFSSL\n " )
293- if get_libwolfssl () == 0 :
292+ libwolfssl_path = get_libwolfssl_path ()
293+ if libwolfssl_path is None :
294294 generate_libwolfssl ()
295- get_libwolfssl ()
295+ libwolfssl_path = get_libwolfssl_path ()
296296
297297# default values
298298OLDTLS_ENABLED = 0
@@ -328,13 +328,23 @@ def generate_libwolfssl():
328328
329329ffi = FFI ()
330330
331- ffi .set_source (
332- "wolfssl._ffi" ,
333- ffi_source ,
334- include_dirs = [wolfssl_inc_path ()],
335- library_dirs = [wolfssl_lib_path ()],
336- libraries = ["wolfssl" ],
337- )
331+ if libwolfssl_path and libwolfssl_path .endswith (".a" ):
332+ # Static linking: pass the .a file directly via extra_objects
333+ ffi .set_source (
334+ "wolfssl._ffi" ,
335+ ffi_source ,
336+ include_dirs = [wolfssl_inc_path ()],
337+ extra_objects = [libwolfssl_path ],
338+ )
339+ else :
340+ # Dynamic linking: use library_dirs + libraries
341+ ffi .set_source (
342+ "wolfssl._ffi" ,
343+ ffi_source ,
344+ include_dirs = [wolfssl_inc_path ()],
345+ library_dirs = [wolfssl_lib_path ()],
346+ libraries = ["wolfssl" ],
347+ )
338348
339349cdef = """
340350 /*
0 commit comments