2017-09-12 14:52:23 +03:00
|
|
|
# frozen_string_literal: true
|
2010-05-06 10:59:24 +04:00
|
|
|
require 'mkmf'
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
2015-01-17 13:52:17 +03:00
|
|
|
bundle = enable_config('bundled-libffi')
|
|
|
|
if ! bundle
|
2014-12-20 05:23:00 +03:00
|
|
|
dir_config 'libffi'
|
2010-05-06 10:59:24 +04:00
|
|
|
|
2014-12-23 09:22:00 +03:00
|
|
|
pkg_config("libffi") and
|
|
|
|
ver = pkg_config("libffi", "modversion")
|
2013-04-11 01:15:37 +04:00
|
|
|
|
2014-12-22 06:17:36 +03:00
|
|
|
if have_header(ffi_header = 'ffi.h')
|
2014-12-20 05:23:00 +03:00
|
|
|
true
|
2014-12-22 06:17:36 +03:00
|
|
|
elsif have_header(ffi_header = 'ffi/ffi.h')
|
2010-05-06 10:59:24 +04:00
|
|
|
$defs.push(format('-DUSE_HEADER_HACKS'))
|
2014-12-20 05:23:00 +03:00
|
|
|
true
|
|
|
|
end and (have_library('ffi') || have_library('libffi'))
|
|
|
|
end or
|
|
|
|
begin
|
2018-10-20 10:27:18 +03:00
|
|
|
# for https://github.com/ruby/fiddle
|
|
|
|
if bundle && File.exit?("../../bin/extlibs.rb")
|
|
|
|
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
|
2015-01-17 13:52:17 +03:00
|
|
|
ver = bundle != false &&
|
|
|
|
Dir.glob("#{$srcdir}/libffi-*/")
|
2014-12-20 05:23:00 +03:00
|
|
|
.map {|n| File.basename(n)}
|
|
|
|
.max_by {|n| n.scan(/\d+/).map(&:to_i)}
|
2014-12-21 11:12:18 +03:00
|
|
|
unless ver
|
|
|
|
raise "missing libffi. Please install libffi."
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
2014-12-21 11:12:18 +03:00
|
|
|
|
2014-12-23 09:21:49 +03:00
|
|
|
srcdir = "#{$srcdir}/#{ver}"
|
2014-12-22 06:17:36 +03:00
|
|
|
ffi_header = 'ffi.h'
|
2014-12-22 06:42:13 +03:00
|
|
|
libffi = Struct.new(*%I[dir srcdir builddir include lib a cflags ldflags opt arch]).new
|
2014-12-21 11:12:18 +03:00
|
|
|
libffi.dir = ver
|
|
|
|
if $srcdir == "."
|
|
|
|
libffi.builddir = "#{ver}/#{RUBY_PLATFORM}"
|
|
|
|
libffi.srcdir = "."
|
|
|
|
else
|
|
|
|
libffi.builddir = libffi.dir
|
2014-12-23 09:21:49 +03:00
|
|
|
libffi.srcdir = relative_from(srcdir, "..")
|
2014-12-21 11:12:18 +03:00
|
|
|
end
|
|
|
|
libffi.include = "#{libffi.builddir}/include"
|
|
|
|
libffi.lib = "#{libffi.builddir}/.libs"
|
2014-12-25 15:58:18 +03:00
|
|
|
libffi.a = "#{libffi.lib}/libffi_convenience.#{$LIBEXT}"
|
2014-12-23 09:21:49 +03:00
|
|
|
nowarn = CONFIG.merge("warnflags"=>"")
|
2017-09-12 14:52:23 +03:00
|
|
|
libffi.cflags = RbConfig.expand("$(CFLAGS)".dup, nowarn)
|
2014-12-21 11:12:18 +03:00
|
|
|
ver = ver[/libffi-(.*)/, 1]
|
2014-12-22 06:17:36 +03:00
|
|
|
|
|
|
|
FileUtils.mkdir_p(libffi.dir)
|
|
|
|
libffi.opt = CONFIG['configure_args'][/'(-C)'/, 1]
|
2017-09-12 14:52:23 +03:00
|
|
|
libffi.ldflags = RbConfig.expand("$(LDFLAGS) #{libpathflag([relative_from($topdir, "..")])} #{$LIBRUBYARG}".dup)
|
2014-12-22 06:17:36 +03:00
|
|
|
libffi.arch = RbConfig::CONFIG['host']
|
2014-12-23 09:21:49 +03:00
|
|
|
if $mswin
|
2015-01-11 06:54:45 +03:00
|
|
|
unless find_executable(as = /x64/ =~ libffi.arch ? "ml64" : "ml")
|
|
|
|
raise "missing #{as} command."
|
|
|
|
end
|
2014-12-23 09:21:49 +03:00
|
|
|
$defs << "-DFFI_BUILDING"
|
2014-12-24 05:29:56 +03: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 09:21:49 +03:00
|
|
|
else
|
2014-12-24 05:29:56 +03:00
|
|
|
args = %W[sh #{libffi.srcdir}/configure ]
|
|
|
|
opts = {chdir: libffi.dir}
|
2014-12-23 09:21:49 +03:00
|
|
|
end
|
2014-12-24 05:29:56 +03:00
|
|
|
cc = RbConfig::CONFIG['CC']
|
|
|
|
cxx = RbConfig::CONFIG['CXX']
|
|
|
|
ld = RbConfig::CONFIG['LD']
|
|
|
|
args.concat %W[
|
|
|
|
--srcdir=#{libffi.srcdir}
|
2014-12-25 15:58:18 +03:00
|
|
|
--host=#{libffi.arch}
|
2014-12-23 09:21:49 +03:00
|
|
|
--enable-builddir=#{RUBY_PLATFORM}
|
|
|
|
]
|
2015-03-13 05:37:49 +03:00
|
|
|
args << ($enable_shared || !$static ? '--enable-shared' : '--enable-static')
|
2014-12-23 09:21:49 +03:00
|
|
|
args << libffi.opt if libffi.opt
|
|
|
|
args.concat %W[
|
|
|
|
CC=#{cc} CFLAGS=#{libffi.cflags}
|
2017-09-12 14:52:23 +03:00
|
|
|
CXX=#{cxx} CXXFLAGS=#{RbConfig.expand("$(CXXFLAGS)".dup, nowarn)}
|
2014-12-23 09:21:49 +03:00
|
|
|
LD=#{ld} LDFLAGS=#{libffi.ldflags}
|
2014-12-22 06:17:36 +03:00
|
|
|
]
|
|
|
|
|
2014-12-23 09:21:49 +03:00
|
|
|
FileUtils.rm_f("#{libffi.include}/ffitarget.h")
|
2014-12-22 06:17:36 +03:00
|
|
|
Logging::open do
|
2014-12-24 05:29:56 +03:00
|
|
|
Logging.message("%p in %p\n", args, opts)
|
2014-12-30 09:13:33 +03:00
|
|
|
unless system(*args, **opts)
|
|
|
|
begin
|
|
|
|
IO.copy_stream(libffi.dir + "/config.log", Logging.instance_variable_get(:@logfile))
|
|
|
|
rescue SystemCallError => e
|
|
|
|
Logfile.message("%s\n", e.message)
|
|
|
|
end
|
2014-12-22 06:17:36 +03:00
|
|
|
raise "failed to configure libffi. Please install libffi."
|
2014-12-30 09:13:33 +03:00
|
|
|
end
|
2014-12-22 06:17:36 +03:00
|
|
|
end
|
2014-12-24 03:36:42 +03:00
|
|
|
if $mswin && File.file?("#{libffi.include}/ffitarget.h")
|
|
|
|
FileUtils.rm_f("#{libffi.include}/ffitarget.h")
|
|
|
|
end
|
2014-12-23 09:21:49 +03:00
|
|
|
unless File.file?("#{libffi.include}/ffitarget.h")
|
|
|
|
FileUtils.cp("#{srcdir}/src/x86/ffitarget.h", libffi.include, preserve: true)
|
|
|
|
end
|
2014-12-22 06:17:36 +03:00
|
|
|
$INCFLAGS << " -I" << libffi.include
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
|
|
|
|
2014-12-20 05:23:00 +03:00
|
|
|
if ver
|
|
|
|
ver = ver.gsub(/-rc\d+/, '') # If ver contains rc version, just ignored.
|
|
|
|
ver = (ver.split('.') + [0,0])[0,3]
|
|
|
|
$defs.push(%{-DRUBY_LIBFFI_MODVERSION=#{ '%d%03d%03d' % ver }})
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
have_header 'sys/mman.h'
|
|
|
|
|
2012-11-27 23:54:50 +04: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 06:17:36 +03:00
|
|
|
have_const('FFI_STDCALL', ffi_header)
|
2012-11-30 19:57:21 +04:00
|
|
|
|
2012-02-25 09:47:16 +04: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}
|
|
|
|
$defs << format("-DTYPE_%s=TYPE_%s", signed||type, size)
|
|
|
|
end
|
|
|
|
if signed
|
|
|
|
check_signedness(type.downcase, "stddef.h")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-21 10:15:04 +03:00
|
|
|
if libffi
|
2015-07-29 10:53:12 +03:00
|
|
|
$LOCAL_LIBS.prepend("./#{libffi.a} ").strip! # to exts.mk
|
2015-07-27 06:45:11 +03:00
|
|
|
$INCFLAGS.gsub!(/-I#{libffi.dir}/, '-I$(LIBFFI_DIR)')
|
2014-12-21 10:15:04 +03:00
|
|
|
end
|
2015-11-24 00:20:56 +03:00
|
|
|
$INCFLAGS << " -I$(top_srcdir)"
|
2014-12-20 05:23:00 +03:00
|
|
|
create_makefile 'fiddle' do |conf|
|
2014-12-23 14:33:54 +03:00
|
|
|
if !libffi
|
|
|
|
next conf << "LIBFFI_CLEAN = none\n"
|
2014-12-24 05:29:56 +03:00
|
|
|
elsif $gnumake && !$nmake
|
2017-02-10 07:24:08 +03:00
|
|
|
submake_arg = "-C $(LIBFFI_DIR)\n"
|
2014-12-20 05:23:00 +03:00
|
|
|
else
|
2017-02-10 07:24:08 +03:00
|
|
|
submake_pre = "cd $(LIBFFI_DIR) && #{config_string("exec")}".strip
|
2014-12-20 05:23:00 +03:00
|
|
|
end
|
2014-12-24 05:29:56 +03: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 06:53:05 +03:00
|
|
|
sep = "/"
|
|
|
|
seprpl = config_string('BUILD_FILE_SEPARATOR') {|s| sep = s; ":/=#{s}" if s != "/"} || ""
|
2014-12-22 06:17:36 +03:00
|
|
|
conf << <<-MK.gsub(/^ +| +$/, '')
|
2014-12-20 05:23:00 +03:00
|
|
|
PWD =
|
2014-12-24 05:29:56 +03:00
|
|
|
LIBFFI_CONFIGURE = #{cmd}
|
2014-12-22 06:17:36 +03:00
|
|
|
LIBFFI_ARCH = #{libffi.arch}
|
2015-07-27 06:45:11 +03:00
|
|
|
LIBFFI_SRCDIR = #{libffi.srcdir.sub(libffi.dir, '$(LIBFFI_DIR)')}
|
2014-12-21 08:45:01 +03:00
|
|
|
LIBFFI_DIR = #{libffi.dir}
|
2015-07-27 06:45:11 +03:00
|
|
|
LIBFFI_A = #{libffi.a.sub(libffi.dir, '$(LIBFFI_DIR)')}
|
2014-12-21 08:45:01 +03:00
|
|
|
LIBFFI_CFLAGS = #{libffi.cflags}
|
2014-12-22 06:42:13 +03:00
|
|
|
LIBFFI_LDFLAGS = #{libffi.ldflags}
|
2014-12-21 08:45:01 +03:00
|
|
|
FFI_H = $(LIBFFI_DIR)/include/ffi.h
|
2017-02-09 15:08:56 +03:00
|
|
|
SUBMAKE_PRE = #{submake_pre}
|
|
|
|
SUBMAKE_ARG = #{submake_arg}
|
2014-12-23 14:33:54 +03:00
|
|
|
LIBFFI_CLEAN = libffi
|
2014-12-20 06:53:05 +03:00
|
|
|
MK
|
2014-12-20 05:23:00 +03:00
|
|
|
end
|
|
|
|
|
2014-12-21 08:45:01 +03:00
|
|
|
if libffi
|
2014-12-21 10:15:04 +03:00
|
|
|
$LIBPATH.pop
|
2014-12-20 05:23:00 +03:00
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
|
|
|
|
# :startdoc:
|