[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / enc / make_encmake.rb
blob96d1944bcba31c712b1950afbc3d5f32541922ad
1 #! ./miniruby
3 dir = File.expand_path("../..", __FILE__)
4 $:.unshift(dir)
5 $:.unshift(".")
6 if $".grep(/mkmf/).empty?
7   $" << "mkmf.rb"
8   load File.expand_path("lib/mkmf.rb", dir)
9 end
10 require 'erb'
12 CONFIG['srcdir'] = RbConfig::CONFIG['srcdir']
13 CONFIG["MAKEDIRS"] ||= '$(MINIRUBY) -run -e mkdir -- -p'
15 BUILTIN_ENCS = []
16 BUILTIN_TRANSES = []
17 ENC_PATTERNS = []
18 NOENC_PATTERNS = []
19 TRANS_PATTERNS = []
20 NOTRANS_PATTERNS = []
21 module_type = :dynamic
23 until ARGV.empty?
24   case ARGV[0]
25   when /\A--builtin-encs=/
26     BUILTIN_ENCS.concat $'.split.map {|e| File.basename(e, '.*') << '.c'}
27     ARGV.shift
28   when /\A--builtin-transes=/
29     BUILTIN_TRANSES.concat $'.split.map {|e| File.basename(e, '.*') }
30     ARGV.shift
31   when /\A--encs=/
32     ENC_PATTERNS.concat $'.split
33     ARGV.shift
34   when /\A--no-encs=/
35     NOENC_PATTERNS.concat $'.split
36     ARGV.shift
37   when /\A--transes=/
38     TRANS_PATTERNS.concat $'.split
39     ARGV.shift
40   when /\A--no-transes=/
41     NOTRANS_PATTERNS.concat $'.split
42     ARGV.shift
43   when /\A--module$/
44     ARGV.shift
45   when /\A--modulestatic$/
46     module_type = :static
47     ARGV.shift
48   else
49     break
50   end
51 end
53 ALPHANUMERIC_ORDER = proc {|e| e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten}
54 def target_encodings
55   encs = Dir.open($srcdir) {|d| d.grep(/.+\.c\z/)} - BUILTIN_ENCS - ["mktable.c", "encinit.c"]
56   encs.each {|e| e.chomp!(".c")}
57   encs.reject! {|e| !ENC_PATTERNS.any? {|p| File.fnmatch?(p, e)}} if !ENC_PATTERNS.empty?
58   encs.reject! {|e| NOENC_PATTERNS.any? {|p| File.fnmatch?(p, e)}}
59   encs = encs.sort_by(&ALPHANUMERIC_ORDER)
60   deps = Hash.new {[]}
61   inc_srcs = Hash.new {[]}
62   default_deps = %w[regenc.h oniguruma.h config.h defines.h]
63   encs.delete(db = "encdb")
64   encs.each do |e|
65     File.foreach("#$srcdir/#{e}.c") do |l|
66       if /^\s*#\s*include\s+(?:"([^\"]+)"|<(ruby\/\sw+.h)>)/ =~ l
67         n = $1 || $2
68         if /\.c$/ =~ n
69           inc_srcs[e] <<= $`
70           n = "enc/#{n}"
71         end
72         deps[e] <<= n unless default_deps.include?(n)
73       end
74     end
75   end
76   class << inc_srcs; self; end.class_eval do
77     define_method(:expand) do |d|
78       d.map {|n| deps[n] | self.expand(self[n])}.flatten
79     end
80   end
81   inc_srcs.each do |e, d|
82     deps[e].concat(inc_srcs.expand(d))
83   end
84   encs.unshift(db)
85   return encs, deps
86 end
88 def target_transcoders
89   atrans = []
90   trans = Dir.open($srcdir+"/trans") {|d|
91     d.select {|e|
92       if e.chomp!('.trans')
93         atrans << e
94         true
95       elsif e.chomp!('.c')
96         true
97       end
98     }
99   }
100   trans -= BUILTIN_TRANSES
101   atrans -= BUILTIN_TRANSES
102   trans.uniq!
103   atrans.reject! {|e| !TRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}} if !TRANS_PATTERNS.empty?
104   atrans.reject! {|e| NOTRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}}
105   trans.reject! {|e| !TRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}} if !TRANS_PATTERNS.empty?
106   trans.reject! {|e| NOTRANS_PATTERNS.any? {|p| File.fnmatch?(p, e)}}
107   atrans = atrans.sort_by(&ALPHANUMERIC_ORDER)
108   trans = trans.sort_by(&ALPHANUMERIC_ORDER)
109   trans.delete(db = "transdb")
110   trans.unshift(db)
111   trans.compact!
112   trans |= atrans
113   trans.map! {|e| "trans/#{e}"}
115   return atrans, trans
118 # Constants that "depend" needs.
119 MODULE_TYPE = module_type
120 ENCS, ENC_DEPS = target_encodings
121 ATRANS, TRANS = target_transcoders
123 if File.exist?(depend = File.join($srcdir, "depend"))
124   erb = ERB.new(File.read(depend), trim_mode: '%')
125   erb.filename = depend
126   tmp = erb.result(binding)
127   dep = "\n#### depend ####\n\n" + depend_rules(tmp).join
128 else
129   dep = ""
131 mkin = File.read(File.join($srcdir, "Makefile.in"))
132 # Variables that should not be expanded in Makefile.in to allow
133 # overriding inherited variables at make-time.
134 not_expand_vars = %w(CFLAGS)
135 mkin.gsub!(/@(#{RbConfig::CONFIG.keys.join('|')})@/) do
136   not_expand_vars.include?($1) ? CONFIG[$1] : RbConfig::CONFIG[$1]
138 File.open(ARGV[0], 'wb') {|f|
139   f.puts mkin, dep
141 if MODULE_TYPE == :static
142   filename = "encinit.c.erb"
143   erb = ERB.new(File.read(File.join($srcdir, filename)), trim_mode: '%-')
144   erb.filename = "enc/#{filename}"
145   tmp = erb.result(binding)
146   begin
147     Dir.mkdir 'enc'
148   rescue Errno::EEXIST
149   end
150   require 'tool/lib/output'
151   Output.new(path: "enc/encinit.c", ifchange: true).write(tmp)