From b20eed2785cfaf0fc3d2d60e8e3573a8da0ad4ac Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 15:12:00 +0000 Subject: [PATCH 1/8] gitignore: add filter for 'build' directory --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e28b710..45519c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/bazel-* +/build .DS_Store \ No newline at end of file From e89de5c12610b985da5f476881cb921d9b62e62c Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 15:15:11 +0000 Subject: [PATCH 2/8] Remove Bazel build files --- .bazelrc | 7 ----- BUILD | 1 - WORKSPACE | 42 ---------------------------- gtestverilog/BUILD | 64 ------------------------------------------- gtestverilog/defs.bzl | 45 ------------------------------ 5 files changed, 159 deletions(-) delete mode 100644 .bazelrc delete mode 100644 BUILD delete mode 100644 WORKSPACE delete mode 100644 gtestverilog/BUILD delete mode 100644 gtestverilog/defs.bzl diff --git a/.bazelrc b/.bazelrc deleted file mode 100644 index 0222a99..0000000 --- a/.bazelrc +++ /dev/null @@ -1,7 +0,0 @@ -# Compile cpp with c++20 standard -build --cxxopt='-std=c++2a' - -# "bazel build --config debug" -build:debug -c dbg -build:debug --cxxopt="-g" -build:debug --strip="never" \ No newline at end of file diff --git a/BUILD b/BUILD deleted file mode 100644 index da25e64..0000000 --- a/BUILD +++ /dev/null @@ -1 +0,0 @@ -# empty BUILD file diff --git a/WORKSPACE b/WORKSPACE deleted file mode 100644 index ff8b5ab..0000000 --- a/WORKSPACE +++ /dev/null @@ -1,42 +0,0 @@ -# BAZEL Build System - -load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") - -###################################################################### -# GoogleTest - -git_repository( - name = "com_google_googletest", - remote = "https://github.com/google/googletest", - commit = "18f8200e3079b0e54fa00cb7ac55d4c39dcf6da6", -) - -###################################################################### -# Verilator - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "rules_verilator", - strip_prefix = "rules_verilator-jim-rc1", - url = "https://github.com/JimKnowler/rules_verilator/archive/jim-rc1.zip", -) - -load( - "@rules_verilator//verilator:repositories.bzl", - "rules_verilator_dependencies", - "rules_verilator_toolchains", -) - -rules_verilator_dependencies() -rules_verilator_toolchains() - -# Register toolchain dependencies -load("@rules_m4//m4:m4.bzl", "m4_register_toolchains") -m4_register_toolchains() - -load("@rules_flex//flex:flex.bzl", "flex_register_toolchains") -flex_register_toolchains() - -load("@rules_bison//bison:bison.bzl", "bison_register_toolchains") -bison_register_toolchains() \ No newline at end of file diff --git a/gtestverilog/BUILD b/gtestverilog/BUILD deleted file mode 100644 index aa020fe..0000000 --- a/gtestverilog/BUILD +++ /dev/null @@ -1,64 +0,0 @@ -load("@rules_verilator//verilator:defs.bzl", "verilator_cc_library") -load(":defs.bzl", "gtest_verilog_testbench") - -py_binary( - name='generate_testbench', - srcs=['tools/generate_testbench.py'], - visibility=['//visibility:public'], -) - -cc_library( - name = "lib", - srcs = glob([ - "lib/*.cpp", - "lib/*.h" - ]), - hdrs = glob([ - "*.h" - ]), - deps = [ - "@com_google_googletest//:gtest", - ], - visibility = ["//visibility:public"] -) - -cc_test( - name = "test", - srcs = glob( - include =[ - "test/*.cpp", - "test/*.h" - ] - ), - deps = [ - "@com_google_googletest//:gtest", - ":lib" - ], -) - -verilator_cc_library( - name = "Counter", - srcs = ["example/Counter.v"], -) - -gtest_verilog_testbench( - name = "CounterTestBench", - deps = [":Counter"], -) - -cc_test( - name = "example", - srcs = [ - ":CounterTestBench", - ] + glob( - include = [ - "example/*.cpp", - "example/*.h", - ] - ), - deps = [ - "@com_google_googletest//:gtest", - ":lib", - ":Counter", - ] -) \ No newline at end of file diff --git a/gtestverilog/defs.bzl b/gtestverilog/defs.bzl deleted file mode 100644 index ca481b0..0000000 --- a/gtestverilog/defs.bzl +++ /dev/null @@ -1,45 +0,0 @@ - -def _gtest_verilog_testbench_impl(ctx): - input = None - verilated_header_dir = None - - for dep in ctx.attr.deps: - headers = dep[CcInfo].compilation_context.direct_headers - for header in headers: - if not header.is_directory: - fail("header is not a directory") - - input = header - verilated_header_dir = header.path - - if None == verilated_header_dir: - fail("missing header directory in output from verilator") - - ctx.actions.run( - inputs = [input], - outputs = [ - ctx.outputs.source_file, - ctx.outputs.header_file - ], - arguments = [ - "--name", ctx.attr.name, - "--verilated-header-dir", verilated_header_dir, - "--output-source", ctx.outputs.source_file.path, - "--output-header", ctx.outputs.header_file.path - ], - executable = ctx.executable.generate_testbench, - progress_message = "generating gtest-testbench for " + ctx.attr.name, - mnemonic = "GTestVerilogTestbench" - ) - -gtest_verilog_testbench = rule( - implementation = _gtest_verilog_testbench_impl, - attrs = { - "deps" : attr.label_list(providers = [CcInfo]), - "generate_testbench" : attr.label(executable=True, cfg="host", default="//gtestverilog:generate_testbench") - }, - outputs = { - "source_file": "%{name}.cpp", - "header_file": "%{name}.h" - }, -) \ No newline at end of file From bf30295d8006e8211d543431a940f83f5de4dd6b Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 16:18:05 +0000 Subject: [PATCH 3/8] cmake: partial conversion --- CMakeLists.txt | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9500f11 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,93 @@ +cmake_minimum_required(VERSION 3.31.6) + +project(googletest-verilog + VERSION 0.2.0 + LANGUAGES CXX +) + +set(CMAKE_CXX_STANDARD 20) + +# +# GoogleTest Library +# + +# download google test +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/releases/download/v1.17.0/googletest-1.17.0.tar.gz +) + +# prevent googletest from overriding our compiler/linker options +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +# +# Verilog library +# + +# based on verilator documentation +# https://veripool.org/guide/latest/verilating.html#cmake + +find_package(verilator REQUIRED HINTS $ENV{VERILATOR_ROOT}) + +message(STATUS "VERILATOR_ROOT = ${VERILATOR_ROOT}") +message(STATUS "VERILATOR_BIN = ${VERILATOR_BIN}") + +# +# googletest-verilog static library +# + +include_directories(include) + +add_library(googletest-verilog + src/ConsoleColour.cpp + src/MatchesTrace.cpp + src/PortDescription.cpp + src/Step.cpp + src/Trace.cpp + src/TraceBuilder.cpp +) + +# add googletest/googlemock includes, without linking to gtest +get_target_property(GTEST_INCLUDES GTest::gtest INTERFACE_INCLUDE_DIRECTORIES) +get_target_property(GMOCK_INCLUDES GTest::gmock INTERFACE_INCLUDE_DIRECTORIES) +target_include_directories(googletest-verilog PRIVATE ${GTEST_INCLUDES} ${GMOCK_INCLUDES}) + +# +# googletest-verilog internal Unit Tests +# + +add_executable(googletest-verilog-test + test/main.cpp + test/MatchesTrace.test.cpp + test/Step.test.cpp + test/TestBench.test.cpp + test/Trace.test.cpp + test/TraceBuilder.test.cpp +) + +target_link_libraries(googletest-verilog-test + gtest + gmock + googletest-verilog +) + +# +# googletest-verilog Example Usage +# + +add_executable(googletest-verilog-example + example/main.cpp + example/Counter.test.cpp +) + +verilate(googletest-verilog-example SOURCES example/Counter.v) + +target_link_libraries(googletest-verilog-example PRIVATE + googletest-verilog + gtest + gmock +) + +target_include_directories(googletest-verilog-example PRIVATE ${VERILATOR_INCLUDE_DIRS}) From b1193eb11b59846fd6e601ff75ebbf7680e9bfeb Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 16:18:19 +0000 Subject: [PATCH 4/8] README.md: start updating documentation for cmake based build --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e23268..eac5dcf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,13 @@ -- bazel build //gtestverilog:test && ./bazel-bin/gtestverilog/test --gtest_filter="*" +# googletest-verilog -- bazel build //gtestverilog:example --incompatible_require_linker_input_cc_api=false && ./bazel-bin/gtestverilog/example --gtest_filter="*" +Add support for writing C++ unit-tests for verilog modules with [googletest](https://github.com/google/googletest). +Unit-tests are run in simulation with [verilator](https://github.com/verilator/verilator). + +# Dependencies + +MacOS - to install with homebrew: + +``` brew +brew install verilator cmake +``` From 04170f6414f58a2a8a041e2bfc03b1cad9ce18a5 Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 16:18:35 +0000 Subject: [PATCH 5/8] reorganise source code in preparation for cmake build --- {gtestverilog/example => example}/Counter.test.cpp | 11 ++++++++--- {gtestverilog/example => example}/Counter.v | 0 {gtestverilog/example => example}/main.cpp | 0 gtestverilog/gtestverilog.h | 5 ----- .../googletest-verilog}/ConsoleColour.h | 0 .../googletest-verilog}/MatchesTrace.h | 7 ++++++- .../googletest-verilog}/PortDescription.h | 0 .../lib => include/googletest-verilog}/PortValue.h | 0 .../lib => include/googletest-verilog}/Step.h | 0 .../lib => include/googletest-verilog}/TestBench.h | 0 .../lib => include/googletest-verilog}/Trace.h | 0 .../googletest-verilog}/TraceBuilder.h | 0 include/googletest-verilog/googletest-verilog.h | 5 +++++ {gtestverilog/lib => src}/ConsoleColour.cpp | 2 +- {gtestverilog/lib => src}/MatchesTrace.cpp | 2 +- {gtestverilog/lib => src}/PortDescription.cpp | 2 +- {gtestverilog/lib => src}/Step.cpp | 2 +- {gtestverilog/lib => src}/Trace.cpp | 12 ++++++------ {gtestverilog/lib => src}/TraceBuilder.cpp | 2 +- {gtestverilog/test => test}/MatchesTrace.test.cpp | 2 +- {gtestverilog/test => test}/Step.test.cpp | 2 +- {gtestverilog/test => test}/TestBench.test.cpp | 2 +- {gtestverilog/test => test}/Trace.test.cpp | 2 +- {gtestverilog/test => test}/TraceBuilder.test.cpp | 2 +- {gtestverilog/test => test}/main.cpp | 0 {gtestverilog/tools => tools}/generate_testbench.py | 2 +- 26 files changed, 36 insertions(+), 26 deletions(-) rename {gtestverilog/example => example}/Counter.test.cpp (92%) rename {gtestverilog/example => example}/Counter.v (100%) rename {gtestverilog/example => example}/main.cpp (100%) delete mode 100644 gtestverilog/gtestverilog.h rename {gtestverilog/lib => include/googletest-verilog}/ConsoleColour.h (100%) rename {gtestverilog/lib => include/googletest-verilog}/MatchesTrace.h (70%) rename {gtestverilog/lib => include/googletest-verilog}/PortDescription.h (100%) rename {gtestverilog/lib => include/googletest-verilog}/PortValue.h (100%) rename {gtestverilog/lib => include/googletest-verilog}/Step.h (100%) rename {gtestverilog/lib => include/googletest-verilog}/TestBench.h (100%) rename {gtestverilog/lib => include/googletest-verilog}/Trace.h (100%) rename {gtestverilog/lib => include/googletest-verilog}/TraceBuilder.h (100%) create mode 100644 include/googletest-verilog/googletest-verilog.h rename {gtestverilog/lib => src}/ConsoleColour.cpp (97%) rename {gtestverilog/lib => src}/MatchesTrace.cpp (98%) rename {gtestverilog/lib => src}/PortDescription.cpp (91%) rename {gtestverilog/lib => src}/Step.cpp (97%) rename {gtestverilog/lib => src}/Trace.cpp (94%) rename {gtestverilog/lib => src}/TraceBuilder.cpp (99%) rename {gtestverilog/test => test}/MatchesTrace.test.cpp (98%) rename {gtestverilog/test => test}/Step.test.cpp (98%) rename {gtestverilog/test => test}/TestBench.test.cpp (97%) rename {gtestverilog/test => test}/Trace.test.cpp (99%) rename {gtestverilog/test => test}/TraceBuilder.test.cpp (99%) rename {gtestverilog/test => test}/main.cpp (100%) rename {gtestverilog/tools => tools}/generate_testbench.py (99%) diff --git a/gtestverilog/example/Counter.test.cpp b/example/Counter.test.cpp similarity index 92% rename from gtestverilog/example/Counter.test.cpp rename to example/Counter.test.cpp index 35e3f27..3daa3fa 100644 --- a/gtestverilog/example/Counter.test.cpp +++ b/example/Counter.test.cpp @@ -1,10 +1,13 @@ #include using namespace testing; -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; -#include "gtestverilog/CounterTestBench.h" +// JK - disable, until we get automated test bench generation working again +#if 0 + +#include "googletest-verilog-example/CounterTestBench.h" using namespace countertestbench; namespace { @@ -88,4 +91,6 @@ TEST_F(Counter, ShouldHandleLargeTraces) { .port(i_simulate_sequential).signal( "10" ).repeat(1001); ASSERT_THAT(testBench.trace, MatchesTrace(traceExpected)); -} \ No newline at end of file +} + +#endif diff --git a/gtestverilog/example/Counter.v b/example/Counter.v similarity index 100% rename from gtestverilog/example/Counter.v rename to example/Counter.v diff --git a/gtestverilog/example/main.cpp b/example/main.cpp similarity index 100% rename from gtestverilog/example/main.cpp rename to example/main.cpp diff --git a/gtestverilog/gtestverilog.h b/gtestverilog/gtestverilog.h deleted file mode 100644 index fb45382..0000000 --- a/gtestverilog/gtestverilog.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include "lib/MatchesTrace.h" -#include "lib/TestBench.h" -#include "lib/TraceBuilder.h" \ No newline at end of file diff --git a/gtestverilog/lib/ConsoleColour.h b/include/googletest-verilog/ConsoleColour.h similarity index 100% rename from gtestverilog/lib/ConsoleColour.h rename to include/googletest-verilog/ConsoleColour.h diff --git a/gtestverilog/lib/MatchesTrace.h b/include/googletest-verilog/MatchesTrace.h similarity index 70% rename from gtestverilog/lib/MatchesTrace.h rename to include/googletest-verilog/MatchesTrace.h index b3bb6fb..3f22fb9 100644 --- a/gtestverilog/lib/MatchesTrace.h +++ b/include/googletest-verilog/MatchesTrace.h @@ -1,14 +1,19 @@ +#include #include #include "Trace.h" +#ifndef MATCHER_P +#error "matcher_p is not defined here" +#endif + namespace gtestverilog { namespace matches_trace { bool compare(const Trace& a, const Trace& b, ::testing::MatchResultListener& listener); } - MATCHER_P(MatchesTrace, trace, "") { + MATCHER_P(MatchesTrace, trace, "Matches Trace") { if (!matches_trace::compare(arg, trace, *result_listener)) { return false; } diff --git a/gtestverilog/lib/PortDescription.h b/include/googletest-verilog/PortDescription.h similarity index 100% rename from gtestverilog/lib/PortDescription.h rename to include/googletest-verilog/PortDescription.h diff --git a/gtestverilog/lib/PortValue.h b/include/googletest-verilog/PortValue.h similarity index 100% rename from gtestverilog/lib/PortValue.h rename to include/googletest-verilog/PortValue.h diff --git a/gtestverilog/lib/Step.h b/include/googletest-verilog/Step.h similarity index 100% rename from gtestverilog/lib/Step.h rename to include/googletest-verilog/Step.h diff --git a/gtestverilog/lib/TestBench.h b/include/googletest-verilog/TestBench.h similarity index 100% rename from gtestverilog/lib/TestBench.h rename to include/googletest-verilog/TestBench.h diff --git a/gtestverilog/lib/Trace.h b/include/googletest-verilog/Trace.h similarity index 100% rename from gtestverilog/lib/Trace.h rename to include/googletest-verilog/Trace.h diff --git a/gtestverilog/lib/TraceBuilder.h b/include/googletest-verilog/TraceBuilder.h similarity index 100% rename from gtestverilog/lib/TraceBuilder.h rename to include/googletest-verilog/TraceBuilder.h diff --git a/include/googletest-verilog/googletest-verilog.h b/include/googletest-verilog/googletest-verilog.h new file mode 100644 index 0000000..3120565 --- /dev/null +++ b/include/googletest-verilog/googletest-verilog.h @@ -0,0 +1,5 @@ +#pragma once + +#include "MatchesTrace.h" +#include "TestBench.h" +#include "TraceBuilder.h" \ No newline at end of file diff --git a/gtestverilog/lib/ConsoleColour.cpp b/src/ConsoleColour.cpp similarity index 97% rename from gtestverilog/lib/ConsoleColour.cpp rename to src/ConsoleColour.cpp index cca6989..ef1fe1e 100644 --- a/gtestverilog/lib/ConsoleColour.cpp +++ b/src/ConsoleColour.cpp @@ -1,4 +1,4 @@ -#include "ConsoleColour.h" +#include "googletest-verilog/ConsoleColour.h" namespace gtestverilog { diff --git a/gtestverilog/lib/MatchesTrace.cpp b/src/MatchesTrace.cpp similarity index 98% rename from gtestverilog/lib/MatchesTrace.cpp rename to src/MatchesTrace.cpp index b431a02..4c836a3 100644 --- a/gtestverilog/lib/MatchesTrace.cpp +++ b/src/MatchesTrace.cpp @@ -1,4 +1,4 @@ -#include "MatchesTrace.h" +#include "googletest-verilog/MatchesTrace.h" #include diff --git a/gtestverilog/lib/PortDescription.cpp b/src/PortDescription.cpp similarity index 91% rename from gtestverilog/lib/PortDescription.cpp rename to src/PortDescription.cpp index 10bdce5..df09790 100644 --- a/gtestverilog/lib/PortDescription.cpp +++ b/src/PortDescription.cpp @@ -1,4 +1,4 @@ -#include "PortDescription.h" +#include "googletest-verilog/PortDescription.h" namespace gtestverilog { diff --git a/gtestverilog/lib/Step.cpp b/src/Step.cpp similarity index 97% rename from gtestverilog/lib/Step.cpp rename to src/Step.cpp index bb0eedc..e357d29 100644 --- a/gtestverilog/lib/Step.cpp +++ b/src/Step.cpp @@ -1,4 +1,4 @@ -#include "Step.h" +#include "googletest-verilog/Step.h" namespace gtestverilog { diff --git a/gtestverilog/lib/Trace.cpp b/src/Trace.cpp similarity index 94% rename from gtestverilog/lib/Trace.cpp rename to src/Trace.cpp index a3e9c26..46dcac2 100644 --- a/gtestverilog/lib/Trace.cpp +++ b/src/Trace.cpp @@ -1,4 +1,4 @@ -#include "Trace.h" +#include "googletest-verilog/Trace.h" #include #include @@ -42,7 +42,7 @@ namespace gtestverilog { static void renderTimeline(std::ostream& os, size_t x, size_t numSteps) { char buffer[64]; - sprintf(buffer, "%*s", int(x), " "); + snprintf(buffer, sizeof(buffer), "%*s", int(x), " "); os << buffer; const int kDividerSize = 5; @@ -50,7 +50,7 @@ namespace gtestverilog { for (int i=0; i 1) { - sprintf(buffer, "[%02lu:%02lu] ", ((nibbleIndex + 1) * 4) - 1, (nibbleIndex * 4)); + snprintf(buffer, sizeof(buffer), "[%02lu:%02lu] ", ((nibbleIndex + 1) * 4) - 1, (nibbleIndex * 4)); os << buffer; } else { os << "....... "; @@ -98,7 +98,7 @@ namespace gtestverilog { } uint8_t nibble = uint8_t( (value >> (nibbleIndex * 4)) & 0xf); - sprintf(buffer, "%X", nibble); + snprintf(buffer, sizeof(buffer), "%X", nibble); // use null terminator to force limit to a single hex character buffer[1] = 0; diff --git a/gtestverilog/lib/TraceBuilder.cpp b/src/TraceBuilder.cpp similarity index 99% rename from gtestverilog/lib/TraceBuilder.cpp rename to src/TraceBuilder.cpp index a498bf1..f0e81b8 100644 --- a/gtestverilog/lib/TraceBuilder.cpp +++ b/src/TraceBuilder.cpp @@ -1,4 +1,4 @@ -#include "TraceBuilder.h" +#include "googletest-verilog/TraceBuilder.h" namespace gtestverilog { diff --git a/gtestverilog/test/MatchesTrace.test.cpp b/test/MatchesTrace.test.cpp similarity index 98% rename from gtestverilog/test/MatchesTrace.test.cpp rename to test/MatchesTrace.test.cpp index 4e47a38..b73e6bd 100644 --- a/gtestverilog/test/MatchesTrace.test.cpp +++ b/test/MatchesTrace.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; namespace { diff --git a/gtestverilog/test/Step.test.cpp b/test/Step.test.cpp similarity index 98% rename from gtestverilog/test/Step.test.cpp rename to test/Step.test.cpp index 988116a..cafa781 100644 --- a/gtestverilog/test/Step.test.cpp +++ b/test/Step.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; namespace { diff --git a/gtestverilog/test/TestBench.test.cpp b/test/TestBench.test.cpp similarity index 97% rename from gtestverilog/test/TestBench.test.cpp rename to test/TestBench.test.cpp index b77e8f1..6035695 100644 --- a/gtestverilog/test/TestBench.test.cpp +++ b/test/TestBench.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; namespace { diff --git a/gtestverilog/test/Trace.test.cpp b/test/Trace.test.cpp similarity index 99% rename from gtestverilog/test/Trace.test.cpp rename to test/Trace.test.cpp index b55527f..b15cc6f 100644 --- a/gtestverilog/test/Trace.test.cpp +++ b/test/Trace.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; namespace { diff --git a/gtestverilog/test/TraceBuilder.test.cpp b/test/TraceBuilder.test.cpp similarity index 99% rename from gtestverilog/test/TraceBuilder.test.cpp rename to test/TraceBuilder.test.cpp index ae80cba..83ff495 100644 --- a/gtestverilog/test/TraceBuilder.test.cpp +++ b/test/TraceBuilder.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; namespace { diff --git a/gtestverilog/test/main.cpp b/test/main.cpp similarity index 100% rename from gtestverilog/test/main.cpp rename to test/main.cpp diff --git a/gtestverilog/tools/generate_testbench.py b/tools/generate_testbench.py similarity index 99% rename from gtestverilog/tools/generate_testbench.py rename to tools/generate_testbench.py index 4e1d1ac..d7e881f 100644 --- a/gtestverilog/tools/generate_testbench.py +++ b/tools/generate_testbench.py @@ -138,7 +138,7 @@ def write_header_file(): header_top = """ #pragma once -#include "gtestverilog/gtestverilog.h" +#include "googletest-verilog/googletest-verilog.h" #include "{verilated_header_filepath}" namespace {namespace} {{ From 227e6556da144c1f8a9383c0a37d5a1362d71899 Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 21:23:43 +0000 Subject: [PATCH 6/8] cmake: first pass at generating test bench --- CMakeLists.txt | 66 +++++++++++++++++++++++++++++++++++-- example/Counter.test.cpp | 7 +--- tools/generate_testbench.py | 2 +- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9500f11..a4e02f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) # -# Verilog library +# Verilator library # # based on verilator documentation @@ -34,6 +34,68 @@ find_package(verilator REQUIRED HINTS $ENV{VERILATOR_ROOT}) message(STATUS "VERILATOR_ROOT = ${VERILATOR_ROOT}") message(STATUS "VERILATOR_BIN = ${VERILATOR_BIN}") +# +# googletest-verilog test bench generation +# + +find_package(Python3 COMPONENTS Interpreter REQUIRED) + +function(googletest_verilog_testbench TB_TARGET) + cmake_parse_arguments( + TB # prefix + "" # list of options (true / false based on whether option is present or not) + "SOURCE" # single value keywords + "INCLUDE_DIRS" # multi-value keywords + ${ARGN} # arguments provided to the function + ) + + message(STATUS "SOURCE = ${TB_SOURCE}") + message(STATUS "TARGET = ${TB_TARGET}") + message(STATUS "INCLUDE_DIRS = ${TB_INCLUDE_DIRS}") + + set(TB_VERILATED_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/verilated/${TB_TESTBENCH}) + message(STATUS "TB_VERILATED_OUTPUT_DIR = ${TB_VERILATED_OUTPUT_DIR}") + + get_filename_component(TB_SOURCE_BASENAME "${TB_SOURCE}" NAME_WE) + set(TB_TESTBENCH "${TB_SOURCE_BASENAME}TestBench") + set(TB_TESTBENCH_HEADER "${TB_VERILATED_OUTPUT_DIR}/${TB_TESTBENCH}.h") + set(TB_TESTBENCH_SOURCE "${TB_VERILATED_OUTPUT_DIR}/${TB_TESTBENCH}.cpp") + + message(STATUS "TB_TESTBENCH = ${TB_TESTBENCH}") + message(STATUS "TB_TESTBENCH_HEADER = ${TB_TESTBENCH_HEADER}") + message(STATUS "TB_TESTBENCH_SOURCE = ${TB_TESTBENCH_SOURCE}") + message(STATUS "CMAKE_BINARY_DIR = " ${CMAKE_BINARY_DIR}) + message(STATUS "CMAKE_CURRENT_BINARY_DIR = " ${CMAKE_CURRENT_BINARY_DIR}) + + # 1. verilate the verilog module + + verilate(${TB_TARGET} SOURCES ${TB_SOURCE} INCLUDE_DIRS ${TB_INCLUDE_DIRS} DIRECTORY ${TB_VERILATED_OUTPUT_DIR}) + + # 2. generate test bench + set(TOOL_GENERATE_TESTBENCH ${CMAKE_SOURCE_DIR}/tools/generate_testbench.py) + + add_custom_command( + OUTPUT ${TB_TESTBENCH_HEADER} ${TB_TESTBENCH_SOURCE} + COMMAND ${Python3_EXECUTABLE} ${TOOL_GENERATE_TESTBENCH} --name ${TB_TESTBENCH} --verilated-header-dir ${TB_VERILATED_OUTPUT_DIR} --output-header ${TB_TESTBENCH_HEADER} --output-source ${TB_TESTBENCH_SOURCE} + DEPENDS ${TOOL_GENERATE_TESTBENCH} ${TB_SOURCE} + ) + + target_sources(${TB_TARGET} PRIVATE ${TB_TESTBENCH_HEADER} ${TB_TESTBENCH_SOURCE}) + + set(TB_TESTBENCH_TARGET "googletest_verilog_testbench-${TB_TESTBENCH}") + + # 3. use a custom target to force cmake to create testbench first, before compiling TARGET + + add_custom_target(${TB_TESTBENCH_TARGET} + DEPENDS ${TB_TESTBENCH_HEADER} ${TB_TESTBENCH_SOURCE} + ) + + add_dependencies(${TB_TARGET} ${TB_TESTBENCH_TARGET}) + +endfunction() + + + # # googletest-verilog static library # @@ -82,7 +144,7 @@ add_executable(googletest-verilog-example example/Counter.test.cpp ) -verilate(googletest-verilog-example SOURCES example/Counter.v) +googletest_verilog_testbench(googletest-verilog-example SOURCE example/Counter.v) target_link_libraries(googletest-verilog-example PRIVATE googletest-verilog diff --git a/example/Counter.test.cpp b/example/Counter.test.cpp index 3daa3fa..d163885 100644 --- a/example/Counter.test.cpp +++ b/example/Counter.test.cpp @@ -4,10 +4,7 @@ using namespace testing; #include "googletest-verilog/googletest-verilog.h" using namespace gtestverilog; -// JK - disable, until we get automated test bench generation working again -#if 0 - -#include "googletest-verilog-example/CounterTestBench.h" +#include "CounterTestBench.h" using namespace countertestbench; namespace { @@ -92,5 +89,3 @@ TEST_F(Counter, ShouldHandleLargeTraces) { ASSERT_THAT(testBench.trace, MatchesTrace(traceExpected)); } - -#endif diff --git a/tools/generate_testbench.py b/tools/generate_testbench.py index d7e881f..6338838 100644 --- a/tools/generate_testbench.py +++ b/tools/generate_testbench.py @@ -26,7 +26,7 @@ for filepath in verilated_header_filepaths: with open(filepath, "r") as file: for line in file: - results = re.search("\w*VL_[IN|OUT].*\((.*),(.*),(.*)\);\w*", line.lstrip().rstrip()) + results = re.search(r"\w*VL_[IN|OUT].*\((.*),(.*),(.*)\);\w*", line.lstrip().rstrip()) if results: # this is the header with port definitions From dfe6130c0b66de414d2cd92019076248d4af8ea9 Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Sun, 16 Nov 2025 21:26:08 +0000 Subject: [PATCH 7/8] cmake: fix path where test benches are generated --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4e02f2..f27ba9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,15 +53,15 @@ function(googletest_verilog_testbench TB_TARGET) message(STATUS "TARGET = ${TB_TARGET}") message(STATUS "INCLUDE_DIRS = ${TB_INCLUDE_DIRS}") - set(TB_VERILATED_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/verilated/${TB_TESTBENCH}) - message(STATUS "TB_VERILATED_OUTPUT_DIR = ${TB_VERILATED_OUTPUT_DIR}") - get_filename_component(TB_SOURCE_BASENAME "${TB_SOURCE}" NAME_WE) set(TB_TESTBENCH "${TB_SOURCE_BASENAME}TestBench") + + set(TB_VERILATED_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/verilated/${TB_TESTBENCH}) set(TB_TESTBENCH_HEADER "${TB_VERILATED_OUTPUT_DIR}/${TB_TESTBENCH}.h") set(TB_TESTBENCH_SOURCE "${TB_VERILATED_OUTPUT_DIR}/${TB_TESTBENCH}.cpp") message(STATUS "TB_TESTBENCH = ${TB_TESTBENCH}") + message(STATUS "TB_VERILATED_OUTPUT_DIR = ${TB_VERILATED_OUTPUT_DIR}") message(STATUS "TB_TESTBENCH_HEADER = ${TB_TESTBENCH_HEADER}") message(STATUS "TB_TESTBENCH_SOURCE = ${TB_TESTBENCH_SOURCE}") message(STATUS "CMAKE_BINARY_DIR = " ${CMAKE_BINARY_DIR}) From 4b046c89ff949eed892cb5b9c05490ba5d7b09c2 Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Mon, 17 Nov 2025 07:47:58 +0000 Subject: [PATCH 8/8] rename include folder for consistency with 'gtest' and 'gmock' --- CMakeLists.txt | 4 +--- example/Counter.test.cpp | 2 +- include/{googletest-verilog => gtest-verilog}/ConsoleColour.h | 0 include/{googletest-verilog => gtest-verilog}/MatchesTrace.h | 0 .../{googletest-verilog => gtest-verilog}/PortDescription.h | 0 include/{googletest-verilog => gtest-verilog}/PortValue.h | 0 include/{googletest-verilog => gtest-verilog}/Step.h | 0 include/{googletest-verilog => gtest-verilog}/TestBench.h | 0 include/{googletest-verilog => gtest-verilog}/Trace.h | 0 include/{googletest-verilog => gtest-verilog}/TraceBuilder.h | 0 .../googletest-verilog.h => gtest-verilog/gtest-verilog.h} | 0 src/ConsoleColour.cpp | 2 +- src/MatchesTrace.cpp | 2 +- src/PortDescription.cpp | 2 +- src/Step.cpp | 2 +- src/Trace.cpp | 2 +- src/TraceBuilder.cpp | 2 +- test/MatchesTrace.test.cpp | 2 +- test/Step.test.cpp | 2 +- test/TestBench.test.cpp | 2 +- test/Trace.test.cpp | 2 +- test/TraceBuilder.test.cpp | 2 +- tools/generate_testbench.py | 2 +- 23 files changed, 14 insertions(+), 16 deletions(-) rename include/{googletest-verilog => gtest-verilog}/ConsoleColour.h (100%) rename include/{googletest-verilog => gtest-verilog}/MatchesTrace.h (100%) rename include/{googletest-verilog => gtest-verilog}/PortDescription.h (100%) rename include/{googletest-verilog => gtest-verilog}/PortValue.h (100%) rename include/{googletest-verilog => gtest-verilog}/Step.h (100%) rename include/{googletest-verilog => gtest-verilog}/TestBench.h (100%) rename include/{googletest-verilog => gtest-verilog}/Trace.h (100%) rename include/{googletest-verilog => gtest-verilog}/TraceBuilder.h (100%) rename include/{googletest-verilog/googletest-verilog.h => gtest-verilog/gtest-verilog.h} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f27ba9c..92850cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,8 +94,6 @@ function(googletest_verilog_testbench TB_TARGET) endfunction() - - # # googletest-verilog static library # @@ -114,7 +112,7 @@ add_library(googletest-verilog # add googletest/googlemock includes, without linking to gtest get_target_property(GTEST_INCLUDES GTest::gtest INTERFACE_INCLUDE_DIRECTORIES) get_target_property(GMOCK_INCLUDES GTest::gmock INTERFACE_INCLUDE_DIRECTORIES) -target_include_directories(googletest-verilog PRIVATE ${GTEST_INCLUDES} ${GMOCK_INCLUDES}) +target_include_directories(googletest-verilog PUBLIC ${GTEST_INCLUDES} ${GMOCK_INCLUDES}) # # googletest-verilog internal Unit Tests diff --git a/example/Counter.test.cpp b/example/Counter.test.cpp index d163885..5def066 100644 --- a/example/Counter.test.cpp +++ b/example/Counter.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" using namespace gtestverilog; #include "CounterTestBench.h" diff --git a/include/googletest-verilog/ConsoleColour.h b/include/gtest-verilog/ConsoleColour.h similarity index 100% rename from include/googletest-verilog/ConsoleColour.h rename to include/gtest-verilog/ConsoleColour.h diff --git a/include/googletest-verilog/MatchesTrace.h b/include/gtest-verilog/MatchesTrace.h similarity index 100% rename from include/googletest-verilog/MatchesTrace.h rename to include/gtest-verilog/MatchesTrace.h diff --git a/include/googletest-verilog/PortDescription.h b/include/gtest-verilog/PortDescription.h similarity index 100% rename from include/googletest-verilog/PortDescription.h rename to include/gtest-verilog/PortDescription.h diff --git a/include/googletest-verilog/PortValue.h b/include/gtest-verilog/PortValue.h similarity index 100% rename from include/googletest-verilog/PortValue.h rename to include/gtest-verilog/PortValue.h diff --git a/include/googletest-verilog/Step.h b/include/gtest-verilog/Step.h similarity index 100% rename from include/googletest-verilog/Step.h rename to include/gtest-verilog/Step.h diff --git a/include/googletest-verilog/TestBench.h b/include/gtest-verilog/TestBench.h similarity index 100% rename from include/googletest-verilog/TestBench.h rename to include/gtest-verilog/TestBench.h diff --git a/include/googletest-verilog/Trace.h b/include/gtest-verilog/Trace.h similarity index 100% rename from include/googletest-verilog/Trace.h rename to include/gtest-verilog/Trace.h diff --git a/include/googletest-verilog/TraceBuilder.h b/include/gtest-verilog/TraceBuilder.h similarity index 100% rename from include/googletest-verilog/TraceBuilder.h rename to include/gtest-verilog/TraceBuilder.h diff --git a/include/googletest-verilog/googletest-verilog.h b/include/gtest-verilog/gtest-verilog.h similarity index 100% rename from include/googletest-verilog/googletest-verilog.h rename to include/gtest-verilog/gtest-verilog.h diff --git a/src/ConsoleColour.cpp b/src/ConsoleColour.cpp index ef1fe1e..a67fea1 100644 --- a/src/ConsoleColour.cpp +++ b/src/ConsoleColour.cpp @@ -1,4 +1,4 @@ -#include "googletest-verilog/ConsoleColour.h" +#include "gtest-verilog/ConsoleColour.h" namespace gtestverilog { diff --git a/src/MatchesTrace.cpp b/src/MatchesTrace.cpp index 4c836a3..14a94e0 100644 --- a/src/MatchesTrace.cpp +++ b/src/MatchesTrace.cpp @@ -1,4 +1,4 @@ -#include "googletest-verilog/MatchesTrace.h" +#include "gtest-verilog/MatchesTrace.h" #include diff --git a/src/PortDescription.cpp b/src/PortDescription.cpp index df09790..ecaa320 100644 --- a/src/PortDescription.cpp +++ b/src/PortDescription.cpp @@ -1,4 +1,4 @@ -#include "googletest-verilog/PortDescription.h" +#include "gtest-verilog/PortDescription.h" namespace gtestverilog { diff --git a/src/Step.cpp b/src/Step.cpp index e357d29..bb941d9 100644 --- a/src/Step.cpp +++ b/src/Step.cpp @@ -1,4 +1,4 @@ -#include "googletest-verilog/Step.h" +#include "gtest-verilog/Step.h" namespace gtestverilog { diff --git a/src/Trace.cpp b/src/Trace.cpp index 46dcac2..378ec28 100644 --- a/src/Trace.cpp +++ b/src/Trace.cpp @@ -1,4 +1,4 @@ -#include "googletest-verilog/Trace.h" +#include "gtest-verilog/Trace.h" #include #include diff --git a/src/TraceBuilder.cpp b/src/TraceBuilder.cpp index f0e81b8..fd8f962 100644 --- a/src/TraceBuilder.cpp +++ b/src/TraceBuilder.cpp @@ -1,4 +1,4 @@ -#include "googletest-verilog/TraceBuilder.h" +#include "gtest-verilog/TraceBuilder.h" namespace gtestverilog { diff --git a/test/MatchesTrace.test.cpp b/test/MatchesTrace.test.cpp index b73e6bd..297609e 100644 --- a/test/MatchesTrace.test.cpp +++ b/test/MatchesTrace.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" using namespace gtestverilog; namespace { diff --git a/test/Step.test.cpp b/test/Step.test.cpp index cafa781..3f8641e 100644 --- a/test/Step.test.cpp +++ b/test/Step.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" using namespace gtestverilog; namespace { diff --git a/test/TestBench.test.cpp b/test/TestBench.test.cpp index 6035695..3a12d71 100644 --- a/test/TestBench.test.cpp +++ b/test/TestBench.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" using namespace gtestverilog; namespace { diff --git a/test/Trace.test.cpp b/test/Trace.test.cpp index b15cc6f..c5c879a 100644 --- a/test/Trace.test.cpp +++ b/test/Trace.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" using namespace gtestverilog; namespace { diff --git a/test/TraceBuilder.test.cpp b/test/TraceBuilder.test.cpp index 83ff495..33e8ee3 100644 --- a/test/TraceBuilder.test.cpp +++ b/test/TraceBuilder.test.cpp @@ -1,7 +1,7 @@ #include using namespace testing; -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" using namespace gtestverilog; namespace { diff --git a/tools/generate_testbench.py b/tools/generate_testbench.py index 6338838..99dfa70 100644 --- a/tools/generate_testbench.py +++ b/tools/generate_testbench.py @@ -138,7 +138,7 @@ def write_header_file(): header_top = """ #pragma once -#include "googletest-verilog/googletest-verilog.h" +#include "gtest-verilog/gtest-verilog.h" #include "{verilated_header_filepath}" namespace {namespace} {{