From 424f001dc1b5f82c6bc907031b43221d94590985 Mon Sep 17 00:00:00 2001 From: Andre Brisco Date: Sat, 11 May 2024 13:01:39 -0700 Subject: [PATCH] Added `verilator_toolchain` rule. --- .bazelrc | 3 ++ dependency_support/dependency_support.bzl | 12 ++++- verilator/BUILD.bazel | 24 ++++++++++ verilator/defs.bzl | 58 +++++++++++++++-------- 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/.bazelrc b/.bazelrc index f104dd20..18dfd6fb 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 ############################################################################### diff --git a/dependency_support/dependency_support.bzl b/dependency_support/dependency_support.bzl index 0939f47e..a82d1993 100644 --- a/dependency_support/dependency_support.bzl +++ b/dependency_support/dependency_support.bzl @@ -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() @@ -115,3 +120,6 @@ def dependency_support(): rules_license() tk_tcl() verilator() + + if register_toolchains: + native.register_toolchains(str(Label("//verilator:verilator_toolchain"))) diff --git a/verilator/BUILD.bazel b/verilator/BUILD.bazel index 975dccae..e41b3d85 100644 --- a/verilator/BUILD.bazel +++ b/verilator/BUILD.bazel @@ -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"], +) diff --git a/verilator/defs.bzl b/verilator/defs.bzl index affb98c6..c3e26d52 100644 --- a/verilator/defs.bzl +++ b/verilator/defs.bzl @@ -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()] @@ -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), @@ -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, @@ -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( @@ -205,24 +208,6 @@ 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, @@ -230,6 +215,37 @@ verilator_cc_library = rule( ], 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, + ), + }, +)