Skip to content

Commit 480f7bc

Browse files
authored
Merge pull request #64 from cconlon/ffiFixes
Fix static-only wolfSSL linking, improve FFI import errors, and fix make dist
2 parents 05433e9 + 26c0151 commit 480f7bc

5 files changed

Lines changed: 59 additions & 36 deletions

File tree

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ servedocs: docs ## compile the docs watching for changes
8282
watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D .
8383

8484
dist: clean ## builds source and wheel package
85-
python setup.py sdist
86-
87-
./make/osx/build_wheels.sh
88-
89-
./make/manylinux1/build_wheels.sh
90-
85+
python setup.py sdist bdist_wheel
9186
ls -l dist
9287

9388
release: ## package and upload a release

wolfssl/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
try:
3939
from wolfssl._ffi import ffi as _ffi
4040
from wolfssl._ffi import lib as _lib
41-
except ImportError:
42-
pass
41+
except ImportError as e:
42+
from wolfssl.utils import _FFIPlaceholder
43+
_ffi = _FFIPlaceholder(e)
44+
_lib = _FFIPlaceholder(e)
4345

4446
from wolfssl.utils import t2b
4547

@@ -169,7 +171,7 @@ def __init__(self, protocol, server_side=None):
169171
self.verify_mode = CERT_NONE
170172

171173
def __del__(self):
172-
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
174+
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
173175
_lib.wolfSSL_CTX_free(self.native_object)
174176

175177
@property
@@ -474,7 +476,7 @@ def __del__(self):
474476
self._release_native_object()
475477

476478
def _release_native_object(self):
477-
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
479+
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
478480
_lib.wolfSSL_free(self.native_object)
479481
self.native_object = _ffi.NULL
480482

wolfssl/_build_ffi.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
from ctypes import cdll
3636
from collections import namedtuple
3737

38-
libwolfssl_path = ""
39-
40-
4138
def local_path(path):
4239
""" Return path relative to the root of this project
4340
"""
@@ -223,8 +220,13 @@ def build_wolfssl(ref, debug=False):
223220

224221

225222
def make_optional_func_list(libwolfssl_path, funcs):
223+
defined = []
226224
sys.stderr.write("\nlibwolfssl Path: %s\n" % libwolfssl_path)
227-
if libwolfssl_path.endswith(".so"):
225+
if not libwolfssl_path or not os.path.exists(libwolfssl_path):
226+
sys.stderr.write("WARNING: libwolfssl not found, skipping optional "
227+
"function detection\n")
228+
return []
229+
if libwolfssl_path.endswith(".so") or libwolfssl_path.endswith(".dylib"):
228230
libwolfssl = cdll.LoadLibrary(libwolfssl_path)
229231
defined = []
230232
for func in funcs:
@@ -244,16 +246,13 @@ def make_optional_func_list(libwolfssl_path, funcs):
244246
return defined
245247

246248

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
249+
def get_libwolfssl_path():
250+
lib_dir = wolfssl_lib_path()
251+
for ext in (".so", ".dylib", ".a"):
252+
path = os.path.join(lib_dir, "libwolfssl" + ext)
253+
if os.path.exists(path):
254+
return path
255+
return None
257256

258257

259258
def generate_libwolfssl():
@@ -282,6 +281,7 @@ def generate_libwolfssl():
282281
raise RuntimeError("wolfSSL needs to be compiled with "
283282
"--enable-opensslextra")
284283
featureDetection = 1
284+
libwolfssl_path = get_libwolfssl_path()
285285
sys.stderr.write("\nDEBUG: Found <wolfssl/options.h>, attempting native "
286286
"feature detection\n")
287287

@@ -290,9 +290,10 @@ def generate_libwolfssl():
290290
featureDetection = 0
291291
sys.stderr.write("\nDEBUG: Skipping native feature detection, build not "
292292
"using USE_LOCAL_WOLFSSL\n")
293-
if get_libwolfssl() == 0:
293+
libwolfssl_path = get_libwolfssl_path()
294+
if libwolfssl_path is None:
294295
generate_libwolfssl()
295-
get_libwolfssl()
296+
libwolfssl_path = get_libwolfssl_path()
296297

297298
# default values
298299
OLDTLS_ENABLED = 0
@@ -328,13 +329,23 @@ def generate_libwolfssl():
328329

329330
ffi = FFI()
330331

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-
)
332+
if libwolfssl_path and libwolfssl_path.endswith(".a"):
333+
# Static linking: pass the .a file directly via extra_objects
334+
ffi.set_source(
335+
"wolfssl._ffi",
336+
ffi_source,
337+
include_dirs=[wolfssl_inc_path()],
338+
extra_objects=[libwolfssl_path],
339+
)
340+
else:
341+
# Dynamic linking: use library_dirs + libraries
342+
ffi.set_source(
343+
"wolfssl._ffi",
344+
ffi_source,
345+
include_dirs=[wolfssl_inc_path()],
346+
library_dirs=[wolfssl_lib_path()],
347+
libraries=["wolfssl"],
348+
)
338349

339350
cdef = """
340351
/*

wolfssl/_methods.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
try:
2626
from wolfssl._ffi import lib as _lib
2727
from wolfssl._ffi import ffi as _ffi
28-
except ImportError:
29-
pass
28+
except ImportError as e:
29+
from wolfssl.utils import _FFIPlaceholder
30+
_ffi = _FFIPlaceholder(e)
31+
_lib = _FFIPlaceholder(e)
3032

3133

3234
PROTOCOL_SSLv23 = 1
@@ -111,5 +113,5 @@ def __init__(self, protocol, server_side):
111113
raise MemoryError("Cannot allocate method object")
112114

113115
def __del__(self):
114-
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
116+
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
115117
_native_free(self.native_object, _DYNAMIC_TYPE_METHOD)

wolfssl/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
_BINARY_TYPE = bytes if _PY3 else str
3131

3232

33+
class _FFIPlaceholder:
34+
def __init__(self, cause=None):
35+
object.__setattr__(self, '_cause', cause)
36+
37+
def __getattr__(self, name):
38+
raise ImportError(
39+
"wolfssl._ffi is not available. The CFFI bindings have not been "
40+
"compiled. If you installed wolfssl via pip, the build may have "
41+
"failed silently. Try reinstalling with: "
42+
"pip install --no-binary wolfssl wolfssl"
43+
) from self._cause
44+
45+
3346
def t2b(string):
3447
"""
3548
Converts text to binary.

0 commit comments

Comments
 (0)