
target("TestCounter")
    add_rules("verilua")
    add_toolchains("@verilator")

    add_files("./Counter.v")

    set_values("cfg.top", "Counter")
    set_values("cfg.lua_main", "main.lua")


    local svparser_bin = "svparser"
    before_build(function (target)
        local sourcefiles = target:sourcefiles()
        for i, file in ipairs(sourcefiles) do
            local target_module = "Counter"
            local target_file = "Counter.v"
            if file:endswith(target_file) then
                os.exec(svparser_bin .. " -f %s -m %s -od ./.vrtl", file, target_module)
                sourcefiles[i] = "./.vrtl/" .. target_file
            end
        end
        target:set("files", sourcefiles)
    end)


    add_links("Counter_vrtl")
    add_linkdirs("./target/release")
    add_runenvs("LD_LIBRARY_PATH", "./target/release")

    add_values("verilator.flags", "+define+VIRTUAL_RTL")
