Skip to content

refactor(rules/verilator): Switch from cmake to json format for Verilator build file parsing#7178

Merged
waruqi merged 5 commits intoxmake-io:devfrom
24bit-xjkp:dev
Jan 4, 2026
Merged

refactor(rules/verilator): Switch from cmake to json format for Verilator build file parsing#7178
waruqi merged 5 commits intoxmake-io:devfrom
24bit-xjkp:dev

Conversation

@24bit-xjkp
Copy link
Contributor

  1. The "-make cmake" option of Verilator is deprecated and will be removed in future. See Deprecations. Use "-make cmake" option in Verilator v5.042 will lead Verilator to return a non-zero value, thus the script reports an error.
  2. Verilator can generate build options in json format which can be parsed easier, so we switch from cmake to json format.
  3. Add new SystemVerilog language standard v1800-2023 support.
  4. Fix the issue with c++ language standard settings.

BREAKING CHANGE: CMake build file parsing is deprecated.

…ator build file parsing

1. The "-make cmake" option of Verilator is deprecated and will be removed  in future. See [Deprecations](https://verilator.org/guide/latest/deprecations.html). Use "-make cmake" option in Verilator v5.042 will lead Verilator to return a non-zero value, thus the script reports an error.
2. Verilator can generate build options in json format which can be parsed easier, so we switch from cmake to json format.
3. Add new SystemVerilog language standard `v1800-2023` support.
4. Fix the issue with c++ language standard settings.

BREAKING CHANGE: CMake build file parsing is deprecated.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @24bit-xjkp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the Verilator rule to modernize its interaction with the Verilator toolchain. The primary goal is to move away from a deprecated CMake-based build file parsing method to a more maintainable and future-proof JSON-based approach. This change not only resolves compatibility issues with recent Verilator versions but also expands support for the latest SystemVerilog standard and refines C++ language configuration, leading to a more robust and adaptable build system.

Highlights

  • Verilator Build File Parsing: Switched the Verilator build file parsing mechanism from the deprecated CMake format to the more robust and easier-to-parse JSON format. This addresses issues with newer Verilator versions that no longer support CMake output.
  • SystemVerilog Standard Support: Added support for the latest SystemVerilog language standard, v1800-2023, allowing projects to leverage the newest features and syntax.
  • C++ Language Standard Handling: Improved the detection and configuration of C++ language standards, ensuring correct compilation settings when integrating Verilator-generated code.
  • Dynamic Build Option Integration: Enhanced the parsing of Verilator's generated build options from the JSON output, including compiler defines (VM_ switches) and system linker flags, making the build process more flexible and accurate.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Verilator rule to use JSON for build file parsing instead of the deprecated CMake format. This is a great improvement for maintainability and future-proofing. The changes also include support for the new SystemVerilog v1800-2023 standard and a fix for C++ language standard detection.

The overall implementation is good, but I've identified a couple of areas where the new JSON parsing logic could be made more robust. Specifically, the code doesn't handle potential errors during JSON file loading or cases where the file has an unexpected structure. This could lead to build failures. I've provided suggestions to add error handling and safe access patterns to prevent such issues.

Apart from that, the changes are clean and well-aligned with the goals of the pull request.

Comment on lines +30 to +35
local json = json.loadfile(jsonfile)
local global_classes = json["sources"]["global"]
local classefiles_slow = json["sources"]["classes_slow"]
local classefiles_fast = json["sources"]["classes_fast"]
local supportfiles_slow = json["sources"]["support_slow"]
local supportfiles_fast = json["sources"]["support_fast"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The code does not handle cases where json.loadfile fails or when the sources key is missing in the JSON file. This could lead to a script error if the JSON file is not found, is malformed, or does not have the expected structure. It's safer to check the return value of json.loadfile and provide a default empty table for the sources object to prevent runtime errors.

    local data, err = json.loadfile(jsonfile)
    if not data then
        os.raise("failed to load verilator json file %s: %s", jsonfile, err or "unknown error")
    end
    local sources = data["sources"] or {}
    local global_classes = sources["global"]
    local classefiles_slow = sources["classes_slow"]
    local classefiles_fast = sources["classes_fast"]
    local supportfiles_slow = sources["support_slow"]
    local supportfiles_fast = sources["support_fast"]

Comment on lines +119 to +127
local json = json.loadfile(jsonfile)
verilator_root = json["system"]["verilator_root"]
switches.SC = json["options"]["system_c"]
switches.COVERAGE = json["options"]["coverage"]
switches.TIMING = json["options"]["use_timing"]
switches.THREADS = json["options"]["threads"]
switches.TRACE_VCD = json["options"]["trace_vcd"]
switches.TRACE_FST = json["options"]["trace_fst"]
switches.TRACE_SAIF = json["options"]["trace_saif"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The code does not handle cases where json.loadfile fails or when the expected keys (system, options) are missing in the JSON file. This could lead to a script error. It's safer to check the return value of json.loadfile and ensure nested objects exist by providing default empty tables before accessing them. This change makes the JSON parsing more robust for this function.

    local json, err = json.loadfile(jsonfile)
    if not json then
        os.raise("failed to load verilator json file %s: %s", jsonfile, err or "unknown error")
    end
    json["system"] = json["system"] or {}
    json["options"] = json["options"] or {}

    verilator_root = json["system"]["verilator_root"]
    switches.SC = json["options"]["system_c"]
    switches.COVERAGE = json["options"]["coverage"]
    switches.TIMING = json["options"]["use_timing"]
    switches.THREADS = json["options"]["threads"]
    switches.TRACE_VCD = json["options"]["trace_vcd"]
    switches.TRACE_FST = json["options"]["trace_fst"]
    switches.TRACE_SAIF = json["options"]["trace_saif"]

@waruqi
Copy link
Member

waruqi commented Jan 2, 2026

BREAKING CHANGE: CMake build file parsing is deprecated.

先确认下低版本 verilator 是否支持 json ,否则你直接干掉,低版本上会直接 break,应该保持 cmake/json 两者解析同时存在一段时间,如果支持 json ,优先走 json 解析,如果当前 verilator 版本不支持 json 输出,就 fallback 到 cmake 解析

@24bit-xjkp
Copy link
Contributor Author

BREAKING CHANGE: CMake build file parsing is deprecated.

先确认下低版本 verilator 是否支持 json ,否则你直接干掉,低版本上会直接 break,应该保持 cmake/json 两者解析同时存在一段时间,如果支持 json ,优先走 json 解析,如果当前 verilator 版本不支持 json 输出,就 fallback 到 cmake 解析

v5.036开始支持json,我加一个按版本判断的逻辑吧

1. If verilator's version >= 5.036, then we use json file parsing, otherwise, we use cmake file parsing.
--- @return string makefile_type Makefile type, json or cmake
---
function _get_makefile_type(verilator)
local version = assert(find_programver(verilator), "verilator version is unknown!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该用 find_tool("xxx", {version = true}), find_programver 一般是不直接用的

---
function _get_makefile_type(verilator)
local version = assert(find_programver(verilator), "verilator version is unknown!")
local version = assert(find_tool(verilator, {version = true}).version, "verilator version is unknown!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没找到,访问 version 会挂

local tool = assert(find_tool("verilator", {program = verilator, version = true}), "verilator not found!")
local version = tool.version

local cxxlang = false
for _, lang in ipairs(languages) do
if lang:startswith("xx") or lang:startswith("++") then
if lang:startswith("cxx") or lang:startswith("c++") then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无关的不要改,还有 gnu++20 了,这会 break 一些东西

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这原本是写错了,不管是c++ cxx还是gnuxx gnu++都不是xx或者++开头,会导致设了languages还加一个c++20上去

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那应该用 lang:find("xx", 1, true) ,如果只是判断 cxx/c++ , 还漏了 gnu++ 的

--- @brief get makefile type
--- @param verilator string verilator program path
--- @return boolean support_json Whether support json
--- @return string makefile_type Makefile type, json or cmake
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种注释感觉意义不大,一看就是 ai 加的。

@waruqi waruqi merged commit 5631b98 into xmake-io:dev Jan 4, 2026
28 checks passed
@waruqi waruqi added this to the v3.0.7 milestone Jan 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants