diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/optional/capi/spec_helper.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/optional/capi/spec_helper.rb')
-rw-r--r-- | spec/ruby/optional/capi/spec_helper.rb | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb new file mode 100644 index 0000000000..21d2cd04c5 --- /dev/null +++ b/spec/ruby/optional/capi/spec_helper.rb @@ -0,0 +1,109 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +# MRI magic to use built but not installed ruby +$extmk = false + +require 'rbconfig' + +OBJDIR ||= File.expand_path("../../../ext/#{RUBY_ENGINE}/#{RUBY_VERSION}", __FILE__) +mkdir_p(OBJDIR) + +def extension_path + File.expand_path("../ext", __FILE__) +end + +def object_path + OBJDIR +end + +def compile_extension(name) + debug = false + run_mkmf_in_process = RUBY_ENGINE == 'truffleruby' + + ext = "#{name}_spec" + lib = "#{object_path}/#{ext}.#{RbConfig::CONFIG['DLEXT']}" + ruby_header = "#{RbConfig::CONFIG['rubyhdrdir']}/ruby.h" + + return lib if File.exist?(lib) and + File.mtime(lib) > File.mtime("#{extension_path}/rubyspec.h") and + File.mtime(lib) > File.mtime("#{extension_path}/#{ext}.c") and + File.mtime(lib) > File.mtime(ruby_header) and + true # sentinel + + # Copy needed source files to tmpdir + tmpdir = tmp("cext_#{name}") + Dir.mkdir(tmpdir) + begin + ["rubyspec.h", "#{ext}.c"].each do |file| + cp "#{extension_path}/#{file}", "#{tmpdir}/#{file}" + end + + Dir.chdir(tmpdir) do + if run_mkmf_in_process + required = require 'mkmf' + # Reinitialize mkmf if already required + init_mkmf unless required + create_makefile(ext, tmpdir) + else + File.write("extconf.rb", "require 'mkmf'\n" + + "$ruby = ENV.values_at('RUBY_EXE', 'RUBY_FLAGS').join(' ')\n" + + # MRI magic to consider building non-bundled extensions + "$extout = nil\n" + + "create_makefile(#{ext.inspect})\n") + output = ruby_exe("extconf.rb") + raise "extconf failed:\n#{output}" unless $?.success? + $stderr.puts output if debug + end + + # Do not capture stderr as we want to show compiler warnings + make, opts = setup_make + output = IO.popen([make, "V=1", "DESTDIR=", opts], &:read) + raise "#{make} failed:\n#{output}" unless $?.success? + $stderr.puts output if debug + + cp File.basename(lib), lib + end + ensure + rm_r tmpdir + end + + File.chmod(0755, lib) + lib +end + +def setup_make + make = ENV['MAKE'] + make ||= (RbConfig::CONFIG['host_os'].include?("mswin") ? "nmake" : "make") + make_flags = ENV["MAKEFLAGS"] || '' + + # suppress logo of nmake.exe to stderr + if File.basename(make, ".*").downcase == "nmake" and !make_flags.include?("l") + ENV["MAKEFLAGS"] = "l#{make_flags}" + end + + opts = {} + if /(?:\A|\s)--jobserver-(?:auth|fds)=(\d+),(\d+)/ =~ make_flags + begin + r = IO.for_fd($1.to_i(10), "rb", autoclose: false) + w = IO.for_fd($2.to_i(10), "wb", autoclose: false) + rescue Errno::EBADF + else + opts[r] = r + opts[w] = w + end + end + + [make, opts] +end + +def load_extension(name) + require compile_extension(name) +rescue LoadError + if %r{/usr/sbin/execerror ruby "\(ld 3 1 main ([/a-zA-Z0-9_\-.]+_spec\.so)"} =~ $!.message + system('/usr/sbin/execerror', "#{RbConfig::CONFIG["bindir"]}/ruby", "(ld 3 1 main #{$1}") + end + raise +end + +# Constants +CAPI_SIZEOF_LONG = [0].pack('l!').size |