Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SCons
- import os
- def infoBuilder(target, source, env):
- print '<<< Printing info >>>'
- FILE_CONTENT= \
- r'''
- const char * const CfgString = "{}";
- '''
- def generateAction(source, target, env):
- lines = []
- print 'source=' + str(source[0])
- with open(str(source[0])) as f:
- for line in f:
- line = line.strip()
- if line:
- lines.append(line)
- assert lines
- if not os.path.exists('gen'):
- os.makedirs('gen')
- with open('gen/Cfg.c', 'w') as f:
- f.write(FILE_CONTENT.format(lines[0]))
- env.SideEffect('gen/Cfg.c', target)
- with open(str(target[0]), 'w') as f:
- f.write('')
- def genEmitter(target, source, env):
- print 'genEmitter trg:{}'.format(target[0])
- from glob import glob
- for cPath in glob('gen/*.c'):
- oPath = 'obj/' + os.path.splitext(os.path.basename(cPath))[0] + '.o'
- obj = env.Object(target=oPath, source=cPath)
- print 'obj=' + str(obj)
- # env.Depends('prg', obj)
- source.append(obj)
- return target, source
- def linkerAction(target, source, env):
- print 'linkerAction'
- sources = [str(src) for src in source]
- cmd = 'gcc -o {} {}'.format(target[0], ' '.join(sources))
- print cmd
- return os.system(cmd)
- def createRules():
- linker = Builder(action=linkerAction, emitter=genEmitter)
- env = Environment(
- BUILDERS={'Linker': linker},
- CXXFLAGS='-O2 -Wall')
- mainObj = env.Object('obj/main.o', 'src/main.c')
- prg = env.Linker('prg', mainObj) #emitter=genEmitter here doesn't work
- genReady = env.File('gen/genReady')
- env.Command(target=genReady, source='cfg/sample.cfg', action=generateAction)
- env.AlwaysBuild(env.Alias(target='INFO2', action=infoBuilder))
- env.AlwaysBuild(env.Alias(target='INFO', action=infoBuilder))
- env.AlwaysBuild(env.Alias(target='all', source=('INFO', genReady, prg)))
- env.Default('all')
- createRules()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement