wscript (2110B)
1 #! /usr/bin/env python 2 # encoding: utf-8 3 # Thomas Nagy, 2010 (ita) 4 5 """ 6 Calling 'waf build' executes a normal build with Waf 7 Calling 'waf clean dump' will create a makefile corresponding to the build 8 The dependencies will be extracted too 9 """ 10 11 VERSION='0.0.1' 12 APPNAME='cc_test' 13 14 top = '.' 15 16 def options(opt): 17 opt.load('compiler_c') 18 19 def configure(conf): 20 conf.load('compiler_c') 21 22 def build(bld): 23 bld.program(source='main.c', target='app', use='mylib', cflags=['-O2']) 24 bld.stlib(source='a.c', target='mylib') 25 26 # --------------------------------------------------------------------------- 27 28 from waflib import Build, Logs 29 class Dumper(Build.BuildContext): 30 fun = 'dump' 31 cmd = 'dump' 32 33 def dump(bld): 34 # call the build function as if a real build were performed 35 build(bld) 36 37 from waflib import Task 38 bld.commands = [] 39 bld.targets = [] 40 41 # store the command executed 42 old_exec = Task.Task.exec_command 43 def exec_command(self, *k, **kw): 44 ret = old_exec(self, *k, **kw) 45 self.command_executed = k[0] 46 self.path = kw['cwd'] or self.generator.bld.cwd 47 return ret 48 Task.Task.exec_command = exec_command 49 50 # perform a fake build, and accumulate the makefile bits 51 old_process = Task.Task.process 52 def process(self): 53 old_process(self) 54 55 lst = [] 56 for x in self.outputs: 57 lst.append(x.path_from(self.generator.bld.bldnode)) 58 bld.targets.extend(lst) 59 lst.append(':') 60 for x in self.inputs + self.dep_nodes + self.generator.bld.node_deps.get(self.uid(), []): 61 lst.append(x.path_from(self.generator.bld.bldnode)) 62 try: 63 if isinstance(self.command_executed, list): 64 self.command_executed = ' '.join(self.command_executed) 65 except Exception as e: 66 print(e) 67 else: 68 bld.commands.append(' '.join(lst)) 69 bld.commands.append('\tcd %s && %s' % (self.path, self.command_executed)) 70 Task.Task.process = process 71 72 # write the makefile after the build is complete 73 def output_makefile(self): 74 self.commands.insert(0, "all: %s" % " ".join(self.targets)) 75 node = self.bldnode.make_node('Makefile') 76 node.write('\n'.join(self.commands)) 77 Logs.warn('Wrote %r', node) 78 bld.add_post_fun(output_makefile) 79