2016-02-28 09:49:57 +00:00
# coding: utf-8
2019-09-11 18:26:37 +00:00
require " shellwords "
2010-08-02 09:43:45 +00:00
$use_tempfile_for_run = defined? ( RUBY_PLATFORM ) && / mingw /i . match ( RUBY_PLATFORM )
2016-02-28 10:44:58 +00:00
$git_mutex = Mutex . new
2016-02-28 10:33:40 +00:00
$message_mutex = Mutex . new
2018-12-04 15:46:35 +00:00
$action_width = 14
2016-02-28 10:33:40 +00:00
def puts ( message )
$message_mutex . synchronize {
2010-08-01 20:06:53 +00:00
$stdout . puts message
$stdout . flush
2016-02-28 10:33:40 +00:00
}
2010-07-27 18:13:10 +00:00
end
2018-01-02 14:40:24 +00:00
def puts_action ( action , options = { } )
2016-03-28 09:32:00 +00:00
msg = sprintf " %*s " , $action_width , action . gsub ( / + / , '_' ) . upcase
2018-01-02 14:40:24 +00:00
msg += " #{ options [ :target ] } " if ! options [ :target ] . blank?
msg = " #{ options [ :prefix ] } #{ msg } " if ! options [ :prefix ] . blank?
2016-03-28 09:32:00 +00:00
puts msg
end
2018-01-02 14:40:24 +00:00
def puts_qaction ( action , options = { } )
puts_action ( action , options ) unless $verbose
2016-03-28 09:32:00 +00:00
end
2018-01-02 14:40:24 +00:00
def puts_vaction ( action , options = { } )
puts_action ( action , options ) if $verbose
2016-03-28 09:32:00 +00:00
end
2010-07-25 20:43:57 +00:00
def error ( message )
puts message
exit 1
end
def last_exit_code
$? . respond_to? ( :exitstatus ) ? $? . exitstatus : $? . to_i
end
2012-02-25 14:05:14 +00:00
def run cmdline , opts = { }
code = run_wrapper cmdline , opts
exit code if ( code != 0 ) && ! opts [ :allow_failure ] . to_bool
end
def run_wrapper cmdline , opts = { }
2010-07-25 20:43:57 +00:00
cmdline = cmdline . gsub ( / \ n / , ' ' ) . gsub ( / ^ \ s+ / , '' ) . gsub ( / \ s+$ / , '' ) . gsub ( / \ s+ / , ' ' )
2010-08-02 09:43:45 +00:00
code = nil
shell = ENV [ " RUBYSHELL " ] . blank? ? " c:/msys/bin/sh " : ENV [ " RUBYSHELL " ]
2010-07-25 20:43:57 +00:00
puts cmdline unless opts [ :dont_echo ] . to_bool
2012-02-25 14:05:14 +00:00
output = nil
if opts [ :filter_output ]
output = Tempfile . new ( " mkvtoolnix-rake-output " )
cmdline += " > #{ output . path } 2>&1 "
end
2010-08-02 09:43:45 +00:00
if $use_tempfile_for_run
Tempfile . open ( " mkvtoolnix-rake-run " ) do | t |
t . puts cmdline
t . flush
system shell , t . path
code = last_exit_code
t . unlink
end
else
system cmdline
code = last_exit_code
end
2010-07-25 20:43:57 +00:00
2012-02-25 14:05:14 +00:00
code = opts [ :filter_output ] . call code , output . readlines if opts [ :filter_output ]
return code
ensure
if output
output . close
output . unlink
end
2010-07-25 20:43:57 +00:00
end
2016-03-26 11:34:52 +00:00
def runq ( action , target , cmdline , options = { } )
2018-01-02 19:56:14 +00:00
start = Time . now
runner = lambda { run_wrapper cmdline , options . clone . merge ( :dont_echo = > ! $verbose ) }
2018-01-02 14:40:24 +00:00
puts_action action , :target = > target , :prefix = > $run_show_start_stop ? " [start ] " : nil
2018-01-02 19:56:14 +00:00
code = options [ :mutex ] ? options [ :mutex ] . synchronize ( & runner ) : runner . call
2018-01-02 14:40:24 +00:00
duration = Time . now - start
if $run_show_start_stop
puts_action action , :target = > target , :prefix = > sprintf ( " [%s %02d:%02d.%03d] " , code == 0 ? " done " : " fail " , ( duration / 60 ) . to_i , duration . to_i % 60 , ( duration * 1000 ) . to_i % 1000 )
end
2018-01-21 11:48:15 +00:00
if ( code != 0 ) && ! options [ :allow_failure ] . to_bool
if code == 127
puts " Error: command not found: #{ cmdline } "
elsif code == - 1
puts " Error: could not create child process for: #{ cmdline } "
end
exit code
end
2010-07-25 20:43:57 +00:00
end
2010-07-26 10:29:18 +00:00
2016-02-28 10:44:58 +00:00
def runq_git ( msg , cmdline , options = { } )
2018-01-02 19:56:14 +00:00
runq " git #{ cmdline . split ( / \ s+ / ) [ 0 ] } " , " #{ msg } " , " git #{ cmdline } " , :mutex = > $git_mutex
2016-02-28 10:44:58 +00:00
end
2018-12-04 15:46:35 +00:00
def runq_code ( action , options = { } )
start = Time . now
puts_action action , :target = > options [ :target ] , :prefix = > $run_show_start_stop ? " [start ] " : nil
ok = yield
duration = Time . now - start
if $run_show_start_stop
puts_action action , :target = > options [ :target ] , :prefix = > sprintf ( " [%s %02d:%02d.%03d] " , ok ? " done " : " fail " , ( duration / 60 ) . to_i , duration . to_i % 60 , ( duration * 1000 ) . to_i % 1000 )
end
exit 1 if ! ok
end
2016-02-21 10:23:13 +00:00
def ensure_dir dir
2018-01-13 20:30:34 +00:00
File . unlink ( dir ) if FileTest . exist? ( dir ) && ! FileTest . directory? ( dir )
FileUtils . mkdir_p ( dir )
2016-02-21 10:23:13 +00:00
end
2012-03-18 09:55:42 +00:00
def create_dependency_dirs
2016-02-21 10:23:13 +00:00
[ $dependency_dir , $dependency_tmp_dir ] . each { | dir | ensure_dir dir }
2012-03-18 09:55:42 +00:00
end
def dependency_output_name_for file_name
2012-04-11 14:48:15 +00:00
$dependency_tmp_dir + " / " + file_name . gsub ( / [ \/ \ .] / , '_' ) + '.d'
2012-03-18 09:55:42 +00:00
end
2011-12-10 09:29:56 +00:00
def handle_deps ( target , exit_code , skip_abspath = false )
2012-03-18 09:55:42 +00:00
dep_file = dependency_output_name_for target
2010-07-26 10:29:18 +00:00
get_out = lambda do
File . unlink ( dep_file ) if FileTest . exist? ( dep_file )
exit exit_code if 0 != exit_code
return
end
FileTest . exist? ( dep_file ) || get_out . call
2012-03-18 09:55:42 +00:00
create_dependency_dirs
2010-07-26 10:29:18 +00:00
2018-08-14 13:06:05 +00:00
re_source_dir = Regexp . new ( " ^ " + Regexp :: escape ( $source_dir ) + " /* " )
2010-07-26 10:29:18 +00:00
2018-08-14 13:06:05 +00:00
File . open ( " #{ $dependency_dir } / " + target . gsub ( / [ \/ \ .] / , '_' ) + '.dep' , " w " ) do | out |
sources = IO . readlines ( dep_file ) .
2018-08-20 15:12:34 +00:00
map { | l | l . chomp . split ( %r{ + } ) } .
flatten .
map { | l | l . gsub ( %r{ .*: } , '' ) . gsub ( %r{ ^ \ s+ } , '' ) . gsub ( %r{ \ s* \\ \ s*$ } , '' ) . gsub ( re_source_dir , '' ) } .
2018-08-14 13:06:05 +00:00
reject ( & :empty? ) .
2018-08-20 15:12:34 +00:00
reject { | l | skip_abspath && %r{ ^/ } . match ( l ) } .
sort
2011-12-10 09:29:56 +00:00
2018-08-14 13:06:05 +00:00
out . puts ( ( [ target ] + sources ) . join ( " \n " ) )
2010-07-26 10:29:18 +00:00
end
get_out . call
2015-03-24 12:20:18 +00:00
rescue
2010-07-26 10:29:18 +00:00
get_out . call
end
def import_dependencies
2012-04-11 14:48:15 +00:00
return unless FileTest . directory? $dependency_dir
Dir . glob ( " #{ $dependency_dir } /*.dep " ) . each do | file_name |
2015-03-24 12:20:18 +00:00
lines = IO . readlines ( file_name ) . collect ( & :chomp )
2012-04-11 14:48:15 +00:00
target = lines . shift
file target = > lines . select { | dep_name | File . exists? dep_name }
end
2010-07-26 10:29:18 +00:00
end
2010-07-27 16:52:12 +00:00
def arrayify ( * args )
2010-09-23 13:46:50 +00:00
args . collect { | arg | arg . is_a? ( Array ) ? arg . to_a : arg } . flatten
2010-07-27 16:52:12 +00:00
end
def install_dir ( * dirs )
arrayify ( * dirs ) . each do | dir |
dir = c ( dir ) if dir . is_a? Symbol
run " #{ c ( :mkinstalldirs ) } #{ c ( :DESTDIR ) } #{ dir } "
end
end
def install_program ( destination , * files )
destination = c ( destination ) + '/' if destination . is_a? Symbol
arrayify ( * files ) . each do | file |
run " #{ c ( :INSTALL_PROGRAM ) } #{ file } #{ c ( :DESTDIR ) } #{ destination } "
end
end
def install_data ( destination , * files )
destination = c ( destination ) + '/' if destination . is_a? Symbol
arrayify ( * files ) . each do | file |
run " #{ c ( :INSTALL_DATA ) } #{ file } #{ c ( :DESTDIR ) } #{ destination } "
end
end
2010-07-27 17:47:40 +00:00
2016-03-22 09:20:12 +00:00
def remove_files_by_patterns patterns
2013-07-04 19:04:29 +00:00
patterns . collect { | pattern | FileList [ pattern ] . to_a } . flatten . uniq . select { | file_name | File . exists? file_name } . each do | file_name |
2018-01-02 14:40:24 +00:00
puts_vaction " rm " , :target = > file_name
2013-07-04 19:04:29 +00:00
File . unlink file_name
end
end
2013-07-07 12:32:23 +00:00
2014-01-22 11:11:14 +00:00
def read_files * file_names
2015-05-11 16:20:50 +00:00
Hash [ * file_names . flatten . collect { | file_name | [ file_name , IO . readlines ( file_name ) . collect { | line | line . force_encoding " UTF-8 " } ] } . flatten ( 1 ) ]
2014-01-22 11:11:14 +00:00
end
2016-03-29 12:57:08 +00:00
def list_targets? * targets
spec = ENV [ 'RAKE_LIST_TARGETS' ] . to_s
targets . any? { | target | %r{ \ b #{ target } \ b } . match spec }
end
2018-10-28 11:14:08 +00:00
def process_erb t
puts_action " ERB " , :target = > t . name
template = IO . readlines ( t . prerequisites [ 0 ] ) .
map { | line | line . gsub ( %r{ \{ \{ (.+?) \} \} } ) { | match | ( variables [ $1 ] || ENV [ $1 ] ) . to_s } } .
join ( " " )
File . open ( t . name , 'w' ) { | file | file . puts ( ERB . new ( template ) . result ) }
end
2019-02-23 15:46:59 +00:00
def is_clang?
c ( :COMPILER_TYPE ) == " clang "
end
def is_gcc?
c ( :COMPILER_TYPE ) == " gcc "
end
def check_compiler_version compiler , required_version
return false if ( c ( :COMPILER_TYPE ) != compiler )
return check_version ( required_version , c ( :COMPILER_VERSION ) )
end
def check_version required , actual
return Gem :: Version . new ( required ) < = Gem :: Version . new ( actual )
end
2019-05-20 06:40:09 +00:00
def ensure_file file_name , content = " "
if FileTest . exists? ( file_name )
current_content = IO . readlines ( file_name ) . join ( " \n " )
return if current_content == content
end
File . open ( file_name , 'w' ) { | file | file . write ( content ) }
end
2019-09-11 18:26:37 +00:00
def update_version_number_include
git_dir = $source_dir + '/.git'
current_version = nil
wanted_version = c ( :PACKAGE_VERSION )
if FileTest . exists? ( $version_header_name )
lines = IO . readlines ( $version_header_name )
if ! lines . empty? && %r{ # define.*?"([0-9.]+)" } . match ( lines [ 0 ] )
current_version = $1
end
end
if FileTest . directory? ( git_dir )
command = [ " git " , " --git-dir= #{ $source_dir } /.git " , " describe " , " HEAD " ] .
map { | e | Shellwords . escape ( e ) } .
join ( ' ' )
description = ` #{ command } 2> /dev/null ` . chomp
if %r{ ^release- #{ Regexp . escape ( c ( :PACKAGE_VERSION ) ) } -( \ d+) } . match ( description ) && ( $1 . to_i != 0 )
wanted_version += " . #{ $1 } "
end
end
return if current_version == wanted_version
File . open ( $version_header_name , " w " ) do | file |
file . puts ( " # define MKVTOOLNIX_VERSION \" #{ wanted_version } \" " )
end
end
2019-09-27 15:01:22 +00:00
def verify_current_host_is_same_as_configured
current_host = ` #{ $source_dir } /config.guess 2> /dev/null ` . chomp
return if ! current_host || ( current_host == c ( :host ) )
fail " Error: The source tree was configured on a different host system type ( #{ c ( :host ) } ) than the current one ( #{ current_host } ). This is not supported. "
end
2013-07-07 12:32:23 +00:00
class Rake :: Task
2018-01-01 10:02:38 +00:00
def mo_all_prerequisites
todo = [ name ]
result = [ ]
while ! todo . empty?
current = todo . shift
prereqs = Rake :: Task [ current ] . prerequisites
next if prereqs . empty?
result << [ current , prereqs ]
todo += prereqs
end
result . uniq
end
2013-07-07 12:32:23 +00:00
def investigate
result = " ------------------------------ \n "
result << " Investigating #{ name } \n "
result << " class: #{ self . class } \n "
result << " task needed: #{ needed? } \n "
result << " timestamp: #{ timestamp } \n "
result << " pre-requisites: \n "
prereqs = @prerequisites . collect { | name | Rake :: Task [ name ] }
prereqs . sort! { | a , b | a . timestamp < = > b . timestamp }
result += prereqs . collect { | p | " -- #{ p . name } ( #{ p . timestamp } ) \n " } . join ( " " )
latest_prereq = @prerequisites . collect { | n | Rake :: Task [ n ] . timestamp } . max
result << " latest-prerequisite time: #{ latest_prereq } \n "
result << " ................................ \n \n "
return result
end
end