Skip to content

Commit d389bbf

Browse files
hsbtclaude
andauthored
Avoid redefining FFI_GO_CLOSURES defined by libffi headers (#206)
Building fiddle with MSVC against libffi whose `ffitarget.h` defines `FFI_GO_CLOSURES` unconditionally, for example vcpkg libffi 3.5.2 in a ruby/ruby mswin build, reports `warning C4005: 'FFI_GO_CLOSURES': macro redefinition` in every compilation unit. The pre-definition in `fiddle.h` added by #157 collides with libffi's own definition. GCC and clang hide the same redefinition because it happens in a system header there. The pre-definition only exists to silence `-Wundef` warnings from old `ffi.h` that tests `#if FFI_GO_CLOSURES` without the target defining it, and libffi switched that test to `#ifdef` in 3.4.5 (libffi/libffi#796). This restores the conditional approach of #134, but detects whether the libffi headers define the macro with `macro_defined?` at `extconf.rb` time instead of matching compiler-specific warning text, and defines `FFI_GO_CLOSURES=0` only when they do not. Fiddle itself does not use Go closures, so the macro only affects which declarations `ffi.h` exposes. I verified on Windows with MSVC that the eight C4005 warnings disappear with vcpkg libffi 3.5.2, and that a simulated old libffi header set with no `FFI_GO_CLOSURES` definition and an `#if FFI_GO_CLOSURES` test still gets `-DFFI_GO_CLOSURES=0` from `extconf.rb`. `rake test` passes on `x64-mswin64_140`. Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 865d764 commit d389bbf

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

ext/fiddle/extconf.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ def enable_debug_build_flag(flags)
162162
$INCFLAGS << " -I" << libffi.include
163163
end
164164

165+
unless macro_defined?("FFI_GO_CLOSURES", cpp_include(ffi_header || 'ffi.h'))
166+
# ffi.h in libffi 3.4.4 or earlier and the macOS SDK uses
167+
# `#if FFI_GO_CLOSURES`, which warns with -Wundef on targets whose
168+
# ffitarget.h does not define it. Newer libffi defines it in
169+
# ffitarget.h unconditionally, where defining it on our side would be
170+
# a macro redefinition (e.g. warning C4005 with MSVC). Fiddle does
171+
# not use Go closures.
172+
$defs.push('-DFFI_GO_CLOSURES=0')
173+
end
174+
165175
if libffi_version
166176
# If libffi_version contains rc version, just ignored.
167177
libffi_version = libffi_version.gsub(/-rc\d+/, '')

ext/fiddle/fiddle.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
# endif
4141
#endif
4242

43-
#define FFI_GO_CLOSURES 0 /* fiddle does not use go closures */
4443
#ifdef USE_HEADER_HACKS
4544
#include <ffi/ffi.h>
4645
#else

0 commit comments

Comments
 (0)