-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathninja_file_builder.rb
358 lines (328 loc) · 8.99 KB
/
ninja_file_builder.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
require 'erb'
require 'optparse'
require 'ostruct'
require 'yaml'
class NinjaFileBuilder
attr_reader :options
def initialize
@options = OpenStruct.new
@options.prefix = '/usr/local'
@options.input_file = 'build.ninja.yaml.erb'
@options.cc = 'gcc'
@options.cflags = ''
@options.cxx = 'g++'
@options.cxxflags = ''
@options.cppflags = ''
@options.ar = 'ar'
@options.ldflags = ''
@options.libs = ''
@options.configure_args = ''
@options.ruby = File.join(RbConfig::CONFIG['bindir'], 'ruby')
@options.verbose = 0
@option_parser = OptionParser.new
@option_parser.banner = "Usage: #{$0} [options] NAME=VALUE ..."
@option_parser.separator 'Specific options:'
add_option('--prefix=DIR', 'specify prefix') do |v|
@options.prefix = v
end
add_option('--input-file=FILE', '-i', "specify input file") do |v|
@options.input_file = v
end
add_option('--output-file=FILE', '-o', "specify output file") do |v|
@options.output_file = v
end
add_option('--verbose[=NUM]', '-v', Integer, 'verbose level') do |v|
if v
options.verbose = v
else
options.verbose += 1
end
end
@vars = {}
@rules = {}
@builds = {}
@defaults = []
end
def self.build(&block)
self.new.instance_eval(&block)
end
def add_option(*args, &block)
name = nil
args.each do |arg|
if /\A(--?[\-\+\w]+)=?/ =~ arg
name = $1
break
end
end
b = Proc.new do |v|
block.call(v)
@options.configure_args += " #{name}"
case v
when Integer, String
if /\s+/ =~ v.to_s
v = "\"#{v}\""
end
@options.configure_args += "=#{v}"
end
end
@option_parser.on(*args, &b)
end
def parse_args
@option_parser.parse!
ARGV.each do |arg|
case arg
when /\A(\w+)\z/
@options.configure_args += " #{arg}"
when /\A(\w+)=(.+)\z/
k, v = $1, $2
@options.send("#{k}=", v)
v = "\"#{v}\"" if /\s+/ =~ v
@options.configure_args += " #{k}=#{v}"
end
end
end
def save_ninja_file
file = @options.input_file
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
log 1, "Processing #{file}..."
text = ERB.new(text).result(@options.instance_eval('binding'))
when /\.yaml\z/i
log 1, "Processing #{file}..."
text = generate_from_yaml(text)
else
file = @options.output_file || file
log 1, "Writing #{file}..."
File.open(file, 'w') do |f|
f.flock(File::LOCK_EX)
f.write(text)
f.puts(<<EOS)
configure_args = #{@options.configure_args}
rule configure
description = CONFIGURE build.ninja
command = #{@options.ruby} #{$0} $in $configure_args
generator = 1
build #{file}: configure #{orig_file} | #{$0}
EOS
end
return
end
file = base
end
end
private
def generate_from_yaml(text)
h = YAML.load(text)
h.each do |k, v|
(v || []).each do |vk, vv|
__send__("add_#{k}", vk, vv)
end
end
generate
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
unless @defaults.empty?
text << "\ndefault #{@defaults.join(' ')}\n"
end
text
end
def add_var(name, value)
@vars[name] = value
log 2, "VAR #{name}=#{value.inspect}"
end
def add_rule(name, value)
@rules[name] = value
log 2, "RULE #{name}=#{value.inspect}"
end
def add_program(name, value)
rule = value.delete('rule') || value.delete(:rule) || 'link'
value[rule] = extract_objs(name, value)
add_build(name, value)
log 2, "PROGRAM #{name}=#{value.inspect}"
end
def add_library(name, value)
rule = value.delete('rule') || value.delete(:rule) || 'ar'
value[rule] = extract_objs(name, value)
add_build(name, value)
log 2, "LIBRARY #{name}=#{value.inspect}"
end
def add_misc(name, value)
rule = value.delete('rule') || value.delete(:rule)
value[rule] = extract_src(value)
add_build(name, value)
log 2, "MISC #{name}=#{value.inspect}"
end
def add_default(name, value)
@defaults << name
log 2, "DEFAULT #{name}"
end
def add_build(name, value)
@builds[name] = value
end
def extract_objs(name, value)
prefix = File.basename(name).gsub(/\./, '_')
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
when /\A(.+)\.m\Z/
obj = make_obj(prefix, $1)
add_build(obj, {objc: 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 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
def log(lvl, *args)
STDERR.puts(*args) if @options.verbose >= lvl
end
end
if __FILE__ == $0
class NinjaFileBuilder
def check_sdl2(*libs)
options.sdl2_cflags = `sdl2-config --cflags`.chomp
options.sdl2_libs = `sdl2-config --libs`.chomp
libs.each do |lib|
options.sdl2_libs += " -lSDL2_#{lib}"
end
end
def check_boost(*libs)
options.boost_cppflags = '-I$prefix/include'
options.boost_libs = '-L$prefix/lib'
libs.each do |lib|
options.boost_libs += " -lboost_#{lib}"
end
end
def check_wx
options.wx_cppflags = `wx-config --cppflags`
options.wx_libs = `wx-config --libs`
end
end
NinjaFileBuilder.build do
options.cflags = '-Wall -Wextra'
options.cxxflags = '-Wall -Wextra'
options.cppflags = "-I. -I$prefix/include"
options.ldflags = '-L$prefix/lib'
libs = ''
add_option '--debug', '-d', 'enable debug options' do
options.cflags << ' -g'
options.cxxflags << ' -g'
end
add_option '--optimize', '-O', 'enable optimize options' do |v|
options.cflags << ' -O2'
options.cxxflags << ' -O2'
end
add_option '--inline', 'enable inline functions' do
options.cppflags << ' -DSHOOTER_INLINE=inline'
end
add_option '--profile', 'enable profile options' do
options.cflags << ' -coverage'
options.cxxflags << ' -coverage'
options.ldflags << ' -coverage'
options.libs << ' -lgcov'
end
add_option '--enable-c++0x', 'enable C++0x' do
options.cxxflags << ' -std=c++0x'
end
add_option '--enable-c++11', 'enable C++11' do
options.cxxflags << ' -std=c++11'
end
add_option '--with-gcc[=SUFFIX]', 'compile with gcc/g++' do |v|
options.cc = "gcc#{v}"
options.cxx = "g++#{v}"
options.ldflags = "-Wl,-rpath,$prefix/lib/gcc#{v}"
end
add_option '--with-clang', 'compile with clang/clang++' do |v|
options.cc = 'clang'
options.cxx = 'clang++'
end
add_option '--with-csa=PATH', 'compile with Clang Static Analyzer' do |v|
options.cc = File.join(v, 'libexec', 'ccc-analyzer')
options.cxx = File.join(v, 'libexec', 'c++-analyzer')
end
add_option '--with-boost[=LIBS]', 'compile with Boost' do |v|
libs = (v || '').split(/,/)
case RUBY_PLATFORM
when /freebsd/
options.cflags << ' -pthread'
options.ldflags << ' -pthread'
when /darwin/
options.cppflags << ' -DHAVE_MACH_MACH_TIME_H'
libs.map! {|lib| "#{lib}-mt"}
end
check_boost *libs
end
add_option '--with-sdl2[=LIBS]', 'compile with SDL 2' do |v|
libs = (v || '').split(/,/)
check_sdl2 *libs
end
add_option '--with-wx', 'compile with wxWidgets' do
check_wx
end
parse_args
save_ninja_file
end
end