def options(opt):
    opt.load('python')


def configure(conf):
    conf.load('python')
    if not conf.env.ENABLE_CROSS:
        conf.check_python_version((2, 6, 0))
    conf.check_python_headers(features='pyext')  # Extension-only, no embedded


def build(ctx):
    srcnode = ctx.srcnode.make_node('pylib')
    bldnode = ctx.bldnode.make_node('pylib')
    target1 = bldnode.make_node('control.py')
    target2 = bldnode.make_node('magic.py')

    sources = srcnode.ant_glob('*.py')
    builds = [x.get_bld() for x in sources]

    # Remove generated files to ensure they are properly updated
    ctx.exec_command("rm -f %s" % target1.abspath())
    ctx.exec_command("rm -f %s" % target2.abspath())

    # Make sure Python sees .py as well as .pyc/.pyo
    ctx(
        features="subst",
        source=sources,
        target=builds,
    )

    ctx(
        before=['pyc', 'pyo'],
        cwd=srcnode,
        rule='${PYTHON} ${SRC} >${TGT}',
        source=["../wafhelpers/pythonize-header", "../include/ntp_control.h"],
        target=target1,
    )

    ctx(
        before=['pyc', 'pyo'],
        cwd=srcnode,
        rule='${PYTHON} ${SRC} >${TGT}',
        source=["../wafhelpers/pythonize-header", "../include/ntp.h"],
        target=target2,
    )

    # Force early creation of generated files
    ctx.add_group()

    ctx(
        features='py',
        source=builds+[target1, target2],
        install_from=bldnode,
        install_path='${PYTHONDIR}/ntp'
    )
