2017-09-12 07:52:23 -04:00
|
|
|
# frozen_string_literal: true
|
2010-05-06 02:59:24 -04:00
|
|
|
require 'mkmf'
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
2015-01-17 05:52:17 -05:00
|
|
|
bundle = enable_config('bundled-libffi')
|
|
|
|
if ! bundle
|
2014-12-19 21:23:00 -05:00
|
|
|
dir_config 'libffi'
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2014-12-23 01:22:00 -05:00
|
|
|
pkg_config("libffi") and
|
|
|
|
ver = pkg_config("libffi", "modversion")
|
2013-04-10 17:15:37 -04:00
|
|
|
|
2014-12-21 22:17:36 -05:00
|
|
|
if have_header(ffi_header = 'ffi.h')
|
2014-12-19 21:23:00 -05:00
|
|
|
true
|
2014-12-21 22:17:36 -05:00
|
|
|
elsif have_header(ffi_header = 'ffi/ffi.h')
|
2010-05-06 02:59:24 -04:00
|
|
|
$defs.push(format('-DUSE_HEADER_HACKS'))
|
2014-12-19 21:23:00 -05:00
|
|
|
true
|
|
|
|
end and (have_library('ffi') || have_library('libffi'))
|
|
|
|
end or
|
|
|
|
begin
|
2018-10-20 03:27:18 -04:00
|
|
|
# for https://github.com/ruby/fiddle
|
2018-10-20 03:40:45 -04:00
|
|
|
if bundle && File.exist?("../../bin/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
|
2015-01-17 05:52:17 -05:00
|
|
|
ver = bundle != false &&
|
|
|
|
Dir.glob("#{$srcdir}/libffi-*/")
|
2014-12-19 21:23:00 -05:00
|
|
|
.map {|n| File.basename(n)}
|
|
|
|
.max_by {|n| n.scan(/\d+/).map(&:to_i)}
|
2014-12-21 03:12:18 -05:00
|
|
|
unless ver
|
|
|
|
raise "missing libffi. Please install libffi."
|
2010-05-06 02:59:24 -04:00
|
|
|
end
|
2014-12-21 03:12:18 -05:00
|
|
|
|
2014-12-23 01:21:49 -05:00
|
|
|
srcdir = "#{$srcdir}/#{ver}"
|
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
|
2014-12-21 03:12:18 -05:00
|
|
|
libffi.dir = ver
|
|
|
|
if $srcdir == "."
|
|
|
|
libffi.builddir = "#{ver}/#{RUBY_PLATFORM}"
|
|
|
|
libffi.srcdir = "."
|
|
|
|
else
|
|
|
|
libffi.builddir = libffi.dir
|
2014-12-23 01:21:49 -05:00
|
|
|
libffi.srcdir = relative_from(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)
|
2014-12-21 03:12:18 -05:00
|
|
|
ver = ver[/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
|
|
|
--enable-builddir=#{RUBY_PLATFORM}
|
|
|
|
]
|
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
|
|
|
|
Logfile.message("%s\n", e.message)
|
|
|
|
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")
|
|
|
|
FileUtils.cp("#{srcdir}/src/x86/ffitarget.h", libffi.include, preserve: true)
|
|
|
|
end
|
2014-12-21 22:17:36 -05:00
|
|
|
$INCFLAGS << " -I" << libffi.include
|
2010-05-06 02:59:24 -04:00
|
|
|
end
|
|
|
|
|
2014-12-19 21:23:00 -05: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 02:59:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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}
|
|
|
|
$defs << format("-DTYPE_%s=TYPE_%s", signed||type, size)
|
|
|
|
end
|
|
|
|
if signed
|
|
|
|
check_signedness(type.downcase, "stddef.h")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2015-11-23 16:20:56 -05:00
|
|
|
$INCFLAGS << " -I$(top_srcdir)"
|
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:
|