From 68898902911ec4c3ef23e8e6c941d4b0472a1934 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Fri, 17 Jul 2026 12:39:22 +0900 Subject: [PATCH] Avoid redefining FFI_GO_CLOSURES defined by libffi headers libffi defines FFI_GO_CLOSURES unconditionally in ffitarget.h on most targets, so pre-defining it in fiddle.h is a macro redefinition there, which MSVC reports as warning C4005 in every compilation unit, e.g. with vcpkg libffi. Restore the conditional definition of GH-134 in extconf.rb, but with macro_defined? instead of the fragile warning text matching that GH-157 removed. FFI_GO_CLOSURES=0 is only needed for old ffi.h that tests `#if FFI_GO_CLOSURES` and warns with -Wundef when the target's ffitarget.h does not define it. https://github.com/ruby/fiddle/pull/134 https://github.com/ruby/fiddle/pull/157 Co-Authored-By: Claude Fable 5 --- ext/fiddle/extconf.rb | 10 ++++++++++ ext/fiddle/fiddle.h | 1 - 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ext/fiddle/extconf.rb b/ext/fiddle/extconf.rb index f11afa2c..91c8dbb6 100644 --- a/ext/fiddle/extconf.rb +++ b/ext/fiddle/extconf.rb @@ -162,6 +162,16 @@ def enable_debug_build_flag(flags) $INCFLAGS << " -I" << libffi.include end +unless macro_defined?("FFI_GO_CLOSURES", cpp_include(ffi_header || 'ffi.h')) + # ffi.h in libffi 3.4.4 or earlier and the macOS SDK uses + # `#if FFI_GO_CLOSURES`, which warns with -Wundef on targets whose + # ffitarget.h does not define it. Newer libffi defines it in + # ffitarget.h unconditionally, where defining it on our side would be + # a macro redefinition (e.g. warning C4005 with MSVC). Fiddle does + # not use Go closures. + $defs.push('-DFFI_GO_CLOSURES=0') +end + if libffi_version # If libffi_version contains rc version, just ignored. libffi_version = libffi_version.gsub(/-rc\d+/, '') diff --git a/ext/fiddle/fiddle.h b/ext/fiddle/fiddle.h index 49cc6023..c0ab8a91 100644 --- a/ext/fiddle/fiddle.h +++ b/ext/fiddle/fiddle.h @@ -40,7 +40,6 @@ # endif #endif -#define FFI_GO_CLOSURES 0 /* fiddle does not use go closures */ #ifdef USE_HEADER_HACKS #include #else