thuvasooriya/verilator
build verilator with zig
b4d064d1669cc2a8d921eae64f88f4586042c825
zig build -Doptimize=ReleaseFast
./zig-out/bin/verilator --version
./zig-out/bin/verilator --help
./zig-out/bin/verilator --lint-only your_design.v
./zig-out/bin/verilator_coverage --help
Build-time:
Runtime:
The build system automatically:
Generates config headers (generate.zig
)
config_package.h
- Package metadataconfig_rev.h
- Git version from upstream dependency URLRuns Bison (generates parser from upstream)
verilog.y
→ V3ParseBison.c
, V3ParseBison.h
#include
by V3ParseGrammar.cpp
(unity build)Runs Flex (generates lexers from upstream)
verilog.l
→ V3Lexer.yy.cpp
(included by V3ParseLex.cpp
)V3PreLex.l
→ V3PreLex.yy.cpp
(included by V3PreProc.cpp
)flexfix_wrapper.py
(uses upstream flexfix)Runs Astgen (generates AST code from upstream)
V3Ast__gen_*.h
, V3Dfg__gen_*.h
)V3Const__gen.cpp
(compiled separately)astgen_wrapper.py
to invoke upstream astgen scriptCompiles everything
V3Const__gen.cpp
)Installs
bin/verilator
- Perl wrapperbin/verilator_bin
- Compiled binarybin/verilator_coverage
- Perl wrapperbin/verilator_coverage_bin_dbg
- Coverage binaryinclude/
- Runtime support files.
├── build.zig # Main build system
├── generate.zig # Config header generation library
├── build.zig.zon # Verilator source dependency
├── astgen_wrapper.py # Wrapper for upstream astgen
├── flexfix_wrapper.py # Wrapper for upstream flexfix
└── include/
└── FlexLexer.h # Local flex header
Verilator uses "unity builds" where some generated files are #include
d rather than compiled separately.
This allows wrapper code to access internal bison/flex symbols:
V3ParseBison.c
→ included by V3ParseGrammar.cpp
V3Lexer.yy.cpp
→ included by V3ParseLex.cpp
V3PreLex.yy.cpp
→ included by V3PreProc.cpp
Exception: V3Const__gen.cpp
is compiled as a separate object.
Metric | Debug | ReleaseFast |
---|---|---|
verilator_bin | 145 MB | ~15 MB |
verilator_coverage | 3.9 MB | ~800 KB |
Build Time | ~2-3 min | ~5-10 min |
# Version check
$ ./zig-out/bin/verilator --version
Verilator 5.41.0 rev b4d064d
# Coverage tool
$ ./zig-out/bin/verilator_coverage --version
Verilator 5.41.0 rev b4d064d
Edit build.zig.zon
:
.verilator = .{
.url = "https://github.com/verilator/verilator/archive/<commit>.tar.gz",
.hash = "...", // Run zig build to get the hash
},
Then run zig build
and fix any compilation errors from API changes.
build.zig.zon
dependency URL.zig-cache
#include
d instead of compiled separatelyVlcMain.cpp
) is a simple unity build with no generation dependenciesVerilator is licensed under LGPL-3.0-only OR Artistic-2.0. This follows the same terms.