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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
#!/usr/bin/env ruby
################################################################################
#
# scons2ninja: A script to create a Ninja build file from SCons.
#
# Copyright (c) 2013 Remko Tronçon
# Licensed under the simplified BSD license.
# See COPYING for details.
#
################################################################################
require 'pathname'
require 'open3'
################################################################################
# Helper for building Ninja files
################################################################################
class NinjaBuilder
attr_reader :targets
def initialize
@header = ""
@variables = ""
@rules = ""
@build = ""
@pools = ""
@flags = Hash.new{ |h,k| h[k] = Hash.new() }
@targets = []
end
def header(text)
@header << text << "\n"
end
def rule(name, opts = {})
@rules << "rule #{name}\n"
opts.each { |k, v| @rules << " " << k.to_s << " = " << v.to_s << "\n" }
@rules << "\n"
end
def pool(name, opts = {})
@pools << "pool #{name}\n"
opts.each { |k, v| @pools << " " << k.to_s << " = " << v.to_s << "\n" }
@pools << "\n"
end
def variable(name, value)
@variables << "#{name} = #{value}\n"
end
def build(target, rule, sources = nil, opts = {})
@build << "build " << str(target) << ": " << rule
@build << " " << str(sources) if sources
@build << " | " << str(opts[:deps]) if opts[:deps]
@build << " || " << str(opts[:order_deps]) if opts[:order_deps]
@build << "\n"
opts.each do |var, value|
next if [:deps, :order_deps].include? var
var = var.to_s
value = str(value)
value = get_flags_variable(var, value) if var.end_with? "flags"
@build << " #{var} = #{value}\n"
end
@targets += list(target)
end
def header_targets
@targets.select { |target| target.end_with? '.h' or target.end_with? '.hh' }
end
def to_s
result = ""
result << @header << "\n"
result << @variables << "\n"
@flags.each { |_, prefix| prefix.each { |k, v| result << "#{v} = #{k}\n" } }
result << "\n"
result << @pools << "\n"
result << @rules << "\n"
result << @build << "\n"
result
end
private
def str(list)
return list.map{ |x| escape(x) }.join(' ') if list.is_a? Enumerable
return @targets.select { |x| list.match(x) }.map { |x| escape(x) }.join(' ') if list.is_a? Regexp
list
end
def escape(s)
s.gsub(/ /, '$ ')
end
def get_flags_variable(type, flags)
return '' if flags.empty?
type_flags = @flags[type]
unless id = type_flags[flags]
id = "#{type}_#{type_flags.size()}"
type_flags[flags] = id
end
"$#{id}"
end
end
################################################################################
# Helper methods & variables
################################################################################
if RUBY_PLATFORM =~ /(win32|mingw32)/
LIB_PREFIX = ""
LIB_SUFFIX = ""
EXE_SUFFIX = ".exe"
else
LIB_PREFIX = "lib"
LIB_SUFFIX = ".a"
EXE_SUFFIX = ""
end
def list(l)
return [] if nil
return l if l.is_a? Enumerable
[l]
end
def get_unary_flags(prefix, flags)
flags.select {|x| /^#{prefix}/i.match(x)}.map { |x| x[prefix.size .. -1] }
end
def extract_unary_flags(prefix, flags)
flag, flags = flags.partition { |x| /^#{prefix}/i.match(x) }
[flag.map { |x| x[prefix.size .. -1] }, flags]
end
def extract_unary_flag(prefix, flags)
flag, flags = extract_unary_flags(prefix, flags)
[flag[0], flags]
end
def extract_binary_flag(prefix, flags)
i = flags.index(prefix)
flag = flags[i + 1]
flags.delete_at(i)
flags.delete_at(i)
[flag, flags]
end
BINARY_FLAGS = ["-framework", "-arch", "-x", "--output-format", "-isystem", "-include"]
def get_non_flags(flags)
skip = false
result = []
flags.each do |f|
if skip
skip = false
elsif BINARY_FLAGS.include? f
skip = true
elsif not f.start_with? "/" and not f.start_with? "-"
result << f
end
end
result
end
def extract_non_flags(flags)
non_flags = get_non_flags(flags)
[non_flags, flags - non_flags]
end
def to_native_path(path)
path.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
end
def from_native_path(path)
path.gsub(File::ALT_SEPARATOR || File::SEPARATOR, File::SEPARATOR)
end
def get_dependencies(target, build_targets)
result = []
queue = $dependencies[target].dup
while queue.size > 0
n = queue.pop
result << n
queue += $dependencies[n].dup
end
# Filter out Value() results
result.select {|x| build_targets.include? x or File.exists? x }
end
def get_built_libs(libs, libpaths, outputs)
canonical_outputs = outputs.map {|p| File.expand_path(p) }
result = []
libpaths.each do |libpath|
libs.each do |lib|
lib_libpath = Pathname.new(libpath) + "#{LIB_PREFIX}#{lib}#{LIB_SUFFIX}"
if canonical_outputs.include? lib_libpath.expand_path.to_s
result << to_native_path(lib_libpath.to_s)
end
end
end
result
end
script = to_native_path($0)
################################################################################
# Configuration
################################################################################
$ninja_post = []
$scons_cmd = "scons"
$scons_dependencies = Dir['SConstruct'] + Dir['**/SConscript']
def ninja_post (&block)
$ninja_post << block
end
CONFIGURATION_FILE = '.scons2ninja.conf'
load CONFIGURATION_FILE
$scons_dependencies = $scons_dependencies.map {|x| to_native_path(x) }
################################################################################
# Rules
################################################################################
ninja = NinjaBuilder.new
ninja.pool 'scons_pool', depth: 1
if RUBY_PLATFORM =~ /(win32|mingw32)/
ninja.rule 'cl',
deps: 'msvc',
command: '$cl /showIncludes $clflags -c $in /Fo$out',
description: 'CXX $out'
ninja.rule 'link',
command: '$link $in $linkflags $libs /out:$out',
description: 'LINK $out'
ninja.rule 'lib',
command: '$lib $libflags /out:$out $in',
description: 'AR $out'
ninja.rule 'rc',
command: '$rc $rcflags /Fo$out $in',
description: 'RC $out'
# SCons doesn't touch files if they didn't change, which makes
# ninja rebuild the file over and over again. There's no touch on Windows :(
# Could implement it with a script, but for now, delete the file if
# this problem occurs. I'll fix it if it occurs too much.
ninja.rule 'scons',
command: "#{$scons_cmd} $out",
pool: 'scons_pool',
description: 'GEN $out'
ninja.rule 'install', command: 'cmd /c copy $in $out'
ninja.rule 'run', command: '$in'
else
ninja.rule 'cxx',
deps: 'gcc',
depfile: '$out.d',
command: '$cxx -MMD -MF $out.d $cxxflags -c $in -o $out',
description: 'CXX $out'
ninja.rule 'cc',
deps: 'gcc',
depfile: '$out.d',
command: '$cc -MMD -MF $out.d $ccflags -c $in -o $out',
description: 'CC $out'
ninja.rule 'link',
command: '$glink -o $out $in $linkflags',
description: 'LINK $out'
ninja.rule 'ar',
command: 'ar $arflags $out $in && ranlib $out',
description: 'AR $out'
# SCons doesn't touch files if they didn't change, which makes
# ninja rebuild the file over and over again. Touching solves this.
ninja.rule 'scons',
command: "#{$scons_cmd} $out && touch $out",
pool: 'scons_pool',
description: 'GEN $out'
ninja.rule 'install', command: 'install $in $out'
ninja.rule 'run', command: './$in'
end
ninja.rule 'moc',
command: '$moc $mocflags -o $out $in',
description: 'MOC $out'
ninja.rule 'rcc',
command: '$rcc $rccflags -name $name -o $out $in',
description: 'RCC $out'
ninja.rule 'uic',
command: '$uic $uicflags -o $out $in',
description: 'UIC $out'
ninja.rule 'lrelease',
command: '$lrelease $lreleaseflags $in -qm $out',
description: 'LRELEASE $out'
ninja.rule 'ibtool',
command: '$ibtool $ibtoolflags --compile $out $in',
description: 'IBTOOL $out'
ninja.rule 'generator',
command: "ruby #{script} ${generator_args}",
pool: 'scons_pool',
generator: '1',
description: 'Regenerating build.ninja'
################################################################################
# Build Statements
################################################################################
generator_args = ARGV.join(' ')
scons_generate_cmd = "#{$scons_cmd} #{generator_args} --tree=all,prune dump_trace=1"
#scons_generate_cmd = 'cmd /c type scons2ninja.in'
#scons_generate_cmd = 'cat scons2ninja.in'
# Pass 1: Parse dependencies (and prefilter some build rules)
build_lines = []
$dependencies = Hash.new {|h, k| h[k] = [] }
previous_file = nil
Open3.popen3(scons_generate_cmd) do |stdin, f, stderr, thread|
stage = :preamble
skip_nth_line = -1
stack = ['.']
f.each_line do |line|
# Skip lines if requested from previous command
skip_nth_line -= 1 if skip_nth_line >= 0
next if skip_nth_line == 0
line.chop!
break if line.start_with? 'scons: done building targets'
case stage
# Pass all lines from the SCons configuration step to output
when :preamble
if /^scons: Building targets .../.match(line)
stage = :build
else
puts line
end
when :build
if line.start_with? '+-'
stage = :dependencies
# Ignore response files from MSVS
elsif /^Using tempfile/.match(line)
skip_nth_line = 2
else
build_lines << line
end
when :dependencies
# Work around bug in SCons that splits output over multiple lines
next unless /^[\s|]+\+\-/.match(line)
level = line.index('+-') / 2
file = line[level*2+2..-1]
file = file[1..-2] if file.start_with? '['
# Check if we use the 'fixed' format which escapes filenames
file = eval('"' + file[1..-2].gsub('"', '\\"') + '"') if file.start_with? '\''
if level < stack.length
stack = stack[0..level-1]
elsif level > stack.length
raise "Internal Error" if level != stack.length + 1
stack << previous_file
end
# Skip absolute paths
$dependencies[stack[-1]] << file unless Pathname.new(file).absolute?
previous_file = file
end
end
unless thread.value.success?
print "Error calling '#{scons_generate_cmd}': "
print stderr.read
exit(-1)
end
end
# Pass 2: Parse build rules
tools = {}
build_lines.each do |line|
# Custom python function
if m = /^(\w+)\(\[([^\]]*)\]/.match(line)
out = m[2].split(',').map { |x| x[1..-2] }
out.each do |x|
# Note: To be more correct, deps should also include $scons_dependencies,
# but this regenerates a bit too often, so leaving it out for now.
ninja.build x, 'scons', nil, deps: get_dependencies(x, ninja.targets)
end
# TextFile
elsif m = /^Creating '([^']+)'/.match(line)
out = m[1]
# Note: To be more correct, deps should also include $scons_dependencies,
# but this regenerates a bit too often, so leaving it out for now.
ninja.build out, 'scons', nil, deps: get_dependencies(out, ninja.targets)
# Install
elsif m = /^Install file: "(.*)" as "(.*)"/.match(line)
ninja.build m[2], 'install', m[1]
elsif m = /^Install directory: "(.*)" as "(.*)"/.match(line)
Dir["#{m[1]}/**"].each do |file|
source = Pathname.new(file)
native_source = to_native_path(source.to_s)
target = Pathname.new(m[2]) + source.relative_path_from(Pathname.new(m[1]))
native_target = to_native_path(target.to_s)
ninja.build native_target, 'install', native_source
end
# Tools
else
command = line.split
flags = command[1..-1]
tool = File.basename(command[0], File.extname(command[0]))
tool = "cxx" if ["clang++", "g++"].include? tool
tool = "cc" if ["clang", "gcc"].include? tool
tool = "glink" if ["cc", "cxx"].include? tool and not flags.include? "-c"
tool.gsub!(/-qt4$/, '')
tools[tool] = command[0]
case tool
############################################################
# clang/gcc tools
############################################################
when 'cc'
out, flags = extract_binary_flag("-o", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'cc', files, order_deps: '_generated_headers', ccflags: flags
when 'cxx'
out, flags = extract_binary_flag("-o", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'cxx', files, order_deps: '_generated_headers', cxxflags: flags
when 'glink'
out, flags = extract_binary_flag("-o", flags)
files, flags = extract_non_flags(flags)
libs = get_unary_flags('-l', flags)
libpaths = get_unary_flags("-L", flags)
dependencies = get_built_libs(libs, libpaths, ninja.targets)
ninja.build out, 'link', files, deps: dependencies, linkflags: flags
when 'ar'
objects, flags = flags.partition { |x| x.end_with? ".o" }
libs, flags = flags.partition { |x| x.end_with? ".a" }
out = libs[0]
ninja.build out, 'ar', objects, arflags: flags
when 'ranlib'
############################################################
# MSVC tools
############################################################
when 'cl'
out, flags = extract_unary_flag("/Fo", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'cl', files, order_deps: '_generated_headers', clflags: flags
when 'lib'
out, flags = extract_unary_flag("/out:", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'lib', files, libflags: flags
when 'link'
objects, flags = flags.partition { |x| x.end_with? ".obj" }
out, flags = extract_unary_flag("/out:", flags)
libs, flags = flags.partition { |x| not x.start_with? "/" and x.end_with? ".lib" }
libpaths = get_unary_flags("/libpath:", flags)
dependencies = get_built_libs(libs, libpaths, ninja.targets)
ninja.build out, 'link', objects, deps: dependencies,
libs: libs, linkflags: flags
when 'rc'
out, flags = extract_unary_flag("/fo", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'rc', files[0], order_deps: '_generated_headers', rcflags: flags
############################################################
# Qt tools
############################################################
when 'moc'
out, flags = extract_binary_flag("-o", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'moc', files, mocflags: flags
when 'uic'
out, flags = extract_binary_flag("-o", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'uic', files, uicflags: flags
when 'lrelease'
out, flags = extract_binary_flag("-qm", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'lrelease', files, lreleaseflags: flags
when 'rcc'
out, flags = extract_binary_flag("-o", flags)
name, flags = extract_binary_flag("-name", flags)
files, flags = extract_non_flags(flags)
deps = get_dependencies(out, ninja.targets) - files
ninja.build out, 'rcc', files, deps: deps, name: name, rccflags: flags
############################################################
# OS X tools
############################################################
when 'ibtool'
out, flags = extract_binary_flag("--compile", flags)
files, flags = extract_non_flags(flags)
ninja.build out, 'ibtool', files, ibtoolflags: flags
else
raise "Unknown tool: '#{line}'"
end
end
end
# Phony target for all generated headers, used as an order-only dependency from all C/C++ sources
ninja.build '_generated_headers', 'phony', ninja.header_targets
# Regenerate build.ninja file
ninja.build 'build.ninja', 'generator', [], deps: [script, CONFIGURATION_FILE] + $scons_dependencies
# Header & variables
ninja.header "# This file is generated by #{script}"
ninja.variable "ninja_required_version", "1.3"
ninja.variable "generator_args", generator_args
tools.each { |k, v| ninja.variable k, v }
# Extra customizations
$ninja_post.each { |p| p.call(ninja) }
################################################################################
# Result
################################################################################
File.open('build.ninja', 'w') { |f| f.write ninja }
|