Advertisement
ebak32

scons emitter

Feb 11th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import SCons
  2. import os
  3.  
  4. def infoBuilder(target, source, env):
  5.     print '<<< Printing info >>>'
  6.  
  7.  
  8. FILE_CONTENT= \
  9. r'''
  10. const char * const CfgString = "{}";
  11. '''
  12.  
  13. def generateAction(source, target, env):
  14.     lines = []
  15.     print 'source=' + str(source[0])
  16.     with open(str(source[0])) as f:
  17.         for line in f:
  18.             line = line.strip()
  19.             if line:
  20.                 lines.append(line)
  21.     assert lines
  22.     if not os.path.exists('gen'):
  23.         os.makedirs('gen')
  24.     with open('gen/Cfg.c', 'w') as f:
  25.         f.write(FILE_CONTENT.format(lines[0]))
  26.     env.SideEffect('gen/Cfg.c', target)
  27.     with open(str(target[0]), 'w') as f:
  28.         f.write('')
  29.  
  30. def genEmitter(target, source, env):
  31.     print 'genEmitter trg:{}'.format(target[0])
  32.     from glob import glob
  33.     for cPath in glob('gen/*.c'):
  34.         oPath = 'obj/' + os.path.splitext(os.path.basename(cPath))[0] + '.o'
  35.         obj = env.Object(target=oPath, source=cPath)
  36.         print 'obj=' + str(obj)
  37.         # env.Depends('prg', obj)
  38.         source.append(obj)
  39.     return target, source
  40.  
  41.  
  42. def linkerAction(target, source, env):
  43.     print 'linkerAction'
  44.     sources = [str(src) for src in source]
  45.     cmd = 'gcc -o {} {}'.format(target[0], ' '.join(sources))
  46.     print cmd
  47.     return os.system(cmd)
  48.  
  49.  
  50. def createRules():
  51.     linker = Builder(action=linkerAction, emitter=genEmitter)
  52.     env = Environment(
  53.         BUILDERS={'Linker': linker},
  54.         CXXFLAGS='-O2 -Wall')
  55.     mainObj = env.Object('obj/main.o', 'src/main.c')
  56.     prg = env.Linker('prg', mainObj)  #emitter=genEmitter here doesn't work
  57.     genReady = env.File('gen/genReady')
  58.     env.Command(target=genReady, source='cfg/sample.cfg', action=generateAction)
  59.     env.AlwaysBuild(env.Alias(target='INFO2', action=infoBuilder))
  60.     env.AlwaysBuild(env.Alias(target='INFO', action=infoBuilder))
  61.     env.AlwaysBuild(env.Alias(target='all', source=('INFO', genReady, prg)))
  62.     env.Default('all')
  63.  
  64.  
  65. createRules()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement