| 1 |
require 'rbconfig' |
| 2 |
require 'date' |
| 3 |
require 'fileutils' |
| 4 |
require 'yaml' |
| 5 |
require 'rspec/core/rake_task' |
| 6 |
require 'rubygems/package_task' |
| 7 |
require 'rake/extensiontask' |
| 8 |
require_relative "lib/ffi/version" |
| 9 |
require_relative "rakelib/ffi_gem_helper" |
| 10 |
|
| 11 |
BUILD_DIR = "build" |
| 12 |
BUILD_EXT_DIR = File.join(BUILD_DIR, "#{RbConfig::CONFIG['arch']}", 'ffi_c', RUBY_VERSION) |
| 13 |
|
| 14 |
gem_spec = Bundler.load_gemspec('ffi.gemspec') |
| 15 |
|
| 16 |
RSpec::Core::RakeTask.new(:spec) do |config| |
| 17 |
config.rspec_opts = YAML.load_file 'spec/spec.opts' |
| 18 |
end |
| 19 |
|
| 20 |
desc "Build all packages" |
| 21 |
task :package => %w[ gem:java gem:native ] |
| 22 |
|
| 23 |
CLOBBER.include 'lib/ffi/types.conf' |
| 24 |
CLOBBER.include 'pkg' |
| 25 |
CLOBBER.include 'log' |
| 26 |
|
| 27 |
CLEAN.include 'build' |
| 28 |
CLEAN.include 'conftest.dSYM' |
| 29 |
CLEAN.include 'spec/ffi/fixtures/libtest.{dylib,so,dll}' |
| 30 |
CLEAN.include 'spec/ffi/fixtures/*.o' |
| 31 |
CLEAN.include 'spec/ffi/embed-test/ext/*.{o,def}' |
| 32 |
CLEAN.include 'spec/ffi/embed-test/ext/Makefile' |
| 33 |
CLEAN.include "pkg/ffi-*-*/" |
| 34 |
CLEAN.include 'lib/{2,3}.*' |
| 35 |
|
| 36 |
# clean all shipped files, that are not in git |
| 37 |
CLEAN.include( |
| 38 |
gem_spec.files - |
| 39 |
`git --git-dir ext/ffi_c/libffi/.git ls-files -z`.split("\x0").map { |f| File.join("ext/ffi_c/libffi", f) } - |
| 40 |
`git ls-files -z`.split("\x0") |
| 41 |
) |
| 42 |
|
| 43 |
task :distclean => :clobber |
| 44 |
|
| 45 |
desc "Test the extension" |
| 46 |
task :test => [ :spec ] |
| 47 |
|
| 48 |
|
| 49 |
namespace :bench do |
| 50 |
ITER = ENV['ITER'] ? ENV['ITER'].to_i : 100000 |
| 51 |
bench_files = Dir["bench/bench_*.rb"].sort.reject { |f| f == "bench/bench_helper.rb" } |
| 52 |
bench_files.each do |bench| |
| 53 |
task File.basename(bench, ".rb")[6..-1] => :compile do |
| 54 |
sh %{#{Gem.ruby} #{bench} #{ITER}} |
| 55 |
end |
| 56 |
end |
| 57 |
task :all => :compile do |
| 58 |
bench_files.each do |bench| |
| 59 |
sh %{#{Gem.ruby} #{bench}} |
| 60 |
end |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
task 'spec:run' => :compile |
| 65 |
task 'spec:specdoc' => :compile |
| 66 |
|
| 67 |
task :default => :spec |
| 68 |
|
| 69 |
namespace 'java' do |
| 70 |
|
| 71 |
java_gem_spec = gem_spec.dup.tap do |s| |
| 72 |
s.files.reject! { |f| File.fnmatch?("ext/*", f) } |
| 73 |
s.extensions = [] |
| 74 |
s.platform = 'java' |
| 75 |
end |
| 76 |
|
| 77 |
Gem::PackageTask.new(java_gem_spec) do |pkg| |
| 78 |
pkg.need_zip = true |
| 79 |
pkg.need_tar = true |
| 80 |
pkg.package_dir = 'pkg' |
| 81 |
end |
| 82 |
end |
| 83 |
|
| 84 |
task 'gem:java' => 'java:gem' |
| 85 |
|
| 86 |
FfiGemHelper.install_tasks |
| 87 |
# Register binary gems to be pushed to rubygems.org |
| 88 |
Bundler::GemHelper.instance.cross_platforms = %w[ |
| 89 |
x86-mingw32 |
| 90 |
x64-mingw-ucrt |
| 91 |
x64-mingw32 |
| 92 |
x86-linux-gnu |
| 93 |
x86-linux-musl |
| 94 |
x86_64-linux-gnu |
| 95 |
x86_64-linux-musl |
| 96 |
arm-linux-gnu |
| 97 |
arm-linux-musl |
| 98 |
aarch64-linux-gnu |
| 99 |
aarch64-linux-musl |
| 100 |
aarch64-mingw-ucrt |
| 101 |
x86_64-darwin |
| 102 |
arm64-darwin |
| 103 |
] |
| 104 |
|
| 105 |
if RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'rbx' |
| 106 |
require 'rake/extensiontask' |
| 107 |
Rake::ExtensionTask.new('ffi_c', gem_spec) do |ext| |
| 108 |
ext.name = 'ffi_c' # indicate the name of the extension. |
| 109 |
# ext.lib_dir = BUILD_DIR # put binaries into this folder. |
| 110 |
ext.tmp_dir = BUILD_DIR # temporary folder used during compilation. |
| 111 |
ext.cross_compile = true # enable cross compilation (requires cross compile toolchain) |
| 112 |
ext.cross_platform = Bundler::GemHelper.instance.cross_platforms |
| 113 |
ext.cross_compiling do |spec| |
| 114 |
spec.files.reject! { |path| File.fnmatch?('ext/*', path) } |
| 115 |
# Binary gems don't need libffi header+lib files |
| 116 |
spec.metadata.delete("msys2_mingw_dependencies") |
| 117 |
end |
| 118 |
# Enable debug info for 'rake compile' but not for 'gem install' |
| 119 |
ext.config_options << "--enable-debug" |
| 120 |
|
| 121 |
end |
| 122 |
else |
| 123 |
task :compile do |
| 124 |
STDERR.puts "Nothing to compile on #{RUBY_ENGINE}" |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
|
| 129 |
namespace "gem" do |
| 130 |
task 'prepare' do |
| 131 |
require 'rake_compiler_dock' |
| 132 |
sh "bundle package" |
| 133 |
end |
| 134 |
|
| 135 |
Bundler::GemHelper.instance.cross_platforms.each do |plat| |
| 136 |
desc "Build all native binary gems in parallel" |
| 137 |
multitask 'native' => plat |
| 138 |
|
| 139 |
desc "Build the native gem for #{plat}" |
| 140 |
task plat => ['prepare', 'build'] do |
| 141 |
RakeCompilerDock.sh <<-EOT, platform: plat |
| 142 |
sudo apt-get update && sudo apt-get install -y libltdl-dev && |
| 143 |
bundle install --local && |
| 144 |
rake native:#{plat} pkg/#{gem_spec.full_name}-#{plat}.gem MAKE='nice make -j`nproc`' RUBY_CC_VERSION=#{RakeCompilerDock.ruby_cc_version("~>4.0", "~>3.0")} |
| 145 |
EOT |
| 146 |
end |
| 147 |
end |
| 148 |
end |
| 149 |
|
| 150 |
directory "ext/ffi_c/libffi" |
| 151 |
file "ext/ffi_c/libffi/autogen.sh" => "ext/ffi_c/libffi" do |
| 152 |
warn "Downloading libffi ..." |
| 153 |
sh "git submodule update --init --recursive" |
| 154 |
end |
| 155 |
task :libffi => "ext/ffi_c/libffi/autogen.sh" |
| 156 |
|
| 157 |
LIBFFI_GIT_FILES = `git --git-dir ext/ffi_c/libffi/.git ls-files -z`.split("\x0") |
| 158 |
|
| 159 |
# Generate files which are in the gemspec but not in libffi's git repo by running autogen.sh |
| 160 |
gem_spec.files.select do |f| |
| 161 |
f =~ /ext\/ffi_c\/libffi\/(.*)/ && !LIBFFI_GIT_FILES.include?($1) |
| 162 |
end.each do |f| |
| 163 |
file f => "ext/ffi_c/libffi/autogen.sh" do |
| 164 |
chdir "ext/ffi_c/libffi" do |
| 165 |
sh "sh ./autogen.sh" |
| 166 |
end |
| 167 |
touch f |
| 168 |
if gem_spec.files != Gem::Specification.load('./ffi.gemspec').files |
| 169 |
warn "gemspec files have changed -> Please restart rake!" |
| 170 |
exit 1 |
| 171 |
end |
| 172 |
end |
| 173 |
end |
| 174 |
|
| 175 |
# Make sure we have all gemspec files before packaging |
| 176 |
task :build => gem_spec.files |
| 177 |
task :gem => :build |
| 178 |
|
| 179 |
|
| 180 |
require_relative "lib/ffi/platform" |
| 181 |
types_conf = File.expand_path(File.join(FFI::Platform::CONF_DIR, 'types.conf')) |
| 182 |
logfile = File.join(File.dirname(__FILE__), 'types_log') |
| 183 |
|
| 184 |
task types_conf do |task| |
| 185 |
require 'fileutils' |
| 186 |
require_relative "rakelib/types_generator" |
| 187 |
options = {} |
| 188 |
FileUtils.mkdir_p(File.dirname(task.name), mode: 0755 ) |
| 189 |
File.open(task.name, File::CREAT|File::TRUNC|File::RDWR, 0644) do |f| |
| 190 |
f.puts FFI::TypesGenerator.generate(options) |
| 191 |
end |
| 192 |
File.open(logfile, 'w') do |log| |
| 193 |
log.puts(types_conf) |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
desc "Create or update type information for platform #{FFI::Platform::NAME}" |
| 198 |
task :types_conf => types_conf |
| 199 |
|
| 200 |
begin |
| 201 |
require 'yard' |
| 202 |
|
| 203 |
namespace :doc do |
| 204 |
YARD::Rake::YardocTask.new do |yard| |
| 205 |
end |
| 206 |
end |
| 207 |
rescue LoadError |
| 208 |
warn "[warn] YARD unavailable" |
| 209 |
end |