Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move FFI::Platform::CPU from C to Ruby and remove duplicated libtest #663

Merged
merged 2 commits into from Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 5 additions & 80 deletions Rakefile
Expand Up @@ -3,90 +3,24 @@ require 'rbconfig'
require 'rake/clean'
require File.expand_path("./lib/ffi/version")

USE_RAKE_COMPILER = (RUBY_PLATFORM =~ /java/) ? false : true
if USE_RAKE_COMPILER
require 'rake/extensiontask'
end

require 'date'
require 'fileutils'
require 'rbconfig'
require 'rspec/core/rake_task'
require 'rubygems/package_task'

LIBEXT = case RbConfig::CONFIG['host_os'].downcase
when /darwin/
"dylib"
when /mswin|mingw/
"dll"
else
RbConfig::CONFIG['DLEXT']
end

CPU = case RbConfig::CONFIG['host_cpu'].downcase
when /i[3456]86/
# Darwin always reports i686, even when running in 64bit mode
if RbConfig::CONFIG['host_os'] =~ /darwin/ && 0xfee1deadbeef.is_a?(Fixnum)
"x86_64"
else
"i386"
end

when /amd64|x86_64/
"x86_64"

when /ppc64|powerpc64/
"powerpc64"

when /ppc|powerpc/
"powerpc"

when /^arm/
"arm"

else
RbConfig::CONFIG['host_cpu']
end

OS = case RbConfig::CONFIG['host_os'].downcase
when /linux/
"linux"
when /darwin/
"darwin"
when /freebsd/
"freebsd"
when /openbsd/
"openbsd"
when /sunos|solaris/
"solaris"
when /mswin|mingw/
"win32"
else
RbConfig::CONFIG['host_os'].downcase
end

def which(name)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
app = File.join(path, name+ext)
return app if File.executable? app
end
end
nil
def java?
/java/ === RUBY_PLATFORM
end

GMAKE = which('gmake').nil? ? 'make' : 'gmake'

LIBTEST = "build/libtest.#{LIBEXT}"
BUILD_DIR = "build"
BUILD_EXT_DIR = File.join(BUILD_DIR, "#{RbConfig::CONFIG['arch']}", 'ffi_c', RUBY_VERSION)

def gem_spec
@gem_spec ||= Gem::Specification.load('ffi.gemspec')
end

TEST_DEPS = [ LIBTEST ]
TEST_DEPS = []
if RUBY_PLATFORM == "java"
RSpec::Core::RakeTask.new(:spec) do |config|
config.rspec_opts = YAML.load_file 'spec/spec.opts'
Expand Down Expand Up @@ -117,15 +51,6 @@ CLEAN.include 'bin'

task :distclean => :clobber

desc "Build the native test lib"
file "build/libtest.#{LIBEXT}" => FileList['libtest/**/*.[ch]'] do
sh %{#{GMAKE} -f libtest/GNUmakefile CPU=#{CPU} OS=#{OS} }
end


desc "Build test helper lib"
task :libtest => "build/libtest.#{LIBEXT}"

desc "Test the extension"
task :test => [ :spec ]

Expand Down Expand Up @@ -175,8 +100,8 @@ end

task 'gem:java' => 'java:gem'


if USE_RAKE_COMPILER
unless java?
require 'rake/extensiontask'
Rake::ExtensionTask.new('ffi_c', gem_spec) do |ext|
ext.name = 'ffi_c' # indicate the name of the extension.
# ext.lib_dir = BUILD_DIR # put binaries into this folder.
Expand Down
47 changes: 0 additions & 47 deletions ext/ffi_c/Platform.c
Expand Up @@ -49,51 +49,6 @@

static VALUE PlatformModule = Qnil;

/*
* Determine the cpu type at compile time - useful for MacOSX where the the
* system installed ruby incorrectly reports 'host_cpu' as 'powerpc' when running
* on intel.
*/
#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64) || defined(_M_X64) || defined(_M_AMD64)
# define CPU "x86_64"

#elif defined(__i386__) || defined(__i386) || defined(_M_IX86)
# define CPU "i386"

#elif defined(__ppc64__) || defined(__powerpc64__) || defined(_M_PPC)
# define CPU "ppc64"

#elif defined(__ppc__) || defined(__powerpc__) || defined(__powerpc)
# define CPU "ppc"

/*
* Need to check for __sparcv9 first, because __sparc will be defined either way.
* Note that __sparcv9 seems to only be set for Solaris. On Linux, __sparc will
* be set, along with __arch64__ if a 64-bit platform.
*/
#elif defined(__sparcv9__) || defined(__sparcv9)
# define CPU "sparcv9"

#elif defined(__sparc__) || defined(__sparc)
# if defined(__arch64__)
# define CPU "sparcv9"
# else
# define CPU "sparc"
# endif

#elif defined(__arm__) || defined(__arm)
# define CPU "arm"

#elif defined(__mips__) || defined(__mips)
# define CPU "mips"

#elif defined(__s390__)
# define CPU "s390"

#else
# define CPU "unknown"
#endif

static void
export_primitive_types(VALUE module)
{
Expand All @@ -120,10 +75,8 @@ rbffi_Platform_Init(VALUE moduleFFI)
rb_define_const(PlatformModule, "BYTE_ORDER", INT2FIX(BYTE_ORDER));
rb_define_const(PlatformModule, "LITTLE_ENDIAN", INT2FIX(LITTLE_ENDIAN));
rb_define_const(PlatformModule, "BIG_ENDIAN", INT2FIX(BIG_ENDIAN));
rb_define_const(PlatformModule, "CPU", rb_str_new2(CPU));
#if defined(__GNU__) || defined(__GLIBC__)
rb_define_const(PlatformModule, "GNU_LIBC", rb_str_new2(LIBC_SO));
#endif
export_primitive_types(PlatformModule);
}

2 changes: 2 additions & 0 deletions lib/ffi/platform.rb
Expand Up @@ -56,6 +56,8 @@ module Platform

OSVERSION = RbConfig::CONFIG['host_os'].gsub(/[^\d]/, '').to_i

CPU = RbConfig::CONFIG['host_cpu']

ARCH = case CPU.downcase
when /amd64|x86_64/
"x86_64"
Expand Down
52 changes: 0 additions & 52 deletions libtest/Benchmark.c

This file was deleted.

34 changes: 0 additions & 34 deletions libtest/BoolTest.c

This file was deleted.

31 changes: 0 additions & 31 deletions libtest/BufferTest.c

This file was deleted.