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

import sys
import os.path
import os
import pproc as subprocess

# uncomment to enable profiling information
# epydoc uses the profile data to generate call graphs
#os.environ["PYBINDGEN_ENABLE_PROFILING"] = ""


if 0:
    DEPRECATION_ERRORS = '-Werror::DeprecationWarning' # deprecations become errors
else:
    DEPRECATION_ERRORS = '-Wdefault::DeprecationWarning' # normal python behaviour


def build(bld):
    env = bld.env

    env['TOP_SRCDIR'] = bld.srcnode.abspath()

    gen = bld.new_task_gen(
        features='command',
        source='test-generation.py',
        target='test.cc',
        command='${PYTHON} ${SRC[0]} ${TOP_SRCDIR} > ${TGT[0]}')

    if env['CXX']:
        obj = bld.new_task_gen(features='cxx pyext')
        obj.source = 'test.cc'
        if env['CXX_NAME'] == 'gcc':
            obj.env.append_value('CXXFLAGS', ['-Werror', '-Wno-unused'])
    
    ## manual code generation using simple pybindgen API calls
    bindgen = bld.new_task_gen(
        features='command',
        source='foomodulegen.py',
        target='foomodule.cc',
        command='${PYTHON} %s ${SRC[0]} ${TOP_SRCDIR} > ${TGT[0]}' % (DEPRECATION_ERRORS,))

    if env['CXX']:
        obj = bld.new_task_gen(features='cxx cshlib pyext')
        obj.source = [
            'foo.cc',
            'foomodule.cc'
            ]
        obj.target = 'foo'
        obj.install_path = None
        obj.includes = '.'

    ## automatic code scanning using gccxml
    if env['ENABLE_PYGCCXML']:
        ### Same thing, but using gccxml autoscanning

        bld.new_task_gen(
            features='command',
            source='foomodulegen-auto.py foo.h',
            target='foomodule2.cc foomodulegen_generated.py',
            command='${PYTHON} %s ${SRC[0]} ${SRC[1]} ${cpp_path_repr} ${TGT[1]} > ${TGT[0]}' % (DEPRECATION_ERRORS,),
            variables=dict(cpp_path_repr=repr(bindgen.env['CPPPATH']+bindgen.env['CPPPATH_PYEXT'])))

        obj = bld.new_task_gen(features='cxx cshlib pyext')
        obj.source = [
            'foo.cc',
            'foomodule2.cc'
            ]
        obj.target = 'foo2'
        obj.install_path = None
        obj.includes = '.'

        ### Now using the generated python script

        bld.new_task_gen(
            features='command',
            source='foomodulegen3.py foomodulegen_generated.py',
            target='foomodule3.cc',
            command='${PYTHON} %s ${SRC[0]} ${TGT[0]} > ${TGT[0]}' % (DEPRECATION_ERRORS,))

        ## yes, this global manipulation of PYTHONPATH is kind of evil :-/
        ## TODO: add WAF command-output support for customising command OS environment
        os.environ["PYTHONPATH"] = os.pathsep.join([os.environ.get("PYTHONPATH", ''), bindgen.path.abspath(bindgen.env)])

        obj = bld.new_task_gen(features='cxx cshlib pyext')
        obj.source = [
            'foo.cc',
            'foomodule3.cc'
            ]
        obj.target = 'foo3'
        obj.install_path = None # do not install
        obj.includes = '.'

        ## ---

        bld.new_task_gen(
            features='command',
            source='foomodulegen-auto-split.py foo.h',
            target='foomodulegen_split.py foomodulegen_module1.py foomodulegen_module2.py',
            command='${PYTHON} %s ${SRC[0]} ${SRC[1]} ${cpp_path_repr} ${TGT[0]} ${TGT[1]} ${TGT[2]}' % (DEPRECATION_ERRORS,),
            variables=dict(cpp_path_repr=repr(bindgen.env['CPPPATH']+bindgen.env['CPPPATH_PYEXT'])))

        bld.new_task_gen(
            features='command',
            source=[
                'foomodulegen4.py',
                'foomodulegen_split.py',
                'foomodulegen_module1.py',
                'foomodulegen_module2.py',
                ],
            target=[
                'foomodule4.cc',
                'foomodule4.h',
                'foomodulegen_module1.cc',
                'foomodulegen_module2.cc',
                ],
            command='${PYTHON} %s ${SRC[0]} ${TGT[0]}' % (DEPRECATION_ERRORS,))


        obj = bld.new_task_gen(features='cxx cshlib pyext')
        obj.source = [
            'foo.cc',
            'foomodule4.cc',
            'foomodulegen_module1.cc',
            'foomodulegen_module2.cc',
            ]
        obj.target = 'foo4'
        obj.install_path = None # do not install
        obj.includes = '.'

    ## pure C tests
    bld.add_subdirs('c-hello')