2007-12-17 11:47:28 +03:00
|
|
|
#! ./miniruby
|
|
|
|
|
|
|
|
dir = File.expand_path("../..", __FILE__)
|
|
|
|
$:.unshift(dir)
|
|
|
|
$:.unshift(".")
|
2008-08-12 09:25:14 +04:00
|
|
|
if $".grep(/mkmf/).empty?
|
|
|
|
$" << "mkmf.rb"
|
|
|
|
load File.expand_path("lib/mkmf.rb", dir)
|
|
|
|
end
|
2007-12-22 21:35:53 +03:00
|
|
|
require 'erb'
|
2007-12-17 11:47:28 +03:00
|
|
|
|
2015-09-05 18:32:20 +03:00
|
|
|
CONFIG['srcdir'] = RbConfig::CONFIG['srcdir']
|
2010-11-17 12:38:38 +03:00
|
|
|
CONFIG["MAKEDIRS"] ||= '$(MINIRUBY) -run -e mkdir -- -p'
|
2008-08-12 09:25:14 +04:00
|
|
|
|
|
|
|
BUILTIN_ENCS = []
|
2008-08-24 19:58:43 +04:00
|
|
|
BUILTIN_TRANSES = []
|
2008-08-12 09:25:14 +04:00
|
|
|
ENC_PATTERNS = []
|
|
|
|
NOENC_PATTERNS = []
|
2015-06-18 17:03:51 +03:00
|
|
|
TRANS_PATTERNS = []
|
|
|
|
NOTRANS_PATTERNS = []
|
2012-05-16 09:39:06 +04:00
|
|
|
module_type = :dynamic
|
2008-08-12 09:25:14 +04:00
|
|
|
|
|
|
|
until ARGV.empty?
|
|
|
|
case ARGV[0]
|
|
|
|
when /\A--builtin-encs=/
|
|
|
|
BUILTIN_ENCS.concat $'.split.map {|e| File.basename(e, '.*') << '.c'}
|
|
|
|
ARGV.shift
|
2008-08-24 19:58:43 +04:00
|
|
|
when /\A--builtin-transes=/
|
|
|
|
BUILTIN_TRANSES.concat $'.split.map {|e| File.basename(e, '.*') }
|
|
|
|
ARGV.shift
|
2008-08-12 09:25:14 +04:00
|
|
|
when /\A--encs=/
|
|
|
|
ENC_PATTERNS.concat $'.split
|
|
|
|
ARGV.shift
|
|
|
|
when /\A--no-encs=/
|
|
|
|
NOENC_PATTERNS.concat $'.split
|
|
|
|
ARGV.shift
|
2015-06-18 17:03:51 +03:00
|
|
|
when /\A--transes=/
|
|
|
|
TRANS_PATTERNS.concat $'.split
|
|
|
|
ARGV.shift
|
|
|
|
when /\A--no-transes=/
|
|
|
|
NOTRANS_PATTERNS.concat $'.split
|
|
|
|
ARGV.shift
|
2012-05-16 09:39:06 +04:00
|
|
|
when /\A--module$/
|
|
|
|
ARGV.shift
|
|
|
|
when /\A--modulestatic$/
|
|
|
|
module_type = :static
|
|
|
|
ARGV.shift
|
2008-08-12 09:25:14 +04:00
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
2007-12-17 20:30:57 +03:00
|
|
|
end
|
|
|
|
|
2012-05-16 09:39:06 +04:00
|
|
|
ALPHANUMERIC_ORDER = proc {|e| e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten}
|
|
|
|
def target_encodings
|
2015-06-19 22:02:45 +03:00
|
|
|
encs = Dir.open($srcdir) {|d| d.grep(/.+\.c\z/)} - BUILTIN_ENCS - ["mktable.c", "encinit.c"]
|
2012-05-16 09:39:06 +04:00
|
|
|
encs.each {|e| e.chomp!(".c")}
|
|
|
|
encs.reject! {|e| !ENC_PATTERNS.any? {|p| File.fnmatch?(p, e)}} if !ENC_PATTERNS.empty?
|
|
|
|
encs.reject! {|e| NOENC_PATTERNS.any? {|p| File.fnmatch?(p, e)}}
|
|
|
|
encs = encs.sort_by(&ALPHANUMERIC_ORDER)
|
2012-05-21 07:11:28 +04:00
|
|
|
deps = Hash.new {[]}
|
|
|
|
inc_srcs = Hash.new {[]}
|
|
|
|
default_deps = %w[regenc.h oniguruma.h config.h defines.h]
|
2015-06-18 16:30:44 +03:00
|
|
|
encs.delete(db = "encdb")
|
2012-05-21 07:11:28 +04:00
|
|
|
encs.each do |e|
|
|
|
|
File.foreach("#$srcdir/#{e}.c") do |l|
|
|
|
|
if /^\s*#\s*include\s+(?:"([^\"]+)"|<(ruby\/\sw+.h)>)/ =~ l
|
|
|
|
n = $1 || $2
|
|
|
|
if /\.c$/ =~ n
|
|
|
|
inc_srcs[e] <<= $`
|
|
|
|
n = "enc/#{n}"
|
|
|
|
end
|
|
|
|
deps[e] <<= n unless default_deps.include?(n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class << inc_srcs; self; end.class_eval do
|
|
|
|
define_method(:expand) do |d|
|
|
|
|
d.map {|n| deps[n] | self.expand(self[n])}.flatten
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inc_srcs.each do |e, d|
|
|
|
|
deps[e].concat(inc_srcs.expand(d))
|
|
|
|
end
|
|
|
|
encs.unshift(db)
|
|
|
|
return encs, deps
|
2012-05-16 09:39:06 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_transcoders
|
|
|
|
atrans = []
|
|
|
|
trans = Dir.open($srcdir+"/trans") {|d|
|
|
|
|
d.select {|e|
|
|
|
|
if e.chomp!('.trans')
|
|
|
|
atrans << e
|
|
|
|
true
|
|
|
|
elsif e.chomp!('.c')
|
|
|
|
true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trans -= BUILTIN_TRANSES
|
|
|
|
atrans -= BUILTIN_TRANSES
|
|
|
|
trans.uniq!
|
2015-06-18 17:03:51 +03:00
|
|
|
atrans.reject! {|e| !TRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}} if !TRANS_PATTERNS.empty?
|
|
|
|
atrans.reject! {|e| NOTRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}}
|
|
|
|
trans.reject! {|e| !TRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}} if !TRANS_PATTERNS.empty?
|
|
|
|
trans.reject! {|e| NOTRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}}
|
2012-05-16 09:39:06 +04:00
|
|
|
atrans = atrans.sort_by(&ALPHANUMERIC_ORDER)
|
|
|
|
trans = trans.sort_by(&ALPHANUMERIC_ORDER)
|
2015-06-18 16:30:44 +03:00
|
|
|
trans.delete(db = "transdb")
|
|
|
|
trans.unshift(db)
|
2012-05-16 09:39:06 +04:00
|
|
|
trans.compact!
|
|
|
|
trans |= atrans
|
|
|
|
trans.map! {|e| "trans/#{e}"}
|
|
|
|
|
|
|
|
return atrans, trans
|
|
|
|
end
|
|
|
|
|
|
|
|
# Constants that "depend" needs.
|
|
|
|
MODULE_TYPE = module_type
|
2012-05-21 07:11:28 +04:00
|
|
|
ENCS, ENC_DEPS = target_encodings
|
2012-05-16 09:39:06 +04:00
|
|
|
ATRANS, TRANS = target_transcoders
|
|
|
|
|
2007-12-17 19:15:46 +03:00
|
|
|
if File.exist?(depend = File.join($srcdir, "depend"))
|
2018-02-27 14:12:23 +03:00
|
|
|
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
|
2018-02-22 16:28:25 +03:00
|
|
|
erb = ERB.new(File.read(depend), trim_mode: '%')
|
|
|
|
else
|
|
|
|
erb = ERB.new(File.read(depend), nil, '%')
|
|
|
|
end
|
2007-12-22 21:35:53 +03:00
|
|
|
erb.filename = depend
|
|
|
|
tmp = erb.result(binding)
|
2007-12-24 06:49:56 +03:00
|
|
|
dep = "\n#### depend ####\n\n" << depend_rules(tmp).join
|
|
|
|
else
|
|
|
|
dep = ""
|
2007-12-17 19:15:46 +03:00
|
|
|
end
|
2007-12-24 06:49:56 +03:00
|
|
|
mkin = File.read(File.join($srcdir, "Makefile.in"))
|
|
|
|
mkin.gsub!(/@(#{CONFIG.keys.join('|')})@/) {CONFIG[$1]}
|
2007-12-17 20:30:57 +03:00
|
|
|
open(ARGV[0], 'wb') {|f|
|
2007-12-24 06:49:56 +03:00
|
|
|
f.puts mkin, dep
|
2007-12-17 11:47:28 +03:00
|
|
|
}
|
2012-05-16 09:39:06 +04:00
|
|
|
if MODULE_TYPE == :static
|
2014-11-27 19:21:51 +03:00
|
|
|
filename = "encinit.c.erb"
|
2018-02-27 14:12:23 +03:00
|
|
|
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
|
2018-02-22 16:28:25 +03:00
|
|
|
erb = ERB.new(File.read(File.join($srcdir, filename)), trim_mode: '%-')
|
|
|
|
else
|
|
|
|
erb = ERB.new(File.read(File.join($srcdir, filename)), nil, '%-')
|
|
|
|
end
|
2014-11-27 19:21:51 +03:00
|
|
|
erb.filename = "enc/#{filename}"
|
2012-05-16 09:39:06 +04:00
|
|
|
tmp = erb.result(binding)
|
|
|
|
begin
|
|
|
|
Dir.mkdir 'enc'
|
|
|
|
rescue Errno::EEXIST
|
|
|
|
end
|
|
|
|
File.open("enc/encinit.c", "w") {|f|
|
|
|
|
f.puts "/* Automatically generated from enc/encinit.c.erb"
|
|
|
|
f.puts " * Do not edit."
|
|
|
|
f.puts " */"
|
|
|
|
f.puts tmp
|
|
|
|
}
|
|
|
|
end
|