2010-05-06 02:59:24 -04:00
|
|
|
require 'mkmf'
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
2014-12-19 21:23:00 -05:00
|
|
|
if ! enable_config('bundled-libffi', false)
|
|
|
|
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
|
|
|
|
ver = Dir.glob("#{$srcdir}/libffi-*/")
|
|
|
|
.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"
|
|
|
|
libffi.a = "#{libffi.lib}/libffi.#{$LIBEXT}"
|
2014-12-23 01:21:49 -05:00
|
|
|
nowarn = CONFIG.merge("warnflags"=>"")
|
|
|
|
libffi.cflags = RbConfig.expand("$(CFLAGS)", 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]
|
2014-12-21 22:42:13 -05:00
|
|
|
libffi.ldflags = RbConfig.expand("$(LDFLAGS) #{libpathflag([relative_from($topdir, "..")])} #{$LIBRUBYARG}")
|
2014-12-21 22:17:36 -05:00
|
|
|
libffi.arch = RbConfig::CONFIG['host']
|
2014-12-23 01:21:49 -05: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-21 22:17:36 -05:00
|
|
|
args = %W[
|
2014-12-23 01:21:49 -05: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-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
|
|
|
|
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 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
|
|
|
|
$LIBPATH.unshift libffi.lib
|
|
|
|
$LOCAL_LIBS.prepend("#{libffi.a} ").strip!
|
|
|
|
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"
|
|
|
|
elsif $mswin
|
2014-12-23 01:21:49 -05:00
|
|
|
submake = "make -C $(LIBFFI_DIR)\n"
|
|
|
|
elsif $gnumake
|
2014-12-19 21:23:00 -05:00
|
|
|
submake = "$(MAKE) -C $(LIBFFI_DIR)\n"
|
|
|
|
else
|
|
|
|
submake = "cd $(LIBFFI_DIR) && \\\n\t\t" << "#{config_string("exec")} $(MAKE)".strip
|
|
|
|
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-21 22:17:36 -05:00
|
|
|
LIBFFI_CONFIGURE = $(LIBFFI_SRCDIR#{seprpl})#{sep}configure #{libffi.opt}
|
|
|
|
LIBFFI_ARCH = #{libffi.arch}
|
2014-12-21 00:45:01 -05:00
|
|
|
LIBFFI_SRCDIR = #{libffi.srcdir}
|
|
|
|
LIBFFI_DIR = #{libffi.dir}
|
|
|
|
LIBFFI_A = #{libffi.a}
|
|
|
|
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
|
2014-12-19 21:23:00 -05:00
|
|
|
SUBMAKE_LIBFFI = #{submake}
|
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
|
|
|
|
$LOCAL_LIBS.prepend("ext/fiddle/")
|
2014-12-19 21:23:00 -05:00
|
|
|
end
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
# :startdoc:
|