Skip to content

Commit

Permalink
Use vendored taglib for swig
Browse files Browse the repository at this point in the history
This is working towards deterministic `rake swig` (see
#136).
  • Loading branch information
jacobvosmaer committed Apr 7, 2024
1 parent 33066c1 commit 1aaa165
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 34 deletions.
45 changes: 45 additions & 0 deletions tasks/build.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen-string-literal: true

class Build
class << self
def plat
ENV['PLATFORM'] || 'i386-mingw32'
end

def version
ENV['TAGLIB_VERSION'] || '1.9.1'
end

def dir
"taglib-#{version}"
end

def tmp
"#{__dir__}/../tmp"
end

def source
"#{tmp}/#{dir}"
end

def tarball
"#{source}.tar.gz"
end

def tmp_arch
"#{tmp}/#{plat}"
end

def install_dir
"#{tmp_arch}/#{dir}"
end

def build_dir
"#{install_dir}-build"
end

def library
"#{install_dir}/lib/libtag.#{RbConfig::CONFIG['SOEXT']}"
end
end
end
52 changes: 22 additions & 30 deletions tasks/ext.rake
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
# frozen-string-literal: true

require_relative 'build'

# Extension tasks and cross-compiling

host = 'i686-w64-mingw32'
$plat = ENV['PLATFORM'] || 'i386-mingw32'

taglib_version = ENV['TAGLIB_VERSION'] || '1.9.1'
taglib = "taglib-#{taglib_version}"

tmp = "#{Dir.pwd}/tmp"
tmp_arch = "#{tmp}/#{$plat}"
toolchain_file = "#{Dir.pwd}/ext/win.cmake"
install_dir = "#{tmp_arch}/#{taglib}"
install_dll = "#{install_dir}/bin/libtag.dll"
install_so = "#{install_dir}/lib/libtag.so"
$cross_config_options = ["--with-opt-dir=#{install_dir}"]
install_dll = "#{Build.install_dir}/bin/libtag.dll"
$cross_config_options = ["--with-opt-dir=#{Build.install_dir}"]

taglib_url = "https://github.com/taglib/taglib/archive/v#{taglib_version}.tar.gz"
taglib_url = "https://github.com/taglib/taglib/archive/v#{Build.version}.tar.gz"
taglib_options = ['-DCMAKE_BUILD_TYPE=Release',
'-DBUILD_EXAMPLES=OFF',
'-DBUILD_TESTS=OFF',
Expand All @@ -27,7 +20,7 @@ taglib_options = ['-DCMAKE_BUILD_TYPE=Release',

def configure_cross_compile(ext)
ext.cross_compile = true
ext.cross_platform = $plat
ext.cross_platform = Build.plat
ext.cross_config_options.concat($cross_config_options)
ext.cross_compiling do |gem|
gem.files << 'lib/libtag.dll'
Expand Down Expand Up @@ -75,43 +68,42 @@ task :cross do
ENV['CXX'] = "#{host}-g++"
end

file "#{tmp_arch}/stage/lib/libtag.dll" => [install_dll] do |f|
file "#{Build.tmp_arch}/stage/lib/libtag.dll" => [install_dll] do |f|
install install_dll, f
end

file install_dll => ["#{tmp}/#{taglib}"] do
chdir "#{tmp}/#{taglib}" do
sh %(cmake -DCMAKE_INSTALL_PREFIX=#{install_dir} -DCMAKE_TOOLCHAIN_FILE=#{toolchain_file} #{taglib_options})
file install_dll => [Build.source] do
chdir Build.source do
sh %(cmake -DCMAKE_INSTALL_PREFIX=#{Build.install_dir} -DCMAKE_TOOLCHAIN_FILE=#{toolchain_file} #{taglib_options})
sh 'make VERBOSE=1'
sh 'make install'
end
end

task vendor: [install_so]
task vendor: [Build.library]

file install_so => ["#{tmp_arch}/#{taglib}", "#{tmp_arch}/#{taglib}-build", "#{tmp}/#{taglib}"] do
chdir "#{tmp_arch}/#{taglib}-build" do
sh %(cmake -DCMAKE_INSTALL_PREFIX=#{install_dir} #{taglib_options} #{tmp}/#{taglib})
sh 'make install VERBOSE=1'
file Build.library => [Build.install_dir, Build.build_dir, Build.source] do
chdir Build.build_dir do
sh %(cmake -DCMAKE_INSTALL_PREFIX=#{Build.install_dir} #{taglib_options} #{Build.source})
sh 'make install -j 4 VERBOSE=1'
end
end

directory "#{tmp_arch}/#{taglib}"
directory "#{tmp_arch}/#{taglib}-build"
directory Build.install_dir
directory Build.build_dir
directory Build.tmp

file "#{tmp}/#{taglib}" => ["#{tmp}/#{taglib}.tar.gz"] do
chdir tmp do
sh "tar xzf #{taglib}.tar.gz"
file Build.source => [Build.tarball] do
chdir Build.tmp do
sh "tar xzf #{Build.tarball}"
end
end

file "#{tmp}/#{taglib}.tar.gz" => [tmp] do |t|
file Build.tarball => [Build.tmp] do |t|
require 'open-uri'
puts "Downloading #{taglib_url}"

File.open(t.name, 'wb') do |f|
IO.copy_stream(URI.open(taglib_url), f)
end
end

directory tmp
18 changes: 14 additions & 4 deletions tasks/swig.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen-string-literal: true

require_relative 'build'

# Tasks for generating SWIG wrappers in ext

# Execute SWIG for the specified extension.
Expand All @@ -11,24 +13,32 @@
def run_swig(mod)
swig = `which swig`.chomp
swig = `which swig2.0`.chomp if swig.empty?
swiglib = `swig -swiglib`.chomp
abort 'swig failed' unless $?.success?

# Standard search location for headers
include_args = %w[-I/usr/local/include -I/usr/include]
include_args = "-I#{Build.install_dir}/include"

if ENV.key?('TAGLIB_DIR')
unless File.directory?(ENV['TAGLIB_DIR'])
abort 'When defined, the TAGLIB_DIR environment variable must point to a valid directory.'
end

# Push it in front to get it searched first.
include_args.unshift("-I#{ENV['TAGLIB_DIR']}/include")
include_args = "-I#{ENV['TAGLIB_DIR']}/include"
end

sh "cd ext/#{mod} && #{swig} -c++ -ruby -autorename -initname #{mod} #{include_args.join(' ')} #{mod}.i"
Dir.chdir("ext/#{mod}") do
sh "#{swig} -c++ -ruby -autorename -initname #{mod} #{include_args} #{mod}.i"
wrap = "#{mod}_wrap.cxx"
wrapdata = File.read(wrap)
File.write(wrap, wrapdata.gsub(swiglib, '/swig'))
end
end

task swig:
['ext/taglib_base/taglib_base_wrap.cxx',
[Build.library,
'ext/taglib_base/taglib_base_wrap.cxx',
'ext/taglib_mpeg/taglib_mpeg_wrap.cxx',
'ext/taglib_id3v1/taglib_id3v1_wrap.cxx',
'ext/taglib_id3v2/taglib_id3v2_wrap.cxx',
Expand Down

0 comments on commit 1aaa165

Please sign in to comment.