Skip to content

Commit

Permalink
Merge pull request #5062 from xmake-io/verilator
Browse files Browse the repository at this point in the history
Support Verilator target build to static library
  • Loading branch information
waruqi committed May 5, 2024
2 parents 611513a + ba1f9f4 commit f6f17c7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tests/projects/embed/verilator/static/src/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module hello;
initial begin
$display("hello world!");
$finish ;
end
endmodule
12 changes: 12 additions & 0 deletions tests/projects/embed/verilator/static/src/sim_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "hello.h"
#include "verilated.h"

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
hello* top = new hello{contextp};
while (!contextp->gotFinish()) { top->eval(); }
delete top;
delete contextp;
return 0;
}
9 changes: 9 additions & 0 deletions tests/projects/embed/verilator/static/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_requires("verilator")
target("hello")
add_rules("verilator.static")
set_toolchains("@verilator")
add_files("src/*.v")

target("test")
add_deps("hello")
add_files("src/*.cpp")
10 changes: 5 additions & 5 deletions xmake/rules/verilator/verilator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ endmodule]])
if not os.isfile(autogendir) then
os.mkdir(autogendir)
end
target:add("includedirs", autogendir)
target:add("includedirs", path.join(verilator_root, "include"))
target:add("includedirs", path.join(verilator_root, "include", "vltstd"))
target:add("includedirs", autogendir, {public = true})
target:add("includedirs", path.join(verilator_root, "include"), {public = true})
target:add("includedirs", path.join(verilator_root, "include", "vltstd"), {public = true})

-- set languages
local languages = target:get("languages")
Expand All @@ -188,12 +188,12 @@ endmodule]])
end
end
if not cxxlang then
target:add("languages", "c++20")
target:add("languages", "c++20", {public = true})
end

-- add definitions for switches
for k, v in table.orderpairs(switches) do
target:add("defines", "VM_" .. k .. "=" .. v)
target:add("defines", "VM_" .. k .. "=" .. v, {public = true})
end

-- add syslinks
Expand Down
28 changes: 28 additions & 0 deletions xmake/rules/verilator/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

-- @see https://github.com/xmake-io/xmake/issues/3257
rule("verilator.binary")
add_deps("c++")
set_extensions(".v", ".sv")
on_load(function (target)
target:set("kind", "binary")
Expand All @@ -45,3 +46,30 @@ rule("verilator.binary")
import("verilator").buildcmd_cppfiles(target, batchcmds, sourcebatch, opt)
end)

rule("verilator.static")
add_deps("c++")
set_extensions(".v", ".sv")
on_load(function (target)
target:set("kind", "static")
end)

on_config(function (target)
import("verilator").config(target)
end)

before_build_files(function (target, sourcebatch)
-- Just to avoid before_buildcmd_files being executed at build time
end)

on_build_files(function (target, batchjobs, sourcebatch, opt)
import("verilator").build_cppfiles(target, batchjobs, sourcebatch, opt)
end, {batch = true, distcc = true})

before_buildcmd_files(function(target, batchcmds, sourcebatch, opt)
import("verilator").buildcmd_vfiles(target, batchcmds, sourcebatch, opt)
end)

on_buildcmd_files(function (target, batchcmds, sourcebatch, opt)
import("verilator").buildcmd_cppfiles(target, batchcmds, sourcebatch, opt)
end)

0 comments on commit f6f17c7

Please sign in to comment.