summaryrefslogtreecommitdiffstats
blob: 1af52d704131689aa577e78f9e043f74004c241e (plain)
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env python
# -*- coding: utf-8 -*- 

################################################################################
#
# 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.
#
################################################################################

import re, os, os.path, subprocess, sys, fnmatch

################################################################################
# Helper methods & variables
################################################################################
      
SCRIPT = sys.argv[0]
SCONS_ARGS = ' '.join(sys.argv[1:])

# TODO: Make this a tool-specific map
BINARY_FLAGS = ["-framework", "-arch", "-x", "--output-format", "-isystem", "-include"]

if sys.platform == 'win32' :
  LIB_PREFIX = ""
  LIB_SUFFIX = ""
  EXE_SUFFIX = ".exe"
else :
  LIB_PREFIX = "lib"
  LIB_SUFFIX = ".a"
  EXE_SUFFIX = ""

def is_regexp(x) :
  return 'match' in dir(x)

def is_list(l) :
  return type(l) is list

def escape(s) :
  return s.replace(' ', '$ ').replace(':', '$:')
  
def to_list(l) :
  if not l :
    return []
  if is_list(l) :
    return l
  return [l]

def partition(l, f) :
  x = []
  y = []
  for v in l :
    if f(v) :
      x.append(v)
    else :
      y.append(v)
  return (x, y)

def get_unary_flags(prefix, flags) :
  return [x[len(prefix):] for x in flags if x.lower().startswith(prefix.lower())]

def extract_unary_flags(prefix, flags) :
  f1, f2 = partition(flags, lambda x : x.lower().startswith(prefix.lower()))
  return ([f[len(prefix):] for f in f1], f2)

def extract_unary_flag(prefix, flags) :
  flag, flags = extract_unary_flags(prefix, flags)
  return (flag[0], flags)

def extract_binary_flag(prefix, flags) :
  i = flags.index(prefix)
  flag = flags[i + 1]
  del flags[i]
  del flags[i]
  return (flag, flags)

def get_non_flags(flags) :
  skip = False
  result = []
  for f in flags :
    if skip :
      skip = False
    elif f in BINARY_FLAGS :
      skip = True
    elif not f.startswith("/") and not f.startswith("-") :
      result.append(f)
  return result

def extract_non_flags(flags) :
  non_flags = get_non_flags(flags)
  return (non_flags, filter(lambda x : x not in non_flags, flags))

def get_dependencies(target, build_targets) :
  result = []
  queue = list(dependencies.get(target, []))
  while len(queue) > 0 :
    n = queue.pop()
    # Filter out Value() results
    if n in build_targets or os.path.exists(n) :
      result.append(n)
      queue += list(dependencies.get(n, []))
  return result

def get_built_libs(libs, libpaths, outputs) :
  canonical_outputs = [os.path.abspath(p) for p in outputs]
  result = []
  for libpath in libpaths :
    for lib in libs :
      lib_libpath = os.path.join(libpath, LIB_PREFIX + lib + LIB_SUFFIX)
      if os.path.abspath(lib_libpath) in canonical_outputs :
        result.append(lib_libpath)
  return result

def parse_tool_command(line) :
  command = line.split(' ')
  flags = command[1:]
  tool = os.path.splitext(os.path.basename(command[0]))[0]
  if tool.startswith('clang++') or tool.startswith('g++') :
    tool = "cxx"
  elif tool.startswith('clang') or tool.startswith('gcc') :
    tool = "cc"
  if tool in ["cc", "cxx"] and not "-c" in flags :
    tool = "glink"
  tool = tool.replace('-qt4', '')
  return tool, command, flags

def rglob(pattern, root = '.') :
  return [os.path.join(path, f) for path, dirs, files in os.walk(root) for f in fnmatch.filter(files, pattern)]

################################################################################
# Helper for building Ninja files
################################################################################

