Skip to content

Commit

Permalink
Add support for verilog_module from rules_verilog. (#9)
Browse files Browse the repository at this point in the history
`verilog_module` provides a concise way to provide
`verilator_cc_library` the verilog sources, dependencies, and top
module name of a verilog module.
  • Loading branch information
agoessling authored Nov 22, 2020
1 parent 8025d5c commit 6831bd5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,28 @@ cc_binary(
)
```

Verilog libraries can also be specifed as dependencies
The details of a verilog module (sources, top name, etc.) can also be specified by a `verilog_module`
which can be reused in other rules.

```python
load("@rules_verilator//verilator:defs.bzl", "sv_library", "verilator_cc_library")
load("@rules_verilog//verilog:defs.bzl", "verilog_module")

sv_library(
name = "alu_lib",
load("@rules_verilator//verilator:defs.bzl", "verilator_cc_library")

verilog_module(
name = "alu_module",
top = "alu",
srcs = ["alu.sv"],
)

verilator_cc_library(
name = "alu",
mtop = "alu",
deps = [":alu_lib"],
module = ":alu_module",
)
```

See [test/alu/BUILD](test/alu/BUILD) for working examples.

## License

Released under Apache 2.0.
19 changes: 19 additions & 0 deletions test/alu/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_verilog//verilog:defs.bzl", "verilog_module")

load("@rules_verilator//verilator:defs.bzl", "sv_library", "verilator_cc_library")

sv_library(
Expand All @@ -16,3 +18,20 @@ cc_binary(
srcs = ["alu.cpp"],
deps = [":alu"],
)

verilog_module(
name = "alu_module",
top = "alu",
srcs = ["alu.sv"],
)

verilator_cc_library(
name = "alu_with_module",
module = ":alu_module",
)

cc_binary(
name = "alu_bin_with_module",
srcs = ["alu.cpp"],
deps = [":alu_with_module"],
)
24 changes: 17 additions & 7 deletions verilator/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_verilog//verilog:defs.bzl", "VerilogModuleInfo")

load(
"@rules_verilator//verilator/internal:cc_actions.bzl",
"cc_compile_and_link_static_library",
Expand Down Expand Up @@ -80,14 +82,18 @@ def _verilator_cc_library(ctx):
verilator_toolchain = ctx.toolchains[_TOOLCHAIN_TYPE].verilator_toolchain

# Gather all the Verilog source files, including transitive dependencies
module_srcs = ctx.attr.module[VerilogModuleInfo].files.to_list() if ctx.attr.module else []
srcs = get_transitive_sources(
ctx.files.srcs + ctx.files.hdrs,
ctx.files.srcs + ctx.files.hdrs + module_srcs,
ctx.attr.deps,
)

# Default Verilator output prefix (e.g. "Vtop")
mtop = ctx.label.name if not ctx.attr.mtop else ctx.attr.mtop
prefix = ctx.attr.prefix + ctx.attr.mtop
if ctx.attr.module:
mtop = ctx.attr.module[VerilogModuleInfo].top
else:
mtop = ctx.label.name if not ctx.attr.mtop else ctx.attr.mtop
prefix = ctx.attr.prefix + mtop

# Output directories/files
verilator_output = ctx.actions.declare_directory(ctx.label.name + "-gen")
Expand Down Expand Up @@ -155,21 +161,25 @@ verilator_cc_library = rule(
_verilator_cc_library,
attrs = {
"srcs": attr.label_list(
doc = "List of verilog source files",
doc = "[Deprecated] List of verilog source files",
mandatory = False,
allow_files = [".v", ".sv"],
),
"hdrs": attr.label_list(
doc = "List of verilog header files",
doc = "[Deprecated] List of verilog header files",
allow_files = [".v", ".sv", ".vh", ".svh"],
),
"deps": attr.label_list(
doc = "List of verilog and C++ dependencies",
doc = "[Deprectated] List of verilog dependencies",
),
"mtop": attr.string(
doc = "Top level module. Defaults to the rule name if not specified",
doc = "[Deprecated] Top level module. Defaults to the rule name if not specified",
mandatory = False,
),
"module": attr.label(
doc = "Label of verilog_module to verilate",
providers = [VerilogModuleInfo],
),
"trace": attr.bool(
doc = "Enable tracing for Verilator",
default = False,
Expand Down
7 changes: 7 additions & 0 deletions verilator/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def rules_verilator_dependencies(version = _DEFAULT_VERSION):
urls = ["https://github.com/jmillikin/rules_bison/releases/download/v0.1/rules_bison-v0.1.tar.xz"],
sha256 = "5c57552a129b0d8eeb9252341ee975ec2720c35baf2f0d154756310c1ff572a0",
)
_maybe(
http_archive,
name = "rules_verilog",
urls = ["https://github.com/agoessling/rules_verilog/archive/v0.1.0.zip"],
strip_prefix = "rules_verilog-0.1.0",
sha256 = "401b3f591f296f6fd2f6656f01afc1f93111e10b81b9a9d291f9c04b3e4a3e8b",
)

def rules_verilator_toolchains(version = _DEFAULT_VERSION):
repo_name = "verilator_v{version}".format(version = version)
Expand Down

0 comments on commit 6831bd5

Please sign in to comment.