## -*- python -*-
import Action
import Object
import Params
import Task

import os, sys
sys.path.insert(0, os.getcwd()) # to work with uninstalled pybindgen

from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink)


def _pybindgen_action(task):
    target, = task.m_outputs
    out_file = file(target.bldpath(task.m_env), "w")
    task.function(out_file)
    out_file.close()

class PyBindGen(Object.genobj):
    def __init__(self, env=None):
        Object.genobj.__init__(self, 'other')
        self.install_var = 0
        self.env = env
        if not self.env:
            self.env = Params.g_build.m_allenvs['default'].copy()
        self.function = None
        self.target = None
        self.prio = 5
    def apply(self):
        function = self.function
        task = self.create_task('pybindgen', self.env, self.prio)
        task.function = self.function
        target = self.path.find_build(self.target)
        task.set_outputs([target])
        task.set_inputs([self.path.find_source('wscript')])
    def install(self):
        pass


    
def my_module_gen(out_file):
    print >> out_file, "#include <Python.h>"
    print >> out_file, "#include \"foo.h\""

    mod = Module('foo')

    mod.add_function(Function(ReturnValue('int'), 'print_something',
                              [Parameter('const char*', 'message')]))

    mod.add_function(Function(ReturnValue('int'), 'print_something_else',
                              [Parameter('const char*', 'message2')]))

    mod.generate(FileCodeSink(out_file))



def build(bld):
    Object.register('pybindgen', PyBindGen)
    Action.Action('pybindgen', func=_pybindgen_action, color='BLUE')

    bindgen = bld.create_obj('pybindgen')
    bindgen.target = 'foomodule.cc'
    bindgen.function = my_module_gen
    bindgen.prio = 1

    obj = bld.create_obj('cpp', 'shlib', 'pyext')
    obj.source = [
        'foo.cc',
        'foomodule.cc'
        ]
    obj.target = 'foo'

    ## Workaround a WAF bug (http://code.google.com/p/waf/issues/detail?id=37)
    obj.env.append_value("CPPPATH", bld.m_curdirnode.srcpath(obj.env))

