-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_build_ninja.rb
248 lines (231 loc) · 6.1 KB
/
gen_build_ninja.rb
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
require 'pp'
require 'yaml'
class NinjaGenerator
class << self
def generate_from_yaml(yaml, opt)
b = new(opt)
h = YAML.load(yaml)
h.each do |k, v|
(v || []).each do |vk, vv|
b.__send__("add_#{k}", vk, vv)
end
end
b.generate
end
end
def initialize(opt)
@vars = {}
@rules = {}
@builds = {}
add_var 'cc', opt.cc
add_var 'cxx', opt.cxx
add_var 'cflags', opt.cflags
add_var 'cxxflags', opt.cxxflags
add_var 'cppflags', opt.cppflags
add_var 'ar', 'ar'
add_rule 'cc', description: 'CC $out', command: '$cc -MMD -MF $out.d $cflags $cppflags -o $out -c $in', depfile: '$out.d'
add_rule 'cxx', description: 'CXX $out', command: '$cxx -MMD -MF $out.d $cxxflags $cppflags -o $out -c $in', depfile: '$out.d'
add_rule 'link', description: 'LINK $out', command: '$cxx $ldflags -o $out $in $libs'
add_rule 'ar', description: 'AR $out', command: '$ar crs $out $in'
end
def add_var(name, value)
@vars[name] = value
end
def add_rule(name, value)
@rules[name] = value
end
def add_program(name, value)
rule = value.delete('rule') || value.delete(:rule) || 'link'
value[rule] = extract_objs(name, value)
add_build(name, value)
end
def add_library(name, value)
rule = value.delete('rule') || value.delete(:rule) || 'ar'
value[rule] = extract_objs(name, value)
add_build(name, value)
end
def add_misc(name, value)
rule = value.delete('rule') || value.delete(:rule)
value[rule] = extract_src(value)
add_build(name, value)
end
def add_build(name, value)
@builds[name] = value
end
def extract_objs(name, value)
prefix = File.basename(name)
objs = extract_src(value)
flags = extract_flags(value, /\Ac(xx|pp)?flags/i)
objs.map! do |i|
case i
when /\A(.+)\.c\Z/
obj = make_obj(prefix, $1)
add_build(obj, {cc: i}.merge(flags))
obj
when /\A(.+)\.(cpp|cxx|cc|C)\Z/
obj = make_obj(prefix, $1)
add_build(obj, {cxx: i}.merge(flags))
obj
else
i
end
end
objs
end
def make_obj(prefix, _1)
dir, base = File.dirname(_1), File.basename(_1)
File.join('$builddir', "#{prefix}-#{base}.o").gsub(%r|/./|, '/')
end
def extract_src(value)
value.delete('src').to_a + value.delete(:src).to_a
end
def extract_flags(value, re)
flags = {}
value.keys.grep(re).each do |k|
flags[k] = value.delete(k)
end
flags
end
def generate
text = ''
@vars.each do |k, v|
text << "#{k} = #{v}\n"
end
text << "\n"
@rules.each do |k, v|
text << "rule #{k}\n"
v.each do |vk, vv|
text << " #{vk} = #{vv}\n"
end
end
text << "\n"
@builds.each do |k, v|
rule = (v.keys.map(&:to_s) & @rules.keys.map(&:to_s)).first
raise "missing rule: #{v.keys.join(', ')}" if rule.nil?
srcs = extract_array(v, rule)
implicits = extract_array(v, 'implicit')
text << "build #{k}: #{rule}"
text << " #{srcs.join(' ')}" unless srcs.empty?
text << " | #{implicits.join(' ')}" unless implicits.empty?
text << "\n"
v.each do |vk, vv|
text << " #{vk} = #{vv}\n"
end
end
text
end
def extract(obj, key)
obj.delete(key.intern) || obj.delete(key.to_s)
end
def extract_array(obj, key)
value = extract(obj, key)
value = [value] unless Array === value
value.compact!
value
end
end
if __FILE__ == $0
require 'erb'
require 'optparse'
def usage
puts(ARGV.options)
exit(1)
end
opt = Struct.new(:cc, :cxx, :cflags, :cppflags, :cxxflags, :debug, :inline, :optimize, :prefix, :configure_args).new
opt.prefix = if /darwin/ =~ RUBY_PLATFORM
'/Users/don/local'
else
'/usr/local'
end
def opt.get_binding
binding
end
opt.configure_args = []
opt.cc = 'gcc'
opt.cxx = 'g++'
ARGV.options do |q|
q.on('--prefix=DIR', 'specify prefix') do |v|
opt.prefix = v
end
q.on('--[no-]debug', '-d', 'enable/disable options for debugging') do |v|
opt.debug = v
end
q.on('--[no-]optimize', '-O', 'enable/disable options for optimizing') do |v|
opt.optimize = v
end
q.on('--[no-]inline', '-i', 'enable/disable inline functions') do |v|
opt.inline = v
end
q.on('--with-clang', 'compile with clang') do
opt.cc = 'clang'
opt.cxx = 'clang++'
end
q.on('--with-gcc', 'compile with/without gcc') do
opt.cc = 'gcc'
opt.cxx = 'g++'
end
q.parse!
end || usage
usage if ARGV.empty?
opt.configure_args << '--prefix=$prefix'
opt.cflags ||= '-Wall -Wextra'
opt.cxxflags ||= '-Wall -Wextra'
if opt.debug
opt.cflags += ' -g'
opt.cxxflags += ' -g'
opt.configure_args << "--debug"
else
opt.configure_args << "--no-debug"
end
if opt.optimize
opt.cflags += ' -O2'
opt.cxxflags += ' -O2'
opt.configure_args << "--optimize"
else
opt.cflags += ' -O0'
opt.cxxflags += ' -O0'
opt.configure_args << "--no-optimize"
end
opt.cppflags ||= ''
if /darwin/ =~ RUBY_PLATFORM
opt.cppflags += ' -DHAVE_MACH_MACH_TIME_H'
end
if opt.inline
opt.cppflags += ' -DSHOOTER_INLINE=inline'
opt.configure_args << "--inline"
else
opt.configure_args << "--no-inline"
end
opt.configure_args = opt.configure_args.join(' ')
def process(file, opt)
orig_file = file
text = File.read(file)
loop do
ext = File.extname(file)
base = File.join(File.dirname(file), File.basename(file, ext))
case ext
when /\.erb\z/i
text = ERB.new(text).result(opt.get_binding)
when /\.yaml\z/i
text = NinjaGenerator.generate_from_yaml(text, opt)
else
File.open(file, 'w') do |f|
f.flock(File::LOCK_EX)
f.write(text)
f.puts(<<EOS)
configure_args = #{opt.configure_args}
rule configure
command = ruby #{$0} $in $configure_args
generator = 1
build #{file}: configure #{orig_file} | #{$0}
EOS
end
return
end
file = base
end
end
ARGV.each do |arg|
process(arg, opt)
end
end