Skip to content

Commit

Permalink
Add rule for generating filelist out of Verilog library
Browse files Browse the repository at this point in the history
Internal-tag: [#70063]
Signed-off-by: Maciej Kurc <[email protected]>
  • Loading branch information
mkurc-ant committed Dec 10, 2024
1 parent 1f4a826 commit c4378bb
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions verilog/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""verilog rules"""

load(
":filelist.bzl",
_verilog_filelist = "verilog_filelist",
)
load(
":providers.bzl",
_VerilogInfo = "VerilogInfo",
Expand All @@ -8,3 +12,4 @@ load(

VerilogInfo = _VerilogInfo
verilog_library = _verilog_library
verilog_filelist = _verilog_filelist
54 changes: 54 additions & 0 deletions verilog/filelist.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Verilog filelist generation rules"""

load(":providers.bzl", "VerilogInfo")

def _verilog_filelist_impl(ctx):
"""Collects all direct and transitive sources/headers of a verilog library"""

# Sources
srcs = depset([], transitive = [ctx.attr.lib[VerilogInfo].dag]).to_list()

# Flatten
all_srcs = [info.srcs for info in srcs]
all_hdrs = [info.hdrs for info in srcs]

all_srcs = [f for sub_tuple in all_srcs for f in sub_tuple]
all_hdrs = [f for sub_tuple in all_hdrs for f in sub_tuple]

# Include directories
include_dirs = depset([f.dirname for f in all_hdrs]).to_list()

# Write the .f file
content = []

for name in include_dirs:
content.append(ctx.attr.include_prefix + name)

for name in all_srcs:
content.append(name.path)

file = ctx.actions.declare_file(ctx.label.name + ".f")
ctx.actions.write(file, "\n".join(content) + "\n")

return DefaultInfo(
files = depset([file]),
)

verilog_filelist = rule(
doc = "Generate a .f file from a Verilog library.",
implementation = _verilog_filelist_impl,
attrs = {
"include_prefix": attr.string(
doc = "Prefix for include directories",
default = "+incdir+",
),
"lib": attr.label(
doc = "The Verilog library to use",
providers = [VerilogInfo],
mandatory = True,
),
},
provides = [
DefaultInfo,
],
)
41 changes: 41 additions & 0 deletions verilog/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load("//verilog:defs.bzl", "verilog_filelist", "verilog_library")

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

verilog_library(
name = "not",
srcs = [
"not.v",
],
)

verilog_library(
name = "top",
srcs = [
"top.v",
],
hdrs = [
"defs.vh",
],
deps = [
":not",
],
)

verilog_filelist(
name = "top_filelist",
lib = ":top",
)

genrule(
name = "top_filelist_test",
srcs = [
"golden.f",
":top_filelist",
],
outs = ["diff.txt"],
cmd = "diff $(SRCS) > $(OUTS)",
)
1 change: 1 addition & 0 deletions verilog/tests/defs.vh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`define COUNT 5
3 changes: 3 additions & 0 deletions verilog/tests/golden.f
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
+incdir+verilog/tests
verilog/tests/not.v
verilog/tests/top.v
8 changes: 8 additions & 0 deletions verilog/tests/not.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module not_gate(
input wire A,
output wire Y
);

assign Y = !A;

endmodule
21 changes: 21 additions & 0 deletions verilog/tests/top.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
`include "defs.vh"

module top(
input wire A,
output wire Y
);

wire [`COUNT:0] y;

genvar i;
generate for (i = 0; i < `COUNT; i = i + 1) begin
not_gate xnot_gate (
.A (y[i]),
.Y (y[i+1])
);
end endgenerate

assign y[0] = A;
assign Y = y[`COUNT];

endmodule

0 comments on commit c4378bb

Please sign in to comment.