class NinjaBuilder :
  def __init__(self) :
    self._header = ""
    self.variables = ""
    self.rules = ""
    self._build = ""
    self.pools = ""
    self._flags = {}
    self.targets = []
  
  def header(self, text) :
    self._header += text + "\n"

  def rule(self, name, **kwargs) :
    self.rules += "rule " + name + "\n"
    for k, v in kwargs.iteritems() :
      self.rules += "  " + str(k) + " = " + str(v) + "\n"
    self.rules += "\n"
  
  def pool(self, name, **kwargs) :
    self.pools += "pool " + name + "\n"
    for k, v in kwargs.iteritems() :
      self.pools += "  " + str(k) + " = " + str(v) + "\n"
    self.pools += "\n"

  def variable(self, name, value) :
    self.variables += str(name) + " = " + str(value) + "\n"
  
  def build(self, target, rule, sources = None, **kwargs) :
    self._build += "build " + self.to_string(target) + ": " + rule
    if sources :
      self._build += " " + self.to_string(sources)
    if 'deps' in kwargs and kwargs['deps'] :
      self._build += " | " + self.to_string(kwargs["deps"])
    if 'order_deps' in kwargs :
      self._build += " || " + self.to_string(kwargs['order_deps'])
    self._build += "\n"
    for var, value in kwargs.iteritems() :
      if var in ['deps', 'order_deps'] :
        continue
      value = self.to_string(value)
      if var.endswith("flags") :
        value = self.get_flags_variable(var, value)
      self._build += "  " + var + " = " + value + "\n"
    self.targets += to_list(target)
  
  def header_targets(self) :
    return [x for x in self.targets if x.endswith('.h') or x.endswith('.hh')]
  
  def serialize(self) :
    result = ""
    result += self._header + "\n"
    result += self.variables + "\n"
    for prefix in self._flags.values() :
      for k, v in prefix.iteritems() :
        result += v + " = " + k + "\n"
    result += "\n"
    result += self.pools + "\n"
    result += self.rules + "\n"
    result += self._build + "\n"
    return result
  
  def to_string(self, lst) :
    if is_list(lst) :
      return ' '.join([escape(x) for x in lst]) 
    if is_regexp(lst) :
      return ' '.join([escape(x) for x in self.targets if lst.match(x)])
    return escape(lst)
  
  def get_flags_variable(self, flags_type, flags) :
    if len(flags) == 0 :
      return ''
    if flags_type not in self._flags :
      self._flags[flags_type] = {}
    type_flags = self._flags[flags_type]
    if flags not in type_flags :
      type_flags[flags] = flags_type + "_" + str(len(type_flags))
    return "$" + type_flags[flags]
  

################################################################################
# Configuration
################################################################################

ninja_post = []
scons_cmd = "scons"
scons_dependencies = ['SConstruct'] + rglob('SConscript')

CONFIGURATION_FILE = '.scons2ninja.conf'
execfile(CONFIGURATION_FILE)

scons_dependencies = [os.path.normpath(x) for x in scons_dependencies]


################################################################################
# Rules
################################################################################

ninja = NinjaBuilder()

ninja.pool('scons_pool', depth = 1)

if sys.platform == 'win32' :
  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('link_mt',
    command = '$link $in $linkflags $libs /out:$out ; $mt $mtflags',
    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 + " ${scons_args} $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')


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 = "python " + SCRIPT + " ${scons_args}",
  pool = 'scons_pool',
  generator = '1',
  description = 'Regenerating build.ninja')


################################################################################
# Build Statements
################################################################################

scons_generate_cmd = scons_cmd + " " + SCONS_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 = {}
mtflags = {}
previous_file = None
f = subprocess.Popen(scons_generate_cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell=True)
stage = 'preamble'
skip_nth_line = -1
stack = ['.']
for line in f.stdout.readlines() :
  line = line.rstrip()

  # Skip lines if requested from previous command
  if skip_nth_line >= 0 :
    skip_nth_line -= 1 
  if skip_nth_line == 0 :
    continue

  if line.startswith('scons: done building targets') :
    break

  if stage == "preamble" :
    # Pass all lines from the SCons configuration step to output
    if re.match("^scons: Building targets ...", line) :
      stage = "build"
    else :
      print line
    
  elif stage == "build" :
    if line.startswith('+-') :
      stage = "dependencies"
    elif re.match("^Using tempfile", line) :
      # Ignore response files from MSVS
      skip_nth_line = 2
    else :
      build_lines.append(line)

      # Already detect targets that will need 'mt'
      tool, _, flags = parse_tool_command(line)
      if tool == 'mt' :
        target = get_unary_flags("-outputresource:", flags)[0]
        target = target[0:target.index(';')]
        mtflags[target] = flags

  elif stage == "dependencies" :
    if not re.match('^[\s|]+\+\-', line) :
      # Work around bug in SCons that splits output over multiple lines
      continue

    level = line.index('+-') / 2
    filename = line[level*2+2:]
    if filename.startswith('[') :
      filename = filename[1:-1] 

    # Check if we use the 'fixed' format which escapes filenamenames
    if filename.startswith('\'') and filename.endswith('\'') :
      filename = eval(filename)

    if level < len(stack) :
      stack = stack[0:level]
    elif level > len(stack) :
      if level != len(stack) + 1 :
        raise Exception("Internal Error" )
      stack.append(previous_filename)
    
    # Skip absolute paths
    if not os.path.isabs(filename) :
      target = stack[-1]
      if target not in dependencies :
        dependencies[target] = []
      dependencies[target].append(filename)
    previous_filename = filename

