2017-09-12 11:52:23 +00:00
|
|
|
# frozen_string_literal: true
|
2010-05-06 06:59:24 +00:00
|
|
|
require 'mkmf'
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi_version = nil
|
|
|
|
have_libffi = false
|
2015-01-17 10:52:17 +00:00
|
|
|
bundle = enable_config('bundled-libffi')
|
2020-06-27 11:01:22 +09:00
|
|
|
unless bundle
|
2014-12-20 02:23:00 +00:00
|
|
|
dir_config 'libffi'
|
2010-05-06 06:59:24 +00:00
|
|
|
|
2020-06-27 11:01:22 +09:00
|
|
|
if pkg_config("libffi")
|
|
|
|
libffi_version = pkg_config("libffi", "modversion")
|
|
|
|
end
|
2013-04-10 21:15:37 +00:00
|
|
|
|
2020-06-27 11:01:22 +09:00
|
|
|
have_ffi_header = false
|
2014-12-22 03:17:36 +00:00
|
|
|
if have_header(ffi_header = 'ffi.h')
|
2020-06-27 11:01:22 +09:00
|
|
|
have_ffi_header = true
|
2014-12-22 03:17:36 +00:00
|
|
|
elsif have_header(ffi_header = 'ffi/ffi.h')
|
2020-03-04 18:13:03 +09:00
|
|
|
$defs.push('-DUSE_HEADER_HACKS')
|
2020-06-27 11:01:22 +09:00
|
|
|
have_ffi_header = true
|
|
|
|
end
|
|
|
|
if have_ffi_header && (have_library('ffi') || have_library('libffi'))
|
|
|
|
have_libffi = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless have_libffi
|
2018-10-20 07:27:18 +00:00
|
|
|
# for https://github.com/ruby/fiddle
|
2020-06-27 11:01:22 +09:00
|
|
|
extlibs_rb = File.expand_path("../../bin/extlibs.rb", $srcdir)
|
|
|
|
if bundle && File.exist?(extlibs_rb)
|
2018-10-20 07:27:18 +00:00
|
|
|
require "fileutils"
|
|
|
|
require_relative "../../bin/extlibs"
|
|
|
|
extlibs = ExtLibs.new
|
|
|
|
cache_dir = File.expand_path("../../tmp/.download_cache", $srcdir)
|
|
|
|
ext_dir = File.expand_path("../../ext", $srcdir)
|
|
|
|
Dir.glob("#{$srcdir}/libffi-*/").each{|dir| FileUtils.rm_rf(dir)}
|
|
|
|
extlibs.run(["--cache=#{cache_dir}", ext_dir])
|
|
|
|
end
|
2020-06-28 00:57:37 +09:00
|
|
|
if bundle != false
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi_package_name = Dir.glob("#{$srcdir}/libffi-*/")
|
|
|
|
.map {|n| File.basename(n)}
|
|
|
|
.max_by {|n| n.scan(/\d+/).map(&:to_i)}
|
|
|
|
end
|
|
|
|
unless libffi_package_name
|
2014-12-21 08:12:18 +00:00
|
|
|
raise "missing libffi. Please install libffi."
|
2010-05-06 06:59:24 +00:00
|
|
|
end
|
2014-12-21 08:12:18 +00:00
|
|
|
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi_srcdir = "#{$srcdir}/#{libffi_package_name}"
|
2014-12-22 03:17:36 +00:00
|
|
|
ffi_header = 'ffi.h'
|
2014-12-22 03:42:13 +00:00
|
|
|
libffi = Struct.new(*%I[dir srcdir builddir include lib a cflags ldflags opt arch]).new
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi.dir = libffi_package_name
|
2014-12-21 08:12:18 +00:00
|
|
|
if $srcdir == "."
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi.builddir = libffi_package_name
|
2014-12-21 08:12:18 +00:00
|
|
|
libffi.srcdir = "."
|
|
|
|
else
|
|
|
|
libffi.builddir = libffi.dir
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi.srcdir = relative_from(libffi_srcdir, "..")
|
2014-12-21 08:12:18 +00:00
|
|
|
end
|
|
|
|
libffi.include = "#{libffi.builddir}/include"
|
|
|
|
libffi.lib = "#{libffi.builddir}/.libs"
|
2014-12-25 12:58:18 +00:00
|
|
|
libffi.a = "#{libffi.lib}/libffi_convenience.#{$LIBEXT}"
|
2014-12-23 06:21:49 +00:00
|
|
|
nowarn = CONFIG.merge("warnflags"=>"")
|
2017-09-12 11:52:23 +00:00
|
|
|
libffi.cflags = RbConfig.expand("$(CFLAGS)".dup, nowarn)
|
2020-06-27 11:01:22 +09:00
|
|
|
libffi_version = libffi_package_name[/libffi-(.*)/, 1]
|
2014-12-22 03:17:36 +00:00
|
|
|
|
|
|
|
FileUtils.mkdir_p(libffi.dir)
|
|
|
|
libffi.opt = CONFIG['configure_args'][/'(-C)'/, 1]
|
2017-09-12 11:52:23 +00:00
|
|
|
libffi.ldflags = RbConfig.expand("$(LDFLAGS) #{libpathflag([relative_from($topdir, "..")])} #{$LIBRUBYARG}".dup)
|
2014-12-22 03:17:36 +00:00
|
|
|
libffi.arch = RbConfig::CONFIG['host']
|
2014-12-23 06:21:49 +00:00
|
|
|
if $mswin
|
2015-01-11 03:54:45 +00:00
|
|
|
unless find_executable(as = /x64/ =~ libffi.arch ? "ml64" : "ml")
|
|
|
|
raise "missing #{as} command."
|
|
|
|
end
|
2014-12-23 06:21:49 +00:00
|
|
|
$defs << "-DFFI_BUILDING"
|
2014-12-24 02:29:56 +00:00
|
|
|
libffi_config = "#{relative_from($srcdir, '..')}/win32/libffi-config.rb"
|
|
|
|
config = CONFIG.merge("top_srcdir" => $top_srcdir)
|
|
|
|
args = $ruby.gsub(/:\/=\\/, '')
|
|
|
|
args.gsub!(/\)\\/, ')/')
|
|
|
|
args = args.shellsplit
|
|
|
|
args.map! {|s| RbConfig.expand(s, config)}
|
|
|
|
args << '-C' << libffi.dir << libffi_config
|
|
|
|
opts = {}
|
2014-12-23 06:21:49 +00:00
|
|
|
else
|
2014-12-24 02:29:56 +00:00
|
|
|
args = %W[sh #{libffi.srcdir}/configure ]
|
|
|
|
opts = {chdir: libffi.dir}
|
2014-12-23 06:21:49 +00:00
|
|
|
end
|
2014-12-24 02:29:56 +00:00
|
|
|
cc = RbConfig::CONFIG['CC']
|
|
|
|
cxx = RbConfig::CONFIG['CXX']
|
|
|
|
ld = RbConfig::CONFIG['LD']
|
|
|
|
args.concat %W[
|
|
|
|
--srcdir=#{libffi.srcdir}
|
2014-12-25 12:58:18 +00:00
|
|
|
--host=#{libffi.arch}
|
2014-12-23 06:21:49 +00:00
|
|
|
]
|
2015-03-13 02:37:49 +00:00
|
|
|
args << ($enable_shared || !$static ? '--enable-shared' : '--enable-static')
|
2014-12-23 06:21:49 +00:00
|
|
|
args << libffi.opt if libffi.opt
|
|
|
|
args.concat %W[
|
|
|
|
CC=#{cc} CFLAGS=#{libffi.cflags}
|
2017-09-12 11:52:23 +00:00
|
|
|
CXX=#{cxx} CXXFLAGS=#{RbConfig.expand("$(CXXFLAGS)".dup, nowarn)}
|
2014-12-23 06:21:49 +00:00
|
|
|
LD=#{ld} LDFLAGS=#{libffi.ldflags}
|
2014-12-22 03:17:36 +00:00
|
|
|
]
|
|
|
|
|
2014-12-23 06:21:49 +00:00
|
|
|
FileUtils.rm_f("#{libffi.include}/ffitarget.h")
|
2014-12-22 03:17:36 +00:00
|
|
|
Logging::open do
|
2014-12-24 02:29:56 +00:00
|
|
|
Logging.message("%p in %p\n", args, opts)
|
2014-12-30 06:13:33 +00:00
|
|
|
unless system(*args, **opts)
|
|
|
|
begin
|
|
|
|
IO.copy_stream(libffi.dir + "/config.log", Logging.instance_variable_get(:@logfile))
|
|
|
|
rescue SystemCallError => e
|
2020-06-27 11:01:22 +09:00
|
|
|
Logging.message("%s\n", e.message)
|
2014-12-30 06:13:33 +00:00
|
|
|
end
|
2014-12-22 03:17:36 +00:00
|
|
|
raise "failed to configure libffi. Please install libffi."
|
2014-12-30 06:13:33 +00:00
|
|
|
end
|
2014-12-22 03:17:36 +00:00
|
|
|
end
|
2014-12-24 00:36:42 +00:00
|
|
|
if $mswin && File.file?("#{libffi.include}/ffitarget.h")
|
|
|
|
FileUtils.rm_f("#{libffi.include}/ffitarget.h")
|
|
|
|
end
|
2014-12-23 06:21:49 +00:00
|
|
|
unless File.file?("#{libffi.include}/ffitarget.h")
|
2020-06-27 11:01:22 +09:00
|
|
|
FileUtils.cp("#{libffi_srcdir}/src/x86/ffitarget.h", libffi.include, preserve: true)
|
2014-12-23 06:21:49 +00:00
|
|
|
end
|
2014-12-22 03:17:36 +00:00
|
|
|
$INCFLAGS << " -I" << libffi.include
|
2010-05-06 06:59:24 +00:00
|
|
|
end
|
|
|
|
|
2020-06-27 11:01:22 +09:00
|
|
|
if libffi_version
|
|
|
|
# If libffi_version contains rc version, just ignored.
|
|
|
|
libffi_version = libffi_version.gsub(/-rc\d+/, '')
|
|
|
|
libffi_version = (libffi_version.split('.').map(&:to_i) + [0,0])[0,3]
|
|
|
|
$defs.push(%{-DRUBY_LIBFFI_MODVERSION=#{ '%d%03d%03d' % libffi_version }})
|
|
|
|
warn "libffi_version: #{libffi_version.join('.')}"
|
2020-03-05 11:31:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
case
|
2020-06-27 11:01:22 +09:00
|
|
|
when $mswin, $mingw, (libffi_version && (libffi_version <=> [3, 2]) >= 0)
|
2020-03-05 11:31:08 +09:00
|
|
|
$defs << "-DUSE_FFI_CLOSURE_ALLOC=1"
|
2020-06-27 11:01:22 +09:00
|
|
|
when (libffi_version && (libffi_version <=> [3, 2]) < 0)
|
ext/fiddle/extconf.rb: check if ffi_closure_alloc is available
to define HAVE_FFI_CLOSURE_ALLOC.
The macro is used in closure.c, so have_func check is needed.
If pkg-config is not installed, extconf.rb fails to detect the version
of libffi, and does not add "-DUSE_FFI_CLOSURE_ALLOC=1" even when system
libffi version is >= 3.2.
If USE_FFI_CLOSURE_ALLOC is not defined, closure.c attempts to check if
HAVE_FFI_CLOSURE_ALLOC is defined or not, but have_func was removed with
528a3a17977aa1843a26630c96635c3cb161e729, so the macro is always not
defined.
This resulted in this deprecation warning:
https://rubyci.org/logs/rubyci.s3.amazonaws.com/ubuntu2004/ruby-master/log/20200512T123003Z.log.html.gz
```
compiling closure.c
closure.c: In function 'initialize':
closure.c:265:5: warning: 'ffi_prep_closure' is deprecated: use ffi_prep_closure_loc instead [-Wdeprecated-declarations]
265 | result = ffi_prep_closure(pcl, cif, callback, (void *)self);
| ^~~~~~
In file included from ./fiddle.h:42,
from closure.c:1:
/usr/include/x86_64-linux-gnu/ffi.h:334:1: note: declared here
334 | ffi_prep_closure (ffi_closure*,
| ^~~~~~~~~~~~~~~~
```
2020-05-13 01:17:30 +09:00
|
|
|
else
|
|
|
|
have_func('ffi_closure_alloc', ffi_header)
|
2020-03-04 17:28:21 +09:00
|
|
|
end
|
|
|
|
|
2020-06-27 11:01:22 +09:00
|
|
|
if libffi
|
|
|
|
$defs << "-DHAVE_FFI_PREP_CIF_VAR"
|
|
|
|
else
|
|
|
|
have_func('ffi_prep_cif_var', ffi_header)
|
|
|
|
end
|
2020-06-27 07:25:47 +09:00
|
|
|
|
2010-05-06 06:59:24 +00:00
|
|
|
have_header 'sys/mman.h'
|
|
|
|
|
2012-11-27 19:54:50 +00:00
|
|
|
if have_header "dlfcn.h"
|
|
|
|
have_library "dl"
|
|
|
|
|
|
|
|
%w{ dlopen dlclose dlsym }.each do |func|
|
|
|
|
abort "missing function #{func}" unless have_func(func)
|
|
|
|
end
|
|
|
|
|
|
|
|
have_func "dlerror"
|
|
|
|
elsif have_header "windows.h"
|
|
|
|
%w{ LoadLibrary FreeLibrary GetProcAddress }.each do |func|
|
|
|
|
abort "missing function #{func}" unless have_func(func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-22 03:17:36 +00:00
|
|
|
have_const('FFI_STDCALL', ffi_header)
|
2012-11-30 15:57:21 +00:00
|
|
|
|
2012-02-25 05:47:16 +00:00
|
|
|
config = File.read(RbConfig.expand(File.join($arch_hdrdir, "ruby/config.h")))
|
|
|
|
types = {"SIZE_T"=>"SSIZE_T", "PTRDIFF_T"=>nil, "INTPTR_T"=>nil}
|
|
|
|
types.each do |type, signed|
|
|
|
|
if /^\#define\s+SIZEOF_#{type}\s+(SIZEOF_(.+)|\d+)/ =~ config
|
|
|
|
if size = $2 and size != 'VOIDP'
|
|
|
|
size = types.fetch(size) {size}
|
2020-03-04 18:13:03 +09:00
|
|
|
$defs << "-DTYPE_#{signed||type}=TYPE_#{size}"
|
2012-02-25 05:47:16 +00:00
|
|
|
end
|
|
|
|
if signed
|
|
|
|
check_signedness(type.downcase, "stddef.h")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-21 07:15:04 +00:00
|
|
|
if libffi
|
2015-07-29 07:53:12 +00:00
|
|
|
$LOCAL_LIBS.prepend("./#{libffi.a} ").strip! # to exts.mk
|
2015-07-27 03:45:11 +00:00
|
|
|
$INCFLAGS.gsub!(/-I#{libffi.dir}/, '-I$(LIBFFI_DIR)')
|
2014-12-21 07:15:04 +00:00
|
|
|
end
|
2015-11-23 21:20:56 +00:00
|
|
|
$INCFLAGS << " -I$(top_srcdir)"
|
2014-12-20 02:23:00 +00:00
|
|
|
create_makefile 'fiddle' do |conf|
|
2014-12-23 11:33:54 +00:00
|
|
|
if !libffi
|
|
|
|
next conf << "LIBFFI_CLEAN = none\n"
|
2014-12-24 02:29:56 +00:00
|
|
|
elsif $gnumake && !$nmake
|
2017-02-10 04:24:08 +00:00
|
|
|
submake_arg = "-C $(LIBFFI_DIR)\n"
|
2014-12-20 02:23:00 +00:00
|
|
|
else
|
2017-02-10 04:24:08 +00:00
|
|
|
submake_pre = "cd $(LIBFFI_DIR) && #{config_string("exec")}".strip
|
2014-12-20 02:23:00 +00:00
|
|
|
end
|
2014-12-24 02:29:56 +00:00
|
|
|
if $nmake
|
|
|
|
cmd = "$(RUBY) -C $(LIBFFI_DIR) #{libffi_config} --srcdir=$(LIBFFI_SRCDIR)"
|
|
|
|
else
|
|
|
|
cmd = "cd $(LIBFFI_DIR) && #$exec $(LIBFFI_SRCDIR)/configure #{libffi.opt}"
|
|
|
|
end
|
2014-12-20 03:53:05 +00:00
|
|
|
sep = "/"
|
|
|
|
seprpl = config_string('BUILD_FILE_SEPARATOR') {|s| sep = s; ":/=#{s}" if s != "/"} || ""
|
2014-12-22 03:17:36 +00:00
|
|
|
conf << <<-MK.gsub(/^ +| +$/, '')
|
2014-12-20 02:23:00 +00:00
|
|
|
PWD =
|
2014-12-24 02:29:56 +00:00
|
|
|
LIBFFI_CONFIGURE = #{cmd}
|
2014-12-22 03:17:36 +00:00
|
|
|
LIBFFI_ARCH = #{libffi.arch}
|
2015-07-27 03:45:11 +00:00
|
|
|
LIBFFI_SRCDIR = #{libffi.srcdir.sub(libffi.dir, '$(LIBFFI_DIR)')}
|
2014-12-21 05:45:01 +00:00
|
|
|
LIBFFI_DIR = #{libffi.dir}
|
2015-07-27 03:45:11 +00:00
|
|
|
LIBFFI_A = #{libffi.a.sub(libffi.dir, '$(LIBFFI_DIR)')}
|
2014-12-21 05:45:01 +00:00
|
|
|
LIBFFI_CFLAGS = #{libffi.cflags}
|
2014-12-22 03:42:13 +00:00
|
|
|
LIBFFI_LDFLAGS = #{libffi.ldflags}
|
2014-12-21 05:45:01 +00:00
|
|
|
FFI_H = $(LIBFFI_DIR)/include/ffi.h
|
2017-02-09 12:08:56 +00:00
|
|
|
SUBMAKE_PRE = #{submake_pre}
|
|
|
|
SUBMAKE_ARG = #{submake_arg}
|
2014-12-23 11:33:54 +00:00
|
|
|
LIBFFI_CLEAN = libffi
|
2014-12-20 03:53:05 +00:00
|
|
|
MK
|
2014-12-20 02:23:00 +00:00
|
|
|
end
|
|
|
|
|
2014-12-21 05:45:01 +00:00
|
|
|
if libffi
|
2014-12-21 07:15:04 +00:00
|
|
|
$LIBPATH.pop
|
2014-12-20 02:23:00 +00:00
|
|
|
end
|
2010-05-06 06:59:24 +00:00
|
|
|
|
|
|
|
# :startdoc:
|