2010-05-06 10:59:24 +04:00
|
|
|
require 'mkmf'
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
2014-12-20 05:23:00 +03:00
|
|
|
if ! enable_config('bundled-libffi', false)
|
|
|
|
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
|
|
|
|
ver = Dir.glob("#{$srcdir}/libffi-*/")
|
|
|
|
.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"
|
|
|
|
libffi.a = "#{libffi.lib}/libffi.#{$LIBEXT}"
|
2014-12-23 09:21:49 +03:00
|
|
|
nowarn = CONFIG.merge("warnflags"=>"")
|
|
|
|
libffi.cflags = RbConfig.expand("$(CFLAGS)", 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]
|
2014-12-22 06:42:13 +03:00
|
|
|
libffi.ldflags = RbConfig.expand("$(LDFLAGS) #{libpathflag([relative_from($topdir, "..")])} #{$LIBRUBYARG}")
|
2014-12-22 06:17:36 +03:00
|
|
|
libffi.arch = RbConfig::CONFIG['host']
|
2014-12-23 09:21:49 +03:00
|
|
|
if $mswin
|
|
|
|
$defs << "-DFFI_BUILDING"
|
|
|
|
libffi.opt = '-C'
|
|
|
|
cc = "#{libffi.srcdir}/msvcc.sh"
|
|
|
|
libffi.arch = libffi.arch.sub(/mswin\d+(_\d+)?\z/, 'mingw32')
|
|
|
|
cc << (libffi.arch.sub!(/^x64/, 'x86_64') ? " -m64" : " -m32")
|
|
|
|
libffi.ldflags = ''
|
|
|
|
cxx = cc
|
|
|
|
ld = "link"
|
|
|
|
cpp = "cl -nologo -EP"
|
|
|
|
else
|
|
|
|
cc = RbConfig::CONFIG['CC']
|
|
|
|
ld = RbConfig::CONFIG['LD']
|
|
|
|
end
|
2014-12-22 06:17:36 +03:00
|
|
|
args = %W[
|
2014-12-23 09:21:49 +03:00
|
|
|
sh #{libffi.srcdir}/configure
|
|
|
|
--disable-shared --host=#{libffi.arch}
|
|
|
|
--enable-builddir=#{RUBY_PLATFORM}
|
|
|
|
]
|
|
|
|
args << libffi.opt if libffi.opt
|
|
|
|
args.concat %W[
|
|
|
|
CC=#{cc} CFLAGS=#{libffi.cflags}
|
|
|
|
CXX=#{cxx} CXXFLAGS=#{RbConfig.expand("$(CXXFLAGS)", nowarn)}
|
|
|
|
LD=#{ld} LDFLAGS=#{libffi.ldflags}
|
|
|
|
CPP=#{cpp}
|
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
|
|
|
|
Logging.message("%p in %s\n", args, libffi.dir)
|
|
|
|
system(*args, chdir: libffi.dir) or
|
|
|
|
raise "failed to configure libffi. Please install libffi."
|
|
|
|
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
|
|
|
|
$LIBPATH.unshift libffi.lib
|
|
|
|
$LOCAL_LIBS.prepend("#{libffi.a} ").strip!
|
|
|
|
end
|
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"
|
|
|
|
elsif $mswin
|
2014-12-23 09:21:49 +03:00
|
|
|
submake = "make -C $(LIBFFI_DIR)\n"
|
|
|
|
elsif $gnumake
|
2014-12-20 05:23:00 +03:00
|
|
|
submake = "$(MAKE) -C $(LIBFFI_DIR)\n"
|
|
|
|
else
|
|
|
|
submake = "cd $(LIBFFI_DIR) && \\\n\t\t" << "#{config_string("exec")} $(MAKE)".strip
|
|
|
|
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-22 06:17:36 +03:00
|
|
|
LIBFFI_CONFIGURE = $(LIBFFI_SRCDIR#{seprpl})#{sep}configure #{libffi.opt}
|
|
|
|
LIBFFI_ARCH = #{libffi.arch}
|
2014-12-21 08:45:01 +03:00
|
|
|
LIBFFI_SRCDIR = #{libffi.srcdir}
|
|
|
|
LIBFFI_DIR = #{libffi.dir}
|
|
|
|
LIBFFI_A = #{libffi.a}
|
|
|
|
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
|
2014-12-20 05:23:00 +03:00
|
|
|
SUBMAKE_LIBFFI = #{submake}
|
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
|
|
|
|
$LOCAL_LIBS.prepend("ext/fiddle/")
|
2014-12-20 05:23:00 +03:00
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
|
|
|
|
# :startdoc:
|