Skip to content

Commit

Permalink
Added verilator_toolchain rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
abrisco committed May 11, 2024
1 parent 206e1c5 commit 424f001
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ test:ciremotebuild --tls_client_key=/root/.ssh/buildbuddy-key.pem
test:ciremotebuild --build_metadata=VISIBILITY=PUBLIC
test:ciremotebuild --remote_timeout=3600

# bzlmod is not currently supported
common --noenable_bzlmod

###############################################################################
## User flags
###############################################################################
Expand Down
12 changes: 10 additions & 2 deletions dependency_support/dependency_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ load("@rules_hdl//dependency_support/rules_license:rules_license.bzl", "rules_li
load("@rules_hdl//dependency_support/tk_tcl:tk_tcl.bzl", "tk_tcl")
load("@rules_hdl//dependency_support/verilator:verilator.bzl", "verilator")

def dependency_support():
""" Registers dependencies """
# buildifier: disable=unnamed-macro
def dependency_support(register_toolchains = True):
"""Registers dependencies
Args:
register_toolchains (bool, optional): Register rules_hdl toolchains.
"""
at_clifford_icestorm()
at_clifford_yosys()
bazel_skylib()
Expand Down Expand Up @@ -115,3 +120,6 @@ def dependency_support():
rules_license()
tk_tcl()
verilator()

if register_toolchains:
native.register_toolchains(str(Label("//verilator:verilator_toolchain")))
24 changes: 24 additions & 0 deletions verilator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(":defs.bzl", "verilator_toolchain")

package(
default_applicable_licenses = ["//:package_license"],
default_visibility = ["//visibility:private"],
)

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

verilator_toolchain(
name = "verilator_toolchain_impl",
verilator = "@verilator//:verilator_executable",
deps = [
"@net_zlib//:zlib",
"@verilator//:libverilator",
"@verilator//:svdpi",
],
)

toolchain(
name = "verilator_toolchain",
toolchain = ":verilator_toolchain_impl",
toolchain_type = ":toolchain_type",
visibility = ["//visibility:public"],
)
58 changes: 37 additions & 21 deletions verilator/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def _only_hpp(f):
return None

def _verilator_cc_library(ctx):
verilator_toolchain = ctx.toolchains["@rules_hdl//verilator:toolchain_type"]

transitive_srcs = depset([], transitive = [ctx.attr.module[VerilogInfo].dag])
all_srcs = [verilog_info_struct.srcs for verilog_info_struct in transitive_srcs.to_list()]
all_data = [verilog_info_struct.data for verilog_info_struct in transitive_srcs.to_list()]
Expand Down Expand Up @@ -129,12 +131,14 @@ def _verilator_cc_library(ctx):
args.add("--trace")
for verilog_file in verilog_files:
args.add(verilog_file.path)
args.add_all(verilator_toolchain.extra_vopts)
args.add_all(ctx.attr.vopts, expand_directories = False)

ctx.actions.run(
arguments = [args],
mnemonic = "VerilatorCompile",
executable = ctx.executable._verilator,
executable = verilator_toolchain.verilator,
tools = verilator_toolchain.all_files,
inputs = verilog_files,
outputs = [verilator_output],
progress_message = "[Verilator] Compiling {}".format(ctx.label),
Expand All @@ -159,7 +163,6 @@ def _verilator_cc_library(ctx):

# Do actual compile
defines = ["VM_TRACE"] if ctx.attr.trace else []
deps = [ctx.attr._verilator_lib, ctx.attr._zlib, ctx.attr._verilator_svdpi]

return cc_compile_and_link_static_library(
ctx,
Expand All @@ -168,7 +171,7 @@ def _verilator_cc_library(ctx):
defines = defines,
runfiles = runfiles,
includes = [verilator_output_hpp.path],
deps = deps,
deps = verilator_toolchain.deps,
)

verilator_cc_library = rule(
Expand Down Expand Up @@ -205,31 +208,44 @@ verilator_cc_library = rule(
executable = True,
default = Label("//verilator/private:verilator_copy_tree"),
),
"_verilator": attr.label(
doc = "Verilator binary.",
executable = True,
cfg = "exec",
default = Label("@verilator//:verilator_executable"),
),
"_verilator_lib": attr.label(
doc = "Verilator library",
default = Label("@verilator//:libverilator"),
),
"_verilator_svdpi": attr.label(
doc = "Verilator svdpi lib",
default = Label("@verilator//:svdpi"),
),
"_zlib": attr.label(
doc = "zlib dependency",
default = Label("@net_zlib//:zlib"),
),
},
provides = [
CcInfo,
DefaultInfo,
],
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type",
"@rules_hdl//verilator:toolchain_type",
],
fragments = ["cpp"],
)

def _verilator_toolchain_impl(ctx):
all_files = ctx.attr.verilator[DefaultInfo].default_runfiles.files

return [platform_common.ToolchainInfo(
verilator = ctx.executable.verilator,
deps = ctx.attr.deps,
extra_vopts = ctx.attr.extra_vopts,
all_files = all_files,
)]

verilator_toolchain = rule(
doc = "Define a Verilator toolchain.",
implementation = _verilator_toolchain_impl,
attrs = {
"deps": attr.label_list(
doc = "Global Verilator dependencies to link into downstream targets.",
providers = [CcInfo],
),
"extra_vopts": attr.string_list(
doc = "Extra flags to pass to Verilator compile actions.",
),
"verilator": attr.label(
doc = "The Verilator binary.",
executable = True,
cfg = "exec",
mandatory = True,
),
},
)

0 comments on commit 424f001

Please sign in to comment.