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

Use "make install" instead of manually copying extension binary #191

Merged
merged 1 commit into from Apr 2, 2021
Merged
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
26 changes: 22 additions & 4 deletions lib/rake/extensiontask.rb
Expand Up @@ -115,6 +115,7 @@ def define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION)
tmp_path = "#{@tmp_dir}/#{platf}/#{@name}/#{ruby_ver}"
stage_path = "#{@tmp_dir}/#{platf}/stage"

siteconf_path = "#{tmp_path}/.rake-compiler-siteconf.rb"
tmp_binary_path = "#{tmp_path}/#{binary_path}"
tmp_binary_dir_path = File.dirname(tmp_binary_path)
stage_binary_path = "#{stage_path}/#{lib_path}/#{binary_path}"
Expand All @@ -132,10 +133,27 @@ def define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION)
directory lib_binary_dir_path
directory stage_binary_dir_path

directory File.dirname(siteconf_path)
# Set paths for "make install" destinations
file siteconf_path => File.dirname(siteconf_path) do
File.open(siteconf_path, "w") do |siteconf|
siteconf.puts "require 'rbconfig'"
siteconf.puts "require 'mkmf'"
siteconf.puts "dest_path = mkintpath(#{File.expand_path(lib_path).dump})"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same mechanism as used in rubygems. However we're using an absolute dest_path here, which has to be passed through mkintpath to properly work on Windows. In contrast rubygems uses a relative path and subsequently copies the files from a temporary directory to the destination extension directory.

%w[sitearchdir sitelibdir].each do |dir|
siteconf.puts "RbConfig::MAKEFILE_CONFIG['#{dir}'] = dest_path"
siteconf.puts "RbConfig::CONFIG['#{dir}'] = dest_path"
Comment on lines +144 to +145
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is highly problematic, could you find a way which does not mutate RbConfig, as that can break many things?
See #202

end
end
end

# copy binary from temporary location to final lib
# tmp/extension_name/extension_name.{so,bundle} => lib/
task "copy:#{@name}:#{platf}:#{ruby_ver}" => [lib_binary_dir_path, tmp_binary_path] do
install tmp_binary_path, "#{lib_path}/#{binary_path}"
task "copy:#{@name}:#{platf}:#{ruby_ver}" => [lib_binary_dir_path, tmp_binary_path, "#{tmp_path}/Makefile"] do
# install in lib for native platform only
unless for_platform
sh "#{make} install", chdir: tmp_path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use sh make, install, chdir: tmp_path here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I did it first, but then I noticed, that it doesn't work if one sets the MAKE environment variable to something like MAKE=make -j8 V=1 . In this case a make binary with spaces in the name is tried to be executed instead of being interpret as command line options. In contrast build tools like autoconf or cmake accept options to make as part of the MAKE environment variable. I therefore think it's best how it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
I understand.

end
end
# copy binary from temporary location to staging directory
task "copy:#{@name}:#{platf}:#{ruby_ver}" => [stage_binary_dir_path, tmp_binary_path] do
Expand Down Expand Up @@ -164,12 +182,12 @@ def define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION)

# makefile depends of tmp_dir and config_script
# tmp/extension_name/Makefile
file "#{tmp_path}/Makefile" => [tmp_path, extconf] do |t|
file "#{tmp_path}/Makefile" => [tmp_path, extconf, siteconf_path] do |t|
options = @config_options.dup

# include current directory
include_dirs = ['.'].concat(@config_includes).uniq.join(File::PATH_SEPARATOR)
cmd = [Gem.ruby, "-I#{include_dirs}"]
cmd = [Gem.ruby, "-I#{include_dirs}", "-r#{File.basename(siteconf_path)}"]

# build a relative path to extconf script
abs_tmp_path = (Pathname.new(Dir.pwd) + tmp_path).realpath
Expand Down