if f.wait() != 0 :
  print "Error calling '" + scons_generate_cmd + "'"
  print f.stderr.read()
  exit(-1)

# Pass 2: Parse build rules
tools = {}
for line in build_lines :
  # Custom python function
  m = re.match('^(\w+)\(\[([^\]]*)\]', line)
  if m :
    out = [x[1:-1] for x in m.group(2).split(',')]
    for x in out :
      # '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', None, deps = sorted(get_dependencies(x, ninja.targets)))
    continue
    

  # TextFile
  m = re.match("^Creating '([^']+)'", line)
  if m :
    out = m.group(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', None, deps = sorted(get_dependencies(out, ninja.targets)))
    continue

  # Install
  m = re.match('^Install file: "(.*)" as "(.*)"', line)
  if m :
    ninja.build(m.group(2), 'install', m.group(1))
    continue

  m = re.match('^Install directory: "(.*)" as "(.*)"', line)
  if m :
    for source in rglob('*', m.group(1)) :
      if os.path.isdir(source) :
        continue
      target = os.path.join(m.group(2), os.path.relpath(source, m.group(1)))
      ninja.build(target, 'install', source)
    continue

  # Tools
  tool, command, flags = parse_tool_command(line)
  tools[tool] = command[0]

  ############################################################
  # clang/gcc tools
  ############################################################

  if tool == '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)

  elif tool == '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)

  elif tool == '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)
    deps = get_built_libs(libs, libpaths, ninja.targets)
    ninja.build(out, 'link', files, deps = sorted(deps), linkflags = flags)

  elif tool == 'ar':
    objects, flags = partition(flags, lambda x: x.endswith('.o'))
    libs, flags = partition(flags, lambda x: x.endswith('.a'))
    out = libs[0]
    ninja.build(out, 'ar', objects, arflags = flags)

  elif tool == 'ranlib':
    pass


  ############################################################
  # MSVC tools
  ############################################################
  
  elif tool == '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)

  elif tool == 'lib':
    out, flags = extract_unary_flag("/out:", flags)
    files, flags = extract_non_flags(flags)
    ninja.build(out, 'lib', files, libflags = flags)

  elif tool == 'link':
    objects, flags = partition(flags, lambda x: x.endswith('.obj') or x.endswith('.res'))
    out, flags = extract_unary_flag("/out:", flags)
    libs, flags = partition(flags, lambda x: not x.startswith("/") and x.endswith(".lib"))
    libpaths = get_unary_flags("/libpath:", flags)
    deps = get_built_libs(libs, libpaths, ninja.targets)
    if out in mtflags :
      ninja.build(out, 'link_mt', objects, deps = sorted(deps), 
        libs = libs, linkflags = flags, mtflags = mtflags[out])
    else :
      ninja.build(out, 'link', objects, deps = sorted(deps), 
        libs = libs, linkflags = flags)

  elif tool == '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)

  elif tool == 'mt':
    # Already handled
    pass

  ############################################################
  # Qt tools
  ############################################################
  
  elif tool == 'moc':
    out, flags = extract_binary_flag("-o", flags)
    files, flags = extract_non_flags(flags)
    ninja.build(out, 'moc', files, mocflags = flags)

  elif tool == 'uic':
    out, flags = extract_binary_flag("-o", flags)
    files, flags = extract_non_flags(flags)
    ninja.build(out, 'uic', files, uicflags = flags)

  elif tool == 'lrelease':
    out, flags = extract_binary_flag("-qm", flags)
    files, flags = extract_non_flags(flags)
    ninja.build(out, 'lrelease', files, lreleaseflags = flags)

  elif tool == 'rcc':
    out, flags = extract_binary_flag("-o", flags)
    name, flags = extract_binary_flag("-name", flags)
    files, flags = extract_non_flags(flags)
    deps = list(set(get_dependencies(out, ninja.targets)) - set(files))
    ninja.build(out, 'rcc', files, deps = sorted(deps), name = name, rccflags = flags)

  ############################################################
  # OS X tools
  ############################################################

  elif tool == 'ibtool':
    out, flags = extract_binary_flag("--compile", flags)
    files, flags = extract_non_flags(flags)
    ninja.build(out, 'ibtool', files, ibtoolflags = flags)

  else :
    raise Exception("Unknown tool: '" + line + "'")
    
  
# Phony target for all generated headers, used as an order-only depency 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("scons_args", SCONS_ARGS)
for k, v in tools.iteritems() :
  ninja.variable(k, v)

# Extra customizations
if 'ninja_post' in dir() :
  ninja_post(ninja)


################################################################################
# Result
################################################################################

f = open("build.ninja", "w")
f.write(ninja.serialize())
f.close()