import("core.project.depend")
function cmd_compile(genfile, sourcefile, template, macro, define)
    import("find_sdk")
    local meta = find_sdk.find_my_program("refl")
    template = template or path.join(meta.sdkdir, "template")
    if not macro then --优先使用库定义
        macro = path.join(os.projectdir(), "engine/3rdparty/zlib/include/refl/macro.h")
        if not os.exists(macro) then
            macro = path.join(os.curdir(), "macro.h")
        end
    end
    argv = {"build", sourcefile, "-o", genfile, "-t", template, "-m", macro}
    if define then
        table.insert(argv, "-d")
        table.insert(argv, define)
    end
    print("cmd_meta_compile", genfile)
    os.runv(meta.program, argv)
    return argv
end

function _listen_gen_vs_file(target, batch)
    -- genfile, sourcefile = batch[1], batch[2]
    -- local dependfile = target:dependfile(genfile)
    -- depend.on_changed(
    --     function()
    --         cmd_compile(batch[1], batch[2], template, macro, define)
    --     end,
    --     {dependfile = dependfile, files = sourcefile}
    -- )
end
function compile(target)
    local glsl_batch = target:data("glsl.batch")
    if not glsl_batch then
        return
    end
    for _, batch in ipairs(glsl_batch.vs) do
        local dir = path.directory(batch)
        local vs = path.filename(batch)
        local vert = string.gsub(vs, ".vs.glsl$", ".vert.spv")
        _listen_gen_vs_file(target, dir, vert)
    end
end
function main(target)
    local ps = {}
    local psfiles = target:extraconf("rules", "glsl.env", "ps")
    for _, file in ipairs(psfiles) do
        local p = path.join(target:scriptdir(), file)
        for __, filepath in ipairs(os.files(p)) do
            table.insert(ps, filepath)
        end
    end
    local vsfiles = target:extraconf("rules", "glsl.env", "vs")
    for _, file in ipairs(vsfiles) do
        local p = path.join(target:scriptdir(), file)
        for __, filepath in ipairs(os.files(p)) do
            table.insert(vs, filepath)
        end
    end
    local glslfiles = {ps = ps, vs = vs}
    target:data_set("glsl.batch", glslfiles)
end
