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

# the following two variables are used by the target "waf dist"
VERSION='0.0.1'
APPNAME='cc_test'

# these variables are mandatory ('/' are converted automatically)
top = '.'
out = 'build'

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

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

def build(bld):

	bld(
		features = 'cc cprogram',
		source = bld.path.ant_glob('**/*.c'),
		target = 'test_c_app',
		uselib_local = 'my_static_lib',
		includes = '. /usr/include')

	bld(
		features = 'cc cstaticlib',
		source = 'test_staticlib.c',
		target='my_static_lib')

### cut here ###

import os, datetime
import Scripting, Utils, Options, Logs, Environment
from Constants import *

def main():
	Scripting.commands = Options.arg_line[:]

	while Scripting.commands:
		x = Scripting.commands.pop(0)

		ini = datetime.datetime.now()
		if x == 'configure':
			fun = Scripting.configure
		elif x == 'build':
			fun = Scripting.build
		else:
			fun = getattr(Utils.g_module, x, None)

		if not fun:
			bld = getattr(Utils.g_module, 'build_context', Utils.Context)()
			fake_build(bld)
			for g in bld.task_manager.groups:
				for t in g.tasks:
					s = getattr(t, 'outputs', [])
					for k in s:
						if k.name.startswith(x):

							print "executing ", t, t.run()
							#n2 = bld.root.find_dir(os.getcwd())
							#print k.relpath_gen(n2)
							#t.position=[0, 0]
							#print t.display()
							#print "try -> waf --targets=%s" % t.generator.name or t.generator.target
			break

		#print Utils.g_module.__dict__.keys()
		ctx = getattr(Utils.g_module, x + '_context', Utils.Context)()

		if x in ['init', 'shutdown', 'dist', 'distclean', 'distcheck']:
			try:
				fun(ctx)
			except TypeError:
				fun()
		else:
			fun(ctx)

		ela = ''
		if not Options.options.progress_bar:
			ela = ' (%s)' % Utils.get_elapsed_time(ini)

		if x != 'init' and x != 'shutdown':
			Logs.info('%r finished successfully%s' % (x, ela))

		if not Scripting.commands and x != 'shutdown':
			Scripting.commands.append('shutdown')

Scripting.main = main


def fake_build(bld):
	"""create all the tasks for the project, but do not run the build
	return the build context in use"""
	bld = Scripting.check_configured(bld)

	Options.commands['install'] = False
	Options.commands['uninstall'] = False
	Options.is_install = False

	bld.is_install = 0 # False

	try:
		proj = Environment.Environment(Options.lockfile)
	except IOError:
		raise Utils.WafError("Project not configured (run 'waf configure' first)")

	bld.load_dirs(proj[SRCDIR], proj[BLDDIR])
	bld.load_envs()

	Logs.info("Waf: Entering directory `%s'" % bld.bldnode.abspath())
	bld.add_subdirs([os.path.split(Utils.g_module.root_path)[0]])

	# execute something immediately before the build starts
	bld.pre_build()
	bld.flush()

