#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2010 (ita)

"""
execute "waf clean build", and look at the Makefile generated
"""

VERSION='0.0.1'
APPNAME='cc_test'

top = '.'
out = 'build'

def set_options(opt):
	opt.tool_options('compiler_cc')

def configure(conf):
	conf.check_tool('compiler_cc')

	# to extract the parameters:
	old = conf.__class__.run_c_code
	def run_c_code(*k, **kw):
		print("calling run_c_code with", kw)
		old(*k, **kw)
	conf.__class__.run_c_code = run_c_code

	conf.check_cc(fragment='int main() {return 0; }')

def build(bld):

	bld(
		features = 'cc cprogram',
		source   = 'main.c',
		target   = 'foo',
	)

	#---------------------------------------------------------------------------------
	# dump a Makefile corresponding to the code executed while performing a full build

	import Task
	bld.commands = []
	bld.targets = []

	old = Task.TaskBase.exec_command
	def exec_command(self, *k, **kw):
		ret = old(self, *k, **kw)
		self.command_executed = k[0]
		self.path = kw['cwd'] or self.generator.bld.bldnode.abspath()
		return ret
	Task.TaskBase.exec_command = exec_command

	def call_run(self):

		lst = []

		for x in self.outputs:
			lst.append(x.abspath(self.env))

		bld.targets.extend(lst)

		lst.append(':')

		for x in self.inputs:
			lst.append(x.abspath(self.env))

		ret = self.run()

		try:
			if isinstance(self.command_executed, list):
				self.command_executed = ' '.join(self.command_executed)
		except Exception, e:
			print e
		else:
			bld.commands.append(' '.join(lst))
			bld.commands.append('\tcd %s && %s' % (self.path, self.command_executed))

		return ret

	Task.TaskBase.call_run = call_run

	def output_makefile(self):
		try:
			self.commands.insert(0, "all: %s" % " ".join(self.targets))
			f = open('Makefile', 'w')
			f.write("\n".join(self.commands))
		finally:
			f.close()
	bld.add_post_fun(output_makefile)

