Issues building `cffi` for macOS w/ both `arm64` support and an older deployment target
Hi there, I was glad to see some work re: support for Apple Silicon on macOS! We use cffi
extensively in our app, which we deploy on macOS 10.10+. I've given the latest version of cffi
a try with Xcode 12 and we had to make a few changes to make it build cleanly.
Getting it to a state where we could do all of the following required some code changes:
- build a fat binary with
x86_64
andarm64
- link against the system
libffi
from the macOS SDK - set
MACOSX_DEPLOYMENT_TARGET=10.10
withoutunguarded-availability
warnings.
Our changes can summarized as:
+#if defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE_2019)
+
+// this version known not to have issue described above
+# if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500
+# define CFFI_TRUST_LIBFFI
+# endif
+
+// assume mac + arm64 is impossible unless 10.15+, thus trust.
+# if defined(__arm64__)
+# define CFFI_TRUST_LIBFFI
+
+# if __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
+# pragma clang diagnostic ignored "-Wunguarded-availability"
+# endif
+# endif
+
+# define HAVE_FFI_PREP_CIF_VAR
+# define HAVE_FFI_PREP_CIF_VAR_RUNTIME __builtin_available(macos 10.15, *)
+
+#endif /* defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE_2019) */
and
-#if HAVE_FFI_PREP_CIF_VAR
- if (variadic_nargs_declared >= 0) {
- status = ffi_prep_cif_var(&cif_descr->cif, fabi,
- variadic_nargs_declared, funcbuffer.nargs,
+
+#ifdef HAVE_FFI_PREP_CIF_VAR
+ if (HAVE_FFI_PREP_CIF_VAR_RUNTIME) {
+ if (variadic_nargs_declared >= 0) {
+ status = ffi_prep_cif_var(&cif_descr->cif, fabi,
+ variadic_nargs_declared, funcbuffer.nargs,
+ funcbuffer.rtype, funcbuffer.atypes);
+ } else {
+ status = ffi_prep_cif(&cif_descr->cif, fabi, funcbuffer.nargs,
funcbuffer.rtype, funcbuffer.atypes);
+ }
Not sure whether this type of change works for you, but wanted to share it here as I'm expecting others might run into this. I'd be happy to turn this into an MR, but it appears I need developer rights for this.