2017-09-12 07:52:23 -04:00
|
|
|
# frozen_string_literal: true
|
2010-05-06 02:59:24 -04:00
|
|
|
require 'mkmf'
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
2021-01-02 16:17:56 -05:00
|
|
|
def gcc?
|
|
|
|
RbConfig::CONFIG["GCC"] == "yes"
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_optimization_build_flag(flags)
|
|
|
|
if gcc?
|
|
|
|
expanded_flags = RbConfig.expand(flags.dup)
|
|
|
|
optimization_option_pattern = /(^|\s)?-O\d(\s|$)?/
|
|
|
|
if optimization_option_pattern.match?(expanded_flags)
|
|
|
|
expanded_flags.gsub(optimization_option_pattern, '\\1-Og\\2')
|
|
|
|
else
|
|
|
|
flags + " -Og"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flags
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_debug_build_flag(flags)
|
|
|
|
if gcc?
|
|
|
|
expanded_flags = RbConfig.expand(flags.dup)
|
|
|
|
debug_option_pattern = /(^|\s)-g(?:gdb)?\d?(\s|$)/
|
|
|
|
if debug_option_pattern.match?(expanded_flags)
|
|
|
|
expanded_flags.gsub(debug_option_pattern, '\\1-ggdb3\\2')
|
|
|
|
else
|
|
|
|
flags + " -ggdb3"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flags
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
checking_for(checking_message("--enable-debug-build option")) do
|
|
|
|
enable_debug_build = enable_config("debug-build", false)
|
|
|
|
if enable_debug_build
|
|
|
|
$CFLAGS = disable_optimization_build_flag($CFLAGS)
|
|
|
|
$CFLAGS = enable_debug_build_flag($CFLAGS)
|
|
|
|
end
|
|
|
|
enable_debug_build
|
|
|
|
end
|
|
|
|
|
2020-06-26 22:01:22 -04:00
|
|
|
libffi_version = nil
|
|
|
|
have_libffi = false
|
2015-01-17 05:52:17 -05:00
|
|
|
bundle = enable_config('bundled-libffi')
|
2020-06-26 22:01:22 -04:00
|
|
|
unless bundle
|
2014-12-19 21:23:00 -05:00
|
|
|
dir_config 'libffi'
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2020-06-26 22:01:22 -04:00
|
|
|
if pkg_config("libffi")
|
|
|
|
libffi_version = pkg_config("libffi", "modversion")
|
|
|
|
end
|
2013-04-10 17:15:37 -04:00
|
|
|
|
2020-06-26 22:01:22 -04:00
|
|
|
have_ffi_header = false
|
2014-12-21 22:17:36 -05:00
|
|
|
if have_header(ffi_header = 'ffi.h')
|
2020-06-26 22:01:22 -04:00
|
|
|
have_ffi_header = true
|
2014-12-21 22:17:36 -05:00
|
|
|
elsif have_header(ffi_header = 'ffi/ffi.h')
|
2020-03-04 04:13:03 -05:00
|
|
|
$defs.push('-DUSE_HEADER_HACKS')
|
2020-06-26 22:01:22 -04: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 03:27:18 -04:00
|
|
|
# for https://github.com/ruby/fiddle
|
2020-06-26 22:01:22 -04:00
|
|
|
extlibs_rb = File.expand_path("../../bin/extlibs.rb", $srcdir)
|
|
|
|
if bundle && File.exist?(extlibs_rb)
|
2018-10-20 03:27:18 -04: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-27 11:57:37 -04:00
|
|
|
if bundle != false
|
2020-06-26 22:01:22 -04: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 03:12:18 -05:00
|
|
|
raise "missing libffi. Please install libffi."
|
2010-05-06 02:59:24 -04:00
|
|
|
end
|
2014-12-21 03:12:18 -05:00
|
|
|
|
2020-06-26 22:01:22 -04:00
|
|
|
libffi_srcdir = "#{$srcdir}/#{libffi_package_name}"
|
2014-12-21 22:17:36 -05:00
|
|
|
ffi_header = 'ffi.h'
|
2014-12-21 22:42:13 -05:00
|
|
|
libffi = Struct.new(*%I[dir srcdir builddir include lib a cflags ldflags opt arch]).new
|
2020-06-26 22:01:22 -04:00
|
|
|
libffi.dir = libffi_package_name
|
2014-12-21 03:12:18 -05:00
|
|
|
if $srcdir == "."
|
2020-06-26 22:01:22 -04:00
|
|
|
libffi.builddir = libffi_package_name
|
2014-12-21 03:12:18 -05:00
|
|
|
libffi.srcdir = "."
|
|
|
|
else
|
|
|
|
libffi.builddir = libffi.dir
|
2020-06-26 22:01:22 -04:00
|
|
|
libffi.srcdir = relative_from(libffi_srcdir, "..")
|
2014-12-21 03:12:18 -05:00
|
|
|
end
|
|
|
|
libffi.include = "#{libffi.builddir}/include"
|
|
|
|
libffi.lib = "#{libffi.builddir}/.libs"
|
2014-12-25 07:58:18 -05:00
|
|
|
libffi.a = "#{libffi.lib}/libffi_convenience.#{$LIBEXT}"
|
2014-12-23 01:21:49 -05:00
|
|
|
nowarn = CONFIG.merge("warnflags"=>"")
|
2017-09-12 07:52:23 -04:00
|
|
|
libffi.cflags = RbConfig.expand("$(CFLAGS)".dup, nowarn)
|
2020-06-26 22:01:22 -04:00
|
|
|
libffi_version = libffi_package_name[/libffi-(.*)/, 1]
|
2014-12-21 22:17:36 -05:00
|
|
|
|
|
|
|
FileUtils.mkdir_p(libffi.dir)
|
|
|
|
libffi.opt = CONFIG['configure_args'][/'(-C)'/, 1]
|
2017-09-12 07:52:23 -04:00
|
|
|
libffi.ldflags = RbConfig.expand("$(LDFLAGS) #{libpathflag([relative_from($topdir, "..")])} #{$LIBRUBYARG}".dup)
|
2014-12-21 22:17:36 -05:00
|
|
|
libffi.arch = RbConfig::CONFIG['host']
|
2014-12-23 01:21:49 -05:00
|
|
|
if $mswin
|
2015-01-10 22:54:45 -05:00
|
|
|
unless find_executable(as = /x64/ =~ libffi.arch ? "ml64" : "ml")
|
|
|
|
raise "missing #{as} command."
|
|
|
|
end
|
2014-12-23 01:21:49 -05:00
|
|
|
$defs << "-DFFI_BUILDING"
|
2014-12-23 21:29:56 -05: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 01:21:49 -05:00
|
|
|
else
|
2014-12-23 21:29:56 -05:00
|
|
|
args = %W[sh #{libffi.srcdir}/configure ]
|
|
|
|
opts = {chdir: libffi.dir}
|
2014-12-23 01:21:49 -05:00
|
|
|
end
|
2014-12-23 21:29:56 -05:00
|
|
|
cc = RbConfig::CONFIG['CC']
|
|
|
|
cxx = RbConfig::CONFIG['CXX']
|
|
|
|
ld = RbConfig::CONFIG['LD']
|
|
|
|
args.concat %W[
|
|
|
|
--srcdir=#{libffi.srcdir}
|
2014-12-25 07:58:18 -05:00
|
|
|
--host=#{libffi.arch}
|
2014-12-23 01:21:49 -05:00
|
|
|
]
|
2015-03-12 22:37:49 -04:00
|
|
|
args << ($enable_shared || !$static ? '--enable-shared' : '--enable-static')
|
2014-12-23 01:21:49 -05:00
|
|
|
args << libffi.opt if libffi.opt
|
|
|
|
args.concat %W[
|
|
|
|
CC=#{cc} CFLAGS=#{libffi.cflags}
|
2017-09-12 07:52:23 -04:00
|
|
|
CXX=#{cxx} CXXFLAGS=#{RbConfig.expand("$(CXXFLAGS)".dup, nowarn)}
|
2014-12-23 01:21:49 -05:00
|
|
|
LD=#{ld} LDFLAGS=#{libffi.ldflags}
|
2014-12-21 22:17:36 -05:00
|
|
|
]
|
|
|
|
|
2014-12-23 01:21:49 -05:00
|
|
|
FileUtils.rm_f("#{libffi.include}/ffitarget.h")
|
2014-12-21 22:17:36 -05:00
|
|
|
Logging::open do
|
2014-12-23 21:29:56 -05:00
|
|
|
Logging.message("%p in %p\n", args, opts)
|
2014-12-30 01:13:33 -05:00
|
|
|
unless system(*args, **opts)
|
|
|
|
begin
|
|
|
|
IO.copy_stream(libffi.dir + "/config.log", Logging.instance_variable_get(:@logfile))
|
|
|
|
rescue SystemCallError => e
|
2020-06-26 22:01:22 -04:00
|
|
|
Logging.message("%s\n", e.message)
|
2014-12-30 01:13:33 -05:00
|
|
|
end
|
2014-12-21 22:17:36 -05:00
|
|
|
raise "failed to configure libffi. Please install libffi."
|
2014-12-30 01:13:33 -05:00
|
|
|
end
|
2014-12-21 22:17:36 -05:00
|
|
|
end
|
2014-12-23 19:36:42 -05:00
|
|
|
if $mswin && File.file?("#{libffi.include}/ffitarget.h")
|
|
|
|
FileUtils.rm_f("#{libffi.include}/ffitarget.h")
|
|
|
|
end
|
2014-12-23 01:21:49 -05:00
|
|
|
unless File.file?("#{libffi.include}/ffitarget.h")
|
2020-06-26 22:01:22 -04:00
|
|
|
FileUtils.cp("#{libffi_srcdir}/src/x86/ffitarget.h", libffi.include, preserve: true)
|
2014-12-23 01:21:49 -05:00
|
|
|
end
|
2014-12-21 22:17:36 -05:00
|
|
|
$INCFLAGS << " -I" << libffi.include
|
2010-05-06 02:59:24 -04:00
|
|
|
end
|
|
|
|
|
2020-06-26 22:01:22 -04: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-04 21:31:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
case
|
2020-06-26 22:01:22 -04:00
|
|
|
when $mswin, $mingw, (libffi_version && (libffi_version <=> [3, 2]) >= 0)
|
2020-03-04 21:31:08 -05:00
|
|
|
$defs << "-DUSE_FFI_CLOSURE_ALLOC=1"
|
2020-06-26 22:01:22 -04: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-12 12:17:30 -04:00
|
|
|
else
|
|
|
|
have_func('ffi_closure_alloc', ffi_header)
|
2020-03-04 03:28:21 -05:00
|
|
|
end
|
|
|
|
|
2020-12-22 00:56:47 -05:00
|
|
|
if libffi_version
|
|
|
|
if (libffi_version <=> [3, 0, 11]) >= 0
|
|
|
|
$defs << "-DHAVE_FFI_PREP_CIF_VAR"
|
|
|
|
end
|
2020-06-26 22:01:22 -04:00
|
|
|
else
|
|
|
|
have_func('ffi_prep_cif_var', ffi_header)
|
|
|
|
end
|
2020-06-26 18:25:47 -04:00
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
have_header 'sys/mman.h'
|
|
|
|
|
2012-11-27 14:54:50 -05: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-21 22:17:36 -05:00
|
|
|
have_const('FFI_STDCALL', ffi_header)
|
2012-11-30 10:57:21 -05:00
|
|
|
|
2012-02-25 00:47:16 -05: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 04:13:03 -05:00
|
|
|
$defs << "-DTYPE_#{signed||type}=TYPE_#{size}"
|
2012-02-25 00:47:16 -05:00
|
|
|
end
|
|
|
|
if signed
|
|
|
|
check_signedness(type.downcase, "stddef.h")
|
|
|
|
end
|
2020-12-10 19:41:12 -05:00
|
|
|
else
|
|
|
|
check_signedness(type.downcase, "stddef.h")
|
2012-02-25 00:47:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-10 19:41:12 -05:00
|
|
|
if have_header("ruby/memory_view.h")
|
|
|
|
have_type("rb_memory_view_t", ["ruby/memory_view.h"])
|
|
|
|
end
|
|
|
|
|
2014-12-21 02:15:04 -05:00
|
|
|
if libffi
|
2015-07-29 03:53:12 -04:00
|
|
|
$LOCAL_LIBS.prepend("./#{libffi.a} ").strip! # to exts.mk
|
2015-07-26 23:45:11 -04:00
|
|
|
$INCFLAGS.gsub!(/-I#{libffi.dir}/, '-I$(LIBFFI_DIR)')
|
2014-12-21 02:15:04 -05:00
|
|
|
end
|
2014-12-19 21:23:00 -05:00
|
|
|
create_makefile 'fiddle' do |conf|
|
2014-12-23 06:33:54 -05:00
|
|
|
if !libffi
|
|
|
|
next conf << "LIBFFI_CLEAN = none\n"
|
2014-12-23 21:29:56 -05:00
|
|
|
elsif $gnumake && !$nmake
|
2017-02-09 23:24:08 -05:00
|
|
|
submake_arg = "-C $(LIBFFI_DIR)\n"
|
2014-12-19 21:23:00 -05:00
|
|
|
else
|
2017-02-09 23:24:08 -05:00
|
|
|
submake_pre = "cd $(LIBFFI_DIR) && #{config_string("exec")}".strip
|
2014-12-19 21:23:00 -05:00
|
|
|
end
|
2014-12-23 21:29:56 -05: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-19 22:53:05 -05:00
|
|
|
sep = "/"
|
|
|
|
seprpl = config_string('BUILD_FILE_SEPARATOR') {|s| sep = s; ":/=#{s}" if s != "/"} || ""
|
2014-12-21 22:17:36 -05:00
|
|
|
conf << <<-MK.gsub(/^ +| +$/, '')
|
2014-12-19 21:23:00 -05:00
|
|
|
PWD =
|
2014-12-23 21:29:56 -05:00
|
|
|
LIBFFI_CONFIGURE = #{cmd}
|
2014-12-21 22:17:36 -05:00
|
|
|
LIBFFI_ARCH = #{libffi.arch}
|
2015-07-26 23:45:11 -04:00
|
|
|
LIBFFI_SRCDIR = #{libffi.srcdir.sub(libffi.dir, '$(LIBFFI_DIR)')}
|
2014-12-21 00:45:01 -05:00
|
|
|
LIBFFI_DIR = #{libffi.dir}
|
2015-07-26 23:45:11 -04:00
|
|
|
LIBFFI_A = #{libffi.a.sub(libffi.dir, '$(LIBFFI_DIR)')}
|
2014-12-21 00:45:01 -05:00
|
|
|
LIBFFI_CFLAGS = #{libffi.cflags}
|
2014-12-21 22:42:13 -05:00
|
|
|
LIBFFI_LDFLAGS = #{libffi.ldflags}
|
2014-12-21 00:45:01 -05:00
|
|
|
FFI_H = $(LIBFFI_DIR)/include/ffi.h
|
2017-02-09 07:08:56 -05:00
|
|
|
SUBMAKE_PRE = #{submake_pre}
|
|
|
|
SUBMAKE_ARG = #{submake_arg}
|
2014-12-23 06:33:54 -05:00
|
|
|
LIBFFI_CLEAN = libffi
|
2014-12-19 22:53:05 -05:00
|
|
|
MK
|
2014-12-19 21:23:00 -05:00
|
|
|
end
|
|
|
|
|
2014-12-21 00:45:01 -05:00
|
|
|
if libffi
|
2014-12-21 02:15:04 -05:00
|
|
|
$LIBPATH.pop
|
2014-12-19 21:23:00 -05:00
|
|
|
end
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
# :startdoc:
|