#! /usr/bin/env python
# encoding: utf-8

import os

csources = [ # generate .so files from c sources that can be loaded by guitarix
    "vibe.cc",
    ]

sources = [ # generate .so files that can be loaded by guitarix
    ]

lib_sources = [ # put in static library linked with guitarix
    "zita_rev1.dsp",
    ]

def build(bld):
    if bld.env['FAUST']:
        float_arg = ["-s","40000","--float"]
        if bld.env['FAUST_DOUBLE']:
            arg = ["--double"]
        else:
            arg = ["--float"]
        arg += ["--init-type=plugin-instance"]
        bld(name = "dsp2cc sharedlib",
            source = sources,
            proc = "../tools/dsp2cc",
            gen_dir_suffix = "/generated",
            proc_args = arg+["--template-type=sharedlib"],
            )
        bld(name = "dsp2cc staticlib",
            source = lib_sources,
            proc = "../tools/dsp2cc",
            gen_dir_suffix = "/generated",
            proc_args = arg+["--template-type=staticlib","--in-namespace=pluginlib"],
            )
    else:
        gdir = "generated/"
        for s in sources+lib_sources:
            s = s.replace(".dsp",".cc")
            bld(name = "copy-faust-plugin-cc",
                rule = "cp ${SRC} ${TGT}",
                source = gdir + s,
                target = s,
            )
    def run(task):
        l = []
        for i in task.inputs:
            s = ("namespace %s { PluginDef *plugin(); }\n"
                 % os.path.splitext(os.path.basename(i.name))[0])
            l.append(s)
        f = file(task.outputs[0].bldpath(task.env),"w")
        f.write('#include "gx_plugin.h"\n\nnamespace pluginlib {\n')
        f.write("".join(l))
        f.write("}\n")
        f.close()
    bld(name = "pluginlib.h",
        source = lib_sources,
        rule = run,
        target = "pluginlib.h",
        )
    bld.add_group()
    have_pluginlib = len(lib_sources) != 0
    bld.env["HAVE_PLUGINLIB"] = have_pluginlib
    if have_pluginlib:
        bld(features = ['cxx', 'cstaticlib'],
            includes = ["../headers"],
            source = [s.replace(".dsp",".cc") for s in lib_sources],
            target = "plugins", # (also defines name of task)
            )
    for s in sources:
        bld(name = "create plugin .so",
            features = ['cxx', 'cshlib'],
            type = 'cshlib',
            cxxflags = ["-fvisibility=hidden"],
            includes = ["../headers"],
            source = [s.replace(".dsp",".cc")],
            target = s.replace(".dsp",""),
            )
    for s in csources:
        bld(name = "create plugin .so",
            features = ['cxx', 'cshlib'],
            type = 'cshlib',
            cxxflags = ["-fvisibility=hidden"],
            includes = ["../headers"],
            source = csources,
            target = s.replace(".cc",""),
            install_path = None, # for now...
            )
    

def configure(conf):
    pass
