1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
require 'bundler/gem_tasks'
require 'rake'
require 'rake/clean'
require 'rake/extensiontask'
require 'rake/testtask'
require 'rdoc/task'
require 'rubocop/rake_task'
RakeFileUtils.verbose_flag = false
CLEAN.include FileList['**/*{.o,.so,.dylib,.bundle}'],
FileList['**/extconf.h'],
FileList['**/Makefile'],
FileList['pkg/'],
FileList['ext/magic/share/']
CLOBBER.include FileList['**/tmp'],
FileList['**/*.log'],
FileList['doc/**'],
FileList['tmp/']
CLOBBER.add("ports/*").exclude(%r{ports/archives$})
RUBY_MAGIC_GEM_SPEC = Gem::Specification.load('ruby-magic.gemspec')
RDoc::Task.new do |d|
d.title = 'File Magic in Ruby'
d.main = 'README.md'
d.options << '--line-numbers'
d.rdoc_dir = 'doc/rdoc'
d.rdoc_files.include FileList['ext/**/*.c', 'lib/**/*.rb']
d.rdoc_files.include.add(%w[
README.md
])
end
Rake::TestTask.new do |t|
t.test_files = FileList['test/**/test_*']
t.warning = true
end
RuboCop::RakeTask.new('lint') do |t|
t.patterns = FileList['lib/**/*.rb', 'test/**/*.rb']
t.fail_on_error = false
end
Gem::PackageTask.new(RUBY_MAGIC_GEM_SPEC) do |p|
p.need_zip = false
p.need_tar = false
end
CROSS_RUBY_PLATFORMS = ["x86_64-linux", "x86-linux"]
CROSS_RUBY_VERSIONS = ["2.6.0", "2.7.0", "3.0.0"].join(":")
require "rake_compiler_dock"
ENV["RUBY_CC_VERSION"] = CROSS_RUBY_VERSIONS
Rake::ExtensionTask.new('magic', RUBY_MAGIC_GEM_SPEC) do |e|
e.source_pattern = '*.{c,h}'
e.ext_dir = 'ext/magic'
e.lib_dir = 'lib/magic'
e.cross_compile = true
e.cross_config_options << "--enable-cross-build"
e.cross_platform = CROSS_RUBY_PLATFORMS
e.cross_compiling do |spec|
spec.files.reject! { |path| File.fnmatch?('ports/*', path) }
spec.files.reject! { |path| File.fnmatch?('patches/*', path) }
spec.dependencies.reject! { |dep| dep.name=='mini_portile2' }
db_glob = "ext/magic/share/*.mgc"
raise "magic files are not present at #{db_glob}" if Dir[db_glob].empty?
Dir[db_glob].each { |db| spec.files << db }
end
end
namespace "gem" do
def gem_builder(platform)
# use Task#invoke because the pkg/*gem task is defined at runtime
Rake::Task["native:#{platform}"].invoke
Rake::Task["pkg/#{RUBY_MAGIC_GEM_SPEC.full_name}-#{Gem::Platform.new(platform).to_s}.gem"].invoke
end
CROSS_RUBY_PLATFORMS.each do |platform|
desc "build native gem for #{platform} platform"
task platform do
RakeCompilerDock.sh <<~EOT, platform: platform, verbose: true
gem install bundler --no-document &&
bundle &&
bundle exec rake gem:#{platform}:builder MAKE='nice make -j`nproc`'
EOT
end
namespace platform do
desc "build native gem for #{platform} platform (guest container)"
task "builder" do
gem_builder(platform)
end
end
end
desc "build all native gems"
task "native" => CROSS_RUBY_PLATFORMS
end
def add_file_to_gem(relative_source_path)
dest_path = File.join(gem_build_path, relative_source_path)
dest_dir = File.dirname(dest_path)
mkdir_p dest_dir unless Dir.exist?(dest_dir)
rm_f dest_path if File.exist?(dest_path)
safe_ln relative_source_path, dest_path
RUBY_MAGIC_GEM_SPEC.files << relative_source_path
end
def gem_build_path
File.join "pkg", RUBY_MAGIC_GEM_SPEC.full_name
end
def add_libmagic_and_patches
dependencies = YAML.load_file(File.join(File.dirname(__FILE__), "dependencies.yml"))
archive = File.join("ports", "archives", "file-#{dependencies["libmagic"]["version"]}.tar.gz")
add_file_to_gem archive
patches = Dir['patches/**/*.patch']
patches.each { |patch| add_file_to_gem patch }
end
task gem_build_path do
add_libmagic_and_patches
end
task('default').clear
task default: %w[lint test]
|